bpp-phyl3  3.0.0
bpp::TreeTools Class Reference

Generic utilitary methods dealing with trees. More...

#include <Bpp/Phyl/Tree/TreeTools.h>

+ Collaboration diagram for bpp::TreeTools:

Classes

struct  Moments_
 

Public Member Functions

 TreeTools ()
 
virtual ~TreeTools ()
 

Static Public Member Functions

static std::unique_ptr< TreeMRP (const std::vector< std::unique_ptr< Tree >> &vecTr)
 Matrix Representation Parsimony supertree method. More...
 
static void computeBootstrapValues (Tree &tree, const std::vector< std::unique_ptr< Tree >> &vecTr, bool verbose=true, int format=0)
 Compute bootstrap values. More...
 
static void constrainedMidPointRooting (Tree &tree)
 Determine the mid-point position of the root along the branch that already contains the root. Consequently, the topology of the rooted tree remains identical. More...
 
static std::unique_ptr< TreeMRPMultilabel (const std::vector< Tree * > &vecTr)
 Matrix Representation Parsimony supertree method for multilabel trees. More...
 
Retrieve topology information
static std::vector< int > getLeavesId (const Tree &tree, int nodeId)
 Retrieve all leaves from a subtree. More...
 
static void getLeavesId (const Tree &tree, int nodeId, std::vector< int > &leaves)
 Retrieve all leaves from a subtree. More...
 
static size_t getNumberOfLeaves (const Tree &tree, int nodeId)
 Count the number of leaves from a subtree. More...
 
static int getLeafId (const Tree &tree, int nodeId, const std::string &name)
 Get the id of a leaf given its name in a subtree. More...
 
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. More...
 
static std::vector< int > getPathBetweenAnyTwoNodes (const Tree &tree, int nodeId1, int nodeId2, bool includeAncestor=true)
 Get a vector of ancestor nodes between to nodes. More...
 
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. More...
 
static int getLastCommonAncestor (const Tree &tree, const std::vector< int > &nodeIds)
 Get the id of the last common ancestors of all specified nodes. More...
 
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'. More...
 
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 'generations'. More...
 
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 the root of the subtree. More...
 
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 between leaves and the root of the subtree. More...
 
Conversion tools.

Convert to Newick standard tree description. The description is for a node, and hence is to be surrounded with parenthesis. ex: (A:0.001, (B:0.001, C:0.02)90:0.005)50:0.0005

static std::string nodeToParenthesis (const Tree &tree, int nodeId, bool writeId=false)
 Get the parenthesis description of a subtree. More...
 
static std::string nodeToParenthesis (const Tree &tree, int nodeId, bool bootstrap, const std::string &propertyName)
 Get the parenthesis description of a subtree. More...
 
static std::string treeToParenthesis (const Tree &tree, bool writeId=false)
 Get the parenthesis description of a tree. More...
 
static std::string treeToParenthesis (const Tree &tree, bool bootstrap, const std::string &propertyName)
 Get the parenthesis description of a tree. More...
 
Deal with identifiers
static std::vector< int > getNodesId (const Tree &tree, int nodeId)
 Retrieve all nodes ids nodes from a subtree. More...
 
static void getNodesId (const Tree &tree, int nodeId, std::vector< int > &nodes)
 Retrieve all nodes ids from a subtree. More...
 
static int getMaxId (const Tree &tree, int id)
 Get the maximum identifier used in a (sub)tree. More...
 
static int getMPNUId (const Tree &tree, int id)
 Get the minimum positive non-used identifier in a (sub)tree. More...
 
static bool checkIds (const Tree &tree, bool throwException)
 Check if the ids are uniques. More...
 
Topology methods
static std::unique_ptr< VectorSiteContainerMRPEncode (const std::vector< std::unique_ptr< Tree >> &vecTr)
 Creates a sequence data set corresponding to the Matrix Representation of the input trees. More...
 
static std::unique_ptr< VectorSiteContainerMRPEncodeMultilabel (const std::vector< std::unique_ptr< Tree >> &vecTr)
 Creates a sequence data set corresponding to the Matrix Representation of the input multilabel trees. More...
 
static bool haveSameTopology (const Tree &tr1, const Tree &tr2)
 Tells whether two trees have the same unrooted topology. More...
 
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. More...
 
