bpp-phyl3 3.0.0
TreeTemplateTools.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_TREETEMPLATETOOLS_H
6#define BPP_PHYL_TREE_TREETEMPLATETOOLS_H
7
9
10#include "TreeTools.h"
11#include "Node.h"
12
13// From the STL:
14#include <string>
15#include <vector>
16
17
18namespace bpp
19{
20template<class N> class TreeTemplate;
21
22class PhyloTree;
23
30{
31public:
33 virtual ~TreeTemplateTools() {}
34
35public:
48 template<class N>
49 static std::vector<N*> getLeaves(N& node)
50 {
51 std::vector<N*> leaves;
52 getLeaves<N>(node, leaves);
53 return leaves;
54 }
55
62 template<class N>
63 static void getLeaves(N& node, std::vector<N*>& leaves)
64 {
65 if (node.isLeaf())
66 {
67 leaves.push_back(&node);
68 }
69 for (size_t i = 0; i < node.getNumberOfSons(); i++)
70 {
71 getLeaves<N>(*dynamic_cast<N*>(node.getSon(i)), leaves);
72 }
73 }
74
81 static std::vector<int> getLeavesId(const Node& node)
82 {
83 std::vector<int> ids;
84 getLeavesId(node, ids);
85 return ids;
86 }
87
94 static void getLeavesId(const Node& node, std::vector<int>& ids);
95
102 static std::vector<int> getAncestorsId(const Node& node);
103
112 static int getLeafId(const Node& node, const std::string& name)
113 {
114 int* id = 0;
115 searchLeaf(node, name, id);
116 if (id == 0) throw NodeNotFoundException("TreeTemplateTools::getLeafId().", name);
117 else
118 {
119 int i = *id;
120 delete id;
121 return i;
122 }
123 }
124
133 static void searchLeaf(const Node& node, const std::string& name, int*& id);
134
142 template<class N>
143 static void dropLeaf(TreeTemplate<N>& tree, const std::string& leafName)
144 {
145 N* leaf = tree.getNode(leafName);
146 if (!leaf->hasFather())
147 throw Exception("TreeTemplateTools::dropLeaf(). Leaf is the only node in the tree, can't remove it.");
148 N* parent = leaf->getFather();
149 if (parent->getNumberOfSons() > 2)
150 {
151 // The easy case:
152 parent->removeSon(leaf);
153 delete leaf;
154 }
155 else if (parent->getNumberOfSons() == 2)
156 {
157 // We have to delete the parent node as well:
158 N* brother = parent->getSon(0);
159 if (brother == leaf) brother = parent->getSon(1);
160 if (!parent->hasFather())
161 {
162 // The brother becomes the root:
163 if (leaf->hasDistanceToFather() && brother->hasDistanceToFather())
164 {
165 brother->setDistanceToFather(brother->getDistanceToFather() + leaf->getDistanceToFather());
166 }
167 brother->removeFather();
168 tree.setRootNode(brother);
169 delete parent;
170 delete leaf;
171 }
172 else
173 {
174 N* gParent = parent->getFather();
175 if (brother->hasDistanceToFather() && parent->hasDistanceToFather())
176 {
177 brother->setDistanceToFather(brother->getDistanceToFather() + parent->getDistanceToFather());
178 }
179 size_t pos = gParent->getSonPosition(parent);
180 gParent->setSon(pos, brother);
181 delete parent;
182 delete leaf;
183 }
184 }
185 else
186 {
187 // Dunno what to do in that case :(
188 throw Exception("TreeTemplateTools::dropLeaf. Parent node as only one child, I don't know what to do in that case :(");
189 }
190 }
191
199 template<class N>
200 static void dropSubtree(TreeTemplate<N>& tree, Node* subtree)
201 {
202 if (!subtree->hasFather())
203 throw Exception("TreeTemplateTools::dropSubtree(). Trying to remove the full tree!");
204 N* parent = subtree->getFather();
205 if (parent->getNumberOfSons() > 2)
206 {
207 // The easy case:
208 parent->removeSon(subtree);
209 deleteSubtree(subtree);
210 }
211 else if (parent->getNumberOfSons() == 2)
212 {
213 // We have to delete the parent node as well:
214 N* brother = parent->getSon(0);
215 if (brother == subtree) brother = parent->getSon(1);
216 if (!parent->hasFather())
217 {
218 // The brother becomes the root:
219 if (subtree->hasDistanceToFather() && brother->hasDistanceToFather())
220 {
221 brother->setDistanceToFather(brother->getDistanceToFather() + subtree->getDistanceToFather());
222 }
223 tree.setRootNode(brother);
224 delete parent;
225 deleteSubtree(subtree);
226 }
227 else
228 {
229 N* gParent = parent->getFather();
230 if (brother->hasDistanceToFather() && parent->hasDistanceToFather())
231 {
232 brother->setDistanceToFather(brother->getDistanceToFather() + parent->getDistanceToFather());
233 }
234 size_t pos = gParent->getSonPosition(parent);
235 gParent->setSon(pos, brother);
236 delete parent;
237 deleteSubtree(subtree);
238 }
239 }
240 else
241 {
242 // Dunno what to do in that case :(
243 throw Exception("TreeTemplateTools::dropSubtree. Parent node as only one child, I don't know what to do in that case :(");
244 }
245 }
246
254 template<class N>
255 static void sampleSubtree(TreeTemplate<N>& tree, const std::vector<std::string>& leaves, size_t size)
256 {
257 std::vector<std::string> names = leaves;
258 for (size_t n = names.size(); n > size; --n)
259 {
261 dropLeaf(tree, names[i]);
262 names.erase(names.begin() + static_cast<ptrdiff_t>(i));
263 }
264 }
265
272 template<class N>
273 static std::vector<N*> getNodes(N& node)
274 {
275 std::vector<N*> nodes;
276 getNodes<N>(node, nodes);
277 return nodes;
278 }
279
286 template<class N>
287 static void getNodes(N& node, std::vector<N*>& nodes)
288 {
289 for (size_t i = 0; i < node.getNumberOfSons(); i++)
290 {
291 getNodes<N>(*dynamic_cast<N*>(node.getSon(i)), nodes);
292 }
293 nodes.push_back(&node);
294 }
295
302 static std::vector<int> getNodesId(const Node& node)
303 {
304 std::vector<int> ids;
305 getNodesId(node, ids);
306 return ids;
307 }
308
315 static void getNodesId(const Node& node, std::vector<int>& ids)
316 {
317 for (size_t i = 0; i < node.getNumberOfSons(); i++)
318 {
319 getNodesId(*node.getSon(i), ids);
320 }
321 ids.push_back(node.getId());
322 }
323
330 template<class N>
331 static std::vector<N*> getInnerNodes(N& node)
332 {
333 std::vector<N*> nodes;
334 getInnerNodes<N>(node, nodes);
335 return nodes;
336 }
337
346 template<class N>
347 static void getInnerNodes(N& node, std::vector<N*>& nodes)
348 {
349 for (size_t i = 0; i < node.getNumberOfSons(); i++)
350 {
351 getInnerNodes<N>(*dynamic_cast<N*>(node.getSon(i)), nodes);
352 }
353 if (!node.isLeaf())
354 nodes.push_back(&node); // Do not add leaves!
355 }
356
365 static std::vector<int> getInnerNodesId(const Node& node)
366 {
367 std::vector<int> ids;
368 getInnerNodesId(node, ids);
369 return ids;
370 }
371
378 static void getInnerNodesId(const Node& node, std::vector<int>& ids)
379 {
380 for (size_t i = 0; i < node.getNumberOfSons(); i++)
381 {
382 getInnerNodesId(*node.getSon(i), ids);
383 }
384 if (!node.isLeaf())
385 ids.push_back(node.getId()); // Do not add leaves!
386 }
387
393 template<class N>
394 static std::vector<N*> searchNodeWithId(N& node, int id)
395 {
396 std::vector<N*> nodes;
397 searchNodeWithId<N>(node, id, nodes);
398 return nodes;
399 }
400
406 template<class N>
407 static void searchNodeWithId(N& node, int id, std::vector<N*>& nodes)
408 {
409 for (size_t i = 0; i < node.getNumberOfSons(); ++i)
410 {
411 searchNodeWithId<N>(*dynamic_cast<N*>(node.getSon(i)), id, nodes);
412 }
413 if (node.getId() == id) nodes.push_back(&node);
414 }
415
421 static Node* searchFirstNodeWithId(Node& node, int id)
422 {
423 if (node.getId() == id)
424 return &node;
425 else
426 {
427 for (size_t i = 0; i < node.getNumberOfSons(); ++i)
428 {
429 Node* result = searchFirstNodeWithId(*node.getSon(i), id);
430 if (result)
431 return result;
432 }
433 }
434 return 0;
435 }
436
442 static const Node* searchFirstNodeWithId(const Node& node, int id)
443 {
444 if (node.getId() == id)
445 return &node;
446 else
447 {
448 for (size_t i = 0; i < node.getNumberOfSons(); ++i)
449 {
450 const Node* result = searchFirstNodeWithId(*node.getSon(i), id);
451 if (result)
452 return result;
453 }
454 }
455 return 0;
456 }
457
463 template<class N>
464 static bool hasNodeWithId(const N& node, int id)
465 {
466 if (node.getId() == id) return true;
467 else
468 {
469 for (size_t i = 0; i < node.getNumberOfSons(); i++)
470 {
471 if (hasNodeWithId(*node.getSon(i), id)) return true;
472 }
473 return false;
474 }
475 }
476
482 template<class N>
483 static std::vector<N*> searchNodeWithName(N& node, const std::string& name)
484 {
485 std::vector<N*> nodes;
486 searchNodeWithId<N>(node, name, nodes);
487 return nodes;
488 }
489
495 template<class N>
496 static void searchNodeWithName(N& node, const std::string& name, std::vector<N*>& nodes)
497 {
498 for (size_t i = 0; i < node.getNumberOfSons(); i++)
499 {
500 searchNodeWithName<N>(*dynamic_cast<N*>(node.getSon(i)), name, nodes);
501 }
502 if (node.hasName() && node.getName() == name) nodes.push_back(&node);
503 }
504
510 template<class N>
511 static bool hasNodeWithName(const N& node, const std::string& name)
512 {
513 if (node.hasName() & node.getName() == name) return true;
514 else
515 {
516 for (size_t i = 0; i < node.getNumberOfSons(); i++)
517 {
518 if (hasNodeWithName(*node.getSon(i), name)) return true;
519 }
520 return false;
521 }
522 }
523
531 static bool isRoot(const Node& node) { return !node.hasFather(); }
532
539 static size_t getNumberOfBranches(const Node& node)
540 {
541 size_t nbBranches = 0; // Basal node has no branch
542 for (int i = 0; i < static_cast<int>(node.getNumberOfSons()); i++)
543 {
544 nbBranches += getNumberOfNodes(*node[i]); // All son nodes define one branch each
545 }
546 return nbBranches;
547 }
548
555 static size_t getNumberOfLeaves(const Node& node);
556
563 static size_t getNumberOfNodes(const Node& node)
564 {
565 size_t nbNodes = 1;
566 for (int i = 0; i < static_cast<int>(node.getNumberOfSons()); i++)
567 {
568 nbNodes += getNumberOfNodes(*node[i]);
569 }
570 return nbNodes;
571 }
572
573
580 static std::vector<std::string> getLeavesNames(const Node& node);
581
601 static unsigned int getDepth(const Node& node);
602
623 static unsigned int getDepths(const Node& node, std::map<const Node*, unsigned int>& depths);
624
636 static double getHeight(const Node& node);
637
649 static double getHeights(const Node& node, std::map<const Node*, double>& heights);
650
658 static bool isMultifurcating(const Node& node);
659
671 static bool haveSameOrderedTopology(const Node& n1, const Node& n2);
672
673 static std::vector<Node*> getPathBetweenAnyTwoNodes(Node& node1, Node& node2, bool includeAncestor = true, bool includeAncestorAtEndOfPath = true);
674
675 static std::vector<const Node*> getPathBetweenAnyTwoNodes(const Node& node1, const Node& node2, bool includeAncestor = true, bool includeAncestorAtEndOfPath = true);
676
686 template<class N>
687 static N* cloneSubtree(const Node& node)
688 {
689 // First we copy this node using default copy constructor:
690 N* clone = new N(node);
691 // We remove the link toward the father:
692 // clone->removeFather();
693
694 // Now we perform a hard copy:
695 for (int i = 0; i < static_cast<int>(node.getNumberOfSons()); i++)
696 {
697 clone->addSon(cloneSubtree<N>(*node[i]));
698 }
699 return clone;
700 }
701
707 template<class N>
708 static void deleteSubtree(N* node)
709 {
710 for (size_t i = 0; i < node->getNumberOfSons(); ++i)
711 {
712 N* son = dynamic_cast<N*>(node->getSon(i));
713 deleteSubtree(son);
714 delete son;
715 }
716 }
717
718
719 template<class N>
720 static N* cloneSubtree(const Tree& tree, int nodeId)
721 {
722 // First we copy this node using default copy constructor:
723 N* clone = tree.hasNodeName(nodeId) ? new N(nodeId, tree.getNodeName(nodeId)) : new N(nodeId);
724 // Then we set the length:
725 if (tree.hasDistanceToFather(nodeId))
726 clone->setDistanceToFather(tree.getDistanceToFather(nodeId));
727 // Now we copy all sons:
728 std::vector<int> sonsId = tree.getSonsId(nodeId);
729 for (size_t i = 0; i < sonsId.size(); i++)
730 {
731 clone->addSon(cloneSubtree<N>(tree, sonsId[i]));
732 }
733 // Must copy all properties too:
734 std::vector<std::string> names;
735 names = tree.getNodePropertyNames(nodeId);
736 for (size_t i = 0; i < names.size(); i++)
737 {
738 clone->setNodeProperty(names[i], *tree.getNodeProperty(nodeId, names[i]));
739 }
740 names = tree.getBranchPropertyNames(nodeId);
741 for (size_t i = 0; i < names.size(); i++)
742 {
743 clone->setBranchProperty(names[i], *tree.getBranchProperty(nodeId, names[i]));
744 }
745
746 return clone;
747 }
763 static Vdouble getBranchLengths(const Node& node);
764
774 static double getTotalLength(const Node& node, bool includeAncestor = true);
775
782 static void setBranchLengths(Node& node, double brLen);
783
789 static void deleteBranchLengths(Node& node);
790
797 static void setVoidBranchLengths(Node& node, double brLen);
798
808 static void scaleTree(Node& node, double factor);
809
819 static double getDistanceBetweenAnyTwoNodes(const Node& node1, const Node& node2);
820
839 static std::unique_ptr<DistanceMatrix> getDistanceMatrix(const TreeTemplate<Node>& tree);
840
841private:
853 static void processDistsInSubtree_(const Node* node, DistanceMatrix& matrix, std::vector< std::pair<std::string, double>>& distsToNodeFather);
854
855public:
868 struct Element
869 {
870public:
871 std::string content;
872 std::string length;
873 std::string bootstrap;
874 bool isLeaf;
875
876public:
878 length(),
879 bootstrap(),
880 isLeaf(false) {}
881 };
882
883 static Element getElement(const std::string& elt);
884
898 static Node* parenthesisToNode(const std::string& description, unsigned int& nodeCounter, bool bootstrap = true, const std::string& propertyName = TreeTools::BOOTSTRAP, bool withId = false, bool verbose = true);
899
913 static std::unique_ptr<TreeTemplate<Node>> parenthesisToTree(const std::string& description, bool bootstrap = true, const std::string& propertyName = TreeTools::BOOTSTRAP, bool withId = false, bool verbose = true);
914
924 static std::string nodeToParenthesis(const Node& node, bool writeId = false);
925
938 static std::string nodeToParenthesis(const Node& node, bool bootstrap, const std::string& propertyName);
939
949 static std::string treeToParenthesis(const TreeTemplate<Node>& tree, bool writeId = false);
950
963 static std::string treeToParenthesis(const TreeTemplate<Node>& tree, bool bootstrap, const std::string& propertyName);
964
973 static std::unique_ptr<TreeTemplate<Node>> buildFromPhyloTree(const PhyloTree& treetemp);
974
982 static std::unique_ptr<TreeTemplate<Node>> getRandomTree(std::vector<std::string>& leavesNames, bool rooted = true);
983
997 static std::vector<const Node*> getRemainingNeighbors(const Node* node1, const Node* node2, const Node* node3);
998
1005 static void incrementAllIds(Node* node, int increment);
1006
1019 static void getNodePropertyNames(const Node& node, std::vector<std::string>& propertyNames);
1020
1031 static void getNodeProperties(const Node& node, const std::string& propertyName, std::map<int, const Clonable*>& properties);
1032
1043 static void getNodeProperties(Node& node, const std::string& propertyName, std::map<int, Clonable*>& properties);
1044
1051 static void deleteNodeProperties(Node& node, const std::vector<std::string>& propertyNames);
1052
1059 static void getBranchPropertyNames(const Node& node, std::vector<std::string>& propertyNames);
1060
1071 static void getBranchProperties(const Node& node, const std::string& propertyName, std::map<int, const Clonable*>& properties);
1072
1083 static void getBranchProperties(Node& node, const std::string& propertyName, std::map<int, Clonable*>& properties);
1084
1091 static void deleteBranchProperties(Node& node, const std::vector<std::string>& propertyNames);
1092
1103 static void orderTree(Node& node, bool downward = true, bool orderLeaves = false)
1104 {
1105 orderTree_(node, downward, orderLeaves);
1106 }
1107
1149 static void midRoot(TreeTemplate<Node>& tree, short criterion, bool forceBranchRoot);
1150
1156 static double getRadius(TreeTemplate<Node>& tree);
1157
1158
1173 static void unresolveUncertainNodes(Node& subtree, double threshold, const std::string& property = TreeTools::BOOTSTRAP);
1174
1175private:
1177 {
1178 size_t size;
1179 std::string firstLeaf;
1181 firstLeaf("") {}
1182 };
1183
1184 static OrderTreeData_ orderTree_(Node& node, bool downward, bool orderLeaves);
1185
1197 {
1198 double sum;
1201 };
1202
1210 static Moments_ getSubtreeMoments_(const Node* node);
1211
1226 static void getBestRootInSubtree_(bpp::TreeTemplate<bpp::Node>& tree, short criterion, bpp::Node* node, std::pair<bpp::Node*, std::map<std::string, double>>& bestRoot);
1227
1228public:
1229 static const short MIDROOT_VARIANCE;
1230 static const short MIDROOT_SUM_OF_SQUARES;
1231};
1232} // end of namespace bpp.
1233#endif // BPP_PHYL_TREE_TREETEMPLATETOOLS_H
Exception thrown when something is wrong with a particular node.
The phylogenetic node class.
Definition: Node.h:59
virtual Node * removeSon(size_t pos)
Definition: Node.h:423
virtual int getId() const
Get the node's id.
Definition: Node.h:170
virtual bool hasDistanceToFather() const
Tell is this node has a distance to the father.
Definition: Node.h:288
virtual const Node * getFather() const
Get the father of this node is there is one.
Definition: Node.h:306
virtual const Node * getSon(size_t pos) const
Definition: Node.h:362
virtual bool isLeaf() const
Definition: Node.h:679
virtual bool hasFather() const
Tell if this node has a father node.
Definition: Node.h:346
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
Definition: Node.h:250
virtual size_t getNumberOfSons() const
Definition: Node.h:355
static intType giveIntRandomNumberBetweenZeroAndEntry(intType entry)
Utilitary methods working with TreeTemplate and Node objects.
static void searchNodeWithId(N &node, int id, std::vector< N * > &nodes)
static const Node * searchFirstNodeWithId(const Node &node, int id)
static double getDistanceBetweenAnyTwoNodes(const Node &node1, const Node &node2)
Get the total distance between to nodes.
static size_t getNumberOfNodes(const Node &node)
Get the number of nodes of a subtree defined by a particular node.
static void setBranchLengths(Node &node, double brLen)
Set all the branch lengths of a subtree.
static void midRoot(TreeTemplate< Node > &tree, short criterion, bool forceBranchRoot)
Midroot the tree by minimizing a given criterion ("variance" or "sum of squares")
static bool hasNodeWithId(const N &node, int id)
static std::vector< N * > searchNodeWithName(N &node, const std::string &name)
static void deleteSubtree(N *node)
Recursively delete a subtree structure.
static std::vector< N * > getInnerNodes(N &node)
Retrieve all inner nodes from a subtree.
static void getNodeProperties(const Node &node, const std::string &propertyName, std::map< int, const Clonable * > &properties)
Retrieve all node property objects with a given name over a (sub) tree (const version).
static void searchLeaf(const Node &node, const std::string &name, int *&id)
Get the id of a leaf given its name in a subtree.
static void incrementAllIds(Node *node, int increment)
This method will add a given value (possibly negative) to all identifiers in a (sub)tree.
static bool hasNodeWithName(const N &node, const std::string &name)
static void getInnerNodesId(const Node &node, std::vector< int > &ids)
Retrieve all inner nodes ids from a subtree.
static std::unique_ptr< TreeTemplate< Node > > getRandomTree(std::vector< std::string > &leavesNames, bool rooted=true)
Draw a random tree from a list of taxa, using a Yule process.
static size_t getNumberOfBranches(const Node &node)
Get the number of branches of a subtree defined by a particular node.
static void orderTree(Node &node, bool downward=true, bool orderLeaves=false)
Swap nodes in the subtree so that they are ordered according to the underlying number of leaves.
static void unresolveUncertainNodes(Node &subtree, double threshold, const std::string &property=TreeTools::BOOTSTRAP)
Unresolve nodes with low confidence value.
static void getNodesId(const Node &node, std::vector< int > &ids)
Retrieve all nodes ids from a subtree.
static void getBestRootInSubtree_(bpp::TreeTemplate< bpp::Node > &tree, short criterion, bpp::Node *node, std::pair< bpp::Node *, std::map< std::string, double > > &bestRoot)
Find, in the branches of a subtree, the root that minimizes a criterion over the tree.
static std::vector< std::string > getLeavesNames(const Node &node)
Get the leaves names of a subtree defined by a particular node.
static std::unique_ptr< TreeTemplate< Node > > buildFromPhyloTree(const PhyloTree &treetemp)
static std::vector< int > getLeavesId(const Node &node)
Retrieve all leaves ids from a subtree.
static void getBranchProperties(const Node &node, const std::string &propertyName, std::map< int, const Clonable * > &properties)
Retrieve all branch property objects with a given name over a (sub) tree (const version).
static unsigned int getDepth(const Node &node)
Get the depth of the subtree defined by node 'node', i.e. the maximum number of sons 'generations'.
static std::unique_ptr< TreeTemplate< Node > > parenthesisToTree(const std::string &description, bool bootstrap=true, const std::string &propertyName=TreeTools::BOOTSTRAP, bool withId=false, bool verbose=true)
Parse a string in the parenthesis format and convert it to a tree.
static void getLeaves(N &node, std::vector< N * > &leaves)
Retrieve all leaves from a subtree.
static void dropLeaf(TreeTemplate< N > &tree, const std::string &leafName)
Remove a leaf node and its parent node, while correcting for branch lengths.
static double getHeight(const Node &node)
Get the height of the subtree defined by node 'node', i.e. the maximum distance between leaves and th...
static Vdouble getBranchLengths(const Node &node)
Get all the branch lengths of a subtree.
static size_t getNumberOfLeaves(const Node &node)
Get the number of leaves of a subtree defined by a particular node.
static unsigned int getDepths(const Node &node, std::map< const Node *, unsigned int > &depths)
Get the depths for all nodes of the subtree defined by node 'node', i.e. the maximum number of sons '...
static void scaleTree(Node &node, double factor)
Scale a given tree.
static void getNodes(N &node, std::vector< N * > &nodes)
Retrieve all son nodes from a subtree.
static std::vector< Node * > getPathBetweenAnyTwoNodes(Node &node1, Node &node2, bool includeAncestor=true, bool includeAncestorAtEndOfPath=true)
static std::vector< const Node * > getRemainingNeighbors(const Node *node1, const Node *node2, const Node *node3)
Get a subset of node neighbors.
static std::vector< N * > getNodes(N &node)
Retrieve all son nodes from a subtree.
static Element getElement(const std::string &elt)
static const short MIDROOT_VARIANCE
static void deleteBranchLengths(Node &node)
Remove all the branch lengths of a subtree.
static N * cloneSubtree(const Tree &tree, int nodeId)
static std::unique_ptr< DistanceMatrix > getDistanceMatrix(const TreeTemplate< Node > &tree)
Compute a distance matrix from a tree.
static double getHeights(const Node &node, std::map< const Node *, double > &heights)
Get the heights of all nodes within a subtree defined by node 'node', i.e. the maximum distance betwe...
static void sampleSubtree(TreeTemplate< N > &tree, const std::vector< std::string > &leaves, size_t size)
Sample a subtree by removing leaves randomly.
static const short MIDROOT_SUM_OF_SQUARES
static void setVoidBranchLengths(Node &node, double brLen)
Give a length to branches that don't have one in a subtree.
static std::string treeToParenthesis(const TreeTemplate< Node > &tree, bool writeId=false)
Get the parenthesis description of a tree.
static void getInnerNodes(N &node, std::vector< N * > &nodes)
Retrieve all inner nodes from a subtree.
static std::vector< int > getNodesId(const Node &node)
Retrieve all nodes ids from a subtree.
static double getTotalLength(const Node &node, bool includeAncestor=true)
Get the total length (sum of all branch lengths) of a subtree.
static bool isRoot(const Node &node)
Tell if a particular node is the root of a tree i.e. if it has a father node.
static std::string nodeToParenthesis(const Node &node, bool writeId=false)
Get the parenthesis description of a subtree.
static OrderTreeData_ orderTree_(Node &node, bool downward, bool orderLeaves)
static N * cloneSubtree(const Node &node)
Recursively clone a subtree structure.
static std::vector< int > getAncestorsId(const Node &node)
Retrieve all nodes ids that are ancestors of a node.
static void getBranchPropertyNames(const Node &node, std::vector< std::string > &propertyNames)
Retrieve the names of all available branch properties in the tree.
static void deleteNodeProperties(Node &node, const std::vector< std::string > &propertyNames)
Delete the given properties in the tree.
static void processDistsInSubtree_(const Node *node, DistanceMatrix &matrix, std::vector< std::pair< std::string, double > > &distsToNodeFather)
Inner function used by getDistanceMatrix.
static void searchNodeWithName(N &node, const std::string &name, std::vector< N * > &nodes)
static Node * parenthesisToNode(const std::string &description, unsigned int &nodeCounter, bool bootstrap=true, const std::string &propertyName=TreeTools::BOOTSTRAP, bool withId=false, bool verbose=true)
Parse a string in the parenthesis format and convert it to a subtree.
static std::vector< N * > searchNodeWithId(N &node, int id)
static Node * searchFirstNodeWithId(Node &node, int id)
static bool isMultifurcating(const Node &node)
Tell is a subtree is multifurcating.
static std::vector< int > getInnerNodesId(const Node &node)
Retrieve all inner nodes ids from a subtree.
static bool haveSameOrderedTopology(const Node &n1, const Node &n2)
Tells if two subtrees have the same topology.
static double getRadius(TreeTemplate< Node > &tree)
Get the characteristic radius of a tree (average distance to the root minimizing the sum of squared d...
static std::vector< N * > getLeaves(N &node)
Retrieve all leaves from a subtree.
static void getNodePropertyNames(const Node &node, std::vector< std::string > &propertyNames)
Retrieve the names of all available node properties in the tree.
static void dropSubtree(TreeTemplate< N > &tree, Node *subtree)
Remove a subtree defined by its root node and its parent node, while correcting for branch lengths.
static Moments_ getSubtreeMoments_(const Node *node)
Computes the moment of a subtree.
static void deleteBranchProperties(Node &node, const std::vector< std::string > &propertyNames)
Delete the given properties in the tree.
static int getLeafId(const Node &node, const std::string &name)
Get the id of a leaf given its name in a subtree.
The phylogenetic tree class.
Definition: TreeTemplate.h:59
virtual void setRootNode(N *root)
Definition: TreeTemplate.h:387
virtual N * getNode(int id, bool checkId=false)
Definition: TreeTemplate.h:429
static const std::string BOOTSTRAP
Bootstrap tag.
Definition: TreeTools.h:684
Interface for phylogenetic tree objects.
Definition: Tree.h:115
virtual std::vector< std::string > getBranchPropertyNames(int nodeId) const =0
virtual std::vector< std::string > getNodePropertyNames(int nodeId) const =0
virtual std::string getNodeName(int nodeId) const =0
virtual std::vector< int > getSonsId(int parentId) const =0
virtual Clonable * getBranchProperty(int nodeId, const std::string &name)=0
virtual bool hasDistanceToFather(int nodeId) const =0
virtual Clonable * getNodeProperty(int nodeId, const std::string &name)=0
virtual bool hasNodeName(int nodeId) const =0
virtual double getDistanceToFather(int nodeId) const =0
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
A structure recording, for a subtree, the sum of root-leaf distances, the sum of their squares,...