bpp-seq3  3.0.0
VectorProbabilisticSiteContainer.cpp
Go to the documentation of this file.
1 //
2 // File: VectorProbabilisticSiteContainer.cpp
3 // Authors:
4 // Murray Patterson
5 // Created: 2015-10-19 00:00:00
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for sequences analysis.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #include <Bpp/Text/TextTools.h>
42 
44 
45 using namespace bpp;
46 using namespace std;
47 
48 /********************************************************************************/
49 
55 {}
56 
60  const std::vector<const CruxSymbolListSite*>& vs,
61  const Alphabet* alpha,
62  bool checkPositions) :
67 {
68  if (vs.size() == 0)
69  throw Exception("VectorProbabilisticSiteContainer::VectorProbabilisticSiteContainer(vector). Empty site set.");
70 
71  size_t nbSeq = vs[0]->size();
72  // Seq names and comments:
73 
74  for (size_t i = 0; i < nbSeq; i++)
76 
77  // Now try to add each site:
78  for (size_t i = 0; i < vs.size(); i++)
79  {
80  if (!dynamic_cast<const ProbabilisticSite*>(vs[i]))
81  throw Exception("VectorProbabilisticSiteContainer::VectorProbabilisticSiteContainer : Not a ProbabilisticSite in position " + TextTools::toString(i));
82 
83  addSite(shared_ptr<ProbabilisticSite>(dynamic_cast<ProbabilisticSite*>(vs[i]->clone())), checkPositions); // This may throw an exception if position argument already exists or is size is not valid.
84  }
85 }
86 
87 
89  const std::vector<std::string>& names, const Alphabet* alpha) :
94 {
95  for (auto i : names)
97 }
98 
99 /******************************************************************************/
100 
102  const AlignedValuesContainer& avc) :
103  AbstractValuesContainer(avc.getAlphabet()),
106  AbstractProbabilisticSequenceContainer(avc.getAlphabet())
107 {
108  if (avc.getNumberOfSites() == 0)
109  throw Exception("VectorProbabilisticSiteContainer::VectorProbabilisticSiteContainer(AlignedValuesContainer). Empty site set.");
110 
111  // Seq names and comments:
112  for (const auto& name : avc.getSequenceNames())
114 
115  // Now add each site:
116  for (size_t i = 0; i < avc.getNumberOfSites(); i++)
117  {
118  shared_ptr<ProbabilisticSite> ps(new ProbabilisticSite(avc.getSymbolListSite(i)));
119  addSite(ps);
120  }
121 }
122 
123 
124 /******************************************************************************/
125 
131 {}
132 
133 /******************************************************************************/
134 
136  AbstractValuesContainer(osc.getGeneralComments(), osc.getAlphabet()),
139  AbstractProbabilisticSequenceContainer(osc.getAlphabet())
140 {
141  // Now try to add each site:
142  for (size_t i = 0; i < osc.getNumberOfSequences(); i++)
143  addSequence(osc.getSequence(i), false); // We assume that positions are correct.
144 
145  setSequenceNames(osc.getSequenceNames(), false);
146 }
147 
148 /******************************************************************************/
149 
151 {
152  clear();
154 
155  // Now try to add each site:
156  for (size_t i = 0; i < vpsc.getNumberOfSites(); i++)
157  addSite(shared_ptr<ProbabilisticSite>(vpsc.getSite(i)->clone()), false); // We assume that positions are correct.
158 
159  const auto& vnames=vpsc.getSequenceNames();
160  for (const auto& name:vnames)
162 
163  return *this;
164 }
165 
166 /********************************************************************************/
167 
168 void VectorProbabilisticSiteContainer::addSite(std::shared_ptr<ProbabilisticSite> site, bool checkPosition)
169 {
170  // check size :
171  if (site->size() != getNumberOfSequences())
172  throw Exception("VectorProbabilisticSiteContainer::addSite. Site does not have the appropriate length: " + TextTools::toString(site->size()) + ", should be " + TextTools::toString(getNumberOfSequences()) + ".");
173 
174  // new site's alphabet and site container's alphabet must match :
175  if (site->getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
176  throw AlphabetMismatchException("VectorProbabilisticSiteContainer::addSite.", getAlphabet(), site->getAlphabet());
177 
178  int position = site->getPosition();
179 
180  // check position :
181  if (checkPosition)
182  {
183  // For all positions in vector : throw exception if position already exists
184  for (size_t i = 0; i < getNumberOfSites(); i++)
185  {
186  if (getSite(i) != nullptr && getSite(i)->getPosition() == position)
187  throw Exception("VectorProbabilisticSiteContainer::addSite. Site position: " + TextTools::toString(position) + ", already used in container.");
188  }
189  }
191  if (getNumberOfSequences() == 0)
192  for (size_t i = 0; i < site->size(); i++)
193  {
195  }
196  else
197  {// Clean Sequence Container cache
199  }
200 }
201 
202 /********************************************************************************/
203 
205 {
206  // new sequence's alphabet and site container's alphabet must match :
207 
208  if (sequence.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
209  throw AlphabetMismatchException("VectorProbabilisticSiteContainer::addSequence->", getAlphabet(), sequence.getAlphabet());
210 
211  // check name :
212  if (checkName)
213  if (hasObject(sequence.getName()))
214  throw Exception("VectorProbabilisticSiteContainer::addSequence. Name: " + sequence.getName() + ", already exists in the container.");
215 
216  // if the container has no sequence, we set the size to the size of
217  // this sequence :
218  if (getNumberOfSequences() == 0)
219  {
221  for (size_t i = 0; i < (size_t)sequence.size(); i++)
222  {
223  addSite(shared_ptr<ProbabilisticSite>(new ProbabilisticSite(getAlphabet(), (int)(i) + 1)));
224  }
225  }
226  else if (sequence.size() != getNumberOfSites())
227  throw Exception("VectorProbabilisticSiteContainer::addSequence. Sequence does not have the appropriate length: " + TextTools::toString(sequence.size()) + ", should be " + TextTools::toString(getNumberOfSites()) + ".");
228 
229  // append null sequence
230 
232 
233  // append elements at each site :
234 
235  for (size_t i = 0; i < getNumberOfSites(); ++i)
236  getSite(i)->addElement(sequence.getContent()[i]);
237 
238 
239  // // append comments :
240  setGeneralComments(sequence.getComments());
241 }
242 
243 /********************************************************************************/
244 
245 void VectorProbabilisticSiteContainer::addSequence(const Sequence& sequence, bool checkName)
246 {
247  // new sequence's alphabet and site container's alphabet must match :
248  if (sequence.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
249  throw AlphabetMismatchException("VectorProbabilisticSiteContainer::addSequence", getAlphabet(), sequence.getAlphabet());
250 
251  // check name :
252  if (checkName)
253  if (hasObject(sequence.getName()))
254  throw Exception("VectorProbabilisticSiteContainer::addSequence. Name: " + sequence.getName() + ", already exists in the container.");
255 
256  // if the container has no sequence, we set the size to the size of this sequence :
257  if (getNumberOfSequences() == 0)
258  {
260 
261  for (size_t i = 0; i < (size_t)sequence.size(); i++)
262  addSite(shared_ptr<ProbabilisticSite>(new ProbabilisticSite(getAlphabet(), (int)(i) + 1)));
263  }
264  else if (sequence.size() != getNumberOfSites())
265  throw Exception("VectorProbabilisticSiteContainer::addSequence. Sequence does not have the appropriate length: " + TextTools::toString(sequence.size()) + ", should be " + TextTools::toString(getNumberOfSites()) + ".");
266 
267  // append null sequence
268 
270 
271  // conversion
272 
273  size_t size = getAlphabet()->getSize();
274 
275  vector<double> vd(size, 0.);
276 
277  for (size_t i = 0; i < sequence.size(); i++)
278  {
279  for (size_t s = 0; s < size; s++)
280  {
281  vd[s] = sequence.getStateValueAt(i, (int)s);
282  }
283  getSite(i)->addElement(vd);
284  }
285 
286  // // append comments :
287  setGeneralComments(sequence.getComments());
288 }
289 
290 
291 /********************************************************************************/
292 
294 {
297 }
298 
300 {
301  clear();
302  auto s = std::make_shared<ProbabilisticSite>(getAlphabet());
303 
304  for (size_t i = 0; i < n; i++)
305  addSite(s, false);
306 
307  reindexSites();
308 }
309 
310 /********************************************************************************/
311 
313 {
314  for (size_t i = 0; i < getNumberOfSites(); i++)
315  {
316  getSite(i)->setPosition((int)i + 1);
317  }
318 }
319 
320 /******************************************************************************/
321 
323 {
324  Vint positions(getNumberOfSites());
325  for (size_t i = 0; i < getNumberOfSites(); i++)
326  {
327  positions[i] = getSite(i)->getPosition();
328  }
329 
330  return positions;
331 }
332 
333 /******************************************************************************/
334 
336 {
337  if (vPositions.size() != getNumberOfSites())
338  throw BadSizeException("VectorSiteContainer::setSitePositions bad size of positions vector", vPositions.size(), getNumberOfSites());
339 
340  for (size_t i = 0; i < getNumberOfSites(); i++)
341  {
342  getSite(i)->setPosition(vPositions[i]);
343  }
344 }
345 
346 /******************************************************************************/
347 
349 {
352  return vsc;
353 }
354 
355 /******************************************************************************/
356 
358 {
359  if (i >= getNumberOfSequences())
360  throw IndexOutOfBoundsException("VectorProbabilisticSiteContainer::getSequence.", i, 0, getNumberOfSequences() - 1);
361 
362  // If Sequence already exsits
364  if (!isAvailableName(name))
366 
367  // Main loop : for all sites
368  size_t n = getNumberOfSites();
369  Table<double> sequence(getAlphabet()->getSize(), 0);
370  for (size_t j = 0; j < n; j++)
371  {
372  sequence.addColumn((*getSite(j))[i]);
373  }
374 
375  shared_ptr<ProbabilisticSequence> ns(new BasicProbabilisticSequence(
376  name,
377  sequence,
379  getAlphabet()));
380 
382 
383  return *ns;
384 }
385 
386 /******************************************************************************/
387 
389 {
390  // Look for sequence name:
391  size_t pos = getSequencePosition(name);
392  return getSequence(pos);
393 }
394 
395 
396 /******************************************************************************/
397 
398 void VectorProbabilisticSiteContainer::setSequence(const string& name, const ProbabilisticSequence& sequence, bool checkNames)
399 {
400  // Look for sequence name:
401  size_t pos = getSequencePosition(name);
402  setSequence(pos, sequence, checkNames);
403 }
404 
405 /******************************************************************************/
406 
407 void VectorProbabilisticSiteContainer::setSequence(size_t pos, const ProbabilisticSequence& sequence, bool checkNames)
408 {
409  if (pos >= getNumberOfSequences())
410  throw IndexOutOfBoundsException("VectorProbabilisticSiteContainer::setSequence", pos, 0, getNumberOfSequences() - 1);
411 
412  // New sequence's alphabet and site container's alphabet matching verification
413  if (sequence.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
414  throw AlphabetMismatchException("VectorProbabilisticSiteContainer::setSequence", getAlphabet(), sequence.getAlphabet());
415 
416  const string& sname = sequence.getName();
417 
418  // If the container has only one sequence, we set the size to the size of this sequence:
419  if (getNumberOfSequences() == 1)
420  {
421  realloc(sequence.size());
422  }
423  else
424  {
425  if (sequence.size() != getNumberOfSites())
426  throw BadSizeException("VectorProbabilisticSiteContainer::setSequence. Sequence has not the appropriate length.", sequence.size(), getNumberOfSites());
427 
428  if (checkNames)
429  {
431  throw Exception("VectorProbabilisticSiteContainer::setSequence. Name already exists in another sequence in container " + sname);
432  }
433  }
434 
435  // Update elements at each site:
436  for (size_t i = 0; i < getNumberOfSites(); i++)
437  getSite(i)->setElement(pos, sequence.getValue(i));
438 
439  // update sequence name
441 
442  // nullifies related ProbabilisticSequence in Container
444 }
Partial implementation of the OrderedSequenceContainer interface.
void setGeneralComments(const Comments &comments)
Set the comments of this container.
const Comments & getGeneralComments() const
Get the comments of this container.
AbstractProbabilisticSequenceContainer & operator=(const AbstractProbabilisticSequenceContainer &sc)
Partial implementation of the OrderedSequenceContainer interface.
const Alphabet * getAlphabet() const
Get container's alphabet.
The Container of Aligned Values interface.
virtual const CruxSymbolListSite & getSymbolListSite(size_t siteIndex) const =0
Get a CruxSymbolListSite from a given position.
virtual size_t getNumberOfSites() const =0
Get the number of aligned positions in the container.
Exception thrown when two alphabets do not match.
The Alphabet interface.
Definition: Alphabet.h:133
virtual std::string getAlphabetType() const =0
Identification method.
virtual unsigned int getSize() const =0
Get the number of resolved states in the alphabet (e.g. return 4 for DNA alphabet)....
A basic implementation of the ProbabilisticSequence interface.
const Comments & getComments() const
Get the comments.
Definition: Commentable.h:93
virtual const std::string & getName() const =0
Get the name of this sequence.
virtual const Comments & getComments() const =0
Get the comments.
virtual const T & getValue(size_t pos) const =0
checked access to a character in list.
virtual const Alphabet * getAlphabet() const =0
Get the alphabet associated to the list.
virtual double getStateValueAt(size_t siteIndex, int state) const
virtual size_t size() const =0
Get the number of elements in the list.
void changeName(const std::string &okey, const std::string &nkey)
change the key of an object.
bool isAvailableName(std::string objectName) const
Return if the name is in the map keys and the mapped object is nullptr or empty.
The OrderedSequenceContainer interface.
virtual const Sequence & getSequence(size_t sequenceIndex) const =0
Retrieve a sequence object from the container.
The probabilistic sequence interface.
virtual const std::vector< std::vector< double > > & getContent() const =0
The ProbabilisticSite interface.
The sequence interface.
Definition: Sequence.h:71
virtual size_t getNumberOfSequences() const =0
Get the number of sequences in the container.
virtual std::vector< std::string > getSequenceNames() const =0
Get all the names of the sequences in the container.
void addColumn(const std::vector< T > &newColumn, int pos=-1)
The template VectorMappedContainer class.
const std::shared_ptr< T > getObject(size_t objectIndex) const
Retrieve an object from the container. Set as protected since they will be public under T specific na...
void appendObject(std::shared_ptr< T > object, const std::string &name, bool check=true)
void addObject_(std::shared_ptr< T > object, size_t objectIndex, const std::string &name, bool check=false) const
void clear()
Delete all objects in the container.
std::string getObjectName(size_t objectIndex) const
The template VectorPositionedContainer class.
void appendObject(std::shared_ptr< T > object)
void nullify()
Nullify all elements.
The VectorProbabilisticSiteContainer class.
Vint getSitePositions() const
Get all position attributes of sites.
VectorProbabilisticSiteContainer & operator=(const VectorProbabilisticSiteContainer &vpsc)
void setSequence(const std::string &name, const ProbabilisticSequence &sequence, bool checkName)
Replace a probabilistic sequence in the container.
VectorProbabilisticSiteContainer(const Alphabet *alpha)
Build a new emtpy container.
const ProbabilisticSequence & getSequence(std::size_t i) const
Get Sequence from a position in the container.
void addSite(std::shared_ptr< ProbabilisticSite > site, bool checkPosition=false)
Add a site at the end of the container.
size_t getNumberOfSites() const
Get the number of aligned positions in the container.
void setSequenceNames(const std::vector< std::string > &names, bool checkNames=true)
Set all sequence names.
void clear()
Delete all objects in the container.
size_t getNumberOfSequences() const
Get the number of sequences in the container.
std::vector< std::string > getSequenceNames() const
Get all the names of the sequences in the container.
const std::shared_ptr< ProbabilisticSite > getSite(size_t i) const
Get a site from the container.
void addSequence(const ProbabilisticSequence &sequence, bool checkName=true)
Add a probabilistic sequence to the container.
VectorProbabilisticSiteContainer * clone() const
size_t getSequencePosition(const std::string &name) const
Get the position of a sequence in sequence container from its name.
VectorProbabilisticSiteContainer * createEmptyContainer() const
Return a copy of this container, but with no data inside.
void setSitePositions(Vint vPositions)
Set all position attributes of sites.
std::string toString(T t)
This alphabet is used to deal NumericAlphabet.
std::vector< int > Vint