static std::unique_ptr< BipartitionListbipartitionOccurrences (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. More...
 
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. More...
 
static std::unique_ptr< TreeTemplate< Node > > fullyResolvedConsensus (const std::vector< std::unique_ptr< Tree >> &vecTr, bool checkNames=true)
 Fully-resolved greedy consensus tree method. More...
 
static std::unique_ptr< TreeTemplate< Node > > majorityConsensus (const std::vector< std::unique_ptr< Tree >> &vecTr, bool checkNames=true)
 Majority consensus tree method. More...
 
static std::unique_ptr< TreeTemplate< Node > > strictConsensus (const std::vector< std::unique_ptr< Tree >> &vecTr, bool checkNames=true)
 Strict consensus tree method. More...
 

Some properties.

static const std::string BOOTSTRAP = "bootstrap"
 Bootstrap tag. More...
 
static Moments_ statFromNode_ (Tree &tree, int rootId)
 
static double bestRootPosition_ (Tree &tree, int nodeId1, int nodeId2, double length)
 

Act on branch lengths.

static Vdouble getBranchLengths (const Tree &tree, int nodeId)
 Get all the branch lengths of a subtree. More...
 
static double getTotalLength (const Tree &tree, int nodeId, bool includeAncestor=true)
 Get the total length (sum of all branch lengths) of a subtree. More...
 
static void setBranchLengths (Tree &tree, int nodeId, double brLen)
 Set all the branch lengths of a subtree. More...
 
static void setVoidBranchLengths (Tree &tree, int nodeId, double brLen)
 Give a length to branches that don't have one in a subtree. More...
 
static void scaleTree (Tree &tree, int nodeId, double factor)
 Scale a given tree. More...
 
static void initBranchLengthsGrafen (Tree &tree)
 Grafen's method to initialize branch lengths. More...
 
static void computeBranchLengthsGrafen (Tree &tree, double power=1, bool init=true)
 Compute branch lengths using Grafen's method. More...
 
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. More...
 
static double convertToClockTree2 (Tree &tree, int nodeId)
 Modify a tree's branch lengths to make a clock tree, by rescaling subtrees. More...
 
static double getDistanceBetweenAnyTwoNodes (const Tree &tree, int nodeId1, int nodeId2)
 Get the total distance between two nodes. More...
 
static std::unique_ptr< DistanceMatrixgetDistanceMatrix (const Tree &tree)
 Compute a distance matrix from a tree. More...
 
static void midpointRooting (Tree &tree)
 (Re)root the tree using the midpoint method. More...
 
static size_t initBranchLengthsGrafen (Tree &tree, int nodeId)
 
static void computeBranchLengthsGrafen (Tree &tree, int nodeId, double power, double total, double &height, double &heightRaised)
 

Detailed Description

Generic utilitary methods dealing with trees.

These methods work with all Tree object. However, depending on the tree implementation, they may not be the most efficient.

See also
TreeTemplateTools

Definition at line 30 of file TreeTools.h.

Constructor & Destructor Documentation

◆ TreeTools()

bpp::TreeTools::TreeTools ( )
inline

Definition at line 33 of file TreeTools.h.

◆ ~TreeTools()

virtual bpp::TreeTools::~TreeTools ( )
inlinevirtual

Definition at line 34 of file TreeTools.h.

Member Function Documentation

◆ bestRootPosition_()

double TreeTools::bestRootPosition_ ( Tree tree,
int  nodeId1,
int  nodeId2,
double  length 
)
staticprivate

Definition at line 1175 of file TreeTools.cpp.

References bpp::TreeTools::Moments_::N, and bpp::TreeTools::Moments_::sum.

◆ bipartitionOccurrences()

unique_ptr< BipartitionList > TreeTools::bipartitionOccurrences ( const std::vector< std::unique_ptr< Tree >> &  vecTr,
std::vector< size_t > &  bipScore 
)
static

Counts the total number of occurrences of every bipartition from the input trees.

Returns the list of distinct bipartitions found at least once in the set of input trees, and writes the number of occurrence of each of these bipartitions in vector bipScore.

Author
Nicolas Galtier
Parameters
vecTrVector of input trees (must share a common set of leaves - not checked in this function)
bipScoreOutput as the numbers of occurrences of the returned distinct bipartitions
Returns
A BipartitionList object including only distinct bipartitions

Definition at line 908 of file TreeTools.cpp.

References bpp::BipartitionTools::mergeBipartitionLists().

◆ checkIds()

bool TreeTools::checkIds ( const Tree tree,
bool  throwException 
)
static

Check if the ids are uniques.

Parameters
treeThe tree to check.
throwExceptionIf set to true, the function throws qn exception if a duplicated is found.
Returns
true if the tree has unique ids.

Definition at line 747 of file TreeTools.cpp.

References bpp::Tree::getNodesId(), and bpp::TextTools::toString().

Referenced by bpp::AbstractNonHomogeneousTreeLikelihood::init_(), and bpp::AbstractHomogeneousTreeLikelihood::init_().

◆ computeBootstrapValues()

void TreeTools::computeBootstrapValues ( Tree tree,
const std::vector< std::unique_ptr< Tree >> &  vecTr,
bool  verbose = true,
int  format = 0 
)
static

Compute bootstrap values.

Parameters
treeInput tree. the BOOTSTRAP branch property of the tree will be modified if it already exists.
vecTrA list of trees to compare to 'tree'.
verboseTell if a progress bar should be displayed.
formatThe output format of the tree.

Definition at line 1072 of file TreeTools.cpp.

References bpp::BipartitionTools::areIdentical(), bpp::ApplicationTools::displayGauge(), bpp::BipartitionList::getNumberOfBipartitions(), bpp::Tree::isLeaf(), bpp::pow(), and bpp::Tree::setBranchProperty().

◆ computeBranchLengthsGrafen() [1/2]

void TreeTools::computeBranchLengthsGrafen ( Tree tree,
double  power = 1,
bool  init = true 
)
static

Compute branch lengths using Grafen's method.

The 'height' of each node is divided by the total height of the tree, and the ratio is raised at power 'rho'. A value of rho=0 hence returns a star tree.

Reference: Grafen A. The phylogenetic regression. Philos Trans R Soc Lond B Biol Sci. 1989; 326(1233):119-57

Parameters
treeThe tree to use.
powerThe rho parameter.
initTell if the height must be initialized by calling the initBranchLengthsGrafen() method. Otherwise use branch lengths.
Exceptions
NodeExceptionIf init=false and one branch length is lacking.

Definition at line 584 of file TreeTools.cpp.

References bpp::Tree::getRootId().

Referenced by bpp::LegacyPhylogeneticsApplicationTools::getTrees().

◆ computeBranchLengthsGrafen() [2/2]

void TreeTools::computeBranchLengthsGrafen ( Tree tree,
int  nodeId,
double  power,
double  total,
double &  height,
double &  heightRaised 
)
staticprivate

◆ constrainedMidPointRooting()

void TreeTools::constrainedMidPointRooting ( Tree tree)
static

Determine the mid-point position of the root along the branch that already contains the root. Consequently, the topology of the rooted tree remains identical.

This code uses two inner functions to compute the mid-point position: statFromNode_ and bestRootPosition_. This code is inspired by a code performing a similar calculation in Seaview (Guindon et al., 2010, Mol. Biol. Evol. 27(2):221-4).

Parameters
treeThe rooted tree for which the root has to be moved to its mid-point position, along the branch where it already stands.

Definition at line 1153 of file TreeTools.cpp.

References bpp::Tree::getDistanceToFather(), bpp::Tree::getRootId(), bpp::Tree::getSonsId(), bpp::Tree::isMultifurcating(), bpp::Tree::isRooted(), and bpp::Tree::setDistanceToFather().

Referenced by bpp::LegacyPhylogeneticsApplicationTools::getTrees().

◆ convertToClockTree()

double TreeTools::convertToClockTree ( Tree tree,
int  nodeId,
bool  noneg = false 
)
static

Modify a tree's branch lengths to make a clock tree, by rebalancing branch lengths.

The height of each node is set to the mean height of all son nodes. This may however lead to negative branch lengths, since the mean height may be inferior to one of the son heights, due to short branch lengths. If the 'noneg' is set to yes, the mean height is checked against all son heights. If it is inferior to one of the son heights, the maximum son height is used instead. This results in a multifurcation.

This method is recursive and will be applied on all sons nodes.

Parameters
treeThe tree to use.
nodeIdThe node defining the subtree.
nonegTell if the correction for non negative branch lengths must be used.
Returns
The modified height of the node.
Exceptions
NodeNotFoundExceptionIf the node is not found.
NodeExceptionIf one branch length is lacking.

Definition at line 599 of file TreeTools.cpp.

References bpp::Tree::getDistanceToFather(), bpp::Tree::getSonsId(), bpp::Tree::hasDistanceToFather(), bpp::Tree::hasNode(), and bpp::Tree::setDistanceToFather().

Referenced by bpp::LegacyPhylogeneticsApplicationTools::getTrees().

◆ convertToClockTree2()

double TreeTools::convertToClockTree2 ( Tree tree,
int  nodeId 
)
static

Modify a tree's branch lengths to make a clock tree, by rescaling subtrees.

The height of each node is set to the mean height of all son nodes. All branch lengths of the corresponding subtrees are updated proportionally. This algorithm is smaller than the convertToClockTree method, but may be more accurate.

This method is recursive and will be applied on all sons nodes.

Parameters
treeThe tree to use.
nodeIdThe node defining the subtree.
Returns
The modified height of the node.
Exceptions
NodeNotFoundExceptionIf the node is not found.
NodeExceptionIf one branch length is lacking.

Definition at line 634 of file TreeTools.cpp.

References bpp::Tree::getDistanceToFather(), bpp::Tree::getSonsId(), bpp::Tree::hasDistanceToFather(), and bpp::Tree::hasNode().

◆ fullyResolvedConsensus()

unique_ptr< TreeTemplate< Node > > TreeTools::fullyResolvedConsensus ( const std::vector< std::unique_ptr< Tree >> &  vecTr,
bool  checkNames = true 
)
static

Fully-resolved greedy consensus tree method.

Calls thresholdConsensus with threshold=0, i.e. no constraint on the number of occurrence of bipartitions. The resulting tree is fully resolved.

Author
Nicolas Galtier
Parameters
vecTrVector of input trees (must share a common set of leaves - checked if checkNames is true)
checkNamesTell whether we should check the trees first.

Definition at line 1019 of file TreeTools.cpp.

◆ getAncestors()

vector< int > TreeTools::getAncestors ( const Tree tree,
int  nodeId 
)
static

Get a list of all ids of parents nodes, from the current node (not included) to the root of the tree.

Parameters
treeThe tree to use.
nodeIdThe id of node defining the subtree.
Returns
The list of ancestors ids.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 1106 of file TreeTools.cpp.

References bpp::Tree::getFatherId(), and bpp::Tree::hasFather().

◆ getBranchLengths()

Vdouble TreeTools::getBranchLengths ( const Tree tree,
int  nodeId 
)
static

Get all the branch lengths of a subtree.

Parameters
treeThe tree.
nodeIdThe node defining the subtree.
Returns
A vector with all branch lengths.
Exceptions
NodeNotFoundExceptionIf the node is not found.
NodeExceptionIf a branch length is lacking.

Definition at line 439 of file TreeTools.cpp.

References bpp::Tree::getDistanceToFather(), bpp::Tree::getSonsId(), bpp::Tree::hasDistanceToFather(), and bpp::Tree::hasNode().

◆ getDepth()

size_t TreeTools::getDepth ( const Tree tree,
int  nodeId 
)
static

Get the depth of the subtree defined by node 'node', i.e. the maximum number of sons 'generations'.

ex:

+----------A
|
---+ N1 +-------B
| |
+--------+ N2
|
+------C

Depth of node 'N1' id 2, depth of node 'N2' is 1, depth of leaves is 0.

Parameters
treeThe tree.
nodeIdThe id of node defining the subtree.
Returns
The depth of the subtree.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 138 of file TreeTools.cpp.

References bpp::Tree::getSonsId(), and bpp::Tree::hasNode().

◆ getDepths()

size_t TreeTools::getDepths ( const Tree tree,
int  nodeId,
std::map< int, size_t > &  depths 
)
static

Get the depths for all nodes of the subtree defined by node 'node', i.e. the maximum number of sons 'generations'.

ex:

*    +----------A
*    |
* ---+ N1     +-------B
*    |        |
*    +--------+ N2
*             |
*             +------C
* 

Depth of node 'N1' id 2, depth of node 'N2' is 1, depth of leaves is 0.

Parameters
treeThe tree.
nodeIdThe id of node defining the subtree.
depthsThe map that will contain all the depths of the nodes, with node ids as keys.
Returns
The depth of the subtree.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 155 of file TreeTools.cpp.

References bpp::Tree::getSonsId(), and bpp::Tree::hasNode().

◆ getDistanceBetweenAnyTwoNodes()

double TreeTools::getDistanceBetweenAnyTwoNodes ( const Tree tree,
int  nodeId1,
int  nodeId2 
)
static

Get the total distance between two nodes.

Sum all branch lengths between two nodes.

Parameters
treeThe tree to consider.
nodeId1First node id.
nodeId2Second node id.
Returns
The sum of all branch lengths between the two nodes.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 422 of file TreeTools.cpp.

References bpp::Tree::getDistanceToFather(), and bpp::Tree::hasNode().

◆ getDistanceMatrix()

std::unique_ptr< DistanceMatrix > TreeTools::getDistanceMatrix ( const Tree tree)
static

Compute a distance matrix from a tree.

Compute all distances between each leaves and store them in a matrix. A new DistanceMatrix object is created, and a pointer toward it is returned. The destruction of this matrix is left up to the user.

See also
getDistanceBetweenAnyTwoNodes
Parameters
treeThe tree to use.
Returns
The distance matrix computed from tree.

Definition at line 667 of file TreeTools.cpp.

References bpp::Tree::getLeafId(), and bpp::Tree::getLeavesNames().

◆ getHeight()

double TreeTools::getHeight ( const Tree tree,
int  nodeId 
)
static

Get the height of the subtree defined by node 'node', i.e. the maximum distance between leaves and the root of the subtree.

The distance do not include the branch length of the subtree root node. The height of a leaf is hence 0.

Parameters
treeThe tree.
nodeIdThe id of node defining the subtree.
Returns
The height of the subtree.
Exceptions
NodeNotFoundExceptionIf the node is not found.
NodeExceptionIf a branch length is lacking.

Definition at line 173 of file TreeTools.cpp.

References bpp::Tree::getDistanceToFather(), bpp::Tree::getSonsId(), bpp::Tree::hasDistanceToFather(), and bpp::Tree::hasNode().

Referenced by bpp::LegacyPhylogeneticsApplicationTools::getTrees().

◆ getHeights()

double TreeTools::getHeights ( const Tree tree,
int  nodeId,
std::map< int, double > &  heights 
)
static

Get the heights of all nodes within a subtree defined by node 'node', i.e. the maximum distance between leaves and the root of the subtree.

The height of a leaf is 0.

Parameters
treeThe tree.
nodeIdThe id of node defining the subtree.
heightsThe map that will contain all the heights of the nodes, with node ids as keys.
Returns
The height of the subtree.
Exceptions
NodeNotFoundExceptionIf the node is not found.
NodeExceptionIf a branch length is lacking.

Definition at line 195 of file TreeTools.cpp.

References bpp::Tree::getDistanceToFather(), bpp::Tree::getSonsId(), bpp::Tree::hasDistanceToFather(), and bpp::Tree::hasNode().

◆ getLastCommonAncestor()

int TreeTools::getLastCommonAncestor ( const Tree tree,
const std::vector< int > &  nodeIds 
)
static

Get the id of the last common ancestors of all specified nodes.

Nodes id need not correspond to leaves.

Author
Simon Carrignon
Parameters
treeThe tree to use.
nodeIdsThe ids of the input nodes.
Exceptions
NodeNotFoundExceptionIf at least of of input node is not found.

Definition at line 1120 of file TreeTools.cpp.

References count(), and bpp::Tree::getRootId().

◆ getLeafId()

int TreeTools::getLeafId ( const Tree tree,
int  nodeId,
const std::string &  name 
)
static

Get the id of a leaf given its name in a subtree.

Parameters
treeThe tree
nodeIdThe id of node defining the subtree.
nameThe name of the node.
Returns
The id of the node.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 84 of file TreeTools.cpp.

◆ getLeavesId() [1/2]

vector< int > TreeTools::getLeavesId ( const Tree tree,
int  nodeId 
)
static

Retrieve all leaves from a subtree.

Parameters
treeThe tree
nodeIdThe id of node defining the subtree.
Returns
A vector with the ids of all leaves in the subtree.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 42 of file TreeTools.cpp.

◆ getLeavesId() [2/2]

void TreeTools::getLeavesId ( const Tree tree,
int  nodeId,
std::vector< int > &  leaves 
)
static

Retrieve all leaves from a subtree.

Parameters
treeThe tree
nodeIdThe id of node defining the subtree.
leavesA vector with the ids of all leaves in the subtree.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 49 of file TreeTools.cpp.

References bpp::Tree::getSonsId(), bpp::Tree::hasNode(), and bpp::Tree::isLeaf().

◆ getMaxId()

int TreeTools::getMaxId ( const Tree tree,
int  id 
)
static

Get the maximum identifier used in a (sub)tree.

This is a recursive method.

Parameters
treeThe tree to check.
idThe identifier of the subtree from which the recursion will be performed. Use id=tree.getRootId() to search for the whole tree.
Returns
The identifier number with maximum value.

Definition at line 716 of file TreeTools.cpp.

References bpp::Tree::getSonsId().

◆ getMPNUId()

int TreeTools::getMPNUId ( const Tree tree,
int  id 
)
static

Get the minimum positive non-used identifier in a (sub)tree.

This method uses the recursive method getNodesId, and then sort the ids.

Parameters
treeThe tree to check.
idThe identifier of the subtree from which the recursion will be performed. Use id=tree.getRootId() to search for the whole tree.
Returns
A non-used identifier number.

Definition at line 731 of file TreeTools.cpp.

Referenced by bpp::TreeTemplate< N >::getNextId(), and bpp::TreeTemplateTools::midRoot().

◆ getNodesId() [1/2]

vector< int > TreeTools::getNodesId ( const Tree tree,
int  nodeId 
)
static

Retrieve all nodes ids nodes from a subtree.

Parameters
treeThe tree
nodeIdThe id of the node that defines the subtree.
Returns
A vector of ids of each node in the subtree.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 117 of file TreeTools.cpp.

Referenced by bpp::RNonHomogeneousMixedTreeLikelihood::init().

◆ getNodesId() [2/2]

void TreeTools::getNodesId ( const Tree tree,
int  nodeId,
std::vector< int > &  nodes 
)
static

Retrieve all nodes ids from a subtree.

Parameters
treeThe tree
nodeIdThe id of the node that defines the subtree.
nodesA vector of ids of each node in the subtree.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 124 of file TreeTools.cpp.

References bpp::Tree::getSonsId(), and bpp::Tree::hasNode().

◆ getNumberOfLeaves()

size_t TreeTools::getNumberOfLeaves ( const Tree tree,
int  nodeId 
)
static

Count the number of leaves from a subtree.

Parameters
treeThe tree
nodeIdThe id of node defining the subtree.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 64 of file TreeTools.cpp.

References bpp::Tree::getSonsId(), bpp::Tree::hasNode(), and bpp::Tree::isLeaf().

◆ getPathBetweenAnyTwoNodes()

vector< int > TreeTools::getPathBetweenAnyTwoNodes ( const Tree tree,
int  nodeId1,
int  nodeId2,
bool  includeAncestor = true 
)
static

Get a vector of ancestor nodes between to nodes.

Parameters
treeThe tree to use.
nodeId1Id of first node.
nodeId2Id of second node.
includeAncestorTell if the common ancestor must be included in the vector.
Returns
A vector of ancestor nodes ids.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 369 of file TreeTools.cpp.

References bpp::Tree::getFatherId(), bpp::Tree::hasFather(), and bpp::Tree::hasNode().

◆ getTotalLength()

double TreeTools::getTotalLength ( const Tree tree,
int  nodeId,
bool  includeAncestor = true 
)
static

Get the total length (sum of all branch lengths) of a subtree.

Parameters
treeThe tree.
nodeIdThe node defining the subtree.
includeAncestorTell if the branch length of the most ancient node should be included in the counting. (this should be set to false if this node is the root of the tree for instance).
Returns
The total length of the subtree.
Exceptions
NodeNotFoundExceptionIf the node is not found.
NodeExceptionIf a branch length is lacking.

Definition at line 462 of file TreeTools.cpp.

References bpp::Tree::getDistanceToFather(), bpp::Tree::getSonsId(), bpp::Tree::hasDistanceToFather(), and bpp::Tree::hasNode().

◆ haveSameTopology()

bool TreeTools::haveSameTopology ( const Tree tr1,
const Tree tr2 
)
static

Tells whether two trees have the same unrooted topology.

Author
Nicolas Galtier Note that the location of the root, if any, is ignored.

Definition at line 791 of file TreeTools.cpp.

References bpp::BipartitionTools::areIdentical(), bpp::Tree::getLeavesNames(), and bpp::VectorTools::haveSameElements().

◆ initBranchLengthsGrafen() [1/2]

void TreeTools::initBranchLengthsGrafen ( Tree tree)
static

Grafen's method to initialize branch lengths.

Each height of the node (total distance from the leaves) is set equal to the number of leaf nodes for the corresponding subtrees - 1 for inner nodes, 0 for leaves.

If the tree already has branch lengths, they will be ignored.

Reference: Grafen A. The phylogenetic regression. Philos Trans R Soc Lond B Biol Sci. 1989; 326(1233):119-57

Parameters
treeThe tree.

Definition at line 543 of file TreeTools.cpp.

References bpp::Tree::getRootId().

◆ initBranchLengthsGrafen() [2/2]

size_t TreeTools::initBranchLengthsGrafen ( Tree tree,
int  nodeId 
)
staticprivate

◆ majorityConsensus()

unique_ptr< TreeTemplate< Node > > TreeTools::majorityConsensus ( const std::vector< std::unique_ptr< Tree >> &  vecTr,
bool  checkNames = true 
)
static

Majority consensus tree method.

Calls thresholdConsensus with threshold=0.5: internal branches present in a majority of trees are kept.

Author
Nicolas Galtier
Parameters
vecTrVector of input trees (must share a common set of leaves - checked if checkNames is true)
checkNamesTell whether we should check the trees first.

Definition at line 1026 of file TreeTools.cpp.

◆ midpointRooting()

void TreeTools::midpointRooting ( Tree tree)
static

(Re)root the tree using the midpoint method.

This methods compute the pairwise distance matrix from the tree and get the maximum distance. The root is then set on the branch located at half this distance.

Parameters
treeThe tree to (re)root.

Definition at line 684 of file TreeTools.cpp.

References bpp::Tree::getDistanceToFather(), bpp::Tree::getFatherId(), bpp::Tree::getLeafId(), bpp::Tree::getRootId(), bpp::Tree::getSonsId(), bpp::Tree::isRooted(), bpp::Tree::newOutGroup(), bpp::Tree::setDistanceToFather(), bpp::Tree::unroot(), and bpp::MatrixTools::whichMax().

◆ MRP()

unique_ptr< Tree > TreeTools::MRP ( const std::vector< std::unique_ptr< Tree >> &  vecTr)
static

Matrix Representation Parsimony supertree method.

This implementation of the MRP method takes a BIONJ tree (Jukes-Cantor distances) as the starting tree and optimizes the parsimony score using only NNI (in a PHYML-like way).

Author
Nicolas Galtier
Parameters
vecTrA vector of trees.
Returns
The MRP super tree.

Definition at line 1040 of file TreeTools.cpp.

◆ MRPEncode()

unique_ptr< VectorSiteContainer > TreeTools::MRPEncode ( const std::vector< std::unique_ptr< Tree >> &  vecTr)
static

Creates a sequence data set corresponding to the Matrix Representation of the input trees.

Author
Nicolas Galtier Trees can have distinct sets of elements - missing data will be represented as 'N'. The output alignment (DNA sequences including only A, C and N)) is ready for maximum parsimony analysis according to the MRP supertree method.

