bpp-phyl3  3.0.0
TreeTools.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_PHYL_TREE_TREETOOLS_H
6 #define BPP_PHYL_TREE_TREETOOLS_H
7 
8 #include <Bpp/Exceptions.h>
10 
11 #include "BipartitionList.h"
12 #include "Node.h"
13 #include "Tree.h"
14 #include "TreeExceptions.h"
15 
16 // From SeqLib:
18 #include <Bpp/Seq/DistanceMatrix.h>
19 
20 namespace bpp
21 {
30 class TreeTools
31 {
32 public:
33  TreeTools() {}
34  virtual ~TreeTools() {}
35 
36 public:
51  static std::vector<int> getLeavesId(const Tree& tree, int nodeId);
52 
61  static void getLeavesId(const Tree& tree, int nodeId, std::vector<int>& leaves);
62 
70  static size_t getNumberOfLeaves(const Tree& tree, int nodeId);
71 
81  static int getLeafId(const Tree& tree, int nodeId, const std::string& name);
82 
92  static void searchLeaf(const Tree& tree, int nodeId, const std::string& name, int*& id);
93 
104  static std::vector<int> getPathBetweenAnyTwoNodes(const Tree& tree, int nodeId1, int nodeId2, bool includeAncestor = true);
105 
114  static std::vector<int> getAncestors(const Tree& tree, int nodeId);
115 
126  static int getLastCommonAncestor(const Tree& tree, const std::vector<int>& nodeIds);
127 
149  static size_t getDepth(const Tree& tree, int nodeId);
150 
173  static size_t getDepths(const Tree& tree, int nodeId, std::map<int, size_t>& depths);
174 
188  static double getHeight(const Tree& tree, int nodeId);
189 
203  static double getHeights(const Tree& tree, int nodeId, std::map<int, double>& heights);
204 
222  static Vdouble getBranchLengths(const Tree& tree, int nodeId);
223 
235  static double getTotalLength(const Tree& tree, int nodeId, bool includeAncestor = true);
236 
245  static void setBranchLengths(Tree& tree, int nodeId, double brLen);
246 
255  static void setVoidBranchLengths(Tree& tree, int nodeId, double brLen);
256 
268  static void scaleTree(Tree& tree, int nodeId, double factor);
269 
283  static void initBranchLengthsGrafen(Tree& tree);
284 
300  static void computeBranchLengthsGrafen(Tree& tree, double power = 1, bool init = true);
301 
302 private:
303  static size_t initBranchLengthsGrafen(Tree& tree, int nodeId);
304  static void computeBranchLengthsGrafen(Tree& tree, int nodeId, double power, double total, double& height, double& heightRaised);
305 
306 public:
326  static double convertToClockTree(Tree& tree, int nodeId, bool noneg = false);
327 
343  static double convertToClockTree2(Tree& tree, int nodeId);
344 
356  static double getDistanceBetweenAnyTwoNodes(const Tree& tree, int nodeId1, int nodeId2);
357 
370  static std::unique_ptr<DistanceMatrix> getDistanceMatrix(const Tree& tree);
371 
380  static void midpointRooting(Tree& tree);
405  static std::string nodeToParenthesis(const Tree& tree, int nodeId, bool writeId = false);
406 
421  static std::string nodeToParenthesis(const Tree& tree, int nodeId, bool bootstrap, const std::string& propertyName);
422 
432  static std::string treeToParenthesis(const Tree& tree, bool writeId = false);
433 
446  static std::string treeToParenthesis(const Tree& tree, bool bootstrap, const std::string& propertyName);
447 
464  static std::vector<int> getNodesId(const Tree& tree, int nodeId);
465 
474  static void getNodesId(const Tree& tree, int nodeId, std::vector<int>& nodes);
475 
486  static int getMaxId(const Tree& tree, int id);
487 
498  static int getMPNUId(const Tree& tree, int id);
499 
507  static bool checkIds(const Tree& tree, bool throwException);
508 
526  static std::unique_ptr<VectorSiteContainer> MRPEncode(const std::vector<std::unique_ptr<Tree>>& vecTr);
527 
536  static std::unique_ptr<VectorSiteContainer> MRPEncodeMultilabel(const std::vector<std::unique_ptr<Tree>>& vecTr);
537 
544  static bool haveSameTopology(const Tree& tr1, const Tree& tr2);
545 
561  static int robinsonFouldsDistance(const Tree& tr1, const Tree& tr2, bool checkNames = true, int* missing_in_tr2 = NULL, int* missing_in_tr1 = NULL);
562 
574  static std::unique_ptr<BipartitionList> bipartitionOccurrences(const std::vector<std::unique_ptr<Tree>>& vecTr, std::vector<size_t>& bipScore);
575 
589  static std::unique_ptr<TreeTemplate<Node>> thresholdConsensus(const std::vector<std::unique_ptr<Tree>>& vecTr, double threshold, bool checkNames = true);
590 
601  static std::unique_ptr<TreeTemplate<Node>> fullyResolvedConsensus(const std::vector<std::unique_ptr<Tree>>& vecTr, bool checkNames = true);
602 
612  static std::unique_ptr<TreeTemplate<Node>> majorityConsensus(const std::vector<std::unique_ptr<Tree>>& vecTr, bool checkNames = true);
613 
623  static std::unique_ptr<TreeTemplate<Node>> strictConsensus(const std::vector<std::unique_ptr<Tree>>& vecTr, bool checkNames = true);
624 
638  static std::unique_ptr<Tree> MRP(const std::vector<std::unique_ptr<Tree>>& vecTr);
639 
649  static void computeBootstrapValues(Tree& tree, const std::vector<std::unique_ptr<Tree>>& vecTr, bool verbose = true, int format = 0);
650 
659  static void constrainedMidPointRooting(Tree& tree);
660 
672  static std::unique_ptr<Tree> MRPMultilabel(const std::vector<Tree*>& vecTr);
673 
674 
684  static const std::string BOOTSTRAP;
685 
686 private:
687  struct Moments_
688  {
689  double N;
690  double sum, squaredSum;
691  Moments_() : N(0),
692  sum(0),
693  squaredSum(0) {}
694  };
695 
696  static Moments_ statFromNode_(Tree& tree, int rootId);
697  static double bestRootPosition_(Tree& tree, int nodeId1, int nodeId2, double length);
698 
699 
701 };
702 } // end of namespace bpp.
703 #endif // BPP_PHYL_TREE_TREETOOLS_H
Generic utilitary methods dealing with trees.
Definition: TreeTools.h:31
static std::unique_ptr< VectorSiteContainer > MRPEncode(const std::vector< std::unique_ptr< Tree >> &vecTr)
Creates a sequence data set corresponding to the Matrix Representation of the input trees.
Definition: TreeTools.cpp:765
static void computeBootstrapValues(Tree &tree, const std::vector< std::unique_ptr< Tree >> &vecTr, bool verbose=true, int format=0)
Compute bootstrap values.
Definition: TreeTools.cpp:1072
static size_t getDepth(const Tree &tree, int nodeId)
Get the depth of the subtree defined by node 'node', i.e. the maximum number of sons 'generations'.
Definition: TreeTools.cpp:138
static void initBranchLengthsGrafen(Tree &tree)
Grafen's method to initialize branch lengths.
Definition: TreeTools.cpp:543
static double convertToClockTree(Tree &tree, int nodeId, bool noneg=false)
Modify a tree's branch lengths to make a clock tree, by rebalancing branch lengths.
Definition: TreeTools.cpp:599
static std::vector< int > getLeavesId(const Tree &tree, int nodeId)
Retrieve all leaves from a subtree.
Definition: TreeTools.cpp:42
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:84
static std::unique_ptr< TreeTemplate< Node > > strictConsensus(const std::vector< std::unique_ptr< Tree >> &vecTr, bool checkNames=true)
Strict consensus tree method.
Definition: TreeTools.cpp:1033
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 'node', i.e. the maximum distance betwe...
Definition: TreeTools.cpp:195
static std::string nodeToParenthesis(const Tree &tree, int nodeId, bool writeId=false)
Get the parenthesis description of a subtree.
Definition: TreeTools.cpp:218
static std::unique_ptr< DistanceMatrix > getDistanceMatrix(const Tree &tree)
Compute a distance matrix from a tree.
Definition: TreeTools.cpp:667
static void constrainedMidPointRooting(Tree &tree)
Determine the mid-point position of the root along the branch that already contains the root....
Definition: TreeTools.cpp:1153
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:1120
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:98
static std::unique_ptr< Tree > MRP(const std::vector< std::unique_ptr< Tree >> &vecTr)
Matrix Representation Parsimony supertree method.
Definition: TreeTools.cpp:1040
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:369
virtual ~TreeTools()
Definition: TreeTools.h:34
static std::unique_ptr< Tree > MRPMultilabel(const std::vector< Tree * > &vecTr)
Matrix Representation Parsimony supertree method for multilabel trees.
Definition: TreeTools.cpp:1235
static void setBranchLengths(Tree &tree, int nodeId, double brLen)
Set all the branch lengths of a subtree.
Definition: TreeTools.cpp:479
static double bestRootPosition_(Tree &tree, int nodeId1, int nodeId2, double length)
Definition: TreeTools.cpp:1175
static std::string treeToParenthesis(const Tree &tree, bool writeId=false)
Get the parenthesis description of a tree.
Definition: TreeTools.cpp:296
static Moments_ statFromNode_(Tree &tree, int rootId)
Definition: TreeTools.cpp:1204
static bool checkIds(const Tree &tree, bool throwException)
Check if the ids are uniques.
Definition: TreeTools.cpp:747
static double convertToClockTree2(Tree &tree, int nodeId)
Modify a tree's branch lengths to make a clock tree, by rescaling subtrees.
Definition: TreeTools.cpp:634
static void setVoidBranchLengths(Tree &tree, int nodeId, double brLen)
Give a length to branches that don't have one in a subtree.
Definition: TreeTools.cpp:492
static void scaleTree(Tree &tree, int nodeId, double factor)
Scale a given tree.
Definition: TreeTools.cpp:506
static size_t getNumberOfLeaves(const Tree &tree, int nodeId)
Count the number of leaves from a subtree.
Definition: TreeTools.cpp:64
static void computeBranchLengthsGrafen(Tree &tree, double power=1, bool init=true)
Compute branch lengths using Grafen's method.
Definition: TreeTools.cpp:584
static double getHeight(const Tree &tree, int nodeId)
Get the height of the subtree defined by node 'node', i.e. the maximum distance between leaves and th...
Definition: TreeTools.cpp:173
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:462
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 'node', i.e. the maximum number of sons '...
Definition: TreeTools.cpp:155
static int getMaxId(const Tree &tree, int id)
Get the maximum identifier used in a (sub)tree.
Definition: TreeTools.cpp:716
static const std::string BOOTSTRAP
Bootstrap tag.
Definition: TreeTools.h:684
static std::vector< int > getNodesId(const Tree &tree, int nodeId)
Retrieve all nodes ids nodes from a subtree.
Definition: TreeTools.cpp:117
static int getMPNUId(const Tree &tree, int id)
Get the minimum positive non-used identifier in a (sub)tree.
Definition: TreeTools.cpp:731
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:842
static std::unique_ptr< TreeTemplate< Node > > thresholdConsensus(const std::vector< std::unique_ptr< Tree >> &vecTr, double threshold, bool checkNames=true)
General greedy consensus tree method.
Definition: TreeTools.cpp:969
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:1106
static bool haveSameTopology(const Tree &tr1, const Tree &tr2)
Tells whether two trees have the same unrooted topology.
Definition: TreeTools.cpp:791
static std::unique_ptr< TreeTemplate< Node > > majorityConsensus(const std::vector< std::unique_ptr< Tree >> &vecTr, bool checkNames=true)
Majority consensus tree method.
Definition: TreeTools.cpp:1026
static double getDistanceBetweenAnyTwoNodes(const Tree &tree, int nodeId1, int nodeId2)
Get the total distance between two nodes.
Definition: TreeTools.cpp:422
static void midpointRooting(Tree &tree)
(Re)root the tree using the midpoint method.
Definition: TreeTools.cpp:684
static std::unique_ptr< BipartitionList > bipartitionOccurrences(const std::vector< std::unique_ptr< Tree >> &vecTr, std::vector< size_t > &bipScore)
Counts the total number of occurrences of every bipartition from the input trees.
Definition: TreeTools.cpp:908
static std::unique_ptr< VectorSiteContainer > MRPEncodeMultilabel(const std::vector< std::unique_ptr< Tree >> &vecTr)
Creates a sequence data set corresponding to the Matrix Representation of the input multilabel trees.
Definition: TreeTools.cpp:778
static std::unique_ptr< TreeTemplate< Node > > fullyResolvedConsensus(const std::vector< std::unique_ptr< Tree >> &vecTr, bool checkNames=true)
Fully-resolved greedy consensus tree method.
Definition: TreeTools.cpp:1019
static Vdouble getBranchLengths(const Tree &tree, int nodeId)
Get all the branch lengths of a subtree.
Definition: TreeTools.cpp:439
Interface for phylogenetic tree objects.
Definition: Tree.h:115
Defines the basic types of data flow nodes.
std::vector< double > Vdouble