45 #include <Bpp/Exceptions.h> 46 #include <Bpp/Text/TextTools.h> 47 #include <Bpp/Io/FileTools.h> 48 #include <Bpp/Numeric/Random/RandomTools.h> 51 #include <Bpp/Seq/Alphabet/Alphabet.h> 52 #include <Bpp/Seq/Alphabet/AlphabetTools.h> 105 bitBipartitionList_(),
122 size_t nbword = (
elements_.size() + lword - 1) / lword;
123 size_t nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
125 for (
size_t i = 0; i < nbbip; i++)
128 for (
size_t j = 0; j < nbint; j++)
135 vector<string> underlyingNames;
136 const Tree* tree = &tr;
153 const std::vector<std::string>& elements,
154 const std::vector<int*>& bitBipL) :
155 bitBipartitionList_(),
160 size_t nbword = (elements.size() + lword - 1) / lword;
161 size_t nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
163 for (
size_t i = 0; i < bitBipL.size(); i++)
166 for (
size_t j = 0; j < nbint; j++)
172 vector<string> cpelements_ = elements;
173 std::sort(cpelements_.begin(), cpelements_.end());
174 if (cpelements_ == elements)
183 bitBipartitionList_(),
184 elements_(bipL.elements_),
185 sorted_(bipL.sorted_)
189 size_t nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
196 for (
size_t j = 0; j < nbint; j++)
209 size_t nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
220 for (
size_t j = 0; j < nbint; j++)
245 map<string, bool> bip;
247 if (i >= bitBipartitionList_.size())
248 throw Exception(
"Bipartition index exceeds BipartitionList size");
250 for (
size_t j = 0; j < elements_.size(); j++)
253 bip[elements_[j]] =
true;
255 bip[elements_[j]] =
false;
264 if (i >= bitBipartitionList_.size())
265 throw Exception(
"Bipartition index exceeds BipartitionList size");
267 return bitBipartitionList_[i];
277 map<string, bool>::iterator it;
279 for (it = bipart.begin(); it != bipart.end(); it++)
281 keys.push_back(it->first);
284 std::sort(elements.begin(), elements.end());
285 std::sort(keys.begin(), keys.end());
287 if (elements == keys)
297 throw Exception(
"Distinct bipartition element sets");
300 size_t nbword = (elements_.size() + lword - 1) / lword;
301 size_t nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
302 bitBipartitionList_.push_back(
new int[nbint]);
303 size_t ind = bitBipartitionList_.size() - 1;
304 for (
size_t j = 0; j < nbint; j++)
306 bitBipartitionList_[ind][j] = 0;
309 for (
size_t i = 0; i < elements_.size(); i++)
311 if (bipart[elements_[i]] ==
true)
322 if (i >= bitBipartitionList_.size())
323 throw Exception(
"Bipartition index exceeds BipartitionList size");
325 delete[] bitBipartitionList_[i];
326 bitBipartitionList_.erase(bitBipartitionList_.begin() +
static_cast<ptrdiff_t
>(i));
337 throw Exception(
"Distinct bipartition element sets");
339 for (i = 0; i < bitBipartitionList_.size(); i++)
342 for (j = 0; j < elements_.size(); j++)
346 if (bipart[elements_[j]])
353 if (bipart[elements_[j]])
361 if (j == elements_.size())
373 if (k1 >= bitBipartitionList_.size())
374 throw Exception(
"Bipartition index exceeds BipartitionList size");
375 if (k2 >= bitBipartitionList_.size())
376 throw Exception(
"Bipartition index exceeds BipartitionList size");
379 for (
size_t j = 0; j < elements_.size(); j++)
407 if (k1 >= bitBipartitionList_.size())
408 throw Exception(
"Bipartition index exceeds BipartitionList size");
409 if (k2 >= bitBipartitionList_.size())
410 throw Exception(
"Bipartition index exceeds BipartitionList size");
412 uu = uz = zu = zz =
false;
414 for (
size_t j = 0; j < elements_.size(); j++)
430 if (uu && uz && zu && zz)
456 if (checkElements && !haveSameElementsThan(bipart))
457 throw Exception(
"Distinct bipartition element sets");
458 size_t nbBip = bitBipartitionList_.size();
461 for (
size_t i = 0; i < nbBip; i++)
463 if (!areCompatible(i, nbBip))
477 vector<StringAndInt> relements_;
481 for (
size_t i = 0; i <
elements_.size(); i++)
484 sai.
ind =
static_cast<int>(i);
485 relements_.push_back(sai);
488 std::sort(relements_.begin(), relements_.end());
490 for (
size_t i = 0; i <
elements_.size(); i++)
498 size_t nbword = (
elements_.size() + lword - 1) / lword;
499 size_t nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
501 for (
size_t j = nbbip; j < 2 * nbbip; j++)
504 for (
size_t k = 0; k < nbint; k++)
508 for (
size_t i = 0; i <
elements_.size(); i++)
517 for (
size_t j = 0; j < nbbip; j++)
531 if (k >= bitBipartitionList_.size())
532 throw Exception(
"Bipartition index exceeds BipartitionList size");
534 for (
size_t i = 0; i < elements_.size(); i++)
540 if (size <= elements_.size() / 2)
543 return elements_.size() - size;
551 for (
size_t i = size; i > 0; i--)
562 map<string, bool> bip;
564 for (
size_t i = 0; i <
elements_.size(); i++)
568 for (
size_t i = 0; i <
elements_.size(); i++)
582 vector<int*> sortedBitBipL;
583 vector<IntAndInt> iaiVec;
590 iaiVec.push_back(iai);
593 std::sort(iaiVec.begin(), iaiVec.end());
607 if (k >= bitBipartitionList_.size())
608 throw Exception(
"Bipartition index exceeds BipartitionList size");
610 size_t nbword = (elements_.size() + lword - 1) / lword;
611 size_t nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
612 int* flipbip =
new int[nbint];
613 for (
size_t i = 0; i < nbint; i++)
618 delete[] bitBipartitionList_[k];
619 bitBipartitionList_[k] = flipbip;
626 bool deletion =
true;
653 vector<int*> sortedBitBipL;
655 vector<Node*> vecNd, sonNd;
657 size_t lword, nbword, nbint, ii;
662 throw Exception(
"Trying to build a tree from incompatible bipartitions");
676 alive.push_back(
true);
680 nbword = (
elements_.size() + lword - 1) / lword;
681 nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
682 bip =
new int[1]; bip[0] = 0;
701 for (
size_t j = 0; j < i; j++)
705 for (ii = 0; ii < nbint; ii++)
708 if (bip[0] != sortedBitBipL[i][ii])
713 sonNd.push_back(vecNd[j]);
718 vecNd[i] =
new Node();
719 for (
size_t k = 0; k < sonNd.size(); k++)
721 vecNd[i]->addSon(sonNd[k]);
745 vector<string> underelements_, retelements_;
748 underelements_.push_back(nd->
getName());
753 for (
size_t j = 0; j < retelements_.size(); j++)
755 underelements_.push_back(retelements_[j]);
760 return underelements_;
766 return underelements_;
770 if (underelements_.size() <= elements.size() / 2)
775 for (
size_t i = 0; i < elements.size(); i++)
783 for (
size_t i = 0; i < underelements_.size(); i++)
786 while (underelements_[i] != elements[taxa_ind])
797 index->push_back(nd->
getId());
799 return underelements_;
806 vector< map<string, bool> > bipl;
816 for (
size_t j = 0; j < el.size(); j++)
820 mat(j, i) = bipl[i][el[j]];
size_t getNumberOfBipartitions() const
virtual N * getRootNode()
BipartitionList & operator=(const BipartitionList &bipl)
Assignment operator.
RowMatrix< int > toMatrix() const
Create a matrix representation of the bifurcations.
virtual void addSon(size_t pos, Node *node)
virtual size_t getNumberOfNodes() const =0
bool haveSameElementsThan(std::map< std::string, bool > &bipart) const
virtual std::string getName() const
Get the name associated to this node, if there is one, otherwise throw a NodeException.
bool areIdentical(size_t k1, size_t k2) const
int * getBitBipartition(size_t i)
void flip(size_t i)
Replaces ones by zeros and zeros by ones in the ith bipartition.
virtual ~BipartitionList()
void sortByPartitionSize()
Sort bipartitions by partition size.
bool areCompatible(size_t k1, size_t k2) const
Tells whether 2 bipartitions from the list are compatible.
virtual const Node * getSon(size_t pos) const
The phylogenetic tree class.
virtual const Node * getFather() const
Get the father of this node is there is one.
std::map< std::string, bool > getBipartition(size_t i) const
Interface for phylogenetic tree objects.
TreeTemplate< Node > * toTree() const
Translate into a tree.
void removeRedundantBipartitions()
const std::vector< std::string > & getElementNames() const
bool areAllCompatibleWith(std::map< std::string, bool > &bipart, bool checkElements=true) const
Tells whether all bipartitions in the list are compatible with a given bipartition.
void addTrivialBipartitions(bool checkExisting)
Adds bipartitions corresponding to external branches if missing.
virtual bool hasFather() const
Tell if this node has a father node.
bool areAllCompatible() const
Tells whether all bipartitions in the list are compatible with each other.
bool operator<(StringAndInt sai1, StringAndInt sai2)
std::vector< std::string > elements_
bool containsBipartition(std::map< std::string, bool > &bipart, bool checkElements=1) const
virtual int getId() const
Get the node's id.
std::vector< std::string > buildBitBipartitions(const Node *nd, std::vector< int *> &bitbip, const std::vector< std::string > &elements, size_t *cpt, std::vector< int > *index) const
BipartitionList * clone() const
BipartitionList(const Tree &tr, bool sorted=true, std::vector< int > *index=0)
The main contructor.
void removeTrivialBipartitions()
Removes bipartitions corresponding to external branches (1 vs n-1)
void addBipartition(std::map< std::string, bool > &bipart, bool checkElements=1)
size_t getNumberOfElements() const
virtual std::vector< std::string > getLeavesNames() const =0
The phylogenetic node class.
This class deals with the bipartitions defined by trees.
void deleteBipartition(size_t i)
std::vector< int * > bitBipartitionList_
virtual size_t getNumberOfSons() const
void resetNodesId()
Number nodes.
size_t getPartitionSize(size_t i) const
Returns the size of the smallest of the two partitions (e.g. 1 for external branches) ...
virtual bool isRooted() const =0
Tell if the tree is rooted.
const std::vector< int * > & getBitBipartitionList() const