Definition at line 765 of file TreeTools.cpp.

References bpp::BipartitionTools::MRPEncode().

◆ MRPEncodeMultilabel()

unique_ptr< VectorSiteContainer > TreeTools::MRPEncodeMultilabel ( const std::vector< std::unique_ptr< Tree >> &  vecTr)
static

Creates a sequence data set corresponding to the Matrix Representation of the input multilabel trees.

Author
Nicolas Galtier and Bastien Boussau Trees can have distinct sets of elements - missing data will be represented as 'N'. The output alignment (DNA sequences including only A, C and N)) is ready for maximum parsimony analysis according to the MRP supertree method.

Definition at line 778 of file TreeTools.cpp.

References bpp::BipartitionTools::MRPEncodeMultilabel().

◆ MRPMultilabel()

unique_ptr< Tree > TreeTools::MRPMultilabel ( const std::vector< Tree * > &  vecTr)
static

Matrix Representation Parsimony supertree method for multilabel trees.

This implementation of the MRP method takes a BIONJ tree (Jukes-Cantor distances) as the starting tree and optimizes the parsimony score using only NNI (in a PHYML-like way).

Author
Nicolas Galtier slightly modified by Bastien Boussau
Parameters
vecTrA vector of trees.
Returns
The MRP super tree.

