41 #include <Bpp/Text/TextTools.h> 51 CompressedVectorSiteContainer::CompressedVectorSiteContainer(
52 const std::vector<const Site*>& vs,
62 if (vs.size() == 0)
throw Exception(
"CompressedVectorSiteContainer::CompressedVectorSiteContainer. Empty site set.");
64 size_t nbSeq = vs[0]->size();
66 comments_.resize(nbSeq);
67 for (
size_t i = 0; i < nbSeq; i++)
69 names_[i] =
"Seq_" + TextTools::toString(i);
73 for (
size_t i = 0; i < vs.size(); i++)
78 sequences_.resize(nbSeq);
83 CompressedVectorSiteContainer::CompressedVectorSiteContainer(
size_t size,
const Alphabet* alpha) :
92 for (
size_t i = 0; i < size; i++)
94 names_[i] =
"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++)
132 sites_(vsc.sites_.size()),
135 comments_(vsc.getNumberOfSequences()),
136 sequences_(vsc.getNumberOfSequences())
140 for (
size_t i = 0; i < vsc.
sites_.size(); i++)
161 names_(sc.getSequencesNames()),
162 comments_(sc.getNumberOfSequences()),
163 sequences_(sc.getNumberOfSequences())
186 for (
size_t i = 0; i < vsc.
sites_.size(); i++)
194 for (
size_t i = 0; i < nbSeq; i++)
219 for (
size_t i = 0; i < nbSeq; i++)
232 if (i >= getNumberOfSites())
233 throw IndexOutOfBoundsException(
"CompressedVectorSiteContainer::getSite.", i, 0, getNumberOfSites() - 1);
234 return *sites_[index_[i]];
241 if (pos >= getNumberOfSites())
throw IndexOutOfBoundsException(
"CompressedVectorSiteContainer::setSite.", pos, 0, getNumberOfSites() - 1);
244 if (site.size() != getNumberOfSequences())
throw SiteException(
"AlignedSequenceContainer::addSite. Site does not have the appropriate length", &site);
247 if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
250 size_t current = index_[pos];
251 size_t siteIndex = getSiteIndex_(site);
252 if (siteIndex == current)
256 else if (siteIndex < sites_.size())
259 index_[pos] = siteIndex;
263 for (
size_t i = 0; test && i < index_.size(); ++i)
265 if (index_[i] == current)
274 delete sites_[current];
275 sites_.erase(sites_.begin() +
static_cast<ptrdiff_t
>(current));
277 for (
size_t i = 0; i < index_.size(); ++i)
279 if (index_[i] > current) index_[i]--;
286 Site* copy =
dynamic_cast<Site*
>(site.clone());
291 for (
size_t i = 0; test && i < index_.size(); ++i)
293 if (i != pos && index_[i] == current)
302 delete sites_[current];
303 sites_[current] = copy;
308 sites_.push_back(copy);
309 index_[pos] = siteIndex;
318 if (i >= getNumberOfSites())
throw IndexOutOfBoundsException(
"CompressedVectorSiteContainer::removeSite.", i, 0, getNumberOfSites() - 1);
320 Site* site =
dynamic_cast<Site *
>(sites_[index_[i]]->clone());
329 if (siteIndex >= getNumberOfSites())
330 throw IndexOutOfBoundsException(
"CompressedVectorSiteContainer::deleteSite.", siteIndex, 0, getNumberOfSites() - 1);
332 size_t current = index_[siteIndex];
334 for (
size_t j = 0; test && j < index_.size(); ++j)
336 if (j != siteIndex && index_[j] == current)
345 delete sites_[current];
346 sites_.erase(sites_.begin() +
static_cast<ptrdiff_t
>(current));
348 for (
size_t j = 0; j < index_.size(); ++j)
350 if (index_[j] > current) index_[j]--;
353 index_.erase(index_.begin() +
static_cast<ptrdiff_t
>(siteIndex));
361 for (
size_t i = 0; i < length; ++i) {
362 deleteSite(siteIndex + i);
371 if (site.size() != getNumberOfSequences())
throw SiteException(
"CompressedVectorSiteContainer::addSite. Site does not have the appropriate length", &site);
374 if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
379 size_t siteIndex = getSiteIndex_(site);
380 if (siteIndex == sites_.size())
383 Site* copy =
dynamic_cast<Site*
>(site.clone());
384 sites_.push_back(copy);
386 index_.push_back(siteIndex);
393 if (siteIndex >= getNumberOfSites())
throw IndexOutOfBoundsException(
"CompressedVectorSiteContainer::addSite", siteIndex, 0, getNumberOfSites() - 1);
396 if (site.size() != getNumberOfSequences())
throw SiteException(
"CompressedVectorSiteContainer::addSite. Site does not have the appropriate length", &site);
399 if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
404 size_t index = getSiteIndex_(site);
405 if (index == sites_.size())
408 Site* copy =
dynamic_cast<Site*
>(site.clone());
409 sites_.push_back(copy);
411 index_.insert(index_.begin() +
static_cast<ptrdiff_t
>(siteIndex), index);
419 for (vector<Site*>::iterator i =
sites_.begin(); i <
sites_.end(); i++)
421 (*i)->setPosition(pos++);
431 for (
size_t i = 0; i < n; i++)
442 if (i >= getNumberOfSequences())
throw IndexOutOfBoundsException(
"CompressedVectorSiteContainer::getSequence.", i, 0, getNumberOfSequences() - 1);
445 size_t n = getNumberOfSites();
446 vector<int> sequence(n);
447 for (
size_t j = 0; j < n; j++)
449 sequence[j] = sites_[index_[j]]->getContent()[i];
451 if (sequences_[i])
delete sequences_[i];
452 sequences_[i] =
new BasicSequence(names_[i], sequence, *comments_[i], getAlphabet());
453 return *sequences_[i];
461 size_t pos = getSequencePosition(name);
462 return getSequence(pos);
470 for (
size_t pos = 0; pos <
names_.size(); pos++) {
471 if (
names_[pos] == name)
return true;
481 for (
size_t pos = 0; pos < names_.size(); pos++)
483 if (names_[pos] == name)
return pos;
493 for (
size_t i = 0; i <
sites_.size(); i++)
499 for (
size_t i = 0; i <
comments_.size(); i++)
505 for (
size_t i = 0; i <
sequences_.size(); i++)
522 vector<string> seqnames(
names_.size());
523 for (
size_t i = 0; i <
names_.size(); i++)
533 const vector<string>& names,
537 if (names.size() != getNumberOfSequences())
538 throw IndexOutOfBoundsException(
"CompressedVectorSiteContainer::setSequenceNames: bad number of names.", names.size(), getNumberOfSequences(), getNumberOfSequences());
541 for (
size_t i = 0; i < names.size(); i++)
544 for (
size_t j = 0; j < i; j++)
546 if (names[j] == names[i])
547 throw Exception(
"CompressedVectorSiteContainer::setSequencesNames : Sequence's name already exists in container");
551 for (
size_t i = 0; i < names.size(); i++)
553 names_[i] = names[i];
561 comments_[sequenceIndex] =
new Comments(comments);
577 size_t pos =
sites_.size();
579 for (
size_t i = 0; i <
sites_.size(); ++i)
582 for (
size_t j = 0; test && j < site.
size(); ++j)
584 if (site[j] != (*
sites_[i])[j])
Exception thrown when a sequence is not found The sequence not found exception base class...
std::vector< std::string > Comments
Declaration of Comments type.
const Site & getSite(size_t siteIndex) const
Get a site from the container.
The SiteContainer interface.
This alphabet is used to deal NumericAlphabet.
The site exception base class.
const Alphabet * getAlphabet() const
Get sequence container's alphabet.
std::vector< size_t > index_
const Sequence & getSequence(size_t sequenceIndex) const
Retrieve a sequence object from the container.
void setSite(size_t siteIndex, const Site &site, bool checkPosition=false)
Set a site in the container.
void reindexSites()
Set all positions attributes.
CompressedVectorSiteContainer(const std::vector< const Site *> &vs, const Alphabet *alpha)
Build a new container from a set of sites.
AbstractSequenceContainer & operator=(const AbstractSequenceContainer &sc)
Partial implementation of the OrderedSequenceContainer interface.
size_t getNumberOfSites() const
Get the number of sites in the container.
CompressedVectorSiteContainer * createEmptyContainer() const
Return a copy of this container, but with no sequence inside.
const Comments & getGeneralComments() const
Get the comments of this container.
virtual const Comments & getComments(size_t sequenceIndex) const =0
Get comments of a particular sequence.
Vint getSitePositions() const
Get all position attributes of sites.
A low memory, yet restricted, version of the VectorSiteContainer class.
std::vector< Sequence * > sequences_
size_t getNumberOfSequences() const
Get the number of sequences in the container.
bool hasSequence(const std::string &name) const
Check if a sequence with a given name is present in the container.
void addSite(const Site &site, bool checkPosition=false)
Add a site in the container.
void setSequencesNames(const std::vector< std::string > &names, bool checkNames=true)
Set all sequence names.
std::vector< std::string > getSequencesNames() const
Get all the names of the sequences in the container.
virtual const Site & getSite(size_t siteIndex) const =0
Get a site from the container.
void clear()
Delete all sequences in the container.
CompressedVectorSiteContainer & operator=(const CompressedVectorSiteContainer &vsc)
void deleteSite(size_t siteIndex)
Delete a site in the container.
A basic implementation of the Sequence interface.
virtual size_t size() const
Get the number of elements in the list.
void setComments(size_t sequenceIndex, const Comments &comments)
Set the comments of a particular sequence.
size_t getSequencePosition(const std::string &name) const
Get the position of a sequence in sequence container from its name.
std::vector< std::string > names_
virtual size_t getNumberOfSites() const =0
Get the number of sites in the container.
size_t getSiteIndex_(const Site &site)
std::vector< Site * > sites_
Exception thrown when two alphabets do not match.
std::vector< Comments * > comments_
virtual std::vector< std::string > getSequencesNames() const =0
Get all the names of the sequences in the container.
Site * removeSite(size_t siteIndex)
Remove a site from the container.
void deleteSites(size_t siteIndex, size_t length)
Delete a continuous range of sites in the container.
void setGeneralComments(const Comments &comments)
Set the comments of this 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.