bpp-seq  2.2.0
VectorSiteContainer.cpp
Go to the documentation of this file.
1 //
2 // File: VectorSiteContainer.cpp
3 // Created by: Julien Dutheil
4 // Created on: Mon Oct 6 11:50:40 2003
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for sequences analysis.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
40 #include "VectorSiteContainer.h"
41 
42 #include <iostream>
43 
44 using namespace std;
45 
46 #include <Bpp/Text/TextTools.h>
47 
48 using namespace bpp;
49 
52 VectorSiteContainer::VectorSiteContainer(
53  const std::vector<const Site*>& vs,
54  const Alphabet* alpha,
55  bool checkPositions)
56 throw (Exception) :
58  sites_(0),
59  names_(0),
60  comments_(0),
61  sequences_(0)
62 {
63  if (vs.size() == 0)
64  throw Exception("VectorSiteContainer::VectorSiteContainer. Empty site set.");
65  // Seq names and comments:
66  size_t nbSeq = vs[0]->size();
67  names_.resize(nbSeq);
68  comments_.resize(nbSeq);
69  for (size_t i = 0; i < nbSeq; i++)
70  {
71  names_[i] = "Seq_" + TextTools::toString(i);
72  comments_[i] = new Comments();
73  }
74  // Now try to add each site:
75  for (size_t i = 0; i < vs.size(); i++)
76  {
77  addSite(*vs[i], checkPositions); // This may throw an exception if position argument already exists or is size is not valid.
78  }
79 
80  sequences_.resize(nbSeq);
81 }
82 
83 /******************************************************************************/
84 
85 VectorSiteContainer::VectorSiteContainer(size_t size, const Alphabet* alpha) :
87  sites_(0),
88  names_(size),
89  comments_(size),
90  sequences_(size)
91 {
92  // Seq names and comments:
93  for (size_t i = 0; i < size; i++)
94  {
95  names_[i] = string("Seq_") + TextTools::toString(i);
96  comments_[i] = new Comments();
97  }
98 }
99 
100 /******************************************************************************/
101 
102 VectorSiteContainer::VectorSiteContainer(const std::vector<std::string>& names, const Alphabet* alpha) :
104  sites_(0),
105  names_(names.size()),
106  comments_(names.size()),
107  sequences_(names.size())
108 {
109  // Seq names and comments:
110  for (size_t i = 0; i < names.size(); i++)
111  {
112  names_[i] = names[i];
113  comments_[i] = new Comments();
114  }
115 }
116 
117 /******************************************************************************/
118 
121  sites_(0),
122  names_(0),
123  comments_(0),
124  sequences_(0)
125 {}
126 
127 /******************************************************************************/
128 
131  sites_(0),
132  names_(vsc.names_),
133  comments_(vsc.getNumberOfSequences()),
134  sequences_(vsc.getNumberOfSequences())
135 {
136  // Now try to add each site:
137  for (size_t i = 0; i < vsc.getNumberOfSites(); i++)
138  {
139  addSite(vsc.getSite(i), false); // We assume that positions are correct.
140  }
141  // Seq comments:
142  for (size_t i = 0; i < vsc.getNumberOfSequences(); i++)
143  {
144  comments_[i] = new Comments(vsc.getComments(i));
145  }
146 }
147 
148 /******************************************************************************/
149 
152  sites_(0),
153  names_(sc.getSequencesNames()),
154  comments_(sc.getNumberOfSequences()),
155  sequences_(sc.getNumberOfSequences())
156 {
157  // Now try to add each site:
158  for (size_t i = 0; i < sc.getNumberOfSites(); i++)
159  {
160  addSite(sc.getSite(i), false); // We assume that positions are correct.
161  }
162  // Seq comments:
163  for (size_t i = 0; i < sc.getNumberOfSequences(); i++)
164  {
165  comments_[i] = new Comments(sc.getComments(i));
166  }
167 }
168 
169 /******************************************************************************/
170 
173  sites_(0),
174  names_(0),
175  comments_(0),
176  sequences_(0)
177 {
178  for (size_t i = 0; i < osc.getNumberOfSequences(); i++)
179  {
180  addSequence(osc.getSequence(i), false);
181  }
182  reindexSites();
183 }
184 
185 /******************************************************************************/
186 
189  sites_(0),
190  names_(0),
191  comments_(0),
192  sequences_(0)
193 {
194  vector<string> names = sc.getSequencesNames();
195  for (size_t i = 0; i < names.size(); i++)
196  {
197  addSequence(sc.getSequence(names[i]), false);
198  }
199  reindexSites();
200 }
201 
202 /******************************************************************************/
203 
205 {
206  clear();
208  // Seq names:
209  names_.resize(vsc.getNumberOfSequences());
211  // Now try to add each site:
212  for (size_t i = 0; i < vsc.getNumberOfSites(); i++)
213  {
214  addSite(vsc.getSite(i), false); // We assume that positions are correct.
215  }
216  // Seq comments:
217  size_t nbSeq = vsc.getNumberOfSequences();
218  comments_.resize(nbSeq);
219  for (size_t i = 0; i < nbSeq; i++)
220  {
221  comments_[i] = new Comments(vsc.getComments(i));
222  }
223  sequences_.resize(nbSeq);
224 
225  return *this;
226 }
227 
228 /******************************************************************************/
229 
231 {
232  clear();
234  // Seq names:
235  names_.resize(sc.getNumberOfSequences());
237  // Now try to add each site:
238  for (size_t i = 0; i < sc.getNumberOfSites(); i++)
239  {
240  addSite(sc.getSite(i), false); // We assume that positions are correct.
241  }
242  // Seq comments:
243  size_t nbSeq = sc.getNumberOfSequences();
244  comments_.resize(nbSeq);
245  for (size_t i = 0; i < nbSeq; i++)
246  {
247  comments_[i] = new Comments(sc.getComments(i));
248  }
249  sequences_.resize(nbSeq);
250 
251  return *this;
252 }
253 
254 /******************************************************************************/
255 
257 {
258  clear();
260 
261  size_t nbSeq = osc.getNumberOfSequences();
262  for (size_t i = 0; i < nbSeq; i++)
263  {
264  addSequence(osc.getSequence(i), false);
265  }
266  reindexSites();
267 
268  return *this;
269 }
270 
271 /******************************************************************************/
272 
274 {
275  clear();
277 
278  vector<string> names = sc.getSequencesNames();
279  for (size_t i = 0; i < names.size(); i++)
280  {
281  addSequence(sc.getSequence(names[i]), false);
282  }
283  reindexSites();
284 
285  return *this;
286 }
287 
288 /******************************************************************************/
289 
290 const Site& VectorSiteContainer::getSite(size_t i) const throw (IndexOutOfBoundsException)
291 {
292  if (i >= getNumberOfSites())
293  throw IndexOutOfBoundsException("VectorSiteContainer::getSite.", i, 0, getNumberOfSites() - 1);
294  return *sites_[i];
295 }
296 
297 /******************************************************************************/
298 
299 void VectorSiteContainer::setSite(size_t pos, const Site& site, bool checkPositions) throw (Exception)
300 {
301  if (pos >= getNumberOfSites())
302  throw IndexOutOfBoundsException("VectorSiteContainer::setSite.", pos, 0, getNumberOfSites() - 1);
303 
304  // Check size:
305  if (site.size() != getNumberOfSequences())
306  throw SiteException("AlignedSequenceContainer::addSite. Site does not have the appropriate length", &site);
307 
308  // New site's alphabet and site container's alphabet matching verification
309  if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
310  throw AlphabetMismatchException("VectorSiteContainer::setSite", getAlphabet(), site.getAlphabet());
311 
312  // Check position:
313  if (checkPositions)
314  {
315  int position = site.getPosition();
316  // For all positions in vector : throw exception if position already exists
317  for (size_t i = 0; i < sites_.size(); i++)
318  {
319  if (sites_[i]->getPosition() == position)
320  throw SiteException("VectorSiteContainer::setSite: Site position already exists in container", &site);
321  }
322  }
323  delete sites_[pos];
324  sites_[pos] = dynamic_cast<Site*>(site.clone());
325 }
326 
327 /******************************************************************************/
328 
329 Site* VectorSiteContainer::removeSite(size_t i) throw (IndexOutOfBoundsException)
330 {
331  if (i >= getNumberOfSites())
332  throw IndexOutOfBoundsException("VectorSiteContainer::removeSite.", i, 0, getNumberOfSites() - 1);
333  Site* site = sites_[i];
334  sites_.erase(sites_.begin() + static_cast<ptrdiff_t>(i));
335  return site;
336 }
337 
338 /******************************************************************************/
339 
340 void VectorSiteContainer::deleteSite(size_t i) throw (IndexOutOfBoundsException)
341 {
342  if (i >= getNumberOfSites())
343  throw IndexOutOfBoundsException("VectorSiteContainer::deleteSite.", i, 0, getNumberOfSites() - 1);
344  delete sites_[i];
345  sites_.erase(sites_.begin() + static_cast<ptrdiff_t>(i));
346 }
347 
348 /******************************************************************************/
349 
350 void VectorSiteContainer::deleteSites(size_t siteIndex, size_t length) throw (IndexOutOfBoundsException)
351 {
352  if (siteIndex + length > getNumberOfSites())
353  throw IndexOutOfBoundsException("VectorSiteContainer::deleteSites.", siteIndex + length, 0, getNumberOfSites() - 1);
354  for (size_t i = siteIndex; i < siteIndex + length; ++i)
355  {
356  delete sites_[i];
357  }
358  sites_.erase(sites_.begin() + static_cast<ptrdiff_t>(siteIndex), sites_.begin() + static_cast<ptrdiff_t>(siteIndex + length));
359 }
360 
361 /******************************************************************************/
362 
363 void VectorSiteContainer::addSite(const Site& site, bool checkPositions) throw (Exception)
364 {
365  // Check size:
366  if (site.size() != getNumberOfSequences())
367  throw SiteException("VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
368 
369  // New site's alphabet and site container's alphabet matching verification
370  if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
371  {
372  throw AlphabetMismatchException("VectorSiteContainer::addSite", getAlphabet(), site.getAlphabet());
373  }
374 
375  // Check position:
376  if (checkPositions)
377  {
378  int position = site.getPosition();
379  // For all positions in vector : throw exception if position already exists
380  for (size_t i = 0; i < sites_.size(); i++)
381  {
382  if (sites_[i]->getPosition() == position)
383  throw SiteException("VectorSiteContainer::addSite. Site position already exists in container", &site);
384  }
385  }
386 
387  sites_.push_back(dynamic_cast<Site*>(site.clone()));
388 }
389 
390 /******************************************************************************/
391 
392 void VectorSiteContainer::addSite(const Site& site, int position, bool checkPositions) throw (Exception)
393 {
394  // Check size:
395  if (site.size() != getNumberOfSequences())
396  throw SiteException("VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
397 
398  // New site's alphabet and site container's alphabet matching verification
399  if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
400  {
401  throw AlphabetMismatchException("VectorSiteContainer::addSite", getAlphabet(), site.getAlphabet());
402  }
403 
404  // Check position:
405  if (checkPositions)
406  {
407  // For all positions in vector : throw exception if position already exists
408  for (size_t i = 0; i < sites_.size(); i++)
409  {
410  if (sites_[i]->getPosition() == position)
411  throw SiteException("VectorSiteContainer::addSite. Site position already exists in container", &site);
412  }
413  }
414  Site* copy = dynamic_cast<Site*>(site.clone());
415  copy->setPosition(position);
416  sites_.push_back(copy);
417 }
418 
419 /******************************************************************************/
420 
421 void VectorSiteContainer::addSite(const Site& site, size_t siteIndex, bool checkPositions) throw (Exception)
422 {
423  if (siteIndex >= getNumberOfSites())
424  throw IndexOutOfBoundsException("VectorSiteContainer::addSite", siteIndex, 0, getNumberOfSites() - 1);
425 
426  // Check size:
427  if (site.size() != getNumberOfSequences())
428  throw SiteException("VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
429 
430  // New site's alphabet and site container's alphabet matching verification
431  if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
432  {
433  throw AlphabetMismatchException("VectorSiteContainer::addSite", getAlphabet(), site.getAlphabet());
434  }
435 
436  // Check position:
437  if (checkPositions)
438  {
439  int position = site.getPosition();
440  // For all positions in vector : throw exception if position already exists
441  for (size_t i = 0; i < sites_.size(); i++)
442  {
443  if (sites_[i]->getPosition() == position)
444  throw SiteException("VectorSiteContainer::addSite. Site position already exists in container", &site);
445  }
446  }
447 
448  // insert(begin() + pos, new Site(site));
449  sites_.insert(sites_.begin() + static_cast<ptrdiff_t>(siteIndex), dynamic_cast<Site*>(site.clone()));
450 }
451 
452 /******************************************************************************/
453 
454 void VectorSiteContainer::addSite(const Site& site, size_t siteIndex, int position, bool checkPositions) throw (Exception)
455 {
456  if (siteIndex >= getNumberOfSites())
457  throw IndexOutOfBoundsException("VectorSiteContainer::addSite", siteIndex, 0, getNumberOfSites() - 1);
458 
459  // Check size:
460  if (site.size() != getNumberOfSequences())
461  throw SiteException("VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
462 
463  // New site's alphabet and site container's alphabet matching verification
464  if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
465  {
466  throw AlphabetMismatchException("VectorSiteContainer::addSite", getAlphabet(), site.getAlphabet());
467  }
468 
469  // Check position:
470  if (checkPositions)
471  {
472  // For all positions in vector : throw exception if position already exists
473  for (size_t i = 0; i < sites_.size(); i++)
474  {
475  if (sites_[i]->getPosition() == position)
476  throw SiteException("VectorSiteContainer::addSite. Site position already exists in container", &site);
477  }
478  }
479 
480  Site* copy = dynamic_cast<Site*>(site.clone());
481  copy->setPosition(position);
482  sites_.insert(sites_.begin() + static_cast<ptrdiff_t>(siteIndex), copy);
483 }
484 
485 /******************************************************************************/
486 
488 {
489  return sites_.size();
490 }
491 
492 /******************************************************************************/
493 
495 {
496  int pos = 1; // first position is 1.
497  for (vector<Site*>::iterator i = sites_.begin(); i < sites_.end(); i++)
498  {
499  (*i)->setPosition(pos++);
500  }
501 }
502 
503 /******************************************************************************/
504 
506 {
507  Vint positions(sites_.size());
508  for (size_t i = 0; i < sites_.size(); i++)
509  {
510  positions[i] = sites_[i]->getPosition();
511  }
512  return positions;
513 }
514 
515 /******************************************************************************/
516 
517 const Sequence& VectorSiteContainer::getSequence(size_t i) const throw (IndexOutOfBoundsException)
518 {
519  if (i >= getNumberOfSequences())
520  throw IndexOutOfBoundsException("VectorSiteContainer::getSequence.", i, 0, getNumberOfSequences() - 1);
521 
522  // Main loop : for all sites
523  size_t n = getNumberOfSites();
524  vector<int> sequence(n);
525  for (size_t j = 0; j < n; j++)
526  {
527  sequence[j] = sites_[j]->getContent()[i];
528  }
529  if (sequences_[i])
530  delete sequences_[i];
531  sequences_[i] = new BasicSequence(names_[i], sequence, *comments_[i], getAlphabet());
532  return *sequences_[i];
533 }
534 
535 /******************************************************************************/
536 
538 {
539  // Look for sequence name:
540  size_t pos = getSequencePosition(name);
541  return getSequence(pos);
542 }
543 
544 /******************************************************************************/
545 
546 bool VectorSiteContainer::hasSequence(const string& name) const
547 {
548  // Look for sequence name:
549  for (size_t pos = 0; pos < names_.size(); pos++)
550  {
551  if (names_[pos] == name)
552  return true;
553  }
554  return false;
555 }
556 
557 /******************************************************************************/
558 
560 {
561  // Look for sequence name:
562  for (size_t pos = 0; pos < names_.size(); pos++)
563  {
564  if (names_[pos] == name)
565  return pos;
566  }
567  throw SequenceNotFoundException("VectorSiteContainer::getSequencePosition().", name);
568 }
569 
570 /******************************************************************************/
571 
572 void VectorSiteContainer::setSequence(const string& name, const Sequence& sequence, bool checkNames) throw (Exception)
573 {
574  // Look for sequence name:
575  size_t pos = getSequencePosition(name);
576  setSequence(pos, sequence, checkNames);
577 }
578 
579 /******************************************************************************/
580 
581 void VectorSiteContainer::setSequence(size_t pos, const Sequence& sequence, bool checkNames)
582 throw (Exception)
583 {
584  if (pos >= getNumberOfSequences())
585  throw IndexOutOfBoundsException("VectorSiteContainer::setSequence", pos, 0, getNumberOfSequences() - 1);
586 
587  // New sequence's alphabet and site container's alphabet matching verification
588  if (sequence.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
589  throw AlphabetMismatchException("VectorSiteContainer::addSite", getAlphabet(), sequence.getAlphabet());
590 
591  // If the container has only one sequence, we set the size to the size of this sequence:
592  if (getNumberOfSequences() == 1)
593  realloc(sequence.size());
594 
595  if (sequence.size() != sites_.size())
596  throw SequenceException("VectorSiteContainer::setSequence. Sequence has not the appropriate length.", &sequence);
597 
598  if (checkNames)
599  {
600  for (size_t i = 0; i < names_.size(); i++)
601  {
602  if (i != pos && sequence.getName() == names_[i])
603  throw SequenceException("VectorSiteContainer::settSequence. Name already exists in container.", &sequence);
604  }
605  }
606  // Update name:
607  names_[pos] = sequence.getName();
608  // Update elements at each site:
609  for (size_t i = 0; i < sites_.size(); i++)
610  {
611  sites_[i]->setElement(pos, sequence.getValue(i));
612  }
613  // Update comments:
614  if (comments_[pos])
615  delete comments_[pos];
616  comments_[pos] = new Comments(sequence.getComments());
617  // Update sequences:
618  if (sequences_[pos])
619  delete sequences_[pos];
620  sequences_[pos] = 0;
621 }
622 
623 /******************************************************************************/
624 
625 Sequence* VectorSiteContainer::removeSequence(size_t i) throw (IndexOutOfBoundsException)
626 {
627  if (i >= getNumberOfSequences())
628  throw IndexOutOfBoundsException("VectorSiteContainer::removeSequence.", i, 0, getNumberOfSequences() - 1);
629 
630  getSequence(i); // Actuallizes pointer.
631  Sequence* sequence = sequences_[i];
632  for (size_t j = 0; j < sites_.size(); j++)
633  {
634  // For each site:
635  sites_[j]->deleteElement(i);
636  }
637 
638  // Now actualize names and comments:
639  names_.erase(names_.begin() + static_cast<ptrdiff_t>(i));
640  if (comments_[i])
641  delete comments_[i];
642  comments_.erase(comments_.begin() + static_cast<ptrdiff_t>(i));
643  // We remove the sequence, so the destruction of the sequence is up to the user:
644  // if (sequences_[i] != 0) delete sequences_[i];
645  sequences_.erase(sequences_.begin() + static_cast<ptrdiff_t>(i));
646  return sequence;
647 }
648 
649 /******************************************************************************/
650 
652 {
653  // Look for sequence name:
654  size_t pos = getSequencePosition(name);
655  return removeSequence(pos);
656 }
657 
658 /******************************************************************************/
659 
660 void VectorSiteContainer::deleteSequence(size_t i) throw (IndexOutOfBoundsException)
661 {
662  if (i >= getNumberOfSequences())
663  throw IndexOutOfBoundsException("VectorSiteContainer::demeteSequence.", i, 0, getNumberOfSequences() - 1);
664  for (size_t j = 0; j < sites_.size(); j++)
665  {
666  sites_[j]->deleteElement(i);
667  }
668 
669  // Now actualize names and comments:
670  names_.erase(names_.begin() + static_cast<ptrdiff_t>(i));
671  if (comments_[i])
672  delete comments_[i];
673  comments_.erase(comments_.begin() + static_cast<ptrdiff_t>(i));
674  if (sequences_[i])
675  delete sequences_[i];
676  sequences_.erase(sequences_.begin() + static_cast<ptrdiff_t>(i));
677 }
678 
679 /******************************************************************************/
680 
682 {
683  // Look for sequence name:
684  size_t pos = getSequencePosition(name);
685  deleteSequence(pos);
686 }
687 
688 /******************************************************************************/
689 
690 void VectorSiteContainer::addSequence(const Sequence& sequence, bool checkNames) throw (Exception)
691 {
692  // If the container has no sequence, we set the size to the size of this sequence:
693  if (getNumberOfSequences() == 0)
694  realloc(sequence.size());
695 
696  // New sequence's alphabet and site container's alphabet matching verification
697  if (sequence.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
698  throw AlphabetMismatchException("VectorSiteContainer::addSequence", getAlphabet(), sequence.getAlphabet());
699 
700  if (sequence.size() != sites_.size())
701  throw SequenceException("VectorSiteContainer::addSequence. Sequence has not the appropriate length: " + TextTools::toString(sequence.size()) + ", should be " + TextTools::toString(sites_.size()) + ".", &sequence);
702 
703  if (checkNames)
704  {
705  for (size_t i = 0; i < names_.size(); i++)
706  {
707  if (sequence.getName() == names_[i])
708  throw SequenceException("VectorSiteContainer::addSequence. Name already exists in container.", &sequence);
709  }
710  }
711 
712  // Append name:
713  names_.push_back(sequence.getName());
714 
715  // Append elements at each site:
716  for (size_t i = 0; i < sites_.size(); i++)
717  {
718  sites_[i]->addElement(sequence.getValue(i));
719  }
720 
721  // Append comments:
722  comments_.push_back(new Comments(sequence.getComments()));
723 
724  // Sequences pointers:
725  sequences_.push_back(0);
726 }
727 
728 /******************************************************************************/
729 
731  const Sequence& sequence,
732  size_t pos,
733  bool checkNames)
734 throw (Exception)
735 {
736  if (pos >= getNumberOfSequences())
737  throw IndexOutOfBoundsException("VectorSiteContainer::addSequence.", pos, 0, getNumberOfSequences() - 1);
738  if (sequence.size() != sites_.size())
739  throw SequenceNotAlignedException("VectorSiteContainer::setSequence", &sequence);
740 
741  // New sequence's alphabet and site container's alphabet matching verification
742  if (sequence.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
743  {
744  throw AlphabetMismatchException("VectorSiteContainer::addSite", getAlphabet(), sequence.getAlphabet());
745  }
746 
747  if (checkNames)
748  {
749  for (size_t i = 0; i < names_.size(); i++)
750  {
751  if (sequence.getName() == names_[i])
752  throw SequenceException("VectorSiteContainer::addSequence. Name already exists in container.", &sequence);
753  }
754  }
755 
756  for (size_t i = 0; i < sites_.size(); i++)
757  {
758  // For each site:
759  sites_[i]->addElement(pos, sequence.getValue(i));
760  }
761  // Actualize names and comments:
762  names_.insert(names_.begin() + static_cast<ptrdiff_t>(pos), sequence.getName());
763  comments_.insert(comments_.begin() + static_cast<ptrdiff_t>(pos), new Comments(sequence.getComments()));
764  sequences_.insert(sequences_.begin() + static_cast<ptrdiff_t>(pos), 0);
765 }
766 
767 /******************************************************************************/
768 
770 {
771  // Must delete all sites in the container:
772  for (size_t i = 0; i < sites_.size(); i++)
773  {
774  delete sites_[i];
775  }
776 
777  // must delete all comments too:
778  for (size_t i = 0; i < comments_.size(); i++)
779  {
780  if (comments_[i] != 0)
781  delete comments_[i];
782  }
783 
784  // Delete all sequences retrieved:
785  for (size_t i = 0; i < sequences_.size(); i++)
786  {
787  if (sequences_[i] != 0)
788  delete (sequences_[i]);
789  }
790 
791  // Delete all sites pointers
792  sites_.clear();
793  names_.clear();
794  comments_.clear();
795  sequences_.clear();
796 }
797 
798 /******************************************************************************/
799 
801 {
802  clear();
803  sites_.resize(n);
804  for (size_t i = 0; i < n; i++)
805  {
806  sites_[i] = new Site(getAlphabet());
807  }
808  reindexSites();
809 }
810 
811 /******************************************************************************/
812 
814 {
815  return names_;
816 }
817 
818 /******************************************************************************/
819 
821  const vector<string>& names,
822  bool checkNames)
823 throw (Exception)
824 {
825  if (names.size() != getNumberOfSequences())
826  throw IndexOutOfBoundsException("VectorSiteContainer::setSequenceNames: bad number of names.", names.size(), getNumberOfSequences(), getNumberOfSequences());
827  if (checkNames)
828  {
829  for (size_t i = 0; i < names.size(); i++)
830  {
831  // For all names in vector : throw exception if name already exists
832  for (size_t j = 0; j < i; j++)
833  {
834  if (names[j] == names[i])
835  throw Exception("VectorSiteContainer::setSequencesNames : Sequence's name already exists in container");
836  }
837  }
838  }
839  for (size_t i = 0; i < names.size(); i++)
840  {
841  names_[i] = names[i];
842  }
843 }
844 
845 /******************************************************************************/
846 
847 void VectorSiteContainer::setComments(size_t sequenceIndex, const Comments& comments) throw (IndexOutOfBoundsException)
848 {
849  comments_[sequenceIndex] = new Comments(comments);
850 }
851 
852 /******************************************************************************/
853 
855 {
858  return vsc;
859 }
860 
861 /******************************************************************************/
862 
virtual void setPosition(int position)
Set the position of this site.
Definition: Site.h:169
Exception thrown when a sequence is not found The sequence not found exception base class...
void addSequence(const Sequence &sequence, bool checkName=true)
Add a sequence to the container.
std::vector< std::string > Comments
Declaration of Comments type.
Definition: Sequence.h:60
std::vector< Comments * > comments_
void setSite(size_t siteIndex, const Site &site, bool checkPosition=true)
Set a site in the container.
The SiteContainer interface.
Definition: SiteContainer.h:63
The OrderedSequenceContainer interface.
const Sequence & getSequence(size_t sequenceIndex) const
Retrieve a sequence object from the container.
This alphabet is used to deal NumericAlphabet.
The site exception base class.
const Alphabet * getAlphabet() const
Get sequence container&#39;s alphabet.
The Alphabet interface.
Definition: Alphabet.h:130
STL namespace.
Site * removeSite(size_t siteIndex)
Remove a site from the container.
std::vector< std::string > names_
AbstractSequenceContainer & operator=(const AbstractSequenceContainer &sc)
Partial implementation of the OrderedSequenceContainer interface.
void clear()
Delete all sequences in the container.
void deleteSite(size_t siteIndex)
Delete a site in the container.
void setComments(size_t sequenceIndex, const Comments &comments)
Set the comments of a particular sequence.
const Comments & getGeneralComments() const
Get the comments of this container.
void setSequencesNames(const std::vector< std::string > &names, bool checkNames=true)
Set all sequence names.
virtual const Comments & getComments(size_t sequenceIndex) const =0
Get comments of a particular sequence.
std::vector< Sequence * > sequences_
Vint getSitePositions() const
Get all position attributes of sites.
virtual const Sequence & getSequence(size_t sequenceIndex) const =0
Retrieve a sequence object from the container.
VectorSiteContainer & operator=(const VectorSiteContainer &vsc)
virtual void deleteElement(size_t pos)=0
Delete the element at position &#39;pos&#39;.
void setSequence(const std::string &name, const Sequence &sequence, bool checkName)
Replace a sequence in the container.
void addSite(const Site &site, bool checkPosition=true)
Add a site in the container.
Sequence * removeSequence(size_t sequenceIndex)
Extract (and remove) a sequence from the container.
void deleteSequence(size_t sequenceIndex)
Delete a sequence of the container.
void deleteSites(size_t siteIndex, size_t length)
Delete a continuous range of sites in the container.
virtual const Site & getSite(size_t siteIndex) const =0
Get a site from the container.
const Site & getSite(size_t siteIndex) const
Get a site from the container.
size_t getNumberOfSites() const
Get the number of sites in the container.
A basic implementation of the Sequence interface.
Definition: Sequence.h:207
void reindexSites()
Set all positions attributes.
The sequence exception base class.
virtual const Sequence & getSequence(const std::string &name) const =0
Retrieve a sequence object from the container.
VectorSiteContainer * createEmptyContainer() const
Return a copy of this container, but with no sequence inside.
virtual size_t getNumberOfSites() const =0
Get the number of sites in the container.
size_t getSequencePosition(const std::string &name) const
Get the position of a sequence in sequence container from its name.
virtual std::vector< std::string > getSequencesNames() const =0
Get all the names of the sequences in the container.
std::vector< Site * > sites_
The sequence interface.
Definition: Sequence.h:74
std::vector< std::string > getSequencesNames() const
Get all the names of the sequences in the container.
The Site class.
Definition: Site.h:61
VectorSiteContainer(const std::vector< const Site *> &vs, const Alphabet *alpha, bool checkPositions=true)
Build a new container from a set of sites.
Exception thrown when two alphabets do not match.
virtual std::vector< std::string > getSequencesNames() const =0
Get all the names of the sequences in the container.
bool hasSequence(const std::string &name) const
Check if a sequence with a given name is present in the container.
The SequenceContainer interface.
Exception thrown when a sequence is not align with others.
void setGeneralComments(const Comments &comments)
Set the comments of this container.
The VectorSiteContainer class.
size_t getNumberOfSequences() const
Get the number of sequences in the container.
const Comments & getComments(const std::string &name) const
Get comments of a particular sequence.
virtual size_t getNumberOfSequences() const =0
Get the number of sequences in the container.