Definition at line 1235 of file TreeTools.cpp.

◆ nodeToParenthesis() [1/2]

string TreeTools::nodeToParenthesis ( const Tree tree,
int  nodeId,
bool  bootstrap,
const std::string &  propertyName 
)
static

Get the parenthesis description of a subtree.

Parameters
treeThe tree
nodeIdThe node defining the subtree.
bootstrapTell is bootstrap values must be written. If so, the content of the property with name TreeTools::BOOTSTRAP will be written as bootstrap value. The property should be a Number<double> object. Otherwise, the content of the property with name 'propertyName' will be written. In this later case, the property should be a String object.
propertyNameThe name of the property to use. Only used if bootstrap = false.
Returns
A string in the parenthesis format.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 256 of file TreeTools.cpp.

References bpp::Tree::getBranchProperty(), bpp::Tree::getDistanceToFather(), bpp::Tree::getNodeName(), bpp::Tree::getSonsId(), bpp::Tree::hasBranchProperty(), bpp::Tree::hasDistanceToFather(), bpp::Tree::hasNode(), and bpp::Tree::hasNoSon().

◆ nodeToParenthesis() [2/2]

string TreeTools::nodeToParenthesis ( const Tree tree,
int  nodeId,
bool  writeId = false 
)
static

Get the parenthesis description of a subtree.

