69 bitBipartitionList_(),
86 size_t nbword = (
elements_.size() + lword - 1) / lword;
87 size_t nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
89 for (
size_t i = 0; i < nbbip; i++)
92 for (
size_t j = 0; j < nbint; j++)
99 vector<string> underlyingNames;
100 const Tree* tree = &tr;
117 const std::vector<std::string>& elements,
118 const std::vector<int*>& bitBipL) :
119 bitBipartitionList_(),
124 size_t nbword = (elements.size() + lword - 1) / lword;
125 size_t nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
127 for (
size_t i = 0; i < bitBipL.size(); i++)
130 for (
size_t j = 0; j < nbint; j++)
136 vector<string> cpelements_ = elements;
137 std::sort(cpelements_.begin(), cpelements_.end());
138 if (cpelements_ == elements)
147 bitBipartitionList_(),
148 elements_(bipL.elements_),
149 sorted_(bipL.sorted_)
153 size_t nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
160 for (
size_t j = 0; j < nbint; j++)
173 size_t nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
184 for (
size_t j = 0; j < nbint; j++)
209 map<string, bool> bip;
212 throw Exception(
"Bipartition index exceeds BipartitionList size");
214 for (
size_t j = 0; j <
elements_.size(); j++)
229 throw Exception(
"Bipartition index exceeds BipartitionList size");
241 map<string, bool>::iterator it;
243 for (it = bipart.begin(); it != bipart.end(); it++)
245 keys.push_back(it->first);
248 std::sort(elements.begin(), elements.end());
249 std::sort(keys.begin(), keys.end());
251 if (elements == keys)
261 throw Exception(
"Distinct bipartition element sets");
264 size_t nbword = (
elements_.size() + lword - 1) / lword;
265 size_t nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
268 for (
size_t j = 0; j < nbint; j++)
273 for (
size_t i = 0; i <
elements_.size(); i++)
287 throw Exception(
"Bipartition index exceeds BipartitionList size");
301 throw Exception(
"Distinct bipartition element sets");
338 throw Exception(
"Bipartition index exceeds BipartitionList size");
340 throw Exception(
"Bipartition index exceeds BipartitionList size");
343 for (
size_t j = 0; j <
elements_.size(); j++)
372 throw Exception(
"Bipartition index exceeds BipartitionList size");
374 throw Exception(
"Bipartition index exceeds BipartitionList size");
376 uu = uz = zu = zz =
false;
378 for (
size_t j = 0; j <
elements_.size(); j++)
394 if (uu && uz && zu && zz)
421 throw Exception(
"Distinct bipartition element sets");
425 for (
size_t i = 0; i < nbBip; i++)
441 vector<StringAndInt> relements_;
445 for (
size_t i = 0; i <
elements_.size(); i++)
448 sai.
ind =
static_cast<int>(i);
449 relements_.push_back(sai);
452 std::sort(relements_.begin(), relements_.end());
454 for (
size_t i = 0; i <
elements_.size(); i++)
462 size_t nbword = (
elements_.size() + lword - 1) / lword;
463 size_t nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
465 for (
size_t j = nbbip; j < 2 * nbbip; j++)
468 for (
size_t k = 0; k < nbint; k++)
472 for (
size_t i = 0; i <
elements_.size(); i++)
481 for (
size_t j = 0; j < nbbip; j++)
496 throw Exception(
"Bipartition index exceeds BipartitionList size");
498 for (
size_t i = 0; i <
elements_.size(); i++)
515 for (
size_t i = size; i > 0; i--)
526 map<string, bool> bip;
528 for (
size_t i = 0; i <
elements_.size(); i++)
532 for (
size_t i = 0; i <
elements_.size(); i++)
546 vector<int*> sortedBitBipL;
547 vector<IntAndInt> iaiVec;
554 iaiVec.push_back(iai);
557 std::sort(iaiVec.begin(), iaiVec.end());
572 throw Exception(
"Bipartition index exceeds BipartitionList size");
574 size_t nbword = (
elements_.size() + lword - 1) / lword;
575 size_t nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
576 int* flipbip =
new int[nbint];
577 for (
size_t i = 0; i < nbint; i++)
590 bool deletion =
true;
616 unique_ptr<BipartitionList> sortedBipL;
617 vector<int*> sortedBitBipL;
619 vector<Node*> vecNd, sonNd;
621 size_t lword, nbword, nbint, ii;
626 throw Exception(
"Trying to build a tree from incompatible bipartitions");
629 for (
size_t i = 0; i < sortedBipL->getNumberOfBipartitions(); i++)
631 if (sortedBipL->getPartitionSize(i) > sortedBipL->getNumberOfElements() / 2)
634 sortedBipL->sortByPartitionSize();
635 sortedBipL->removeRedundantBipartitions();
636 sortedBitBipL = sortedBipL->getBitBipartitionList();
638 for (
size_t i = 0; i < sortedBipL->getNumberOfBipartitions(); i++)
640 alive.push_back(
true);
642 vecNd.resize(sortedBipL->getNumberOfBipartitions() + 1);
644 nbword = (
elements_.size() + lword - 1) / lword;
645 nbint = nbword * lword / (CHAR_BIT *
sizeof(int));
646 bip =
new int[1]; bip[0] = 0;
649 for (
size_t i = 0; i < sortedBipL->getNumberOfBipartitions(); i++)
651 if (sortedBipL->getPartitionSize(i) == 1)
653 for (
size_t j = 0; j < sortedBipL->getNumberOfElements(); j++)
665 for (
size_t j = 0; j < i; j++)
669 for (ii = 0; ii < nbint; ii++)
672 if (bip[0] != sortedBitBipL[i][ii])
677 sonNd.push_back(vecNd[j]);
682 vecNd[i] =
new Node();
683 for (
size_t k = 0; k < sonNd.size(); k++)
685 vecNd[i]->addSon(sonNd[k]);
692 for (
size_t i = 0; i < sortedBipL->getNumberOfBipartitions(); i++)
699 auto tr = make_unique<TreeTemplate<Node>>(rootNd);
709 vector<string> underelements_, retelements_;
712 underelements_.push_back(nd->
getName());
717 for (
size_t j = 0; j < retelements_.size(); j++)
719 underelements_.push_back(retelements_[j]);
724 return underelements_;
730 return underelements_;
734 if (underelements_.size() <= elements.size() / 2)
739 for (
size_t i = 0; i < elements.size(); i++)
747 for (
size_t i = 0; i < underelements_.size(); i++)
750 while (underelements_[i] != elements[taxa_ind])
761 index->push_back(nd->
getId());
763 return underelements_;
770 vector< map<string, bool>> bipl;
780 for (
size_t j = 0; j < el.size(); j++)
784 mat(j, i) = bipl[i][el[j]];
bool operator<(StringAndInt sai1, StringAndInt sai2)
This class deals with the bipartitions defined by trees.
void addTrivialBipartitions(bool checkExisting)
Adds bipartitions corresponding to external branches if missing.
std::vector< int * > bitBipartitionList_
void flip(size_t i)
Replaces ones by zeros and zeros by ones in the ith bipartition.
void sortByPartitionSize()
Sort bipartitions by partition size.
void addBipartition(std::map< std::string, bool > &bipart, bool checkElements=1)
bool containsBipartition(std::map< std::string, bool > &bipart, bool checkElements=1) const
size_t getNumberOfElements() const
bool haveSameElementsThan(std::map< std::string, bool > &bipart) const
std::vector< std::string > elements_
std::map< std::string, bool > getBipartition(size_t i) const
void removeTrivialBipartitions()
Removes bipartitions corresponding to external branches (1 vs n-1)
std::unique_ptr< TreeTemplate< Node > > toTree() const
Translate into a tree.
BipartitionList(const Tree &tr, bool sorted=true, std::vector< int > *index=0)
The main constructor.
BipartitionList * clone() const
size_t getPartitionSize(size_t i) const
Returns the size of the smallest of the two partitions (e.g. 1 for external branches)
bool areIdentical(size_t k1, size_t k2) const
RowMatrix< int > toMatrix() const
Create a matrix representation of the bifurcations.
bool areCompatible(size_t k1, size_t k2) const
Tells whether 2 bipartitions from the list are compatible.
virtual ~BipartitionList()
size_t getNumberOfBipartitions() const
BipartitionList & operator=(const BipartitionList &bipl)
Assignment operator.
int * getBitBipartition(size_t i)
void removeRedundantBipartitions()
const std::vector< std::string > & getElementNames() const
const std::vector< int * > & getBitBipartitionList() const
void deleteBipartition(size_t i)
bool areAllCompatible() const
Tells whether all bipartitions in the list are compatible with each other.
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
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.
The phylogenetic node class.
virtual std::string getName() const
Get the name associated to this node, if there is one, otherwise throw a NodeException.
virtual int getId() const
Get the node's id.
virtual void addSon(size_t pos, Node *node)
virtual const Node * getSon(size_t pos) const
virtual const Node * getFather() const
Get the father of this node is there is one.
virtual bool hasFather() const
Tell if this node has a father node.
virtual size_t getNumberOfSons() const
The phylogenetic tree class.
virtual N * getRootNode()
Interface for phylogenetic tree objects.
virtual bool isRooted() const =0
Tell if the tree is rooted.
virtual size_t getNumberOfNodes() const =0
virtual std::vector< std::string > getLeavesNames() const =0
Defines the basic types of data flow nodes.