46 #include <Bpp/Text/TextTools.h> 52 VectorSiteContainer::VectorSiteContainer(
53 const std::vector<const Site*>& vs,
64 throw Exception(
"VectorSiteContainer::VectorSiteContainer. Empty site set.");
66 size_t nbSeq = vs[0]->size();
68 comments_.resize(nbSeq);
69 for (
size_t i = 0; i < nbSeq; i++)
71 names_[i] =
"Seq_" + TextTools::toString(i);
75 for (
size_t i = 0; i < vs.size(); i++)
77 addSite(*vs[i], checkPositions);
80 sequences_.resize(nbSeq);
85 VectorSiteContainer::VectorSiteContainer(
size_t size,
const Alphabet* alpha) :
93 for (
size_t i = 0; i < size; i++)
95 names_[i] = string(
"Seq_") + TextTools::toString(i);
105 names_(names.size()),
106 comments_(names.size()),
107 sequences_(names.size())
110 for (
size_t i = 0; i < names.size(); i++)
133 comments_(vsc.getNumberOfSequences()),
134 sequences_(vsc.getNumberOfSequences())
153 names_(sc.getSequencesNames()),
154 comments_(sc.getNumberOfSequences()),
155 sequences_(sc.getNumberOfSequences())
195 for (
size_t i = 0; i < names.size(); i++)
219 for (
size_t i = 0; i < nbSeq; i++)
245 for (
size_t i = 0; i < nbSeq; i++)
262 for (
size_t i = 0; i < nbSeq; i++)
279 for (
size_t i = 0; i < names.size(); i++)
292 if (i >= getNumberOfSites())
293 throw IndexOutOfBoundsException(
"VectorSiteContainer::getSite.", i, 0, getNumberOfSites() - 1);
301 if (pos >= getNumberOfSites())
302 throw IndexOutOfBoundsException(
"VectorSiteContainer::setSite.", pos, 0, getNumberOfSites() - 1);
305 if (site.size() != getNumberOfSequences())
306 throw SiteException(
"AlignedSequenceContainer::addSite. Site does not have the appropriate length", &site);
309 if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
315 int position = site.getPosition();
317 for (
size_t i = 0; i < sites_.size(); i++)
319 if (sites_[i]->getPosition() == position)
320 throw SiteException(
"VectorSiteContainer::setSite: Site position already exists in container", &site);
324 sites_[pos] =
dynamic_cast<Site*
>(site.clone());
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));
342 if (i >= getNumberOfSites())
343 throw IndexOutOfBoundsException(
"VectorSiteContainer::deleteSite.", i, 0, getNumberOfSites() - 1);
345 sites_.erase(sites_.begin() +
static_cast<ptrdiff_t
>(i));
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)
358 sites_.erase(sites_.begin() +
static_cast<ptrdiff_t
>(siteIndex), sites_.begin() +
static_cast<ptrdiff_t
>(siteIndex + length));
366 if (site.size() != getNumberOfSequences())
367 throw SiteException(
"VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
370 if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
378 int position = site.getPosition();
380 for (
size_t i = 0; i < sites_.size(); i++)
382 if (sites_[i]->getPosition() == position)
383 throw SiteException(
"VectorSiteContainer::addSite. Site position already exists in container", &site);
387 sites_.push_back(dynamic_cast<Site*>(site.clone()));
395 if (site.size() != getNumberOfSequences())
396 throw SiteException(
"VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
399 if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
408 for (
size_t i = 0; i < sites_.size(); i++)
410 if (sites_[i]->getPosition() == position)
411 throw SiteException(
"VectorSiteContainer::addSite. Site position already exists in container", &site);
414 Site* copy =
dynamic_cast<Site*
>(site.clone());
416 sites_.push_back(copy);
423 if (siteIndex >= getNumberOfSites())
424 throw IndexOutOfBoundsException(
"VectorSiteContainer::addSite", siteIndex, 0, getNumberOfSites() - 1);
427 if (site.size() != getNumberOfSequences())
428 throw SiteException(
"VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
431 if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
439 int position = site.getPosition();
441 for (
size_t i = 0; i < sites_.size(); i++)
443 if (sites_[i]->getPosition() == position)
444 throw SiteException(
"VectorSiteContainer::addSite. Site position already exists in container", &site);
449 sites_.insert(sites_.begin() +
static_cast<ptrdiff_t
>(siteIndex), dynamic_cast<Site*>(site.clone()));
456 if (siteIndex >= getNumberOfSites())
457 throw IndexOutOfBoundsException(
"VectorSiteContainer::addSite", siteIndex, 0, getNumberOfSites() - 1);
460 if (site.size() != getNumberOfSequences())
461 throw SiteException(
"VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
464 if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
473 for (
size_t i = 0; i < sites_.size(); i++)
475 if (sites_[i]->getPosition() == position)
476 throw SiteException(
"VectorSiteContainer::addSite. Site position already exists in container", &site);
480 Site* copy =
dynamic_cast<Site*
>(site.clone());
482 sites_.insert(sites_.begin() +
static_cast<ptrdiff_t
>(siteIndex), copy);
497 for (vector<Site*>::iterator i =
sites_.begin(); i <
sites_.end(); i++)
499 (*i)->setPosition(pos++);
507 Vint positions(
sites_.size());
508 for (
size_t i = 0; i <
sites_.size(); i++)
510 positions[i] =
sites_[i]->getPosition();
519 if (i >= getNumberOfSequences())
520 throw IndexOutOfBoundsException(
"VectorSiteContainer::getSequence.", i, 0, getNumberOfSequences() - 1);
523 size_t n = getNumberOfSites();
524 vector<int> sequence(n);
525 for (
size_t j = 0; j < n; j++)
527 sequence[j] = sites_[j]->getContent()[i];
530 delete sequences_[i];
531 sequences_[i] =
new BasicSequence(names_[i], sequence, *comments_[i], getAlphabet());
532 return *sequences_[i];
540 size_t pos = getSequencePosition(name);
541 return getSequence(pos);
549 for (
size_t pos = 0; pos <
names_.size(); pos++)
562 for (
size_t pos = 0; pos < names_.size(); pos++)
564 if (names_[pos] == name)
575 size_t pos = getSequencePosition(name);
576 setSequence(pos, sequence, checkNames);
584 if (pos >= getNumberOfSequences())
585 throw IndexOutOfBoundsException(
"VectorSiteContainer::setSequence", pos, 0, getNumberOfSequences() - 1);
588 if (sequence.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
592 if (getNumberOfSequences() == 1)
593 realloc(sequence.size());
595 if (sequence.size() != sites_.size())
596 throw SequenceException(
"VectorSiteContainer::setSequence. Sequence has not the appropriate length.", &sequence);
600 for (
size_t i = 0; i < names_.size(); i++)
602 if (i != pos && sequence.getName() == names_[i])
603 throw SequenceException(
"VectorSiteContainer::settSequence. Name already exists in container.", &sequence);
607 names_[pos] = sequence.getName();
609 for (
size_t i = 0; i < sites_.size(); i++)
611 sites_[i]->setElement(pos, sequence.getValue(i));
615 delete comments_[pos];
616 comments_[pos] =
new Comments(sequence.getComments());
619 delete sequences_[pos];
627 if (i >= getNumberOfSequences())
628 throw IndexOutOfBoundsException(
"VectorSiteContainer::removeSequence.", i, 0, getNumberOfSequences() - 1);
632 for (
size_t j = 0; j < sites_.size(); j++)
639 names_.erase(names_.begin() +
static_cast<ptrdiff_t
>(i));
642 comments_.erase(comments_.begin() +
static_cast<ptrdiff_t
>(i));
645 sequences_.erase(sequences_.begin() +
static_cast<ptrdiff_t
>(i));
654 size_t pos = getSequencePosition(name);
655 return removeSequence(pos);
662 if (i >= getNumberOfSequences())
663 throw IndexOutOfBoundsException(
"VectorSiteContainer::demeteSequence.", i, 0, getNumberOfSequences() - 1);
664 for (
size_t j = 0; j < sites_.size(); j++)
666 sites_[j]->deleteElement(i);
670 names_.erase(names_.begin() +
static_cast<ptrdiff_t
>(i));
673 comments_.erase(comments_.begin() +
static_cast<ptrdiff_t
>(i));
675 delete sequences_[i];
676 sequences_.erase(sequences_.begin() +
static_cast<ptrdiff_t
>(i));
684 size_t pos = getSequencePosition(name);
693 if (getNumberOfSequences() == 0)
694 realloc(sequence.size());
697 if (sequence.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
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);
705 for (
size_t i = 0; i < names_.size(); i++)
707 if (sequence.getName() == names_[i])
708 throw SequenceException(
"VectorSiteContainer::addSequence. Name already exists in container.", &sequence);
713 names_.push_back(sequence.getName());
716 for (
size_t i = 0; i < sites_.size(); i++)
718 sites_[i]->addElement(sequence.getValue(i));
722 comments_.push_back(
new Comments(sequence.getComments()));
725 sequences_.push_back(0);
736 if (pos >= getNumberOfSequences())
737 throw IndexOutOfBoundsException(
"VectorSiteContainer::addSequence.", pos, 0, getNumberOfSequences() - 1);
738 if (sequence.size() != sites_.size())
742 if (sequence.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
749 for (
size_t i = 0; i < names_.size(); i++)
751 if (sequence.getName() == names_[i])
752 throw SequenceException(
"VectorSiteContainer::addSequence. Name already exists in container.", &sequence);
756 for (
size_t i = 0; i < sites_.size(); i++)
759 sites_[i]->addElement(pos, sequence.getValue(i));
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);
772 for (
size_t i = 0; i <
sites_.size(); i++)
778 for (
size_t i = 0; i <
comments_.size(); i++)
785 for (
size_t i = 0; i <
sequences_.size(); i++)
804 for (
size_t i = 0; i < n; i++)
821 const vector<string>& names,
825 if (names.size() != getNumberOfSequences())
826 throw IndexOutOfBoundsException(
"VectorSiteContainer::setSequenceNames: bad number of names.", names.size(), getNumberOfSequences(), getNumberOfSequences());
829 for (
size_t i = 0; i < names.size(); i++)
832 for (
size_t j = 0; j < i; j++)
834 if (names[j] == names[i])
835 throw Exception(
"VectorSiteContainer::setSequencesNames : Sequence's name already exists in container");
839 for (
size_t i = 0; i < names.size(); i++)
841 names_[i] = names[i];
849 comments_[sequenceIndex] =
new Comments(comments);
virtual void setPosition(int position)
Set the position of this site.
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.
std::vector< Comments * > comments_
void setSite(size_t siteIndex, const Site &site, bool checkPosition=true)
Set a site in the container.
The SiteContainer interface.
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's alphabet.
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 'pos'.
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.
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_
std::vector< std::string > getSequencesNames() const
Get all the names of the sequences in the container.
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.