Parameters
treeThe tree
nodeIdThe id of node defining the subtree.
writeIdTells if node ids must be printed. This will overwrite bootstrap values if there are ones. Leaves id will be added to the leave names, separated by a '_' character.
Returns
A string in the parenthesis format.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 218 of file TreeTools.cpp.

References bpp::Tree::getBranchProperty(), bpp::Tree::getDistanceToFather(), bpp::Tree::getNodeName(), bpp::Tree::getSonsId(), bpp::Tree::hasBranchProperty(), bpp::Tree::hasDistanceToFather(), bpp::Tree::hasNode(), and bpp::Tree::hasNoSon().

◆ robinsonFouldsDistance()

int TreeTools::robinsonFouldsDistance ( const Tree tr1,
const Tree tr2,
bool  checkNames = true,
int *  missing_in_tr2 = NULL,
int *  missing_in_tr1 = NULL 
)
static

Calculates the Robinson-Foulds topological distance between two trees.

The two trees must share a common set of leaves (checked if checkNames is true) Three numbers are calculated:

Author
Nicolas Galtier
Parameters
tr1First input tree.
tr2Second input tree.
missing_in_tr2Output as the number of bipartitions occurring in the first tree but not the second
missing_in_tr1Output as the number of bipartitions occurring in the second tree but not the first
checkNamesTell whether we should check the trees first.
Returns
Robinson-Foulds distance = *missing_in_tr1 + *missing_in_tr2
Exceptions
ExceptionIf checkNames is set to true and trees do not share the same leaves names.

