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 
18 namespace bpp
19 {
20 template<class N> class TreeTemplate;
21 
22 class PhyloTree;
23 
30 {
31 public:
33  virtual ~TreeTemplateTools() {}
34 
35 public:
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 
143  template<class N>
144  static void dropLeaf(TreeTemplate<N>& tree, const std::string& leafName)
145  {
146  N* leaf = tree.getNode(leafName);
147  if (!leaf->hasFather())
148  throw Exception("TreeTemplateTools::dropLeaf(). Leaf is the only node in the tree, can't remove it.");
149  N* parent = leaf->getFather();
150  if (parent->getNumberOfSons() > 2)
151  {
152  // The easy case:
153  parent->removeSon(leaf);
154  delete leaf;
155  }
156  else if (parent->getNumberOfSons() == 2)
157  {
158  // We have to delete the parent node as well:
159  N* brother = parent->getSon(0);
160  if (brother == leaf) brother = parent->getSon(1);
161  if (!parent->hasFather())
162  {
163  // The brother becomes the root:
164  if (leaf->hasDistanceToFather() && brother->hasDistanceToFather())
165  {
166  brother->setDistanceToFather(brother->getDistanceToFather() + leaf->getDistanceToFather());
167  }
168  brother->removeFather();
169  tree.setRootNode(brother);
170  delete parent;
171  delete leaf;
172  }
173  else
174  {
175  N* gParent = parent->getFather();
176  if (brother->hasDistanceToFather() && parent->hasDistanceToFather())
177  {
178  brother->setDistanceToFather(brother->getDistanceToFather() + parent->getDistanceToFather());
179  }
180  size_t pos = gParent->getSonPosition(parent);
181  gParent->setSon(pos, brother);
182  delete parent;
183  delete leaf;
184  }
185  }
186  else
187  {
188  // Dunno what to do in that case :(
189  throw Exception("TreeTemplateTools::dropLeaf. Parent node as only one child, I don't know what to do in that case :(");
190  }
191  }
192 
200  template<class N>
201  static void dropSubtree(TreeTemplate<N>& tree, Node* subtree)
202  {
203  if (!subtree->hasFather())
204  throw Exception("TreeTemplateTools::dropSubtree(). Trying to remove the full tree!");
205  N* parent = subtree->getFather();
206  if (parent->getNumberOfSons() > 2)
207  {
208  // The easy case:
209  parent->removeSon(subtree);
210  deleteSubtree(subtree);
211  }
212  else if (parent->getNumberOfSons() == 2)
213  {
214  // We have to delete the parent node as well:
215  N* brother = parent->getSon(0);
216  if (brother == subtree) brother = parent->getSon(1);
217  if (!parent->hasFather())
218  {
219  // The brother becomes the root:
220  if (subtree->hasDistanceToFather() && brother->hasDistanceToFather())
221  {
222  brother->setDistanceToFather(brother->getDistanceToFather() + subtree->getDistanceToFather());
223  }
224  tree.setRootNode(brother);
225  delete parent;
226  deleteSubtree(subtree);
227  }
228  else
229  {
230  N* gParent = parent->getFather();
231  if (brother->hasDistanceToFather() && parent->hasDistanceToFather())
232  {
233  brother->setDistanceToFather(brother->getDistanceToFather() + parent->getDistanceToFather());
234  }
235  size_t pos = gParent->getSonPosition(parent);
236  gParent->setSon(pos, brother);
237  delete parent;
238  deleteSubtree(subtree);
239  }
240  }
241  else
242  {
243  // Dunno what to do in that case :(
244  throw Exception("TreeTemplateTools::dropSubtree. Parent node as only one child, I don't know what to do in that case :(");
245  }
246  }
247 
255  template<class N>
256  static void sampleSubtree(TreeTemplate<N>& tree, const std::vector<std::string>& leaves, size_t size)
257  {
258  std::vector<std::string> names = leaves;
259  for (size_t n = names.size(); n > size; --n)
260  {
262  dropLeaf(tree, names[i]);
263  names.erase(names.begin() + static_cast<ptrdiff_t>(i));
264  }
265  }
266 
273  template<class N>
274  static std::vector<N*> getNodes(N& node)
275  {
276  std::vector<N*> nodes;
277  getNodes<N>(node, nodes);
278  return nodes;
279  }
280 
287  template<class N>
288  static void getNodes(N& node, std::vector<N*>& nodes)
289  {
290  for (size_t i = 0; i < node.getNumberOfSons(); i++)
291  {
292  getNodes<N>(*dynamic_cast<N*>(node.getSon(i)), nodes);
293  }
294  nodes.push_back(&node);
295  }
296 
303  static std::vector<int> getNodesId(const Node& node)
304  {
305  std::vector<int> ids;
306  getNodesId(node, ids);
307  return ids;
308  }
309 
316  static void getNodesId(const Node& node, std::vector<int>& ids)
317  {
318  for (size_t i = 0; i < node.getNumberOfSons(); i++)
319  {
320  getNodesId(*node.getSon(i), ids);
321  }
322  ids.push_back(node.getId());
323  }
324 
331  template<class N>
332  static std::vector<N*> getInnerNodes(N& node)
333  {
334  std::vector<N*> nodes;
335  getInnerNodes<N>(node, nodes);
336  return nodes;
337  }
338 
347  template<class N>
348  static void getInnerNodes(N& node, std::vector<N*>& nodes)
349  {
350  for (size_t i = 0; i < node.getNumberOfSons(); i++)
351  {
352  getInnerNodes<N>(*dynamic_cast<N*>(node.getSon(i)), nodes);
353  }
354  if (!node.isLeaf())
355  nodes.push_back(&node); // Do not add leaves!
356  }
357 
366  static std::vector<int> getInnerNodesId(const Node& node)
367  {
368  std::vector<int> ids;
369  getInnerNodesId(node, ids);
370  return ids;
371  }
372 
379  static void getInnerNodesId(const Node& node, std::vector<int>& ids)
380  {
381  for (size_t i = 0; i < node.getNumberOfSons(); i++)
382  {
383  getInnerNodesId(*node.getSon(i), ids);
384  }
385  if (!node.isLeaf())
386  ids.push_back(node.getId()); // Do not add leaves!
387  }
388 
394  template<class N>
395  static std::vector<N*> searchNodeWithId(N& node, int id)
396  {
397  std::vector<N*> nodes;
398  searchNodeWithId<N>(node, id, nodes);
399  return nodes;
400  }
401 
407  template<class N>
408  static void searchNodeWithId(N& node, int id, std::vector<N*>& nodes)
409  {
410  for (size_t i = 0; i < node.getNumberOfSons(); ++i)
411  {
412  searchNodeWithId<N>(*dynamic_cast<N*>(node.getSon(i)), id, nodes);
413  }
414  if (node.getId() == id) nodes.push_back(&node);
415  }
416 
422  static Node* searchFirstNodeWithId(Node& node, int id)
423  {
424  if (node.getId() == id)
425  return &node;
426  else
427  {
428  for (size_t i = 0; i < node.getNumberOfSons(); ++i)
429  {
430  Node* result = searchFirstNodeWithId(*node.getSon(i), id);
431  if (result)
432  return result;
433  }
434  }
435  return 0;
436  }
437 
443  static const Node* searchFirstNodeWithId(const Node& node, int id)
444  {
445  if (node.getId() == id)
446  return &node;
447  else
448  {
449  for (size_t i = 0; i < node.getNumberOfSons(); ++i)
450  {
451  const Node* result = searchFirstNodeWithId(*node.getSon(i), id);
452  if (result)
453  return result;
454  }
455  }
456  return 0;
457  }
458 
464  template<class N>
465  static bool hasNodeWithId(const N& node, int id)
466  {
467  if (node.getId() == id) return true;
468  else
469  {
470  for (size_t i = 0; i < node.getNumberOfSons(); i++)
471  {
472  if (hasNodeWithId(*node.getSon(i), id)) return true;
473  }
474  return false;
475  }
476  }
477 
483  template<class N>
484  static std::vector<N*> searchNodeWithName(N& node, const std::string& name)
485  {
486  std::vector<N*> nodes;
487  searchNodeWithId<N>(node, name, nodes);
488  return nodes;
489  }
490 
496  template<class N>
497  static void searchNodeWithName(N& node, const std::string& name, std::vector<N*>& nodes)
498  {
499  for (size_t i = 0; i < node.getNumberOfSons(); i++)
500  {
501  searchNodeWithName<N>(*dynamic_cast<N*>(node.getSon(i)), name, nodes);
502  }
503  if (node.hasName() && node.getName() == name) nodes.push_back(&node);
504  }
505 
511  template<class N>
512  static bool hasNodeWithName(const N& node, const std::string& name)
513  {
514  if (node.hasName() & node.getName() == name) return true;
515  else
516  {
517  for (size_t i = 0; i < node.getNumberOfSons(); i++)
518  {
519  if (hasNodeWithName(*node.getSon(i), name)) return true;
520  }
521  return false;
522  }
523  }
524 
532  static bool isRoot(const Node& node) { return !node.hasFather(); }
533 
540  static size_t getNumberOfBranches(const Node& node)
541  {
542  size_t nbBranches = 0; // Basal node has no branch
543  for (int i = 0; i < static_cast<int>(node.getNumberOfSons()); i++)
544  {
545  nbBranches += getNumberOfNodes(*node[i]); // All son nodes define one branch each
546  }
547  return nbBranches;
548  }
549 
556  static size_t getNumberOfLeaves(const Node& node);
557 
564  static size_t getNumberOfNodes(const Node& node)
565  {
566  size_t nbNodes = 1;
567  for (int i = 0; i < static_cast<int>(node.getNumberOfSons()); i++)
568  {
569  nbNodes += getNumberOfNodes(*node[i]);
570  }
571  return nbNodes;
572  }
573 
574 
581  static std::vector<std::string> getLeavesNames(const Node& node);
582 
602  static unsigned int getDepth(const Node& node);
603 
624  static unsigned int getDepths(const Node& node, std::map<const Node*, unsigned int>& depths);
625 
637  static double getHeight(const Node& node);
638 
650  static double getHeights(const Node& node, std::map<const Node*, double>& heights);
651 
659  static bool isMultifurcating(const Node& node);
660 
672  static bool haveSameOrderedTopology(const Node& n1, const Node& n2);
673 
674  static std::vector<Node*> getPathBetweenAnyTwoNodes(Node& node1, Node& node2, bool includeAncestor = true, bool includeAncestorAtEndOfPath = true);
675 
676  static std::vector<const Node*> getPathBetweenAnyTwoNodes(const Node& node1, const Node& node2, bool includeAncestor = true, bool includeAncestorAtEndOfPath = true);
677 
687  template<class N>
688  static N* cloneSubtree(const Node& node)
689  {
690  // First we copy this node using default copy constructor:
691  N* clone = new N(node);
692  // We remove the link toward the father:
693  // clone->removeFather();
694 
695  // Now we perform a hard copy:
696  for (int i = 0; i < static_cast<int>(node.getNumberOfSons()); i++)
697  {
698  clone->addSon(cloneSubtree<N>(*node[i]));
699  }
700  return clone;
701  }
702 
708  template<class N>
709  static void deleteSubtree(N* node)
710  {
711  for (size_t i = 0; i < node->getNumberOfSons(); ++i)
712  {
713  N* son = dynamic_cast<N*>(node->getSon(i));
714  deleteSubtree(son);
715  delete son;
716  }
717  }
718 
719 
720  template<class N>
721  static N* cloneSubtree(const Tree& tree, int nodeId)
722  {
723  // First we copy this node using default copy constructor:
724  N* clone = tree.hasNodeName(nodeId) ? new N(nodeId, tree.getNodeName(nodeId)) : new N(nodeId);
725  // Then we set the length:
726  if (tree.hasDistanceToFather(nodeId))
727  clone->setDistanceToFather(tree.getDistanceToFather(nodeId));
728  // Now we copy all sons:
729  std::vector<int> sonsId = tree.getSonsId(nodeId);
730  for (size_t i = 0; i < sonsId.size(); i++)
731  {
732  clone->addSon(cloneSubtree<N>(tree, sonsId[i]));
733  }
734  // Must copy all properties too:
735  std::vector<std::string> names;
736  names = tree.getNodePropertyNames(nodeId);
737  for (size_t i = 0; i < names.size(); i++)
738  {
739  clone->setNodeProperty(names[i], *tree.getNodeProperty(nodeId, names[i]));
740  }
741  names = tree.getBranchPropertyNames(nodeId);
742  for (size_t i = 0; i < names.size(); i++)
743  {
744  clone->setBranchProperty(names[i], *tree.getBranchProperty(nodeId, names[i]));
745  }
746 
747  return clone;
748  }
764  static Vdouble getBranchLengths(const Node& node);
765 
775  static double getTotalLength(const Node& node, bool includeAncestor = true);
776 
783  static void setBranchLengths(Node& node, double brLen);
784 
790  static void deleteBranchLengths(Node& node);
791 
798  static void setVoidBranchLengths(Node& node, double brLen);
799 
809  static void scaleTree(Node& node, double factor);
810 
820  static double getDistanceBetweenAnyTwoNodes(const Node& node1, const Node& node2);
821 
840  static std::unique_ptr<DistanceMatrix> getDistanceMatrix(const TreeTemplate<Node>& tree);
841 
842 private:
854  static void processDistsInSubtree_(const Node* node, DistanceMatrix& matrix, std::vector< std::pair<std::string, double>>& distsToNodeFather);
855 
856 public:
869  struct Element
870  {
871 public:
872  std::string content;
873  std::string length;
874  std::string bootstrap;
875  bool isLeaf;
876 
877 public:
879  length(),
880  bootstrap(),
881  isLeaf(false) {}
882  };
883 
884  static Element getElement(const std::string& elt);
885 
899  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);
900 
914  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);
915 
925  static std::string nodeToParenthesis(const Node& node, bool writeId = false);
926 
939  static std::string nodeToParenthesis(const Node& node, bool bootstrap, const std::string& propertyName);
940 
950  static std::string treeToParenthesis(const TreeTemplate<Node>& tree, bool writeId = false);
951 
964  static std::string treeToParenthesis(const TreeTemplate<Node>& tree, bool bootstrap, const std::string& propertyName);
965 
974  static std::unique_ptr<TreeTemplate<Node>> buildFromPhyloTree(const PhyloTree& treetemp);
975 
983  static std::unique_ptr<TreeTemplate<Node>> getRandomTree(std::vector<std::string>& leavesNames, bool rooted = true);
984 
998  static std::vector<const Node*> getRemainingNeighbors(const Node* node1, const Node* node2, const Node* node3);
999 
1006  static void incrementAllIds(Node* node, int increment);
1007 
1020  static void getNodePropertyNames(const Node& node, std::vector<std::string>& propertyNames);
1021 
1032  static void getNodeProperties(const Node& node, const std::string& propertyName, std::map<int, const Clonable*>& properties);
1033 
1044  static void getNodeProperties(Node& node, const std::string& propertyName, std::map<int, Clonable*>& properties);
1045 
1052  static void deleteNodeProperties(Node& node, const std::vector<std::string>& propertyNames);
1053 
1060  static void getBranchPropertyNames(const Node& node, std::vector<std::string>& propertyNames);
1061 
1072  static void getBranchProperties(const Node& node, const std::string& propertyName, std::map<int, const Clonable*>& properties);
1073 
1084  static void getBranchProperties(Node& node, const std::string& propertyName, std::map<int, Clonable*>& properties);
1085 
1092  static void deleteBranchProperties(Node& node, const std::vector<std::string>& propertyNames);
1093 
1104  static void orderTree(Node& node, bool downward = true, bool orderLeaves = false)
1105  {
1106  orderTree_(node, downward, orderLeaves);
1107  }
1108 
1150  static void midRoot(TreeTemplate<Node>& tree, short criterion, bool forceBranchRoot);
1151 
1157  static double getRadius(TreeTemplate<Node>& tree);
1158 
1159 
1174  static void unresolveUncertainNodes(Node& subtree, double threshold, const std::string& property = TreeTools::BOOTSTRAP);
1175 
1176 private:
1178  {
1179  size_t size;
1180  std::string firstLeaf;
1182  firstLeaf("") {}
1183  };
1184 
1185  static OrderTreeData_ orderTree_(Node& node, bool downward, bool orderLeaves);
1186 
1197  struct Moments_
1198  {
1199  double sum;
1200  double squaresSum;
1202  };
1203 
1211  static Moments_ getSubtreeMoments_(const Node* node);
1212 
1227  static void getBestRootInSubtree_(bpp::TreeTemplate<bpp::Node>& tree, short criterion, bpp::Node* node, std::pair<bpp::Node*, std::map<std::string, double>>& bestRoot);
1228 
1229 public:
1230  static const short MIDROOT_VARIANCE;
1231  static const short MIDROOT_SUM_OF_SQUARES;
1232 };
1233 } // end of namespace bpp.
1234 #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:411
virtual int getId() const
Get the node's id.
Definition: Node.h:170
virtual const Node * getSon(size_t pos) const
Definition: Node.h:362
virtual const Node * getFather() const
Get the father of this node is there is one.
Definition: Node.h:306
virtual bool hasDistanceToFather() const
Tell is this node has a distance to the father.
Definition: Node.h:288
virtual bool isLeaf() const
Definition: Node.h:667
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 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 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 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 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 N * cloneSubtree(const Tree &tree, int nodeId)
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 std::vector< N * > getNodes(N &node)
Retrieve all son nodes from a subtree.
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 void processDistsInSubtree_(const Node *node, DistanceMatrix &matrix, std::vector< std::pair< std::string, double >> &distsToNodeFather)
Inner function used by getDistanceMatrix.
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 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 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 N * cloneSubtree(const Node &node)
Recursively clone a subtree structure.
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 Element getElement(const std::string &elt)
static Node * searchFirstNodeWithId(Node &node, int id)
static const short MIDROOT_VARIANCE
static void deleteBranchLengths(Node &node)
Remove all the branch lengths of a subtree.
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 std::vector< N * > searchNodeWithName(N &node, const std::string &name)
static void getInnerNodes(N &node, std::vector< N * > &nodes)
Retrieve all inner nodes 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 std::vector< int > getInnerNodesId(const Node &node)
Retrieve all inner nodes ids from a subtree.
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 std::vector< int > getNodesId(const Node &node)
Retrieve all nodes ids from a subtree.
static void deleteNodeProperties(Node &node, const std::vector< std::string > &propertyNames)
Delete the given properties in the tree.
static std::vector< int > getLeavesId(const Node &node)
Retrieve all leaves ids from a subtree.
static const Node * searchFirstNodeWithId(const Node &node, int id)
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 * > getLeaves(N &node)
Retrieve all leaves from a subtree.
static bool isMultifurcating(const Node &node)
Tell is a subtree is multifurcating.
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 void getNodePropertyNames(const Node &node, std::vector< std::string > &propertyNames)
Retrieve the names of all available node properties in the tree.
static std::vector< N * > searchNodeWithId(N &node, int id)
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:405
static const std::string BOOTSTRAP
Bootstrap tag.
Definition: TreeTools.h:684
Interface for phylogenetic tree objects.
Definition: Tree.h:115
virtual std::vector< int > getSonsId(int parentId) const =0
virtual std::string getNodeName(int nodeId) const =0
virtual Clonable * getBranchProperty(int nodeId, const std::string &name)=0
virtual Clonable * getNodeProperty(int nodeId, const std::string &name)=0
virtual std::vector< std::string > getBranchPropertyNames(int nodeId) const =0
virtual bool hasDistanceToFather(int nodeId) const =0
virtual bool hasNodeName(int nodeId) const =0
virtual std::vector< std::string > getNodePropertyNames(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,...