bpp-phyl  2.2.0
TreeTemplateTools.h
Go to the documentation of this file.
1 //
2 // File: TreeTemplateTools.h
3 // Created by: Julien Dutheil
4 // Created on: Fri Oct 13 13:00 2006
5 // From file TreeTools.h
6 // Created on: Wed Aug 6 13:45:28 2003
7 //
8 
9 /*
10  Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
11 
12  This software is a computer program whose purpose is to provide classes
13  for phylogenetic data analysis.
14 
15  This software is governed by the CeCILL license under French law and
16  abiding by the rules of distribution of free software. You can use,
17  modify and/ or redistribute the software under the terms of the CeCILL
18  license as circulated by CEA, CNRS and INRIA at the following URL
19  "http://www.cecill.info".
20 
21  As a counterpart to the access to the source code and rights to copy,
22  modify and redistribute granted by the license, users are provided only
23  with a limited warranty and the software's author, the holder of the
24  economic rights, and the successive licensors have only limited
25  liability.
26 
27  In this respect, the user's attention is drawn to the risks associated
28  with loading, using, modifying and/or developing or reproducing the
29  software by the user in light of its specific status of free software,
30  that may mean that it is complicated to manipulate, and that also
31  therefore means that it is reserved for developers and experienced
32  professionals having in-depth computer knowledge. Users are therefore
33  encouraged to load and test the software's suitability as regards their
34  requirements in conditions enabling the security of their systems and/or
35  data to be ensured and, more generally, to use and operate it in the
36  same conditions as regards security.
37 
38  The fact that you are presently reading this means that you have had
39  knowledge of the CeCILL license and that you accept its terms.
40  */
41 
42 #ifndef _TREETEMPLATETOOLS_H_
43 #define _TREETEMPLATETOOLS_H_
44 
45 #include "TreeTools.h"
46 #include <Bpp/Numeric/Random/RandomTools.h>
47 
48 // From the STL:
49 #include <string>
50 #include <vector>
51 
52 namespace bpp
53 {
54 template<class N> class TreeTemplate;
55 
56 
63 {
64 public:
66  virtual ~TreeTemplateTools() {}
67 
68 public:
81  template<class N>
82  static std::vector<N*> getLeaves(N& node)
83  {
84  std::vector<N*> leaves;
85  getLeaves<N>(node, leaves);
86  return leaves;
87  }
88 
95  template<class N>
96  static void getLeaves(N& node, std::vector<N*>& leaves)
97  {
98  if (node.isLeaf())
99  {
100  leaves.push_back(&node);
101  }
102  for (size_t i = 0; i < node.getNumberOfSons(); i++)
103  {
104  getLeaves<N>(*node.getSon(i), leaves);
105  }
106  }
107 
114  static std::vector<int> getLeavesId(const Node& node)
115  {
116  std::vector<int> ids;
117  getLeavesId(node, ids);
118  return ids;
119  }
120 
127  static void getLeavesId(const Node& node, std::vector<int>& ids)
128  {
129  if (node.isLeaf())
130  {
131  ids.push_back(node.getId());
132  }
133  for (size_t i = 0; i < node.getNumberOfSons(); i++)
134  {
135  getLeavesId(*node.getSon(i), ids);
136  }
137  }
138 
145  static std::vector<int> getAncestorsId(const Node& node)
146  {
147  std::vector<int> ids;
148  const Node* n = &node;
149  while (n->hasFather())
150  {
151  n = n->getFather();
152  ids.push_back(n->getId());
153  }
154  return ids;
155  }
156 
165  static int getLeafId(const Node& node, const std::string& name) throw (NodeNotFoundException)
166  {
167  int* id = 0;
168  searchLeaf(node, name, id);
169  if (id == 0) throw NodeNotFoundException("TreeTemplateTools::getLeafId().", name);
170  else
171  {
172  int i = *id;
173  delete id;
174  return i;
175  }
176  }
177 
186  static void searchLeaf(const Node& node, const std::string& name, int*& id) throw (NodeNotFoundException)
187  {
188  if (node.isLeaf())
189  {
190  if (node.getName() == name)
191  {
192  id = new int(node.getId());
193  return;
194  }
195  }
196  for (size_t i = 0; i < node.getNumberOfSons(); i++)
197  {
198  searchLeaf(*node.getSon(i), name, id);
199  }
200  }
201 
209  template<class N>
210  static void dropLeaf(TreeTemplate<N>& tree, const std::string& leafName) throw (NodeNotFoundException, Exception)
211  {
212  N* leaf = tree.getNode(leafName);
213  if (!leaf->hasFather())
214  throw Exception("TreeTemplateTools::dropLeaf(). Leaf is the only node in the tree, can't remove it.");
215  N* parent = leaf->getFather();
216  if (parent->getNumberOfSons() > 2)
217  {
218  // The easy case:
219  parent->removeSon(leaf);
220  delete leaf;
221  }
222  else if (parent->getNumberOfSons() == 2)
223  {
224  // We have to delete the parent node as well:
225  N* brother = parent->getSon(0);
226  if (brother == leaf) brother = parent->getSon(1);
227  if (!parent->hasFather())
228  {
229  // The brother becomes the root:
230  if (leaf->hasDistanceToFather() && brother->hasDistanceToFather())
231  {
232  brother->setDistanceToFather(brother->getDistanceToFather() + leaf->getDistanceToFather());
233  }
234  brother->removeFather();
235  tree.setRootNode(brother);
236  delete parent;
237  delete leaf;
238  }
239  else
240  {
241  N* gParent = parent->getFather();
242  if (brother->hasDistanceToFather() && parent->hasDistanceToFather())
243  {
244  brother->setDistanceToFather(brother->getDistanceToFather() + parent->getDistanceToFather());
245  }
246  size_t pos = gParent->getSonPosition(parent);
247  gParent->setSon(pos, brother);
248  delete parent;
249  delete leaf;
250  }
251  }
252  else
253  {
254  // Dunno what to do in that case :(
255  throw Exception("TreeTemplateTools::dropLeaf. Parent node as only one child, I don't know what to do in that case :(");
256  }
257  }
258 
266  template<class N>
267  static void dropSubtree(TreeTemplate<N>& tree, Node* subtree) throw (Exception)
268  {
269  if (!subtree->hasFather())
270  throw Exception("TreeTemplateTools::dropSubtree(). Trying to remove the full tree!");
271  N* parent = subtree->getFather();
272  if (parent->getNumberOfSons() > 2)
273  {
274  // The easy case:
275  parent->removeSon(subtree);
276  deleteSubtree(subtree);
277  }
278  else if (parent->getNumberOfSons() == 2)
279  {
280  // We have to delete the parent node as well:
281  N* brother = parent->getSon(0);
282  if (brother == subtree) brother = parent->getSon(1);
283  if (!parent->hasFather())
284  {
285  // The brother becomes the root:
286  if (subtree->hasDistanceToFather() && brother->hasDistanceToFather())
287  {
288  brother->setDistanceToFather(brother->getDistanceToFather() + subtree->getDistanceToFather());
289  }
290  tree.setRootNode(brother);
291  delete parent;
292  deleteSubtree(subtree);
293  }
294  else
295  {
296  N* gParent = parent->getFather();
297  if (brother->hasDistanceToFather() && parent->hasDistanceToFather())
298  {
299  brother->setDistanceToFather(brother->getDistanceToFather() + parent->getDistanceToFather());
300  }
301  size_t pos = gParent->getSonPosition(parent);
302  gParent->setSon(pos, brother);
303  delete parent;
304  deleteSubtree(subtree);
305  }
306  }
307  else
308  {
309  // Dunno what to do in that case :(
310  throw Exception("TreeTemplateTools::dropSubtree. Parent node as only one child, I don't know what to do in that case :(");
311  }
312  }
313 
321  template<class N>
322  static void sampleSubtree(TreeTemplate<N>& tree, const std::vector<std::string>& leaves, size_t size)
323  {
324  std::vector<std::string> names = leaves;
325  for (size_t n = names.size(); n > size; --n)
326  {
327  size_t i = RandomTools::giveIntRandomNumberBetweenZeroAndEntry(n);
328  dropLeaf(tree, names[i]);
329  names.erase(names.begin() + static_cast<ptrdiff_t>(i));
330  }
331  }
332 
339  template<class N>
340  static std::vector<N*> getNodes(N& node)
341  {
342  std::vector<N*> nodes;
343  getNodes<N>(node, nodes);
344  return nodes;
345  }
346 
353  template<class N>
354  static void getNodes(N& node, std::vector<N*>& nodes)
355  {
356  for (size_t i = 0; i < node.getNumberOfSons(); i++)
357  {
358  getNodes<N>(*node.getSon(i), nodes);
359  }
360  nodes.push_back(&node);
361  }
362 
369  static std::vector<int> getNodesId(const Node& node)
370  {
371  std::vector<int> ids;
372  getNodesId(node, ids);
373  return ids;
374  }
375 
382  static std::vector<int> getBranchesId(const Node& node)
383  {
384  std::vector<int> ids;
385  getBranchesId(node, ids);
386  return ids;
387  }
388 
395  static void getNodesId(const Node& node, std::vector<int>& ids)
396  {
397  for (size_t i = 0; i < node.getNumberOfSons(); i++)
398  {
399  getNodesId(*node.getSon(i), ids);
400  }
401  ids.push_back(node.getId());
402  }
403 
410  static void getBranchesId(const Node& node, std::vector<int>& ids)
411  {
412  for (size_t i = 0; i < node.getNumberOfSons(); i++)
413  {
414  getNodesId(*node.getSon(i), ids);
415  }
416  }
417 
424  template<class N>
425  static std::vector<N*> getInnerNodes(N& node)
426  {
427  std::vector<N*> nodes;
428  getInnerNodes<N>(node, nodes);
429  return nodes;
430  }
431 
440  template<class N>
441  static void getInnerNodes(N& node, std::vector<N*>& nodes)
442  {
443  for (size_t i = 0; i < node.getNumberOfSons(); i++)
444  {
445  getInnerNodes<N>(*node.getSon(i), nodes);
446  }
447  if (!node.isLeaf())
448  nodes.push_back(&node); // Do not add leaves!
449  }
450 
459  static std::vector<int> getInnerNodesId(const Node& node)
460  {
461  std::vector<int> ids;
462  getInnerNodesId(node, ids);
463  return ids;
464  }
465 
472  static void getInnerNodesId(const Node& node, std::vector<int>& ids)
473  {
474  for (size_t i = 0; i < node.getNumberOfSons(); i++)
475  {
476  getInnerNodesId(*node.getSon(i), ids);
477  }
478  if (!node.isLeaf())
479  ids.push_back(node.getId()); // Do not add leaves!
480  }
481 
487  template<class N>
488  static std::vector<N*> searchNodeWithId(N& node, int id)
489  {
490  std::vector<N*> nodes;
491  searchNodeWithId<N>(node, id, nodes);
492  return nodes;
493  }
494 
500  template<class N>
501  static void searchNodeWithId(N& node, int id, std::vector<N*>& nodes)
502  {
503  for (size_t i = 0; i < node.getNumberOfSons(); ++i)
504  {
505  searchNodeWithId<N>(*node.getSon(i), id, nodes);
506  }
507  if (node.getId() == id) nodes.push_back(&node);
508  }
509 
515  static Node* searchFirstNodeWithId(Node& node, int id)
516  {
517  if (node.getId() == id)
518  return &node;
519  else
520  {
521  for (size_t i = 0; i < node.getNumberOfSons(); ++i)
522  {
523  Node* result = searchFirstNodeWithId(*node.getSon(i), id);
524  if (result)
525  return result;
526  }
527  }
528  return 0;
529  }
530 
536  static const Node* searchFirstNodeWithId(const Node& node, int id)
537  {
538  if (node.getId() == id)
539  return &node;
540  else
541  {
542  for (size_t i = 0; i < node.getNumberOfSons(); ++i)
543  {
544  const Node* result = searchFirstNodeWithId(*node.getSon(i), id);
545  if (result)
546  return result;
547  }
548  }
549  return 0;
550  }
551 
557  template<class N>
558  static bool hasNodeWithId(const N& node, int id)
559  {
560  if (node.getId() == id) return true;
561  else
562  {
563  for (size_t i = 0; i < node.getNumberOfSons(); i++)
564  {
565  if (hasNodeWithId(*node.getSon(i), id)) return true;
566  }
567  return false;
568  }
569  }
570 
576  template<class N>
577  static std::vector<N*> searchNodeWithName(N& node, const std::string& name)
578  {
579  std::vector<N*> nodes;
580  searchNodeWithId<N>(node, name, nodes);
581  return nodes;
582  }
583 
589  template<class N>
590  static void searchNodeWithName(N& node, const std::string& name, std::vector<N*>& nodes)
591  {
592  for (size_t i = 0; i < node.getNumberOfSons(); i++)
593  {
594  searchNodeWithName<N>(*node.getSon(i), name, nodes);
595  }
596  if (node.hasName() && node.getName() == name) nodes.push_back(&node);
597  }
598 
604  template<class N>
605  static bool hasNodeWithName(const N& node, const std::string& name)
606  {
607  if (node.hasName() & node.getName() == name) return true;
608  else
609  {
610  for (size_t i = 0; i < node.getNumberOfSons(); i++)
611  {
612  if (hasNodeWithName(*node.getSon(i), name)) return true;
613  }
614  return false;
615  }
616  }
617 
625  static bool isRoot(const Node& node) { return !node.hasFather(); }
626 
633  static unsigned int getNumberOfLeaves(const Node& node);
634 
641  static unsigned int getNumberOfNodes(const Node& node);
642 
649  static std::vector<std::string> getLeavesNames(const Node& node);
650 
670  static unsigned int getDepth(const Node& node);
671 
692  static unsigned int getDepths(const Node& node, std::map<const Node*, unsigned int>& depths);
693 
705  static double getHeight(const Node& node);
706 
718  static double getHeights(const Node& node, std::map<const Node*, double>& heights);
719 
727  static bool isMultifurcating(const Node& node);
728 
740  static bool haveSameOrderedTopology(const Node& n1, const Node& n2);
741 
742  static std::vector<Node*> getPathBetweenAnyTwoNodes(Node& node1, Node& node2, bool includeAncestor = true);
743 
744  static std::vector<const Node*> getPathBetweenAnyTwoNodes(const Node& node1, const Node& node2, bool includeAncestor = true);
745 
755  template<class N>
756  static N* cloneSubtree(const Node& node)
757  {
758  // First we copy this node using default copy constuctor:
759  N* clone = new N(node);
760  // We remove the link toward the father:
761  // clone->removeFather();
762 
763  // Now we perform a hard copy:
764  for (int i = 0; i < static_cast<int>(node.getNumberOfSons()); i++)
765  {
766  clone->addSon(cloneSubtree<N>(*node[i]));
767  }
768  return clone;
769  }
770 
776  template<class N>
777  static void deleteSubtree(N* node)
778  {
779  for (size_t i = 0; i < node->getNumberOfSons(); ++i)
780  {
781  N* son = node->getSon(i);
782  deleteSubtree(son);
783  delete son;
784  }
785  }
786 
787 
788  template<class N>
789  static N* cloneSubtree(const Tree& tree, int nodeId)
790  {
791  // First we copy this node using default copy constuctor:
792  N* clone = tree.hasNodeName(nodeId) ? new N(nodeId, tree.getNodeName(nodeId)) : new N(nodeId);
793  // Then we set the length:
794  if (tree.hasDistanceToFather(nodeId))
795  clone->setDistanceToFather(tree.getDistanceToFather(nodeId));
796  // Now we copy all sons:
797  std::vector<int> sonsId = tree.getSonsId(nodeId);
798  for (size_t i = 0; i < sonsId.size(); i++)
799  {
800  clone->addSon(cloneSubtree<N>(tree, sonsId[i]));
801  }
802  // Must copy all properties too:
803  std::vector<std::string> names;
804  names = tree.getNodePropertyNames(nodeId);
805  for (size_t i = 0; i < names.size(); i++)
806  {
807  clone->setNodeProperty(names[i], *tree.getNodeProperty(nodeId, names[i]));
808  }
809  names = tree.getBranchPropertyNames(nodeId);
810  for (size_t i = 0; i < names.size(); i++)
811  {
812  clone->setBranchProperty(names[i], *tree.getBranchProperty(nodeId, names[i]));
813  }
814 
815  return clone;
816  }
832  static Vdouble getBranchLengths(const Node& node) throw (NodePException);
833 
843  static double getTotalLength(const Node& node, bool includeAncestor = true) throw (NodePException);
844 
851  static void setBranchLengths(Node& node, double brLen);
852 
858  static void deleteBranchLengths(Node& node);
859 
866  static void setVoidBranchLengths(Node& node, double brLen);
867 
877  static void scaleTree(Node& node, double factor) throw (NodePException);
878 
888  static double getDistanceBetweenAnyTwoNodes(const Node& node1, const Node& node2);
889 
908  static DistanceMatrix* getDistanceMatrix(const TreeTemplate<Node>& tree);
909 
910 private:
922  static void processDistsInSubtree_(const Node* node, DistanceMatrix& matrix, std::vector< std::pair<std::string, double> >& distsToNodeFather);
923 
924 public:
937  struct Element
938  {
939 public:
940  std::string content;
941  std::string length;
942  std::string bootstrap;
943  bool isLeaf;
944 
945 public:
946  Element() : content(),
947  length(),
948  bootstrap(),
949  isLeaf(false) {}
950  };
951 
952  static Element getElement(const std::string& elt) throw (IOException);
953 
965  static Node* parenthesisToNode(const std::string& description, bool bootstrap = true, const std::string& propertyName = TreeTools::BOOTSTRAP, bool withId = false);
966 
979  static TreeTemplate<Node>* parenthesisToTree(const std::string& description, bool bootstrap = true, const std::string& propertyName = TreeTools::BOOTSTRAP, bool withId = false) throw (Exception);
980 
990  static std::string nodeToParenthesis(const Node& node, bool writeId = false);
991 
1004  static std::string nodeToParenthesis(const Node& node, bool bootstrap, const std::string& propertyName);
1005 
1015  static std::string treeToParenthesis(const TreeTemplate<Node>& tree, bool writeId = false);
1016 
1029  static std::string treeToParenthesis(const TreeTemplate<Node>& tree, bool bootstrap, const std::string& propertyName);
1030 
1046  static TreeTemplate<Node>* getRandomTree(std::vector<std::string>& leavesNames, bool rooted = true);
1047 
1061  static std::vector<const Node*> getRemainingNeighbors(const Node* node1, const Node* node2, const Node* node3);
1062 
1069  static void incrementAllIds(Node* node, int increment);
1070 
1083  static void getNodePropertyNames(const Node& node, std::vector<std::string>& propertyNames);
1084 
1095  static void getNodeProperties(const Node& node, const std::string& propertyName, std::map<int, const Clonable*>& properties);
1096 
1107  static void getNodeProperties(Node& node, const std::string& propertyName, std::map<int, Clonable*>& properties);
1108 
1115  static void getBranchPropertyNames(const Node& node, std::vector<std::string>& propertyNames);
1116 
1127  static void getBranchProperties(const Node& node, const std::string& propertyName, std::map<int, const Clonable*>& properties);
1128 
1139  static void getBranchProperties(Node& node, const std::string& propertyName, std::map<int, Clonable*>& properties);
1140 
1148  static void orderTree(Node& node, bool downward = true, bool orderLeaves = false)
1149  {
1150  orderTree_(node, downward, orderLeaves);
1151  }
1195  static void midRoot(TreeTemplate<Node>& tree, short criterion, bool forceBranchRoot);
1196 
1202  static double getRadius(TreeTemplate<Node>& tree);
1203 
1204 
1219  static void unresolveUncertainNodes(Node& subtree, double threshold, const std::string& property = TreeTools::BOOTSTRAP);
1220 
1221 private:
1223  {
1224  size_t size;
1225  std::string firstLeaf;
1227  firstLeaf("") {}
1228  };
1229 
1230  static OrderTreeData_ orderTree_(Node& node, bool downward, bool orderLeaves);
1231 
1242  struct Moments_
1243  {
1244  double sum;
1245  double squaresSum;
1247  };
1248 
1256  static Moments_ getSubtreeMoments_(const Node* node);
1257 
1272  static void getBestRootInSubtree_(bpp::TreeTemplate<bpp::Node>& tree, short criterion, bpp::Node* node, std::pair<bpp::Node*, std::map<std::string, double> >& bestRoot);
1273 
1274 public:
1275  static const short MIDROOT_VARIANCE;
1276  static const short MIDROOT_SUM_OF_SQUARES;
1277 };
1278 } // end of namespace bpp.
1279 
1280 #endif // _TREETEMPLATETOOLS_H_
1281 
Utilitary methods working with TreeTemplate and Node objects.
virtual Clonable * getNodeProperty(int nodeId, const std::string &name)=0
static std::vector< N * > searchNodeWithId(N &node, int id)
static unsigned int getDepth(const Node &node)
Get the depth of the subtree defined by node &#39;node&#39;, i.e. the maximum number of sons &#39;generations&#39;...
static double getRadius(TreeTemplate< Node > &tree)
Get the caracteristic radius of a tree (average distance to the root minimizing the sum of squared di...
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 void getBranchesId(const Node &node, std::vector< int > &ids)
Retrieve all branches ids from a subtree.
static std::vector< int > getAncestorsId(const Node &node)
Retrieve all nodes ids that are ancestors of a node.
virtual std::vector< std::string > getBranchPropertyNames(int nodeId) const =0
static std::vector< N * > getNodes(N &node)
Retrieve all son nodes from a subtree.
static std::vector< const Node * > getRemainingNeighbors(const Node *node1, const Node *node2, const Node *node3)
Get a subset of node neighbors.
static bool hasNodeWithName(const N &node, const std::string &name)
A structure recording, for a subtree, the sum of root-leaf distances, the sum of their squares...
static double getDistanceBetweenAnyTwoNodes(const Node &node1, const Node &node2)
Get the total distance between to nodes.
static void sampleSubtree(TreeTemplate< N > &tree, const std::vector< std::string > &leaves, size_t size)
Sample a subtree by removing leaves randomly.
static void deleteSubtree(N *node)
Recursively delete a subtree structure.
static void scaleTree(Node &node, double factor)
Scale a given tree.
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).
virtual const Node * getSon(size_t pos) const
Definition: Node.h:395
STL namespace.
static void dropLeaf(TreeTemplate< N > &tree, const std::string &leafName)
Remove a leaf node and its parent node, while correcting for branch lengths.
The phylogenetic tree class.
static bool haveSameOrderedTopology(const Node &n1, const Node &n2)
Tells if two subtrees have the same topology.
static void searchNodeWithId(N &node, int id, std::vector< N *> &nodes)
static OrderTreeData_ orderTree_(Node &node, bool downward, bool orderLeaves)
static const short MIDROOT_VARIANCE
static Node * searchFirstNodeWithId(Node &node, int id)
virtual const Node * getFather() const
Get the father of this node is there is one.
Definition: Node.h:339
Interface for phylogenetic tree objects.
Definition: Tree.h:148
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 &#39;node&#39;, i.e. the maximum number of sons &#39;...
static N * cloneSubtree(const Node &node)
Recursively clone a subtree structure.
static const Node * searchFirstNodeWithId(const Node &node, int id)
virtual bool hasFather() const
Tell if this node has a father node.
Definition: Node.h:379
virtual bool isLeaf() const
Definition: Node.h:692
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 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 unsigned int getNumberOfNodes(const Node &node)
Get the number of nodes of a subtree defined by a particular node.
static void incrementAllIds(Node *node, int increment)
This method will add a given value (possibly negative) to all identifiers in a (sub)tree.
static void searchNodeWithName(N &node, const std::string &name, std::vector< N *> &nodes)
static void midRoot(TreeTemplate< Node > &tree, short criterion, bool forceBranchRoot)
Midroot the tree by minimizing a given criterion ("variance" or "sum of squares") ...
static std::vector< N * > searchNodeWithName(N &node, const std::string &name)
virtual int getId() const
Get the node&#39;s id.
Definition: Node.h:203
static void getLeavesId(const Node &node, std::vector< int > &ids)
Retrieve all leaves ids from a subtree.
static std::vector< N * > getLeaves(N &node)
Retrieve all leaves from a subtree.
static double getHeight(const Node &node)
Get the height of the subtree defined by node &#39;node&#39;, i.e. the maximum distance between leaves and th...
static bool hasNodeWithId(const N &node, int id)
static std::string treeToParenthesis(const TreeTemplate< Node > &tree, bool writeId=false)
Get the parenthesis description of a tree.
static void getBranchPropertyNames(const Node &node, std::vector< std::string > &propertyNames)
Retrieve the names of all available branch properties in the tree.
static void getNodePropertyNames(const Node &node, std::vector< std::string > &propertyNames)
Retrieve the names of all available node properties in the tree.
static void deleteBranchLengths(Node &node)
Remove all the branch lengths of a subtree.
static N * cloneSubtree(const Tree &tree, int nodeId)
Exception thrown when something is wrong with a particular node.
static void getInnerNodesId(const Node &node, std::vector< int > &ids)
Retrieve all inner nodes ids from a subtree.
static double getHeights(const Node &node, std::map< const Node *, double > &heights)
Get the heights of all nodes within a subtree defined by node &#39;node&#39;, i.e. the maximum distance betwe...
static void processDistsInSubtree_(const Node *node, DistanceMatrix &matrix, std::vector< std::pair< std::string, double > > &distsToNodeFather)
Inner function used by getDistanceMatrix.
static void setBranchLengths(Node &node, double brLen)
Set all the branch lengths of a subtree.
The phylogenetic node class.
Definition: Node.h:90
static void unresolveUncertainNodes(Node &subtree, double threshold, const std::string &property=TreeTools::BOOTSTRAP)
Unresolve nodes with low confidence value.
static unsigned int getNumberOfLeaves(const Node &node)
Get the number of leaves of a subtree defined by a particular node.
virtual std::vector< std::string > getNodePropertyNames(int nodeId) const =0
static int getLeafId(const Node &node, const std::string &name)
Get the id of a leaf given its name in a subtree.
virtual double getDistanceToFather(int nodeId) const =0
static Moments_ getSubtreeMoments_(const Node *node)
Computes the moment of a subtree.
static bool isMultifurcating(const Node &node)
Tell is a subtree is multifurcating.
static Vdouble getBranchLengths(const Node &node)
Get all the branch lengths of a subtree.
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 double getTotalLength(const Node &node, bool includeAncestor=true)
Get the total length (sum of all branch lengths) of a subtree.
static void setVoidBranchLengths(Node &node, double brLen)
Give a length to branches that don&#39;t have one in a subtree.
virtual bool hasNodeName(int nodeId) const =0
static std::vector< N * > getInnerNodes(N &node)
Retrieve all inner nodes from a subtree.
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)
virtual bool hasDistanceToFather(int nodeId) const =0
static const std::string BOOTSTRAP
Bootstrap tag.
Definition: TreeTools.h:723
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).
virtual size_t getNumberOfSons() const
Definition: Node.h:388
static std::string nodeToParenthesis(const Node &node, bool writeId=false)
Get the parenthesis description of a subtree.
static TreeTemplate< Node > * parenthesisToTree(const std::string &description, bool bootstrap=true, const std::string &propertyName=TreeTools::BOOTSTRAP, bool withId=false)
Parse a string in the parenthesis format and convert it to a tree.
static std::vector< int > getBranchesId(const Node &node)
Retrieve all branches ids from a subtree.
General exception thrown when something is wrong with a particular node.
virtual Clonable * getBranchProperty(int nodeId, const std::string &name)=0
static std::vector< int > getInnerNodesId(const Node &node)
Retrieve all inner nodes ids from a subtree.
static Element getElement(const std::string &elt)
static Node * parenthesisToNode(const std::string &description, bool bootstrap=true, const std::string &propertyName=TreeTools::BOOTSTRAP, bool withId=false)
Parse a string in the parenthesis format and convert it to a subtree.
static void getInnerNodes(N &node, std::vector< N *> &nodes)
Retrieve all inner nodes from a subtree.
virtual std::vector< int > getSonsId(int parentId) const =0
static DistanceMatrix * getDistanceMatrix(const TreeTemplate< Node > &tree)
Compute a distance matrix from a tree.
static const short MIDROOT_SUM_OF_SQUARES
static void getLeaves(N &node, std::vector< N *> &leaves)
Retrieve all leaves from a subtree.
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 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 std::vector< int > getNodesId(const Node &node)
Retrieve all nodes ids from a subtree.
static std::vector< int > getLeavesId(const Node &node)
Retrieve all leaves 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.
virtual std::string getNodeName(int nodeId) const =0