Definition at line 842 of file TreeTools.cpp.

References bpp::BipartitionTools::areIdentical(), bpp::Tree::getLeavesNames(), and bpp::VectorTools::haveSameElements().

Referenced by bpp::OptimizationTools::buildDistanceTree().

◆ scaleTree()

void TreeTools::scaleTree ( Tree tree,
int  nodeId,
double  factor 
)
static

Scale a given tree.

Multiply all branch lengths by a given factor.

Parameters
treeThe tree.
nodeIdThe node defining the subtree.
factorThe factor to multiply all branch lengths with.
Exceptions
NodeNotFoundExceptionIf the node is not found.
NodeExceptionIf a branch length is lacking.

Definition at line 506 of file TreeTools.cpp.

References bpp::Tree::getDistanceToFather(), bpp::Tree::hasDistanceToFather(), bpp::Tree::hasFather(), bpp::Tree::hasNode(), and bpp::Tree::setDistanceToFather().

◆ searchLeaf()

void TreeTools::searchLeaf ( const Tree tree,
int  nodeId,
const std::string &  name,
int *&  id 
)
static

Get the id of a leaf given its name in a subtree.

Parameters
treeThe tree
nodeIdThe id of node defining the subtree.
nameThe name of the node.
idThe id of the node.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 98 of file TreeTools.cpp.

