bpp-phyl  2.2.0
BipartitionList.cpp
Go to the documentation of this file.
1 //
2 // File: BipartitionList.cpp
3 // Created by: Nicolas Galtier and Julien Dutheil
4 // Created on: Tue Apr 13 15:09 2007
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for phylogenetic data 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 "BipartitionList.h"
41 #include "BipartitionTools.h"
42 
43 #include "TreeTemplate.h"
44 
45 #include <Bpp/Exceptions.h>
46 #include <Bpp/Text/TextTools.h>
47 #include <Bpp/Io/FileTools.h>
48 #include <Bpp/Numeric/Random/RandomTools.h>
49 
50 // From SeqLib:
51 #include <Bpp/Seq/Alphabet/Alphabet.h>
52 #include <Bpp/Seq/Alphabet/AlphabetTools.h>
53 
54 
55 using namespace bpp;
56 
57 // From the STL:
58 #include <iostream>
59 #include <climits> // defines CHAR_BIT
60 
61 using namespace std;
62 
63 /****************************************************************/
64 /* utilitary classes required for sorting elements/bipartitions */
65 /****************************************************************/
66 
68 {
69 public:
70  int ind;
71  string str;
72 
73 public:
74  StringAndInt() : ind(0),
75  str() {}
76 };
77 
79 {
80  if (sai1.str < sai2.str)
81  return true;
82  return false;
83 }
84 
85 /******************************************************************************/
86 
87 class IntAndInt
88 {
89 public:
90  size_t ind;
91  int val;
92 };
93 
94 bool operator<(IntAndInt iai1, IntAndInt iai2)
95 {
96  if (iai1.val < iai2.val)
97  return true;
98  return false;
99 }
100 
101 
102 /******************************************************************************/
103 
104 BipartitionList::BipartitionList(const Tree& tr, bool sorted, std::vector<int>* index) :
105  bitBipartitionList_(),
106  elements_(),
107  sorted_(sorted)
108 {
109  size_t nbbip;
110 
111  elements_ = tr.getLeavesNames();
112 
113  if (tr.isRooted())
114  nbbip = tr.getNumberOfNodes() - 2;
115  else
116  nbbip = tr.getNumberOfNodes() - 1;
117 
118  if (sorted)
119  std::sort(elements_.begin(), elements_.end());
120 
121  size_t lword = static_cast<size_t>(BipartitionTools::LWORD);
122  size_t nbword = (elements_.size() + lword - 1) / lword;
123  size_t nbint = nbword * lword / (CHAR_BIT * sizeof(int));
124 
125  for (size_t i = 0; i < nbbip; i++)
126  {
127  bitBipartitionList_.push_back(new int[nbint]);
128  for (size_t j = 0; j < nbint; j++)
129  {
130  bitBipartitionList_[i][j] = 0;
131  }
132  }
133 
134  size_t cpt = 0;
135  vector<string> underlyingNames;
136  const Tree* tree = &tr;
137  const TreeTemplate<Node>* ttree = dynamic_cast<const TreeTemplate<Node>*>(tree);
138  if (ttree)
139  {
140  // Gain some time...
142  }
143  else
144  {
145  TreeTemplate<Node> tmp(tr);
147  }
148 }
149 
150 /******************************************************************************/
151 
153  const std::vector<std::string>& elements,
154  const std::vector<int*>& bitBipL) :
155  bitBipartitionList_(),
156  elements_(elements),
157  sorted_()
158 {
159  size_t lword = static_cast<size_t>(BipartitionTools::LWORD);
160  size_t nbword = (elements.size() + lword - 1) / lword;
161  size_t nbint = nbword * lword / (CHAR_BIT * sizeof(int));
162 
163  for (size_t i = 0; i < bitBipL.size(); i++)
164  {
165  bitBipartitionList_.push_back(new int[nbint]);
166  for (size_t j = 0; j < nbint; j++)
167  {
168  bitBipartitionList_[i][j] = bitBipL[i][j];
169  }
170  }
171 
172  vector<string> cpelements_ = elements;
173  std::sort(cpelements_.begin(), cpelements_.end());
174  if (cpelements_ == elements)
175  sorted_ = true;
176  else
177  sorted_ = false;
178 }
179 
180 /******************************************************************************/
181 
183  bitBipartitionList_(),
184  elements_(bipL.elements_),
185  sorted_(bipL.sorted_)
186 {
187  size_t lword = static_cast<size_t>(BipartitionTools::LWORD);
188  size_t nbword = (bipL.getNumberOfElements() + lword - 1) / lword;
189  size_t nbint = nbword * lword / (CHAR_BIT * sizeof(int));
190 
192  vector<int*> bitBipL = bipL.getBitBipartitionList();
193  for (size_t i = 0; i < bipL.getNumberOfBipartitions(); i++)
194  {
195  bitBipartitionList_[i] = new int[nbint];
196  for (size_t j = 0; j < nbint; j++)
197  {
198  bitBipartitionList_[i][j] = bitBipL[i][j];
199  }
200  }
201 }
202 
203 /******************************************************************************/
204 
206 {
207  size_t lword = static_cast<size_t>(BipartitionTools::LWORD);
208  size_t nbword = (bipL.getNumberOfElements() + lword - 1) / lword;
209  size_t nbint = nbword * lword / (CHAR_BIT * sizeof(int));
210 
211  for (size_t i = 0; i < bitBipartitionList_.size(); i++)
212  {
213  delete[] bitBipartitionList_[i];
214  }
216  vector<int*> bitBipL = bipL.getBitBipartitionList();
217  for (size_t i = 0; i < bipL.getNumberOfBipartitions(); i++)
218  {
219  bitBipartitionList_[i] = new int[nbint];
220  for (size_t j = 0; j < nbint; j++)
221  {
222  bitBipartitionList_[i][j] = bitBipL[i][j];
223  }
224  }
225 
226  elements_ = bipL.elements_;
227  sorted_ = bipL.sorted_;
228  return *this;
229 }
230 
231 /******************************************************************************/
232 
234 {
235  for (size_t i = 0; i < bitBipartitionList_.size(); i++)
236  {
237  delete[] bitBipartitionList_[i];
238  }
239 }
240 
241 /******************************************************************************/
242 
243 map<string, bool> BipartitionList::getBipartition(size_t i) const throw (Exception)
244 {
245  map<string, bool> bip;
246 
247  if (i >= bitBipartitionList_.size())
248  throw Exception("Bipartition index exceeds BipartitionList size");
249 
250  for (size_t j = 0; j < elements_.size(); j++)
251  {
252  if (BipartitionTools::testBit(bitBipartitionList_[i], static_cast<int>(j)))
253  bip[elements_[j]] = true;
254  else
255  bip[elements_[j]] = false;
256  }
257  return bip;
258 }
259 
260 /******************************************************************************/
261 
262 int* BipartitionList::getBitBipartition(size_t i) throw (Exception)
263 {
264  if (i >= bitBipartitionList_.size())
265  throw Exception("Bipartition index exceeds BipartitionList size");
266 
267  return bitBipartitionList_[i];
268 }
269 
270 /******************************************************************************/
271 
272 bool BipartitionList::haveSameElementsThan(map<string, bool>& bipart) const
273 {
274  vector<string> elements = elements_;
275  vector<string> keys;
276 
277  map<string, bool>::iterator it;
278 
279  for (it = bipart.begin(); it != bipart.end(); it++)
280  {
281  keys.push_back(it->first);
282  }
283 
284  std::sort(elements.begin(), elements.end());
285  std::sort(keys.begin(), keys.end());
286 
287  if (elements == keys)
288  return true;
289  return false;
290 }
291 
292 /******************************************************************************/
293 
294 void BipartitionList::addBipartition(map<string, bool>& bipart, bool checkElements) throw (Exception)
295 {
296  if (checkElements && !BipartitionList::haveSameElementsThan(bipart))
297  throw Exception("Distinct bipartition element sets");
298 
299  size_t lword = static_cast<size_t>(BipartitionTools::LWORD);
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++)
305  {
306  bitBipartitionList_[ind][j] = 0;
307  }
308 
309  for (size_t i = 0; i < elements_.size(); i++)
310  {
311  if (bipart[elements_[i]] == true)
312  BipartitionTools::bit1(bitBipartitionList_[ind], static_cast<int>(i));
313  else
314  BipartitionTools::bit0(bitBipartitionList_[ind], static_cast<int>(i));
315  }
316 }
317 
318 /******************************************************************************/
319 
320 void BipartitionList::deleteBipartition(size_t i) throw (Exception)
321 {
322  if (i >= bitBipartitionList_.size())
323  throw Exception("Bipartition index exceeds BipartitionList size");
324 
325  delete[] bitBipartitionList_[i];
326  bitBipartitionList_.erase(bitBipartitionList_.begin() + static_cast<ptrdiff_t>(i));
327 }
328 
329 /******************************************************************************/
330 
331 bool BipartitionList::containsBipartition(map<string, bool>& bipart, bool checkElements) const throw (Exception)
332 {
333  size_t i, j;
334  bool dac, padac;
335 
336  if (checkElements && !BipartitionList::haveSameElementsThan(bipart))
337  throw Exception("Distinct bipartition element sets");
338 
339  for (i = 0; i < bitBipartitionList_.size(); i++)
340  {
341  dac = padac = false;
342  for (j = 0; j < elements_.size(); j++)
343  {
344  if (BipartitionTools::testBit(bitBipartitionList_[i], static_cast<int>(j)))
345  {
346  if (bipart[elements_[j]])
347  dac = true;
348  else
349  padac = true;
350  }
351  else
352  {
353  if (bipart[elements_[j]])
354  padac = true;
355  else
356  dac = true;
357  }
358  if (dac && padac)
359  break;
360  }
361  if (j == elements_.size())
362  return true;
363  }
364  return false;
365 }
366 
367 /******************************************************************************/
368 
369 bool BipartitionList::areIdentical(size_t k1, size_t k2) const throw (Exception)
370 {
371  bool dac, padac;
372 
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");
377 
378  dac = padac = false;
379  for (size_t j = 0; j < elements_.size(); j++)
380  {
381  if (BipartitionTools::testBit(bitBipartitionList_[k1], static_cast<int>(j)))
382  {
383  if (BipartitionTools::testBit(bitBipartitionList_[k2], static_cast<int>(j)))
384  dac = true;
385  else
386  padac = true;
387  }
388  else
389  {
390  if (BipartitionTools::testBit(bitBipartitionList_[k2], static_cast<int>(j)))
391  padac = true;
392  else
393  dac = true;
394  }
395  if (dac && padac)
396  return false;
397  }
398  return true;
399 }
400 
401 /******************************************************************************/
402 
403 bool BipartitionList::areCompatible(size_t k1, size_t k2) const throw (Exception)
404 {
405  bool uu, uz, zu, zz;
406 
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");
411 
412  uu = uz = zu = zz = false;
413 
414  for (size_t j = 0; j < elements_.size(); j++)
415  {
416  if (BipartitionTools::testBit(bitBipartitionList_[k1], static_cast<int>(j)))
417  {
418  if (BipartitionTools::testBit(bitBipartitionList_[k2], static_cast<int>(j)))
419  uu = true;
420  else
421  uz = true;
422  }
423  else
424  {
425  if (BipartitionTools::testBit(bitBipartitionList_[k2], static_cast<int>(j)))
426  zu = true;
427  else
428  zz = true;
429  }
430  if (uu && uz && zu && zz)
431  return false;
432  }
433 
434  return true;
435 }
436 
437 /******************************************************************************/
438 
440 {
441  for (size_t i = 0; i < bitBipartitionList_.size(); i++)
442  {
443  for (size_t j = i + 1; j < bitBipartitionList_.size(); j++)
444  {
446  return false;
447  }
448  }
449  return true;
450 }
451 
452 /******************************************************************************/
453 
454 bool BipartitionList::areAllCompatibleWith(map<string, bool>& bipart, bool checkElements) const throw (Exception)
455 {
456  if (checkElements && !haveSameElementsThan(bipart))
457  throw Exception("Distinct bipartition element sets");
458  size_t nbBip = bitBipartitionList_.size();
459  const_cast<BipartitionList*>(this)->addBipartition(bipart, false);
460 
461  for (size_t i = 0; i < nbBip; i++)
462  {
463  if (!areCompatible(i, nbBip))
464  {
465  const_cast<BipartitionList*>(this)->deleteBipartition(nbBip);
466  return false;
467  }
468  }
469  const_cast<BipartitionList*>(this)->deleteBipartition(nbBip);
470  return true;
471 }
472 
473 /******************************************************************************/
474 
476 {
477  vector<StringAndInt> relements_;
478  StringAndInt sai;
479  size_t nbbip;
480 
481  for (size_t i = 0; i < elements_.size(); i++)
482  {
483  sai.str = elements_[i];
484  sai.ind = static_cast<int>(i);
485  relements_.push_back(sai);
486  }
487 
488  std::sort(relements_.begin(), relements_.end());
489 
490  for (size_t i = 0; i < elements_.size(); i++)
491  {
492  elements_[i] = relements_[i].str;
493  }
494 
495  nbbip = bitBipartitionList_.size();
496  bitBipartitionList_.resize(2 * nbbip);
497  size_t lword = static_cast<size_t>(BipartitionTools::LWORD);
498  size_t nbword = (elements_.size() + lword - 1) / lword;
499  size_t nbint = nbword * lword / (CHAR_BIT * sizeof(int));
500 
501  for (size_t j = nbbip; j < 2 * nbbip; j++)
502  {
503  bitBipartitionList_[j] = new int[nbint];
504  for (size_t k = 0; k < nbint; k++)
505  {
506  bitBipartitionList_[j][k] = 0;
507  }
508  for (size_t i = 0; i < elements_.size(); i++)
509  {
510  if (BipartitionTools::testBit(bitBipartitionList_[j - nbbip], relements_[i].ind))
511  BipartitionTools::bit1(bitBipartitionList_[j], static_cast<int>(i));
512  else
513  BipartitionTools::bit0(bitBipartitionList_[j], static_cast<int>(i));
514  }
515  }
516 
517  for (size_t j = 0; j < nbbip; j++)
518  {
519  delete[] bitBipartitionList_[j];
520  }
521 
522  bitBipartitionList_.erase(bitBipartitionList_.begin(), bitBipartitionList_.begin() + static_cast<ptrdiff_t>(nbbip));
523  sorted_ = true;
524 }
525 
526 /******************************************************************************/
527 
528 size_t BipartitionList::getPartitionSize(size_t k) const throw (Exception)
529 {
530  size_t size = 0;
531  if (k >= bitBipartitionList_.size())
532  throw Exception("Bipartition index exceeds BipartitionList size");
533 
534  for (size_t i = 0; i < elements_.size(); i++)
535  {
536  if (BipartitionTools::testBit(bitBipartitionList_[k], static_cast<int>(i)))
537  size++;
538  }
539 
540  if (size <= elements_.size() / 2)
541  return size;
542  else
543  return elements_.size() - size;
544 }
545 
546 /******************************************************************************/
547 
549 {
550  size_t size = bitBipartitionList_.size();
551  for (size_t i = size; i > 0; i--)
552  {
553  if (BipartitionList::getPartitionSize(i - 1) < 2)
555  }
556 }
557 
558 /******************************************************************************/
559 
561 {
562  map<string, bool> bip;
563 
564  for (size_t i = 0; i < elements_.size(); i++)
565  {
566  bip[elements_[i]] = false;
567  }
568  for (size_t i = 0; i < elements_.size(); i++)
569  {
570  bip[elements_[i]] = true;
571  if (checkExisting && BipartitionList::containsBipartition(bip, false))
572  continue;
574  bip[elements_[i]] = false;
575  }
576 }
577 
578 /******************************************************************************/
579 
581 {
582  vector<int*> sortedBitBipL;
583  vector<IntAndInt> iaiVec;
584  IntAndInt iai;
585 
586  for (size_t i = 0; i < bitBipartitionList_.size(); i++)
587  {
588  iai.ind = i;
589  iai.val = static_cast<int>(BipartitionList::getPartitionSize(i));
590  iaiVec.push_back(iai);
591  }
592 
593  std::sort(iaiVec.begin(), iaiVec.end());
594 
595  for (size_t i = 0; i < bitBipartitionList_.size(); i++)
596  {
597  sortedBitBipL.push_back(bitBipartitionList_[iaiVec[i].ind]);
598  }
599 
600  bitBipartitionList_ = sortedBitBipL;
601 }
602 
603 /******************************************************************************/
604 
605 void BipartitionList::flip(size_t k) throw (Exception)
606 {
607  if (k >= bitBipartitionList_.size())
608  throw Exception("Bipartition index exceeds BipartitionList size");
609  size_t lword = static_cast<size_t>(BipartitionTools::LWORD);
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++)
614  {
615  flipbip[i] = 0;
616  }
617  BipartitionTools::bitNot(flipbip, bitBipartitionList_[k], nbint);
618  delete[] bitBipartitionList_[k];
619  bitBipartitionList_[k] = flipbip;
620 }
621 
622 /******************************************************************************/
623 
625 {
626  bool deletion = true;
627 
628  while (deletion)
629  {
630  deletion = false;
631  for (size_t i = 0; i < bitBipartitionList_.size(); i++)
632  {
633  for (size_t j = i + 1; j < bitBipartitionList_.size(); j++)
634  {
636  {
638  deletion = true;
639  break;
640  }
641  }
642  if (deletion)
643  break;
644  }
645  }
646 }
647 
648 /******************************************************************************/
649 
650 TreeTemplate<Node>* BipartitionList::toTree() const throw (Exception)
651 {
652  BipartitionList* sortedBipL;
653  vector<int*> sortedBitBipL;
654  int* bip;
655  vector<Node*> vecNd, sonNd;
656  vector<bool> alive;
657  size_t lword, nbword, nbint, ii;
658 
659  /* check, copy and prepare bipartition list */
660 
662  throw Exception("Trying to build a tree from incompatible bipartitions");
663 
664  sortedBipL = dynamic_cast<BipartitionList*>(clone());
665  for (size_t i = 0; i < sortedBipL->getNumberOfBipartitions(); i++)
666  {
667  if (sortedBipL->getPartitionSize(i) > sortedBipL->getNumberOfElements() / 2)
668  sortedBipL->flip(i);
669  }
670  sortedBipL->sortByPartitionSize();
671  sortedBipL->removeRedundantBipartitions();
672  sortedBitBipL = sortedBipL->getBitBipartitionList();
673 
674  for (size_t i = 0; i < sortedBipL->getNumberOfBipartitions(); i++)
675  {
676  alive.push_back(true);
677  }
678  vecNd.resize(sortedBipL->getNumberOfBipartitions() + 1);
679  lword = static_cast<size_t>(BipartitionTools::LWORD);
680  nbword = (elements_.size() + lword - 1) / lword;
681  nbint = nbword * lword / (CHAR_BIT * sizeof(int));
682  bip = new int[1]; bip[0] = 0;
683 
684  /* main loop: create one node per bipartition */
685  for (size_t i = 0; i < sortedBipL->getNumberOfBipartitions(); i++)
686  {
687  if (sortedBipL->getPartitionSize(i) == 1)
688  { // terminal
689  for (size_t j = 0; j < sortedBipL->getNumberOfElements(); j++)
690  {
691  if (BipartitionTools::testBit(sortedBitBipL[i], static_cast<int>(j)))
692  {
693  vecNd[i] = new Node(elements_[j]);
694  break;
695  }
696  }
697  }
698  else
699  { // internal
700  sonNd.clear();
701  for (size_t j = 0; j < i; j++)
702  {
703  if (alive[j])
704  {
705  for (ii = 0; ii < nbint; ii++)
706  {
707  BipartitionTools::bitOr(bip, sortedBitBipL[j] + ii, sortedBitBipL[i] + ii, 1);
708  if (bip[0] != sortedBitBipL[i][ii])
709  break;
710  }
711  if (ii == nbint)
712  {
713  sonNd.push_back(vecNd[j]);
714  alive[j] = false;
715  }
716  }
717  }
718  vecNd[i] = new Node();
719  for (size_t k = 0; k < sonNd.size(); k++)
720  {
721  vecNd[i]->addSon(sonNd[k]);
722  }
723  }
724  }
725 
726  /* create last node, which joins alive bipartitions = fatherless nodes */
727  Node* rootNd = new Node();
728  for (size_t i = 0; i < sortedBipL->getNumberOfBipartitions(); i++)
729  {
730  if (alive[i])
731  rootNd->addSon(vecNd[i]);
732  }
733 
734  /* construct tree and return */
735  TreeTemplate<Node>* tr = new TreeTemplate<Node>(rootNd);
736  tr->resetNodesId();
737  delete sortedBipL;
738  return tr;
739 }
740 
741 /******************************************************************************/
742 
743 vector<string> BipartitionList::buildBitBipartitions(const Node* nd, vector<int*>& bitbip, const vector<string>& elements, size_t* cpt, vector<int>* index) const
744 {
745  vector<string> underelements_, retelements_;
746 
747  if (nd->getNumberOfSons() == 0)
748  underelements_.push_back(nd->getName());
749 
750  for (size_t i = 0; i < nd->getNumberOfSons(); i++)
751  {
752  retelements_ = BipartitionList::buildBitBipartitions(nd->getSon(i), bitbip, elements, cpt, index);
753  for (size_t j = 0; j < retelements_.size(); j++)
754  {
755  underelements_.push_back(retelements_[j]);
756  }
757  }
758 
759  if (!nd->hasFather())
760  return underelements_; // root node
761 
762  if (!nd->getFather()->hasFather())
763  {
764  size_t nbrootson = nd->getFather()->getNumberOfSons();
765  if (nbrootson == 2 && nd == nd->getFather()->getSon(1))
766  return underelements_; // son 2 of root node when root node has 2 sons
767  }
768 
769  bool ones;
770  if (underelements_.size() <= elements.size() / 2)
771  ones = true;
772  else
773  ones = false;
774 
775  for (size_t i = 0; i < elements.size(); i++)
776  {
777  if (ones)
778  BipartitionTools::bit0(bitbip[*cpt], static_cast<int>(i));
779  else
780  BipartitionTools::bit1(bitbip[*cpt], static_cast<int>(i));
781  }
782 
783  for (size_t i = 0; i < underelements_.size(); i++)
784  {
785  size_t taxa_ind = 0;
786  while (underelements_[i] != elements[taxa_ind])
787  taxa_ind++;
788  if (ones)
789  BipartitionTools::bit1(bitbip[*cpt], static_cast<int>(taxa_ind));
790  else
791  BipartitionTools::bit0(bitbip[*cpt], static_cast<int>(taxa_ind));
792  }
793 
794  (*cpt)++;
795 
796  if (index)
797  index->push_back(nd->getId());
798 
799  return underelements_;
800 }
801 
802 /******************************************************************************/
803 
804 RowMatrix<int> BipartitionList::toMatrix() const
805 {
806  vector< map<string, bool> > bipl;
807  for (size_t i = 0; i < getNumberOfBipartitions(); i++)
808  {
809  bipl.push_back(getBipartition(i));
810  }
811 
812  vector<string> el = getElementNames();
813 
814  RowMatrix<int> mat(el.size(), getNumberOfBipartitions());
815 
816  for (size_t j = 0; j < el.size(); j++)
817  {
818  for (size_t i = 0; i < getNumberOfBipartitions(); i++)
819  {
820  mat(j, i) = bipl[i][el[j]];
821  }
822  }
823  return mat;
824 }
825 
826 /******************************************************************************/
827 
size_t getNumberOfBipartitions() const
virtual N * getRootNode()
Definition: TreeTemplate.h:403
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)
Definition: Node.h:407
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.
Definition: Node.h:236
bool areIdentical(size_t k1, size_t k2) const
int * getBitBipartition(size_t i)
static void bit0(int *list, int num)
Sets bit number num of bit array plist to zero.
void flip(size_t i)
Replaces ones by zeros and zeros by ones in the ith bipartition.
void sortByPartitionSize()
Sort bipartitions by partition size.
static void bit1(int *list, int num)
Sets bit number num of bit array list to one.
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
Definition: Node.h:395
STL namespace.
The phylogenetic tree class.
virtual const Node * getFather() const
Get the father of this node is there is one.
Definition: Node.h:339
std::map< std::string, bool > getBipartition(size_t i) const
Interface for phylogenetic tree objects.
Definition: Tree.h:148
TreeTemplate< Node > * toTree() const
Translate into a tree.
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.
Definition: Node.h:379
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&#39;s id.
Definition: Node.h:203
static int LWORD
Unit length (in bits) of arrays of bits. Must be a multiple of CHAR_BIT*sizeof(int). Default value is 64.
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
static void bitNot(int *listnon, int *list, size_t len)
bit-wise logical NOT
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)
static bool testBit(int *list, int num)
Tells whether bit number num in bit array list is one.
size_t getNumberOfElements() const
virtual std::vector< std::string > getLeavesNames() const =0
The phylogenetic node class.
Definition: Node.h:90
This class deals with the bipartitions defined by trees.
void deleteBipartition(size_t i)
std::vector< int * > bitBipartitionList_
virtual size_t getNumberOfSons() const
Definition: Node.h:388
void resetNodesId()
Number nodes.
Definition: TreeTemplate.h:284
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
static void bitOr(int *listou, int *list1, int *list2, size_t len)
bit-wise logical OR between two arrays of bits