bpp-phyl  2.2.0
TreeTools.h
Go to the documentation of this file.
1 //
2 // File: TreeTools.h
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 #ifndef _TREETOOLS_H_
41 #define _TREETOOLS_H_
42 
43 #include "TreeExceptions.h"
44 #include "Node.h"
45 #include "Tree.h"
46 #include "BipartitionList.h"
47 
48 #include <Bpp/Exceptions.h>
49 #include <Bpp/Numeric/VectorTools.h>
50 
51 // From SeqLib:
52 #include <Bpp/Seq/Container/VectorSiteContainer.h>
53 #include <Bpp/Seq/DistanceMatrix.h>
54 
55 namespace bpp
56 {
57 
66 class TreeTools
67 {
68  public:
69  TreeTools() {}
70  virtual ~TreeTools() {}
71 
72  public:
73 
88  static std::vector<int> getLeavesId(const Tree& tree, int nodeId) throw (NodeNotFoundException);
89 
98  static void getLeavesId(const Tree& tree, int nodeId, std::vector<int>& leaves) throw (NodeNotFoundException);
99 
107  static size_t getNumberOfLeaves(const Tree& tree, int nodeId) throw (NodeNotFoundException);
108 
118  static int getLeafId(const Tree& tree, int nodeId, const std::string& name) throw (NodeNotFoundException);
119 
129  static void searchLeaf(const Tree& tree, int nodeId, const std::string& name, int*& id) throw (NodeNotFoundException);
130 
141  static std::vector<int> getPathBetweenAnyTwoNodes(const Tree& tree, int nodeId1, int nodeId2, bool includeAncestor = true) throw (NodeNotFoundException);
142 
151  static std::vector<int> getAncestors(const Tree& tree, int nodeId) throw (NodeNotFoundException);
152 
163  static int getLastCommonAncestor(const Tree& tree, const std::vector<int>& nodeIds) throw (NodeNotFoundException, Exception);
164 
186  static size_t getDepth(const Tree& tree, int nodeId) throw (NodeNotFoundException);
187 
210  static size_t getDepths(const Tree& tree, int nodeId, std::map<int, size_t>& depths) throw (NodeNotFoundException);
211 
225  static double getHeight(const Tree& tree, int nodeId) throw (NodeNotFoundException,NodeException);
226 
240  static double getHeights(const Tree& tree, int nodeId, std::map<int, double>& heights) throw (NodeNotFoundException,NodeException);
241 
259  static Vdouble getBranchLengths(const Tree& tree, int nodeId) throw (NodeNotFoundException,NodeException);
260 
272  static double getTotalLength(const Tree& tree, int nodeId, bool includeAncestor = true) throw (NodeNotFoundException,NodeException);
273 
282  static void setBranchLengths(Tree& tree, int nodeId, double brLen) throw (NodeNotFoundException);
283 
292  static void setVoidBranchLengths(Tree& tree, int nodeId, double brLen) throw (NodeNotFoundException);
293 
305  static void scaleTree(Tree& tree, int nodeId, double factor) throw (NodeNotFoundException,NodeException);
306 
320  static void initBranchLengthsGrafen(Tree& tree);
321 
337  static void computeBranchLengthsGrafen(Tree& tree, double power = 1, bool init = true) throw (NodeException);
338 
339  private:
340  static size_t initBranchLengthsGrafen(Tree& tree, int nodeId) throw (NodeNotFoundException);
341  static void computeBranchLengthsGrafen(Tree& tree, int nodeId, double power, double total, double& height, double& heightRaised) throw (NodeNotFoundException,NodeException);
342 
343  public:
363  static double convertToClockTree(Tree& tree, int nodeId, bool noneg = false);
364 
380  static double convertToClockTree2(Tree& tree, int nodeId);
381 
393  static double getDistanceBetweenAnyTwoNodes(const Tree& tree, int nodeId1, int nodeId2);
394 
407  static DistanceMatrix* getDistanceMatrix(const Tree& tree);
408 
418  static void midpointRooting(Tree& tree);
443  static std::string nodeToParenthesis(const Tree& tree, int nodeId, bool writeId = false) throw (NodeNotFoundException);
444 
459  static std::string nodeToParenthesis(const Tree& tree, int nodeId, bool bootstrap, const std::string& propertyName) throw (NodeNotFoundException);
460 
470  static std::string treeToParenthesis(const Tree& tree, bool writeId = false);
471 
484  static std::string treeToParenthesis(const Tree& tree, bool bootstrap, const std::string& propertyName);
485 
502  static std::vector<int> getNodesId(const Tree& tree, int nodeId) throw (NodeNotFoundException);
503 
512  static void getNodesId(const Tree& tree, int nodeId, std::vector<int>& nodes) throw (NodeNotFoundException);
513 
524  static int getMaxId(const Tree& tree, int id);
525 
536  static int getMPNUId(const Tree& tree, int id);
537 
545  static bool checkIds(const Tree& tree, bool throwException) throw (Exception);
546 
564  static VectorSiteContainer* MRPEncode(const std::vector<Tree*>& vecTr);
565 
574  static VectorSiteContainer* MRPEncodeMultilabel(const std::vector<Tree*>& vecTr);
575 
583  static bool haveSameTopology(const Tree& tr1, const Tree& tr2);
584 
600  static int robinsonFouldsDistance(const Tree& tr1, const Tree& tr2, bool checkNames = true, int* missing_in_tr2 = NULL, int* missing_in_tr1 = NULL) throw (Exception);
601 
613  static BipartitionList* bipartitionOccurrences(const std::vector<Tree*>& vecTr, std::vector<size_t>& bipScore);
614 
628  static TreeTemplate<Node>* thresholdConsensus(const std::vector<Tree*>& vecTr, double threshold, bool checkNames = true) throw (Exception);
629 
640  static TreeTemplate<Node>* fullyResolvedConsensus(const std::vector<Tree*>& vecTr, bool checkNames = true);
641 
651  static TreeTemplate<Node>* majorityConsensus(const std::vector<Tree*>& vecTr, bool checkNames = true);
652 
662  static TreeTemplate<Node>* strictConsensus(const std::vector<Tree*>& vecTr, bool checkNames = true);
663 
677  static Tree* MRP(const std::vector<Tree*>& vecTr);
678 
688  static void computeBootstrapValues(Tree& tree, const std::vector<Tree*>& vecTr, bool verbose = true, int format = 0);
689 
698  static void constrainedMidPointRooting(Tree& tree);
699 
711  static Tree* MRPMultilabel(const std::vector<Tree*>& vecTr);
712 
713 
723  static const std::string BOOTSTRAP;
724 
725  private:
726  struct Moments_ {
727  double N;
728  double sum, squaredSum;
729  Moments_(): N(0), sum(0), squaredSum(0) {}
730  };
731 
732  static Moments_ statFromNode_(Tree& tree, int rootId);
733  static double bestRootPosition_(Tree& tree, int nodeId1, int nodeId2, double length);
734 
735 
738 };
739 
740 } //end of namespace bpp.
741 
742 #endif //_TREETOOLS_H_
743 
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
static TreeTemplate< Node > * thresholdConsensus(const std::vector< Tree *> &vecTr, double threshold, bool checkNames=true)
General greedy consensus tree method.
Definition: TreeTools.cpp:1022
Generic utilitary methods dealing with trees.
Definition: TreeTools.h:66
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
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
static void initBranchLengthsGrafen(Tree &tree)
Grafen&#39;s method to initialize branch lengths.
Definition: TreeTools.cpp:576
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
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
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
static size_t getNumberOfLeaves(const Tree &tree, int nodeId)
Count the number of leaves from a subtree.
Definition: TreeTools.cpp:98
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
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
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
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
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 ~TreeTools()
Definition: TreeTools.h:70
The phylogenetic node class.
Definition: Node.h:90
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.
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
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
static bool haveSameTopology(const Tree &tr1, const Tree &tr2)
Tells whether two trees have the same unrooted topology.
Definition: TreeTools.cpp:841
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
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
General exception thrown when something is wrong with a particular node.
static Tree * MRP(const std::vector< Tree *> &vecTr)
Matrix Representation Parsimony supertree method.
Definition: TreeTools.cpp:1095
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