References bpp::Tree::getNodeName(), and bpp::Tree::hasNoSon().

◆ setBranchLengths()

void TreeTools::setBranchLengths ( Tree tree,
int  nodeId,
double  brLen 
)
static

Set all the branch lengths of a subtree.

Parameters
treeThe tree.
nodeIdThe node defining the subtree.
brLenThe branch length to apply.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 479 of file TreeTools.cpp.

References bpp::Tree::hasNode(), and bpp::Tree::setDistanceToFather().

◆ setVoidBranchLengths()

void TreeTools::setVoidBranchLengths ( Tree tree,
int  nodeId,
double  brLen 
)
static

Give a length to branches that don't have one in a subtree.

Parameters
treeThe tree.
nodeIdThe node defining the subtree.
brLenThe branch length to apply.
Exceptions
NodeNotFoundExceptionIf the node is not found.

Definition at line 492 of file TreeTools.cpp.

References bpp::Tree::hasDistanceToFather(), bpp::Tree::hasNode(), and bpp::Tree::setDistanceToFather().

◆ statFromNode_()

◆ strictConsensus()

unique_ptr< TreeTemplate< Node > > TreeTools::strictConsensus ( const std::vector< std::unique_ptr< Tree >> &  vecTr,
bool  checkNames = true 
)
static

