bpp-phyl  2.2.0
TreeTools.cpp
Go to the documentation of this file.
1 //
2 // File: TreeTools.cpp
3 // Created by: Julien Dutheil
4 // Created on: Wed Aug 6 13:45:28 2003
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 "TreeTools.h"
41 #include "Tree.h"
42 #include "BipartitionTools.h"
43 #include "Model/Nucleotide/JCnuc.h"
45 #include "Distance/BioNJ.h"
47 #include "OptimizationTools.h"
48 
49 #include <Bpp/Text/TextTools.h>
50 #include <Bpp/Text/StringTokenizer.h>
51 #include <Bpp/Numeric/Number.h>
52 #include <Bpp/BppString.h>
53 #include <Bpp/App/ApplicationTools.h>
54 #include <Bpp/Numeric/VectorTools.h>
55 #include <Bpp/Numeric/Prob/ConstantDistribution.h>
56 #include <Bpp/Numeric/Matrix/MatrixTools.h>
57 
58 // From bpp-seq:
59 #include <Bpp/Seq/Alphabet/DNA.h>
60 #include <Bpp/Seq/Container/VectorSiteContainer.h>
61 
62 using namespace bpp;
63 
64 // From the STL:
65 #include <iostream>
66 #include <sstream>
67 
68 using namespace std;
69 
70 /******************************************************************************/
71 
72 const string TreeTools::BOOTSTRAP = "bootstrap";
73 
74 /******************************************************************************/
75 
76 vector<int> TreeTools::getLeavesId(const Tree& tree, int nodeId) throw (NodeNotFoundException)
77 {
78  vector<int> leaves;
79  getLeavesId(tree, nodeId, leaves);
80  return leaves;
81 }
82 
83 void TreeTools::getLeavesId(const Tree& tree, int nodeId, std::vector<int>& leaves) throw (NodeNotFoundException)
84 {
85  if (!tree.hasNode(nodeId))
86  throw NodeNotFoundException("TreeTools::getLeavesId", nodeId);
87  if (tree.isLeaf(nodeId))
88  {
89  leaves.push_back(nodeId);
90  }
91  vector<int> sons = tree.getSonsId(nodeId);
92  for (size_t i = 0; i < sons.size(); i++)
93  {
94  getLeavesId(tree, sons[i], leaves);
95  }
96 }
97 
98 size_t TreeTools::getNumberOfLeaves(const Tree& tree, int nodeId) throw (NodeNotFoundException)
99 {
100  if (!tree.hasNode(nodeId))
101  throw NodeNotFoundException("TreeTools::getNumberOfLeaves", nodeId);
102 
103  size_t n = 0;
104  if (tree.isLeaf(nodeId))
105  {
106  ++n;
107  }
108  vector<int> sons = tree.getSonsId(nodeId);
109  for (size_t i = 0; i < sons.size(); ++i)
110  {
111  n += getNumberOfLeaves(tree, sons[i]);
112  }
113  return n;
114 }
115 
116 /******************************************************************************/
117 
118 int TreeTools::getLeafId(const Tree& tree, int nodeId, const std::string& name)
119 throw (NodeNotFoundException)
120 {
121  int* id = NULL;
122  searchLeaf(tree, nodeId, name, id);
123  if (id == NULL)
124  throw NodeNotFoundException("TreeTools::getLeafId().", name);
125  else
126  {
127  int i = *id;
128  delete id;
129  return i;
130  }
131 }
132 
133 void TreeTools::searchLeaf(const Tree& tree, int nodeId, const string& name, int*& id)
134 throw (NodeNotFoundException)
135 {
136  if (tree.isLeaf(nodeId))
137  {
138  if (tree.getNodeName(nodeId) == name)
139  {
140  id = new int(nodeId);
141  return;
142  }
143  }
144  vector<int> sons;
145  for (size_t i = 0; i < sons.size(); i++)
146  {
147  searchLeaf(tree, nodeId, name, id);
148  }
149 }
150 
151 /******************************************************************************/
152 
153 vector<int> TreeTools::getNodesId(const Tree& tree, int nodeId) throw (NodeNotFoundException)
154 {
155  vector<int> nodes;
156  getNodesId(tree, nodeId, nodes);
157  return nodes;
158 }
159 
160 void TreeTools::getNodesId(const Tree& tree, int nodeId, vector<int>& nodes) throw (NodeNotFoundException)
161 {
162  if (!tree.hasNode(nodeId))
163  throw NodeNotFoundException("TreeTools::getNodesId", nodeId);
164  vector<int> sons = tree.getSonsId(nodeId);
165  for (size_t i = 0; i < sons.size(); i++)
166  {
167  getNodesId(tree, sons[i], nodes);
168  }
169  nodes.push_back(nodeId);
170 }
171 
172 /******************************************************************************/
173 
174 size_t TreeTools::getDepth(const Tree& tree, int nodeId) throw (NodeNotFoundException)
175 {
176  if (!tree.hasNode(nodeId))
177  throw NodeNotFoundException("TreeTools::getDepth", nodeId);
178  size_t d = 0;
179  vector<int> sons = tree.getSonsId(nodeId);
180  for (size_t i = 0; i < sons.size(); i++)
181  {
182  size_t c = getDepth(tree, sons[i]) + 1;
183  if (c > d)
184  d = c;
185  }
186  return d;
187 }
188 
189 /******************************************************************************/
190 
191 size_t TreeTools::getDepths(const Tree& tree, int nodeId, map<int, size_t>& depths) throw (NodeNotFoundException)
192 {
193  if (!tree.hasNode(nodeId))
194  throw NodeNotFoundException("TreeTools::getDepth", nodeId);
195  size_t d = 0;
196  vector<int> sons = tree.getSonsId(nodeId);
197  for (size_t i = 0; i < sons.size(); i++)
198  {
199  size_t c = getDepths(tree, sons[i], depths) + 1;
200  if (c > d)
201  d = c;
202  }
203  depths[nodeId] = d;
204  return d;
205 }
206 
207 /******************************************************************************/
208 
209 double TreeTools::getHeight(const Tree& tree, int nodeId) throw (NodeNotFoundException, NodeException)
210 {
211  if (!tree.hasNode(nodeId))
212  throw NodeNotFoundException("TreeTools::getHeight", nodeId);
213  double d = 0;
214  vector<int> sons = tree.getSonsId(nodeId);
215  for (size_t i = 0; i < sons.size(); i++)
216  {
217  double dist = 0;
218  if (tree.hasDistanceToFather(sons[i]))
219  dist = tree.getDistanceToFather(sons[i]);
220  else
221  throw NodeException("Node without branch length.", sons[i]);
222  double c = getHeight(tree, sons[i]) + dist;
223  if (c > d)
224  d = c;
225  }
226  return d;
227 }
228 
229 /******************************************************************************/
230 
231 double TreeTools::getHeights(const Tree& tree, int nodeId, map<int, double>& heights) throw (NodeNotFoundException, NodeException)
232 {
233  if (!tree.hasNode(nodeId))
234  throw NodeNotFoundException("TreeTools::getHeight", nodeId);
235  double d = 0;
236  vector<int> sons = tree.getSonsId(nodeId);
237  for (size_t i = 0; i < sons.size(); i++)
238  {
239  double dist = 0;
240  if (tree.hasDistanceToFather(sons[i]))
241  dist = tree.getDistanceToFather(sons[i]);
242  else
243  throw NodeException("Node without branch length.", sons[i]);
244  double c = getHeights(tree, sons[i], heights) + dist;
245  if (c > d)
246  d = c;
247  }
248  heights[nodeId] = d;
249  return d;
250 }
251 
252 /******************************************************************************/
253 
254 string TreeTools::nodeToParenthesis(const Tree& tree, int nodeId, bool writeId) throw (NodeNotFoundException)
255 {
256  if (!tree.hasNode(nodeId))
257  throw NodeNotFoundException("TreeTools::nodeToParenthesis", nodeId);
258  ostringstream s;
259  if (tree.isLeaf(nodeId))
260  {
261  s << tree.getNodeName(nodeId);
262  }
263  else
264  {
265  s << "(";
266  vector<int> sonsId = tree.getSonsId(nodeId);
267  s << nodeToParenthesis(tree, sonsId[0], writeId);
268  for (size_t i = 1; i < sonsId.size(); i++)
269  {
270  s << "," << nodeToParenthesis(tree, sonsId[i], writeId);
271  }
272  s << ")";
273  }
274  if (writeId)
275  {
276  if (tree.isLeaf(nodeId))
277  s << "_";
278  s << nodeId;
279  }
280  else
281  {
282  if (tree.hasBranchProperty(nodeId, BOOTSTRAP))
283  s << (dynamic_cast<const Number<double>*>(tree.getBranchProperty(nodeId, BOOTSTRAP))->getValue());
284  }
285  if (tree.hasDistanceToFather(nodeId))
286  s << ":" << tree.getDistanceToFather(nodeId);
287  return s.str();
288 }
289 
290 /******************************************************************************/
291 
292 string TreeTools::nodeToParenthesis(const Tree& tree, int nodeId, bool bootstrap, const string& propertyName) throw (NodeNotFoundException)
293 {
294  if (!tree.hasNode(nodeId))
295  throw NodeNotFoundException("TreeTools::nodeToParenthesis", nodeId);
296  ostringstream s;
297  if (tree.isLeaf(nodeId))
298  {
299  s << tree.getNodeName(nodeId);
300  }
301  else
302  {
303  s << "(";
304  vector<int> sonsId = tree.getSonsId(nodeId);
305  s << nodeToParenthesis(tree, sonsId[0], bootstrap, propertyName);
306  for (size_t i = 1; i < sonsId.size(); i++)
307  {
308  s << "," << nodeToParenthesis(tree, sonsId[i], bootstrap, propertyName);
309  }
310  s << ")";
311 
312  if (bootstrap)
313  {
314  if (tree.hasBranchProperty(nodeId, BOOTSTRAP))
315  s << (dynamic_cast<const Number<double>*>(tree.getBranchProperty(nodeId, BOOTSTRAP))->getValue());
316  }
317  else
318  {
319  if (tree.hasBranchProperty(nodeId, propertyName))
320  s << *(dynamic_cast<const BppString*>(tree.getBranchProperty(nodeId, propertyName)));
321  }
322  }
323  if (tree.hasDistanceToFather(nodeId))
324  s << ":" << tree.getDistanceToFather(nodeId);
325  return s.str();
326 }
327 
328 /******************************************************************************/
329 
330 string TreeTools::treeToParenthesis(const Tree& tree, bool writeId)
331 {
332  ostringstream s;
333  s << "(";
334  int rootId = tree.getRootId();
335  vector<int> sonsId = tree.getSonsId(rootId);
336  if (tree.isLeaf(rootId))
337  {
338  s << tree.getNodeName(rootId);
339  for (size_t i = 0; i < sonsId.size(); i++)
340  {
341  s << "," << nodeToParenthesis(tree, sonsId[i], writeId);
342  }
343  }
344  else
345  {
346  if (sonsId.size() > 0)
347  {
348  s << nodeToParenthesis(tree, sonsId[0], writeId);
349  for (size_t i = 1; i < sonsId.size(); i++)
350  {
351  s << "," << nodeToParenthesis(tree, sonsId[i], writeId);
352  }
353  }
354  // Otherwise, this is an empty tree!
355  }
356  s << ");" << endl;
357  return s.str();
358 }
359 
360 /******************************************************************************/
361 
362 string TreeTools::treeToParenthesis(const Tree& tree, bool bootstrap, const string& propertyName)
363 {
364  ostringstream s;
365  s << "(";
366  int rootId = tree.getRootId();
367  vector<int> sonsId = tree.getSonsId(rootId);
368  if (tree.isLeaf(rootId))
369  {
370  s << tree.getNodeName(rootId);
371  for (size_t i = 0; i < sonsId.size(); i++)
372  {
373  s << "," << nodeToParenthesis(tree, sonsId[i], bootstrap, propertyName);
374  }
375  }
376  else
377  {
378  s << nodeToParenthesis(tree, sonsId[0], bootstrap, propertyName);
379  for (size_t i = 1; i < sonsId.size(); i++)
380  {
381  s << "," << nodeToParenthesis(tree, sonsId[i], bootstrap, propertyName);
382  }
383  }
384  s << ")";
385  if (bootstrap)
386  {
387  if (tree.hasBranchProperty(rootId, BOOTSTRAP))
388  s << (dynamic_cast<const Number<double>*>(tree.getBranchProperty(rootId, BOOTSTRAP))->getValue());
389  }
390  else
391  {
392  if (tree.hasBranchProperty(rootId, propertyName))
393  s << *(dynamic_cast<const BppString*>(tree.getBranchProperty(rootId, propertyName)));
394  }
395  s << ";" << endl;
396  return s.str();
397 }
398 
399 /******************************************************************************/
400 
401 vector<int> TreeTools::getPathBetweenAnyTwoNodes(const Tree& tree, int nodeId1, int nodeId2, bool includeAncestor)
402 throw (NodeNotFoundException)
403 {
404  if (!tree.hasNode(nodeId1))
405  throw NodeNotFoundException("TreeTools::getPathBetweenAnyTwoNodes", nodeId1);
406  if (!tree.hasNode(nodeId2))
407  throw NodeNotFoundException("TreeTools::getPathBetweenAnyTwoNodes", nodeId2);
408  vector<int> path;
409  vector<int> pathMatrix1;
410  vector<int> pathMatrix2;
411 
412  int nodeUp = nodeId1;
413  while (tree.hasFather(nodeUp))
414  {
415  pathMatrix1.push_back(nodeUp);
416  nodeUp = tree.getFatherId(nodeUp);
417  }
418  pathMatrix1.push_back(nodeUp); // The root.
419 
420  nodeUp = nodeId2;
421  while (tree.hasFather(nodeUp))
422  {
423  pathMatrix2.push_back(nodeUp);
424  nodeUp = tree.getFatherId(nodeUp);
425  }
426  pathMatrix2.push_back(nodeUp); // The root.
427  // Must check that the two nodes have the same root!!!
428 
429  size_t tmp1 = pathMatrix1.size();
430  size_t tmp2 = pathMatrix2.size();
431 
432  while ((tmp1 > 0) && (tmp2 > 0))
433  {
434  if (pathMatrix1[tmp1 - 1] != pathMatrix2[tmp2 - 1])
435  break;
436  tmp1--; tmp2--;
437  }
438  // (tmp1 - 1) and (tmp2 - 1) now point toward the first non-common nodes
439 
440  for (size_t y = 0; y < tmp1; ++y)
441  {
442  path.push_back(pathMatrix1[y]);
443  }
444  if (includeAncestor)
445  path.push_back(pathMatrix1[tmp1]); // pushing once, the Node that was common to both.
446  for (size_t j = tmp2; j > 0; --j)
447  {
448  path.push_back(pathMatrix2[j - 1]);
449  }
450  return path;
451 }
452 
453 /******************************************************************************/
454 
455 double TreeTools::getDistanceBetweenAnyTwoNodes(const Tree& tree, int nodeId1, int nodeId2)
456 {
457  if (!tree.hasNode(nodeId1))
458  throw NodeNotFoundException("TreeTools::getDistanceBetweenAnyTwoNodes", nodeId1);
459  if (!tree.hasNode(nodeId2))
460  throw NodeNotFoundException("TreeTools::getDistanceBetweenAnyTwoNodes", nodeId2);
461  vector<int> path = getPathBetweenAnyTwoNodes(tree, nodeId1, nodeId2, false);
462  double d = 0;
463  for (size_t i = 0; i < path.size(); i++)
464  {
465  d += tree.getDistanceToFather(path[i]);
466  }
467  return d;
468 }
469 
470 /******************************************************************************/
471 
472 Vdouble TreeTools::getBranchLengths(const Tree& tree, int nodeId) throw (NodeNotFoundException, NodeException)
473 {
474  if (!tree.hasNode(nodeId))
475  throw NodeNotFoundException("TreeTools::getBranchLengths", nodeId);
476  Vdouble brLen(1);
477  if (tree.hasDistanceToFather(nodeId))
478  brLen[0] = tree.getDistanceToFather(nodeId);
479  else
480  throw NodeException("TreeTools::getbranchLengths(). No branch length.", nodeId);
481  vector<int> sons = tree.getSonsId(nodeId);
482  for (size_t i = 0; i < sons.size(); i++)
483  {
484  Vdouble sonBrLen = getBranchLengths(tree, sons[i]);
485  for (size_t j = 0; j < sonBrLen.size(); j++)
486  {
487  brLen.push_back(sonBrLen[j]);
488  }
489  }
490  return brLen;
491 }
492 
493 /******************************************************************************/
494 
495 double TreeTools::getTotalLength(const Tree& tree, int nodeId, bool includeAncestor) throw (NodeNotFoundException, NodeException)
496 {
497  if (!tree.hasNode(nodeId))
498  throw NodeNotFoundException("TreeTools::getTotalLength", nodeId);
499  if (includeAncestor && !tree.hasDistanceToFather(nodeId))
500  throw NodeException("TreeTools::getTotalLength(). No branch length.", nodeId);
501  double length = includeAncestor ? tree.getDistanceToFather(nodeId) : 0;
502  vector<int> sons = tree.getSonsId(nodeId);
503  for (size_t i = 0; i < sons.size(); i++)
504  {
505  length += getTotalLength(tree, sons[i], true);
506  }
507  return length;
508 }
509 
510 /******************************************************************************/
511 
512 void TreeTools::setBranchLengths(Tree& tree, int nodeId, double brLen) throw (NodeNotFoundException)
513 {
514  if (!tree.hasNode(nodeId))
515  throw NodeNotFoundException("TreeTools::setBranchLengths", nodeId);
516  vector<int> nodes = getNodesId(tree, nodeId);
517  for (size_t i = 0; i < nodes.size(); i++)
518  {
519  tree.setDistanceToFather(nodes[i], brLen);
520  }
521 }
522 
523 /******************************************************************************/
524 
525 void TreeTools::setVoidBranchLengths(Tree& tree, int nodeId, double brLen) throw (NodeNotFoundException)
526 {
527  if (!tree.hasNode(nodeId))
528  throw NodeNotFoundException("TreeTools::setVoidBranchLengths", nodeId);
529  vector<int> nodes = getNodesId(tree, nodeId);
530  for (size_t i = 0; i < nodes.size(); i++)
531  {
532  if (!tree.hasDistanceToFather(nodes[i]))
533  tree.setDistanceToFather(nodes[i], brLen);
534  }
535 }
536 
537 /******************************************************************************/
538 
539 void TreeTools::scaleTree(Tree& tree, int nodeId, double factor) throw (NodeNotFoundException, NodeException)
540 {
541  if (!tree.hasNode(nodeId))
542  throw NodeNotFoundException("TreeTools::scaleTree", nodeId);
543  vector<int> nodes = getNodesId(tree, nodeId);
544  for (size_t i = 0; i < nodes.size(); i++)
545  {
546  if (tree.hasFather(nodes[i]))
547  {
548  if (!tree.hasDistanceToFather(nodes[i]))
549  throw NodeException("TreeTools::scaleTree(). Branch with no length", nodes[i]);
550  double brLen = tree.getDistanceToFather(nodes[i]) * factor;
551  tree.setDistanceToFather(nodes[i], brLen);
552  }
553  }
554 }
555 
556 /******************************************************************************/
557 
559 {
560  if (!tree.hasNode(nodeId))
561  throw NodeNotFoundException("TreeTools::initBranchLengthsGrafen", nodeId);
562  vector<int> sons = tree.getSonsId(nodeId);
563  vector<size_t> h(sons.size());
564  for (size_t i = 0; i < sons.size(); i++)
565  {
566  h[i] = initBranchLengthsGrafen(tree, sons[i]);
567  }
568  size_t thish = sons.size() == 0 ? 0 : VectorTools::sum<size_t>(h) + sons.size() - 1;
569  for (size_t i = 0; i < sons.size(); i++)
570  {
571  tree.setDistanceToFather(sons[i], (double)(thish - h[i]));
572  }
573  return thish;
574 }
575 
577 {
578  initBranchLengthsGrafen(tree, tree.getRootId());
579 }
580 
581 /******************************************************************************/
582 
584  Tree& tree,
585  int nodeId,
586  double power,
587  double total,
588  double& height,
589  double& heightRaised)
591 {
592  if (!tree.hasNode(nodeId))
593  throw NodeNotFoundException("TreeTools::computeBranchLengthsGrafen", nodeId);
594  vector<int> sons = tree.getSonsId(nodeId);
595  vector<double> hr(sons.size());
596  height = 0;
597  for (size_t i = 0; i < sons.size(); i++)
598  {
599  int son = sons[i];
600  if (tree.hasDistanceToFather(son))
601  {
602  double h;
603  computeBranchLengthsGrafen(tree, sons[i], power, total, h, hr[i]);
604  double d = h + tree.getDistanceToFather(son);
605  if (d > height)
606  height = d;
607  }
608  else
609  throw NodeException ("TreeTools::computeBranchLengthsGrafen. Branch length lacking.", son);
610  }
611  heightRaised = pow(height / total, power) * total;
612  for (size_t i = 0; i < sons.size(); i++)
613  {
614  tree.setDistanceToFather(sons[i], heightRaised - hr[i]);
615  }
616 }
617 
618 void TreeTools::computeBranchLengthsGrafen(Tree& tree, double power, bool init)
619 throw (NodeException)
620 {
621  int rootId = tree.getRootId();
622  if (init)
623  {
624  initBranchLengthsGrafen(tree);
625  }
626  // Scale by total heigth:
627  double totalHeight = getHeight(tree, rootId);
628  double h, hr;
629  computeBranchLengthsGrafen(tree, rootId, power, totalHeight, h, hr);
630 }
631 
632 /******************************************************************************/
633 
634 double TreeTools::convertToClockTree(Tree& tree, int nodeId, bool noneg)
635 {
636  if (!tree.hasNode(nodeId))
637  throw NodeNotFoundException("TreeTools::convertToClockTree", nodeId);
638  vector<int> sons = tree.getSonsId(nodeId);
639  vector<double> h(sons.size());
640  // We compute the mean height:
641  double l = 0;
642  double maxh = -1.;
643  for (size_t i = 0; i < sons.size(); i++)
644  {
645  int son = sons[i];
646  if (tree.hasDistanceToFather(son))
647  {
648  h[i] = convertToClockTree(tree, son);
649  if (h[i] > maxh)
650  maxh = h[i];
651  l += h[i] + tree.getDistanceToFather(son);
652  }
653  else
654  throw NodeException ("TreeTools::convertToClockTree. Branch length lacking.", son);
655  }
656  if (sons.size() > 0)
657  l /= (double)sons.size();
658  if (l < maxh)
659  l = maxh;
660  for (size_t i = 0; i < sons.size(); i++)
661  {
662  tree.setDistanceToFather(sons[i], l - h[i]);
663  }
664  return l;
665 }
666 
667 /******************************************************************************/
668 
669 double TreeTools::convertToClockTree2(Tree& tree, int nodeId)
670 {
671  if (!tree.hasNode(nodeId))
672  throw NodeNotFoundException("TreeTools::convertToClockTree2", nodeId);
673  vector<int> sons = tree.getSonsId(nodeId);
674  vector<double> h(sons.size());
675  // We compute the mean height:
676  double l = 0;
677  double maxh = -1.;
678  for (size_t i = 0; i < sons.size(); i++)
679  {
680  int son = sons[i];
681  if (tree.hasDistanceToFather(son))
682  {
683  h[i] = convertToClockTree2(tree, son);
684  if (h[i] > maxh)
685  maxh = h[i];
686  l += h[i] + tree.getDistanceToFather(son);
687  }
688  else
689  throw NodeException ("TreeTools::convertToClockTree2. Branch length lacking.", son);
690  }
691  if (sons.size() > 0)
692  l /= (double)sons.size();
693  for (size_t i = 0; i < sons.size(); i++)
694  {
695  scaleTree(tree, sons[i], h[i] > 0 ? l / h[i] : 0);
696  }
697  return l;
698 }
699 
700 /******************************************************************************/
701 
702 DistanceMatrix* TreeTools::getDistanceMatrix(const Tree& tree)
703 {
704  vector<string> names = tree.getLeavesNames();
705  DistanceMatrix* mat = new DistanceMatrix(names);
706  for (size_t i = 0; i < names.size(); i++)
707  {
708  (*mat)(i, i) = 0;
709  for (size_t j = 0; j < i; j++)
710  {
711  (*mat)(i, j) = (*mat)(j, i) = getDistanceBetweenAnyTwoNodes(tree, tree.getLeafId(names[i]), tree.getLeafId(names[j]));
712  }
713  }
714  return mat;
715 }
716 
717 /******************************************************************************/
718 
720 {
721  throw Exception("TreeTools::midpointRooting(Tree). This function is deprecated, use TreeTemplateTools::midRoot instead!");
722  if (tree.isRooted())
723  tree.unroot();
724  DistanceMatrix* dist = getDistanceMatrix(tree);
725  vector<size_t> pos = MatrixTools::whichMax(dist->asMatrix());
726  double dmid = (*dist)(pos[0], pos[1]) / 2;
727  int id1 = tree.getLeafId(dist->getName(pos[0]));
728  int id2 = tree.getLeafId(dist->getName(pos[1]));
729  int rootId = tree.getRootId();
730  double d1 = getDistanceBetweenAnyTwoNodes(tree, id1, rootId);
731  double d2 = getDistanceBetweenAnyTwoNodes(tree, id2, rootId);
732  int current = d2 > d1 ? id2 : id1;
733  delete dist;
734  double l = tree.getDistanceToFather(current);
735  double c = l;
736  while (c < dmid)
737  {
738  current = tree.getFatherId(current);
739  l = tree.getDistanceToFather(current);
740  c += l;
741  }
742  tree.newOutGroup(current);
743  int brother = tree.getSonsId(tree.getRootId())[1];
744  if (brother == current)
745  brother = tree.getSonsId(tree.getRootId())[0];
746  tree.setDistanceToFather(current, l - (c - dmid));
747  tree.setDistanceToFather(brother, c - dmid);
748 }
749 
750 /******************************************************************************/
751 
752 int TreeTools::getMaxId(const Tree& tree, int id)
753 {
754  int maxId = id;
755  vector<int> sonsId = tree.getSonsId(id);
756  for (size_t i = 0; i < sonsId.size(); i++)
757  {
758  int subMax = getMaxId(tree, sonsId[i]);
759  if (subMax > maxId)
760  maxId = subMax;
761  }
762  return maxId;
763 }
764 
765 /******************************************************************************/
766 
767 int TreeTools::getMPNUId(const Tree& tree, int id)
768 {
769  vector<int> ids = getNodesId(tree, id);
770  sort(ids.begin(), ids.end());
771  // Check if some id is "missing" in the subtree:
772  for (size_t i = 0; i < ids.size(); i++)
773  {
774  if (ids[i] != (int)i)
775  return (int)i;
776  }
777  // Well, all ids are from 0 to n, so send n+1:
778  return (int)ids.size();
779 }
780 
781 /******************************************************************************/
782 
783 bool TreeTools::checkIds(const Tree& tree, bool throwException) throw (Exception)
784 {
785  vector<int> ids = tree.getNodesId();
786  sort(ids.begin(), ids.end());
787  for (size_t i = 1; i < ids.size(); i++)
788  {
789  if (ids[i] == ids[i - 1])
790  {
791  if (throwException)
792  throw Exception("TreeTools::checkIds. This id is used at least twice: " + TextTools::toString(ids[i]));
793  return false;
794  }
795  }
796  return true;
797 }
798 
799 /******************************************************************************/
800 
801 VectorSiteContainer* TreeTools::MRPEncode(const vector<Tree*>& vecTr)
802 {
803  vector<BipartitionList*> vecBipL;
804  for (size_t i = 0; i < vecTr.size(); i++)
805  {
806  vecBipL.push_back(new BipartitionList(*vecTr[i]));
807  }
808 
809  VectorSiteContainer* cont = BipartitionTools::MRPEncode(vecBipL);
810 
811  for (size_t i = 0; i < vecTr.size(); i++)
812  {
813  delete vecBipL[i];
814  }
815 
816  return cont;
817 }
818 
819 /******************************************************************************/
820 
821 VectorSiteContainer* TreeTools::MRPEncodeMultilabel(const vector<Tree*>& vecTr)
822 {
823  vector<BipartitionList*> vecBipL;
824  for (size_t i = 0; i < vecTr.size(); i++)
825  {
826  vecBipL.push_back(new BipartitionList(*vecTr[i]));
827  }
828 
829  VectorSiteContainer* cont = BipartitionTools::MRPEncodeMultilabel(vecBipL);
830 
831  for (size_t i = 0; i < vecTr.size(); i++)
832  {
833  delete vecBipL[i];
834  }
835 
836  return cont;
837 }
838 
839 /******************************************************************************/
840 
841 bool TreeTools::haveSameTopology(const Tree& tr1, const Tree& tr2)
842 {
843  size_t jj, nbbip;
844  BipartitionList* bipL1, * bipL2;
845  vector<size_t> size1, size2;
846 
847  /* compare sets of leaves */
848  if (!VectorTools::haveSameElements(tr1.getLeavesNames(), tr2.getLeavesNames()))
849  return false;
850 
851  /* construct bipartitions */
852  bipL1 = new BipartitionList(tr1, true);
853  bipL1->removeTrivialBipartitions();
855  bipL1->sortByPartitionSize();
856  bipL2 = new BipartitionList(tr2, true);
857  bipL2->removeTrivialBipartitions();
859  bipL2->sortByPartitionSize();
860 
861  /* compare numbers of bipartitions */
862  if (bipL1->getNumberOfBipartitions() != bipL2->getNumberOfBipartitions())
863  return false;
864  nbbip = bipL1->getNumberOfBipartitions();
865 
866  /* compare partition sizes */
867  for (size_t i = 0; i < nbbip; i++)
868  {
869  size1.push_back(bipL1->getPartitionSize(i));
870  size2.push_back(bipL1->getPartitionSize(i));
871  if (size1[i] != size2[i])
872  return false;
873  }
874 
875  /* compare bipartitions */
876  for (size_t i = 0; i < nbbip; i++)
877  {
878  for (jj = 0; jj < nbbip; jj++)
879  {
880  if (size1[i] == size2[jj] && BipartitionTools::areIdentical(*bipL1, i, *bipL2, jj))
881  break;
882  }
883  if (jj == nbbip)
884  return false;
885  }
886 
887  return true;
888 }
889 
890 /******************************************************************************/
891 
892 int TreeTools::robinsonFouldsDistance(const Tree& tr1, const Tree& tr2, bool checkNames, int* missing_in_tr2, int* missing_in_tr1) throw (Exception)
893 {
894  BipartitionList* bipL1, * bipL2;
895  size_t i, j;
896  vector<size_t> size1, size2;
897  vector<bool> bipOK2;
898 
899 
900  if (checkNames && !VectorTools::haveSameElements(tr1.getLeavesNames(), tr2.getLeavesNames()))
901  throw Exception("Distinct leaf sets between trees ");
902 
903  /* prepare things */
904  int missing1 = 0;
905  int missing2 = 0;
906 
907  bipL1 = new BipartitionList(tr1, true);
908  bipL1->removeTrivialBipartitions();
909  bipL1->sortByPartitionSize();
910  bipL2 = new BipartitionList(tr2, true);
911  bipL2->removeTrivialBipartitions();
912  bipL2->sortByPartitionSize();
913 
914 
915  for (i = 0; i < bipL1->getNumberOfBipartitions(); i++)
916  {
917  size1.push_back(bipL1->getPartitionSize(i));
918  }
919  for (i = 0; i < bipL2->getNumberOfBipartitions(); i++)
920  {
921  size2.push_back(bipL2->getPartitionSize(i));
922  }
923 
924  for (i = 0; i < bipL2->getNumberOfBipartitions(); i++)
925  {
926  bipOK2.push_back(false);
927  }
928 
929  /* main loops */
930 
931  for (i = 0; i < bipL1->getNumberOfBipartitions(); i++)
932  {
933  for (j = 0; j < bipL2->getNumberOfBipartitions(); j++)
934  {
935  if (bipOK2[j])
936  continue;
937  if (size1[i] == size2[j] && BipartitionTools::areIdentical(*bipL1, i, *bipL2, j))
938  {
939  bipOK2[j] = true;
940  break;
941  }
942  }
943  if (j == bipL2->getNumberOfBipartitions())
944  missing2++;
945  }
946 
947  missing1 = static_cast<int>(bipL2->getNumberOfBipartitions()) - static_cast<int>(bipL1->getNumberOfBipartitions()) + missing2;
948 
949  if (missing_in_tr1)
950  *missing_in_tr1 = missing1;
951  if (missing_in_tr2)
952  *missing_in_tr2 = missing2;
953  return missing1 + missing2;
954 }
955 
956 /******************************************************************************/
957 
958 BipartitionList* TreeTools::bipartitionOccurrences(const vector<Tree*>& vecTr, vector<size_t>& bipScore)
959 {
960  vector<BipartitionList*> vecBipL;
961  BipartitionList* mergedBipL;
962  vector<size_t> bipSize;
963  size_t nbBip;
964 
965  /* build and merge bipartitions */
966  for (size_t i = 0; i < vecTr.size(); i++)
967  {
968  vecBipL.push_back(new BipartitionList(*vecTr[i]));
969  }
970  mergedBipL = BipartitionTools::mergeBipartitionLists(vecBipL);
971  for (size_t i = 0; i < vecTr.size(); i++)
972  {
973  delete vecBipL[i];
974  }
975 
976  mergedBipL->removeTrivialBipartitions();
977  nbBip = mergedBipL->getNumberOfBipartitions();
978  bipScore.clear();
979  for (size_t i = 0; i < nbBip; i++)
980  {
981  bipSize.push_back(mergedBipL->getPartitionSize(i));
982  bipScore.push_back(1);
983  }
984 
985  /* compare bipartitions */
986  for (size_t i = nbBip; i > 0; i--)
987  {
988  if (bipScore[i - 1] == 0)
989  continue;
990  for (size_t j = i - 1; j > 0; j--)
991  {
992  if (bipScore[j - 1] && bipSize[i - 1] == bipSize[j - 1] && mergedBipL->areIdentical(i - 1, j - 1))
993  {
994  bipScore[i - 1]++;
995  bipScore[j - 1] = 0;
996  }
997  }
998  }
999 
1000  /* keep only distinct bipartitions */
1001  for (size_t i = nbBip; i > 0; i--)
1002  {
1003  if (bipScore[i - 1] == 0)
1004  {
1005  bipScore.erase(bipScore.begin() + static_cast<ptrdiff_t>(i - 1));
1006  mergedBipL->deleteBipartition(i - 1);
1007  }
1008  }
1009 
1010  /* add terminal branches */
1011  mergedBipL->addTrivialBipartitions(false);
1012  for (size_t i = 0; i < mergedBipL->getNumberOfElements(); i++)
1013  {
1014  bipScore.push_back(vecTr.size());
1015  }
1016 
1017  return mergedBipL;
1018 }
1019 
1020 /******************************************************************************/
1021 
1022 TreeTemplate<Node>* TreeTools::thresholdConsensus(const vector<Tree*>& vecTr, double threshold, bool checkNames) throw (Exception)
1023 {
1024  vector<size_t> bipScore;
1025  vector<string> tr0leaves;
1026  BipartitionList* bipL;
1027  double score;
1028 
1029  if (vecTr.size() == 0)
1030  throw Exception("TreeTools::thresholdConsensus. Empty vector passed");
1031 
1032  /* check names */
1033  if (checkNames)
1034  {
1035  tr0leaves = vecTr[0]->getLeavesNames();
1036  for (size_t i = 1; i < vecTr.size(); i++)
1037  {
1038  if (!VectorTools::haveSameElements(vecTr[i]->getLeavesNames(), tr0leaves))
1039  throw Exception("TreeTools::thresholdConsensus. Distinct leaf sets between trees");
1040  }
1041  }
1042 
1043  bipL = bipartitionOccurrences(vecTr, bipScore);
1044 
1045  for (size_t i = bipL->getNumberOfBipartitions(); i > 0; i--)
1046  {
1047  if (bipL->getPartitionSize(i - 1) == 1)
1048  continue;
1049  score = static_cast<int>(bipScore[i - 1]) / static_cast<double>(vecTr.size());
1050  if (score <= threshold && score != 1.)
1051  {
1052  bipL->deleteBipartition(i - 1);
1053  continue;
1054  }
1055  if (score > 0.5)
1056  continue;
1057  for (size_t j = bipL->getNumberOfBipartitions(); j > i; j--)
1058  {
1059  if (!bipL->areCompatible(i - 1, j - 1))
1060  {
1061  bipL->deleteBipartition(i - 1);
1062  break;
1063  }
1064  }
1065  }
1066 
1067  TreeTemplate<Node>* tr = bipL->toTree();
1068  delete bipL;
1069  return tr;
1070 }
1071 
1072 /******************************************************************************/
1073 
1074 TreeTemplate<Node>* TreeTools::fullyResolvedConsensus(const vector<Tree*>& vecTr, bool checkNames)
1075 {
1076  return thresholdConsensus(vecTr, 0., checkNames);
1077 }
1078 
1079 /******************************************************************************/
1080 
1081 TreeTemplate<Node>* TreeTools::majorityConsensus(const vector<Tree*>& vecTr, bool checkNames)
1082 {
1083  return thresholdConsensus(vecTr, 0.5, checkNames);
1084 }
1085 
1086 /******************************************************************************/
1087 
1088 TreeTemplate<Node>* TreeTools::strictConsensus(const vector<Tree*>& vecTr, bool checkNames)
1089 {
1090  return thresholdConsensus(vecTr, 1., checkNames);
1091 }
1092 
1093 /******************************************************************************/
1094 
1095 Tree* TreeTools::MRP(const vector<Tree*>& vecTr)
1096 {
1097  // matrix representation
1098  VectorSiteContainer* sites = TreeTools::MRPEncode(vecTr);
1099 
1100  // starting bioNJ tree
1101  const DNA* alphabet = dynamic_cast<const DNA*>(sites->getAlphabet());
1102  JCnuc* jc = new JCnuc(alphabet);
1103  ConstantDistribution* constRate = new ConstantDistribution(1.);
1104  DistanceEstimation distFunc(jc, constRate, sites, 0, true);
1105  BioNJ bionjTreeBuilder(false, false);
1106  bionjTreeBuilder.setDistanceMatrix(*(distFunc.getMatrix()));
1107  bionjTreeBuilder.computeTree();
1108  if (ApplicationTools::message)
1109  ApplicationTools::message->endLine();
1110  TreeTemplate<Node>* startTree = new TreeTemplate<Node>(*bionjTreeBuilder.getTree());
1111 
1112  // MP optimization
1113  DRTreeParsimonyScore* MPScore = new DRTreeParsimonyScore(*startTree, *sites, false);
1114  MPScore = OptimizationTools::optimizeTreeNNI(MPScore, 0);
1115  delete startTree;
1116  Tree* retTree = new TreeTemplate<Node>(MPScore->getTree());
1117  delete MPScore;
1118 
1119  return retTree;
1120 }
1121 
1122 /******************************************************************************/
1123 
1124 void TreeTools::computeBootstrapValues(Tree& tree, const vector<Tree*>& vecTr, bool verbose, int format)
1125 {
1126  vector<int> index;
1127  BipartitionList bpTree(tree, true, &index);
1128  vector<size_t> occurences;
1129  BipartitionList* bpList = bipartitionOccurrences(vecTr, occurences);
1130 
1131  vector< Number<double> > bootstrapValues(bpTree.getNumberOfBipartitions());
1132 
1133  for (size_t i = 0; i < bpTree.getNumberOfBipartitions(); i++)
1134  {
1135  if (verbose)
1136  ApplicationTools::displayGauge(i, bpTree.getNumberOfBipartitions() - 1, '=');
1137  for (size_t j = 0; j < bpList->getNumberOfBipartitions(); j++)
1138  {
1139  if (BipartitionTools::areIdentical(bpTree, i, *bpList, j))
1140  {
1141  bootstrapValues[i] = format >= 0 ? round(static_cast<double>(occurences[j]) * pow(10., 2 + format) / static_cast<double>(vecTr.size())) / pow(10., format) : static_cast<double>(occurences[j]);
1142  break;
1143  }
1144  }
1145  }
1146 
1147  for (size_t i = 0; i < index.size(); i++)
1148  {
1149  if (!tree.isLeaf(index[i]))
1150  tree.setBranchProperty(index[i], BOOTSTRAP, bootstrapValues[i]);
1151  }
1152 
1153  delete bpList;
1154 }
1155 
1156 /******************************************************************************/
1157 
1158 vector<int> TreeTools::getAncestors(const Tree& tree, int nodeId) throw (NodeNotFoundException)
1159 {
1160  vector<int> ids;
1161  int currentId = nodeId;
1162  while (tree.hasFather(currentId))
1163  {
1164  currentId = tree.getFatherId(currentId);
1165  ids.push_back(currentId);
1166  }
1167  return ids;
1168 }
1169 
1170 /******************************************************************************/
1171 
1172 int TreeTools::getLastCommonAncestor(const Tree& tree, const vector<int>& nodeIds) throw (NodeNotFoundException, Exception)
1173 {
1174  if (nodeIds.size() == 0)
1175  throw Exception("TreeTools::getLastCommonAncestor(). You must provide at least one node id.");
1176  vector< vector<int> > ancestors(nodeIds.size());
1177  for (size_t i = 0; i < nodeIds.size(); i++)
1178  {
1179  ancestors[i] = getAncestors(tree, nodeIds[i]);
1180  ancestors[i].insert(ancestors[i].begin(), nodeIds[i]);
1181  }
1182  int lca = tree.getRootId();
1183  size_t count = 1;
1184  for ( ; ; )
1185  {
1186  if (ancestors[0].size() <= count)
1187  return lca;
1188  int current = ancestors[0][ancestors[0].size() - count - 1];
1189  for (size_t i = 1; i < nodeIds.size(); i++)
1190  {
1191  if (ancestors[i].size() <= count)
1192  return lca;
1193  if (ancestors[i][ancestors[i].size() - count - 1] != current)
1194  return lca;
1195  }
1196  lca = current;
1197  count++;
1198  }
1199  // This line is never reached!
1200  return lca;
1201 }
1202 
1203 /******************************************************************************/
1204 
1206 {
1207  // is the tree rooted?
1208  if (!tree.isRooted())
1209  throw Exception("The tree has to be rooted on the branch of interest to determine the midpoint position of the root");
1210  // is the tree multifurcating?
1211  if (tree.isMultifurcating())
1212  throw Exception("The tree is multifurcated, which is not allowed.");
1213 
1214  double length = 0.;
1215  vector<int> sonsIds = tree.getSonsId(tree.getRootId());
1216  // Length of the branch containing the root:
1217  length = tree.getDistanceToFather(sonsIds.at(0)) + tree.getDistanceToFather(sonsIds.at(1));
1218  // The fraction of the original branch allowing to split its length and to place the root:
1219  double x = bestRootPosition_(tree, sonsIds.at(0), sonsIds.at(1), length);
1220  // The new branch lengths are then computed:
1221  tree.setDistanceToFather(sonsIds.at(0), length * x);
1222  tree.setDistanceToFather(sonsIds.at(1), length * (1 - x));
1223 }
1224 
1225 /******************************************************************************/
1226 
1227 double TreeTools::bestRootPosition_(Tree& tree, int nodeId1, int nodeId2, double length)
1228 {
1229  double x;
1230  Moments_ m1, m2;
1231  double A, B; // C;
1232  // The variance is expressed as a degree 2 polynomial : variance(x) = A * x * x + B * x + C
1233  // The fraction x is then obtained by differentiating this equation.
1234  m1 = statFromNode_(tree, nodeId1);
1235  m2 = statFromNode_(tree, nodeId2);
1236  A = 4 * m1.N * (m2.N * length) * length;
1237  B = 4 * length * (m2.N * m1.sum - m1.N * m2.sum - length * m1.N * m2.N);
1238 // C = (m1.N + m2.N) * (m1.squaredSum + m2.squaredSum) + m1.N * length * m2.N * length +
1239 // 2 * m1.N * length * m2.sum - 2 * m2.N * length * m1.sum -
1240 // (m1.sum + m2.sum) * (m1.sum + m2.sum);
1241 
1242  if (A < 1e-20)
1243  x = 0.5;
1244  else
1245  x = -B / (2 * A);
1246  if (x < 0)
1247  x = 0;
1248  else if (x > 1)
1249  x = 1;
1250 
1251  return x;
1252 }
1253 
1254 /******************************************************************************/
1255 
1257 {
1258  // This function recursively calculates both the sum of the branch lengths and the sum of the squared branch lengths down the node whose ID is rootId.
1259  // If below a particular node there are N leaves, the branch between this node and its father is taken into account N times in the calculation.
1260  Moments_ m;
1261  static Moments_ mtmp;
1262 
1263  if (tree.isLeaf(rootId))
1264  {
1265  m.N = 1;
1266  m.sum = 0.;
1267  m.squaredSum = 0.;
1268  }
1269  else
1270  {
1271  vector<int> sonsId = tree.getSonsId(rootId);
1272  for (size_t i = 0; i < sonsId.size(); i++)
1273  {
1274  mtmp = statFromNode_(tree, sonsId.at(i));
1275  double bLength = tree.getDistanceToFather(sonsId.at(i));
1276  m.N += mtmp.N;
1277  m.sum += mtmp.sum + bLength * mtmp.N;
1278  m.squaredSum += mtmp.squaredSum + 2 * bLength * mtmp.sum + mtmp.N * bLength * bLength;
1279  }
1280  }
1281 
1282  return m;
1283 }
1284 
1285 /******************************************************************************/
1286 
1287 Tree* TreeTools::MRPMultilabel(const vector<Tree*>& vecTr)
1288 {
1289  // matrix representation
1290  VectorSiteContainer* sites = TreeTools::MRPEncode(vecTr);
1291 
1292  // starting bioNJ tree
1293  const DNA* alphabet = dynamic_cast<const DNA*>(sites->getAlphabet());
1294  JCnuc* jc = new JCnuc(alphabet);
1295  ConstantDistribution* constRate = new ConstantDistribution(1.);
1296  DistanceEstimation distFunc(jc, constRate, sites, 0, true);
1297  BioNJ bionjTreeBuilder(false, false);
1298  bionjTreeBuilder.setDistanceMatrix(*(distFunc.getMatrix()));
1299  bionjTreeBuilder.computeTree();
1300  if (ApplicationTools::message)
1301  ApplicationTools::message->endLine();
1302  TreeTemplate<Node>* startTree = new TreeTemplate<Node>(*bionjTreeBuilder.getTree());
1303 
1304  // MP optimization
1305  DRTreeParsimonyScore* MPScore = new DRTreeParsimonyScore(*startTree, *sites, false);
1306  MPScore = OptimizationTools::optimizeTreeNNI(MPScore, 0);
1307  delete startTree;
1308  Tree* retTree = new TreeTemplate<Node>(MPScore->getTree());
1309  delete MPScore;
1310 
1311  return retTree;
1312 }
1313 
1314 
static bool areIdentical(const BipartitionList &bipart1, size_t i1, const BipartitionList &bipart2, size_t i2, bool checkElements=true)
Tells whether two bipartitions from distinct lists are identical.
size_t getNumberOfBipartitions() const
static NNIHomogeneousTreeLikelihood * optimizeTreeNNI(NNIHomogeneousTreeLikelihood *tl, const ParameterList &parameters, bool optimizeNumFirst=true, double tolBefore=100, double tolDuring=100, unsigned int tlEvalMax=1000000, unsigned int numStep=1, OutputStream *messageHandler=ApplicationTools::message, OutputStream *profiler=ApplicationTools::message, bool reparametrization=false, unsigned int verbose=1, const std::string &optMethod=OptimizationTools::OPTIMIZATION_NEWTON, unsigned int nStep=1, const std::string &nniMethod=NNITopologySearch::PHYML)
Optimize all parameters from a TreeLikelihood object, including tree topology using Nearest Neighbor ...
static size_t getDepths(const Tree &tree, int nodeId, std::map< int, size_t > &depths)
Get the depths for all nodes of the subtree defined by node &#39;node&#39;, i.e. the maximum number of sons &#39;...
Definition: TreeTools.cpp:191
void computeTree()
Compute the tree corresponding to the distance matrix.
Definition: BioNJ.cpp:60
static TreeTemplate< Node > * thresholdConsensus(const std::vector< Tree *> &vecTr, double threshold, bool checkNames=true)
General greedy consensus tree method.
Definition: TreeTools.cpp:1022
bool areIdentical(size_t k1, size_t k2) const
static double convertToClockTree(Tree &tree, int nodeId, bool noneg=false)
Modify a tree&#39;s branch lengths to make a clock tree, by rebalancing branch lengths.
Definition: TreeTools.cpp:634
static std::vector< int > getLeavesId(const Tree &tree, int nodeId)
Retrieve all leaves from a subtree.
Definition: TreeTools.cpp:76
static std::string treeToParenthesis(const Tree &tree, bool writeId=false)
Get the parenthesis description of a tree.
Definition: TreeTools.cpp:330
static int getMaxId(const Tree &tree, int id)
Get the maximum identifier used in a (sub)tree.
Definition: TreeTools.cpp:752
static void scaleTree(Tree &tree, int nodeId, double factor)
Scale a given tree.
Definition: TreeTools.cpp:539
static void constrainedMidPointRooting(Tree &tree)
Determine the mid-point position of the root along the branch that already contains the root...
Definition: TreeTools.cpp:1205
static Moments_ statFromNode_(Tree &tree, int rootId)
Definition: TreeTools.cpp:1256
DistanceMatrix * getMatrix() const
Get the distance matrix.
static int getLastCommonAncestor(const Tree &tree, const std::vector< int > &nodeIds)
Get the id of the last common ancestors of all specified nodes.
Definition: TreeTools.cpp:1172
void sortByPartitionSize()
Sort bipartitions by partition size.
static void initBranchLengthsGrafen(Tree &tree)
Grafen&#39;s method to initialize branch lengths.
Definition: TreeTools.cpp:576
virtual int getRootId() const =0
bool areCompatible(size_t k1, size_t k2) const
Tells whether 2 bipartitions from the list are compatible.
virtual TreeTemplate< Node > * getTree() const
Get the computed tree, if there is one.
Double recursive implementation of interface TreeParsimonyScore.
virtual void newOutGroup(int nodeId)=0
Root a tree by specifying an outgroup.
STL namespace.
static size_t getDepth(const Tree &tree, int nodeId)
Get the depth of the subtree defined by node &#39;node&#39;, i.e. the maximum number of sons &#39;generations&#39;...
Definition: TreeTools.cpp:174
The phylogenetic tree class.
static void computeBootstrapValues(Tree &tree, const std::vector< Tree *> &vecTr, bool verbose=true, int format=0)
Compute bootstrap values.
Definition: TreeTools.cpp:1124
Interface for phylogenetic tree objects.
Definition: Tree.h:148
TreeTemplate< Node > * toTree() const
Translate into a tree.
static double getTotalLength(const Tree &tree, int nodeId, bool includeAncestor=true)
Get the total length (sum of all branch lengths) of a subtree.
Definition: TreeTools.cpp:495
virtual void setBranchProperty(int nodeId, const std::string &name, const Clonable &property)=0
static void setVoidBranchLengths(Tree &tree, int nodeId, double brLen)
Give a length to branches that don&#39;t have one in a subtree.
Definition: TreeTools.cpp:525
virtual int getFatherId(int parentId) const =0
static size_t getNumberOfLeaves(const Tree &tree, int nodeId)
Count the number of leaves from a subtree.
Definition: TreeTools.cpp:98
static BipartitionList * mergeBipartitionLists(const std::vector< BipartitionList *> &vecBipartL, bool checkElements=true)
Makes one BipartitionList out of several.
void addTrivialBipartitions(bool checkExisting)
Adds bipartitions corresponding to external branches if missing.
static BipartitionList * bipartitionOccurrences(const std::vector< Tree *> &vecTr, std::vector< size_t > &bipScore)
Counts the total number of occurrences of every bipartition from the input trees. ...
Definition: TreeTools.cpp:958
static TreeTemplate< Node > * majorityConsensus(const std::vector< Tree *> &vecTr, bool checkNames=true)
Majority consensus tree method.
Definition: TreeTools.cpp:1081
static Tree * MRPMultilabel(const std::vector< Tree *> &vecTr)
Matrix Representation Parsimony supertree method for multilabel trees.
Definition: TreeTools.cpp:1287
static std::vector< int > getAncestors(const Tree &tree, int nodeId)
Get a list of all ids of parents nodes, from the current node (not included) to the root of the tree...
Definition: TreeTools.cpp:1158
static bool checkIds(const Tree &tree, bool throwException)
Check if the ids are uniques.
Definition: TreeTools.cpp:783
virtual bool hasNode(int nodeId) const =0
static Vdouble getBranchLengths(const Tree &tree, int nodeId)
Get all the branch lengths of a subtree.
Definition: TreeTools.cpp:472
static void midpointRooting(Tree &tree)
(Re)root the tree using the midpoint method.
Definition: TreeTools.cpp:719
virtual bool isLeaf(int nodeId) const =0
static int getMPNUId(const Tree &tree, int id)
Get the minimum positive non-used identifier in a (sub)tree.
Definition: TreeTools.cpp:767
static std::string nodeToParenthesis(const Tree &tree, int nodeId, bool writeId=false)
Get the parenthesis description of a subtree.
Definition: TreeTools.cpp:254
static TreeTemplate< Node > * strictConsensus(const std::vector< Tree *> &vecTr, bool checkNames=true)
Strict consensus tree method.
Definition: TreeTools.cpp:1088
virtual bool hasBranchProperty(int nodeId, const std::string &name) const =0
void removeTrivialBipartitions()
Removes bipartitions corresponding to external branches (1 vs n-1)
The BioNJ distance method.
Definition: BioNJ.h:55
Exception thrown when something is wrong with a particular node.
static DistanceMatrix * getDistanceMatrix(const Tree &tree)
Compute a distance matrix from a tree.
Definition: TreeTools.cpp:702
size_t getNumberOfElements() const
virtual std::vector< std::string > getLeavesNames() const =0
static VectorSiteContainer * MRPEncode(const std::vector< BipartitionList *> &vecBipartL)
Create a sequence data set corresponding to the Matrix Representation of the input BipartitionList ob...
static double convertToClockTree2(Tree &tree, int nodeId)
Modify a tree&#39;s branch lengths to make a clock tree, by rescaling subtrees.
Definition: TreeTools.cpp:669
virtual void setDistanceToFather(int nodeId, double length)=0
static VectorSiteContainer * MRPEncodeMultilabel(const std::vector< Tree *> &vecTr)
Creates a sequence data set corresponding to the Matrix Representation of the input multilabel trees...
Definition: TreeTools.cpp:821
static int getLeafId(const Tree &tree, int nodeId, const std::string &name)
Get the id of a leaf given its name in a subtree.
Definition: TreeTools.cpp:118
static std::vector< int > getNodesId(const Tree &tree, int nodeId)
Retrieve all nodes ids nodes from a subtree.
Definition: TreeTools.cpp:153
This class deals with the bipartitions defined by trees.
void setDistanceMatrix(const DistanceMatrix &matrix)
Set the distance matrix to use.
Definition: BioNJ.h:101
virtual double getDistanceToFather(int nodeId) const =0
void deleteBipartition(size_t i)
virtual bool unroot()=0
Unroot a rooted tree.
static double getHeight(const Tree &tree, int nodeId)
Get the height of the subtree defined by node &#39;node&#39;, i.e. the maximum distance between leaves and th...
Definition: TreeTools.cpp:209
virtual bool isMultifurcating() const =0
Tell if the tree is multifurcating.
virtual int getLeafId(const std::string &name) const =0
virtual bool hasDistanceToFather(int nodeId) const =0
static const std::string BOOTSTRAP
Bootstrap tag.
Definition: TreeTools.h:723
static std::vector< int > getPathBetweenAnyTwoNodes(const Tree &tree, int nodeId1, int nodeId2, bool includeAncestor=true)
Get a vector of ancestor nodes between to nodes.
Definition: TreeTools.cpp:401
static void computeBranchLengthsGrafen(Tree &tree, double power=1, bool init=true)
Compute branch lengths using Grafen&#39;s method.
Definition: TreeTools.cpp:618
The Jukes-Cantor substitution model for nucleotides.
Definition: JCnuc.h:129
static bool haveSameTopology(const Tree &tr1, const Tree &tr2)
Tells whether two trees have the same unrooted topology.
Definition: TreeTools.cpp:841
Estimate a distance matrix from sequence data, according to a given model.
static void setBranchLengths(Tree &tree, int nodeId, double brLen)
Set all the branch lengths of a subtree.
Definition: TreeTools.cpp:512
static double getDistanceBetweenAnyTwoNodes(const Tree &tree, int nodeId1, int nodeId2)
Get the total distance between two nodes.
Definition: TreeTools.cpp:455
static TreeTemplate< Node > * fullyResolvedConsensus(const std::vector< Tree *> &vecTr, bool checkNames=true)
Fully-resolved greedy consensus tree method.
Definition: TreeTools.cpp:1074
static void searchLeaf(const Tree &tree, int nodeId, const std::string &name, int *&id)
Get the id of a leaf given its name in a subtree.
Definition: TreeTools.cpp:133
static double getHeights(const Tree &tree, int nodeId, std::map< int, double > &heights)
Get the heights of all nodes within a subtree defined by node &#39;node&#39;, i.e. the maximum distance betwe...
Definition: TreeTools.cpp:231
virtual Clonable * getBranchProperty(int nodeId, const std::string &name)=0
virtual std::vector< int > getSonsId(int parentId) const =0
static VectorSiteContainer * MRPEncode(const std::vector< Tree *> &vecTr)
Creates a sequence data set corresponding to the Matrix Representation of the input trees...
Definition: TreeTools.cpp:801
static double bestRootPosition_(Tree &tree, int nodeId1, int nodeId2, double length)
Definition: TreeTools.cpp:1227
size_t getPartitionSize(size_t i) const
Returns the size of the smallest of the two partitions (e.g. 1 for external branches) ...
General exception thrown when something is wrong with a particular node.
static VectorSiteContainer * MRPEncodeMultilabel(const std::vector< BipartitionList *> &vecBipartL)
Create a sequence data set corresponding to the Matrix Representation of the input BipartitionList ob...
static Tree * MRP(const std::vector< Tree *> &vecTr)
Matrix Representation Parsimony supertree method.
Definition: TreeTools.cpp:1095
virtual bool isRooted() const =0
Tell if the tree is rooted.
static int robinsonFouldsDistance(const Tree &tr1, const Tree &tr2, bool checkNames=true, int *missing_in_tr2=NULL, int *missing_in_tr1=NULL)
Calculates the Robinson-Foulds topological distance between two trees.
Definition: TreeTools.cpp:892
virtual std::string getNodeName(int nodeId) const =0