Strict consensus tree method.

Calls thresholdConsensus with threshold=1: only internal branches present in all trees are kept.

Author
Nicolas Galtier
Parameters
vecTrVector of input trees (must share a common set of leaves - checked if checkNames is true)
checkNamesTell whether we should check the trees first.

Definition at line 1033 of file TreeTools.cpp.

◆ thresholdConsensus()

unique_ptr< TreeTemplate< Node > > TreeTools::thresholdConsensus ( const std::vector< std::unique_ptr< Tree >> &  vecTr,
double  threshold,
bool  checkNames = true 
)
static

General greedy consensus tree method.

Calculates the consensus tree of a set of trees defined from the number of occurrences of bipartitions. Bipartitions are considered in decreasing score order. A bipartition is included if it is compatible with all previously included bipartitions, and if its score is higher than a threshold.

Author
Nicolas Galtier
Parameters
vecTrVector of input trees (must share a common set of leaves - checked if checkNames is true)
thresholdMinimal acceptable score =number of occurrence of a bipartition/number of trees (0.<=threshold<=1.)
checkNamesTell whether we should check the trees first.

Definition at line 969 of file TreeTools.cpp.

References bpp::VectorTools::haveSameElements().

◆ treeToParenthesis() [1/2]

string TreeTools::treeToParenthesis ( const Tree tree,
bool  bootstrap,
const std::string &  propertyName 
)
static

Get the parenthesis description of a tree.

Parameters
treeThe tree to convert.
bootstrapTell is bootstrap values must be written. If so, the content of the property with name TreeTools::BOOTSTRAP will be written as bootstrap value. The property should be a Number<double> object. Otherwise, the content of the property with name 'propertyName' will be written. In this later case, the property should be a String object.
propertyNameThe name of the property to use. Only used if bootstrap = false.
Returns
A string in the parenthesis format.

Definition at line 328 of file TreeTools.cpp.

References bpp::Tree::getBranchProperty(), bpp::Tree::getNodeName(), bpp::Tree::getRootId(), bpp::Tree::getSonsId(), bpp::Tree::hasBranchProperty(), and bpp::Tree::hasNoSon().

◆ treeToParenthesis() [2/2]

string TreeTools::treeToParenthesis ( const Tree tree,
bool  writeId = false 
)
static

Get the parenthesis description of a tree.

Parameters
treeThe tree to convert.
writeIdTells if node ids must be printed. This will overwrite bootstrap values if there are ones. Leaves id will be added to the leave names, separated by a '_' character.
Returns
A string in the parenthesis format.

Definition at line 296 of file TreeTools.cpp.

References bpp::Tree::getNodeName(), bpp::Tree::getRootId(), bpp::Tree::getSonsId(), and bpp::Tree::hasNoSon().

Referenced by bpp::NexusIOTree::write_(), and bpp::Newick::write_().

Member Data Documentation

◆ BOOTSTRAP


The documentation for this class was generated from the following files: