bpp-phyl  2.4.0
TreeTemplate.h
Go to the documentation of this file.
1 //
2 // File: TreeTemplate.h
3 // Created by: Julien Dutheil
4 // Celine Scornavacca
5 // Created on: Thu Mar 13 12:03:18 2003
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for phylogenetic data analysis.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39  */
40 
41 #ifndef _TREETEMPLATE_H_
42 #define _TREETEMPLATE_H_
43 
44 #include "TreeExceptions.h"
45 #include "TreeTemplateTools.h"
46 #include "Tree.h"
47 
48 // From the STL:
49 #include <string>
50 #include <vector>
51 #include <map>
52 
53 namespace bpp
54 {
91 template<class N>
92 class TreeTemplate :
93  public Tree
94 {
99 private:
100  N* root_;
101  std::string name_;
102 
103 public:
104  // Constructors and destructor:
105  TreeTemplate() : root_(0),
106  name_() {}
107 
109  root_(0),
110  name_(t.name_)
111  {
112  // Perform a hard copy of the nodes:
113  root_ = TreeTemplateTools::cloneSubtree<N>(*t.getRootNode());
114  }
115 
116  TreeTemplate(const Tree& t) :
117  root_(0),
118  name_(t.getName())
119  {
120  // Create new nodes from an existing tree:
121  root_ = TreeTemplateTools::cloneSubtree<N>(t, t.getRootId());
122  }
123 
124  TreeTemplate(N* root) : root_(root),
125  name_()
126  {
127  root_->removeFather(); // In case this is a subtree from somewhere else...
128  }
129 
131  {
132  // Perform a hard copy of the nodes:
133  if (root_) { TreeTemplateTools::deleteSubtree(root_); delete root_; }
134  root_ = TreeTemplateTools::cloneSubtree<N>(*t.getRootNode());
135  name_ = t.name_;
136  return *this;
137  }
138 
139  TreeTemplate<N>* cloneSubtree(int newRootId) const
140  {
141  N* newRoot = TreeTemplateTools::cloneSubtree<N>(*this, newRootId);
142  return new TreeTemplate<N>(newRoot);
143  }
144 
145  virtual ~TreeTemplate()
146  {
148  delete root_;
149  }
150 
151  TreeTemplate<N>* clone() const { return new TreeTemplate<N>(*this); }
152 
157 public:
158  std::string getName() const { return name_; }
159 
160  void setName(const std::string& name) { name_ = name; }
161 
162  int getRootId() const { return root_->getId(); }
163 
164  size_t getNumberOfLeaves() const { return TreeTemplateTools::getNumberOfLeaves(*root_); }
165 
166  size_t getNumberOfNodes() const { return TreeTemplateTools::getNumberOfNodes(*root_); }
167 
168  size_t getNumberOfBranches() const { return getNumberOfNodes() -1;}
169 
170  int getLeafId(const std::string& name) const { return TreeTemplateTools::getLeafId(*root_, name); }
171 
172  std::vector<int> getLeavesId() const { return TreeTemplateTools::getLeavesId(*root_); }
173 
174  std::vector<int> getNodesId() const { return TreeTemplateTools::getNodesId(*root_); }
175 
176  std::vector<int> getInnerNodesId() const { return TreeTemplateTools::getInnerNodesId(*root_); }
177 
178  std::vector<int> getBranchesId() const { return TreeTemplateTools::getBranchesId(*root_); }
179 
180  std::vector<double> getBranchLengths() const { return TreeTemplateTools::getBranchLengths(*root_); }
181 
182  std::vector<std::string> getLeavesNames() const { return TreeTemplateTools::getLeavesNames(*const_cast<const N*>( root_)); }
183 
184  std::vector<int> getSonsId(int parentId) const { return getNode(parentId)->getSonsId(); }
185 
186  std::vector<int> getAncestorsId(int nodeId) const { return TreeTemplateTools::getAncestorsId(*getNode(nodeId)); }
187 
188  int getFatherId(int parentId) const { return getNode(parentId)->getFatherId(); }
189 
190  bool hasFather(int nodeId) const { return getNode(nodeId)->hasFather(); }
191 
192  std::string getNodeName(int nodeId) const { return getNode(nodeId)->getName(); }
193 
194  bool hasNodeName(int nodeId) const { return getNode(nodeId)->hasName(); }
195 
196  void setNodeName(int nodeId, const std::string& name) { getNode(nodeId)->setName(name); }
197 
198  void deleteNodeName(int nodeId) { return getNode(nodeId)->deleteName(); }
199 
200  bool hasNode(int nodeId) const { return TreeTemplateTools::hasNodeWithId(*root_, nodeId); }
201 
202  bool isLeaf(int nodeId) const { return getNode(nodeId)->isLeaf(); }
203 
204  bool isRoot(int nodeId) const { return TreeTemplateTools::isRoot(*getNode(nodeId)); }
205 
206  double getDistanceToFather(int nodeId) const { return getNode(nodeId)->getDistanceToFather(); }
207 
208  void setDistanceToFather(int nodeId, double length) { getNode(nodeId)->setDistanceToFather(length); }
209 
210  void deleteDistanceToFather(int nodeId) { getNode(nodeId)->deleteDistanceToFather(); }
211 
212  bool hasDistanceToFather(int nodeId) const { return getNode(nodeId)->hasDistanceToFather(); }
213 
214  bool hasNodeProperty(int nodeId, const std::string& name) const { return getNode(nodeId)->hasNodeProperty(name); }
215 
216  void setNodeProperty(int nodeId, const std::string& name, const Clonable& property) { getNode(nodeId)->setNodeProperty(name, property); }
217 
218  Clonable* getNodeProperty(int nodeId, const std::string& name) { return getNode(nodeId)->getNodeProperty(name); }
219 
220  const Clonable* getNodeProperty(int nodeId, const std::string& name) const { return getNode(nodeId)->getNodeProperty(name); }
221 
222  Clonable* removeNodeProperty(int nodeId, const std::string& name) { return getNode(nodeId)->removeNodeProperty(name); }
223 
224  std::vector<std::string> getNodePropertyNames(int nodeId) const { return getNode(nodeId)->getNodePropertyNames(); }
225 
226  bool hasBranchProperty(int nodeId, const std::string& name) const { return getNode(nodeId)->hasBranchProperty(name); }
227 
228  void setBranchProperty(int nodeId, const std::string& name, const Clonable& property) { getNode(nodeId)->setBranchProperty(name, property); }
229 
230  Clonable* getBranchProperty(int nodeId, const std::string& name) { return getNode(nodeId)->getBranchProperty(name); }
231 
232  const Clonable* getBranchProperty(int nodeId, const std::string& name) const { return getNode(nodeId)->getBranchProperty(name); }
233 
234  Clonable* removeBranchProperty(int nodeId, const std::string& name) { return getNode(nodeId)->removeBranchProperty(name); }
235 
236  std::vector<std::string> getBranchPropertyNames(int nodeId) const { return getNode(nodeId)->getBranchPropertyNames(); }
237 
238  void rootAt(int nodeId) { rootAt(getNode(nodeId)); }
239 
240  void newOutGroup(int nodeId) { newOutGroup(getNode(nodeId)); }
241 
242  bool isRooted() const { return root_->getNumberOfSons() == 2; }
243 
244  bool unroot()
245  {
246  if (!isRooted()) throw UnrootedTreeException("Tree::unroot", this);
247  else
248  {
249  N* son1 = root_->getSon(0);
250  N* son2 = root_->getSon(1);
251  if (son1->isLeaf() && son2->isLeaf()) return false; // We can't unroot a single branch!
252 
253  // We manage to have a subtree in position 0:
254  if (son1->isLeaf())
255  {
256  root_->swap(0, 1);
257  son1 = root_->getSon(0);
258  son2 = root_->getSon(1);
259  }
260 
261  // Take care of branch lengths:
262  if (son1->hasDistanceToFather())
263  {
264  if (son2->hasDistanceToFather())
265  {
266  // Both nodes have lengths, we sum them:
267  son2->setDistanceToFather(son1->getDistanceToFather() + son2->getDistanceToFather());
268  }
269  else
270  {
271  // Only node 1 has length, we set it to node 2:
272  son2->setDistanceToFather(son1->getDistanceToFather());
273  }
274  son1->deleteDistanceToFather();
275  } // Else node 2 may or may not have a branch length, we do not care!
276 
277  // Remove the root:
278  root_->removeSons();
279  son1->addSon(son2);
280  delete root_;
281  setRootNode(son1);
282  return true;
283  }
284  }
285 
287  {
288  std::vector<N*> nodes = getNodes();
289  for (size_t i = 0; i < nodes.size(); i++)
290  {
291  nodes[i]->setId(static_cast<int>(i));
292  }
293  }
294 
295  bool isMultifurcating() const
296  {
297  if (root_->getNumberOfSons() > 3) return true;
298  for (size_t i = 0; i < root_->getNumberOfSons(); i++)
299  if (TreeTemplateTools::isMultifurcating(*root_->getSon(i)))
300  return true;
301  return false;
302  }
303 
317  template<class N2>
318  bool hasSameTopologyAs(const TreeTemplate<N2>& tree, bool ordered = false) const
319  {
320  const TreeTemplate<N>* t1 = 0;
321  const TreeTemplate<N2>* t2 = 0;
322  if (ordered)
323  {
324  t1 = this;
325  t2 = &tree;
326  }
327  else
328  {
329  TreeTemplate<N>* t1tmp = this->clone();
330  TreeTemplate<N2>* t2tmp = tree.clone();
331  TreeTemplateTools::orderTree(*t1tmp->getRootNode(), true, true);
332  TreeTemplateTools::orderTree(*t2tmp->getRootNode(), true, true);
333  t1 = t1tmp;
334  t2 = t2tmp;
335  }
336  bool test = TreeTemplateTools::haveSameOrderedTopology(*t1->getRootNode(), *t2->getRootNode());
337  if (!ordered)
338  {
339  delete t1;
340  delete t2;
341  }
342  return test;
343  }
344 
345  std::vector<double> getBranchLengths()
346  {
347  Vdouble brLen(1);
348  for (size_t i = 0; i < root_->getNumberOfSons(); i++)
349  {
350  Vdouble sonBrLen = TreeTemplateTools::getBranchLengths(*root_->getSon(i));
351  for (size_t j = 0; j < sonBrLen.size(); j++) { brLen.push_back(sonBrLen[j]); }
352  }
353  return brLen;
354  }
355 
356  double getTotalLength()
357  {
358  return TreeTemplateTools::getTotalLength(*root_, false);
359  }
360 
361  void setBranchLengths(double brLen)
362  {
363  for (size_t i = 0; i < root_->getNumberOfSons(); i++)
364  {
365  TreeTemplateTools::setBranchLengths(*root_->getSon(i), brLen);
366  }
367  }
368 
369  void setVoidBranchLengths(double brLen)
370  {
371  for (size_t i = 0; i < root_->getNumberOfSons(); i++)
372  {
373  TreeTemplateTools::setVoidBranchLengths(*root_->getSon(i), brLen);
374  }
375  }
376 
377  void scaleTree(double factor)
378  {
379  for (size_t i = 0; i < root_->getNumberOfSons(); i++)
380  {
381  TreeTemplateTools::scaleTree(*root_->getSon(i), factor);
382  }
383  }
384 
385  int getNextId()
386  {
387  return TreeTools::getMPNUId(*this, root_->getId());
388  }
389 
390  void swapNodes(int parentId, size_t i1, size_t i2)
391  {
392  std::vector<N*> nodes = TreeTemplateTools::searchNodeWithId<N>(*root_, parentId);
393  if (nodes.size() == 0) throw NodeNotFoundException("TreeTemplate:swapNodes(): Node with id not found.", TextTools::toString(parentId));
394  for (size_t i = 0; i < nodes.size(); i++) { nodes[i]->swap(i1, i2); }
395  }
396 
397 
403  virtual void setRootNode(N* root) { root_ = root; root_->removeFather(); }
404 
405  virtual N* getRootNode() { return root_; }
406 
407  virtual const N* getRootNode() const { return root_; }
408 
409  virtual std::vector<const N*> getLeaves() const { return TreeTemplateTools::getLeaves(*const_cast<const N*>(root_)); }
410 
411  virtual std::vector<N*> getLeaves() { return TreeTemplateTools::getLeaves(*root_); }
412 
413  virtual std::vector<const N*> getNodes() const { return TreeTemplateTools::getNodes(*const_cast<const N*>(root_)); }
414 
415  virtual std::vector<N*> getNodes() { return TreeTemplateTools::getNodes(*root_); }
416 
417  virtual std::vector<const N*> getInnerNodes() const { return TreeTemplateTools::getInnerNodes(*const_cast<const N*>(root_)); }
418 
419  virtual std::vector<N*> getInnerNodes() { return TreeTemplateTools::getInnerNodes(*root_); }
420 
421  virtual N* getNode(int id, bool checkId = false)
422  {
423  if (checkId) {
424  std::vector<N*> nodes;
425  TreeTemplateTools::searchNodeWithId<N>(*root_, id, nodes);
426  if (nodes.size() > 1) throw Exception("TreeTemplate::getNode(): Non-unique id! (" + TextTools::toString(id) + ").");
427  if (nodes.size() == 0) throw NodeNotFoundException("TreeTemplate::getNode(): Node with id not found.", TextTools::toString(id));
428  return nodes[0];
429  } else {
430  N* node = dynamic_cast<N*>(TreeTemplateTools::searchFirstNodeWithId(*root_, id));
431  if (node)
432  return node;
433  else
434  throw NodeNotFoundException("TreeTemplate::getNode(): Node with id not found.", TextTools::toString(id));
435  }
436  }
437 
438  virtual const N* getNode(int id, bool checkId = false) const
439  {
440  if (checkId) {
441  std::vector<const N*> nodes;
442  TreeTemplateTools::searchNodeWithId<const N>(*root_, id, nodes);
443  if (nodes.size() > 1) throw Exception("TreeTemplate::getNode(): Non-unique id! (" + TextTools::toString(id) + ").");
444  if (nodes.size() == 0) throw NodeNotFoundException("TreeTemplate::getNode(): Node with id not found.", TextTools::toString(id));
445  return nodes[0];
446  } else {
447  const N* node = dynamic_cast<const N*>(TreeTemplateTools::searchFirstNodeWithId(*root_, id));
448  if (node)
449  return node;
450  else
451  throw NodeNotFoundException("TreeTemplate::getNode(): Node with id not found.", TextTools::toString(id));
452  }
453  }
454 
455  virtual N* getNode(const std::string& name)
456  {
457  std::vector<N*> nodes;
458  TreeTemplateTools::searchNodeWithName(*root_, name, nodes);
459  if (nodes.size() > 1) throw NodeNotFoundException("TreeTemplate::getNode(): Non-unique name.", "" + name);
460  if (nodes.size() == 0) throw NodeNotFoundException("TreeTemplate::getNode(): Node with name not found.", "" + name);
461  return nodes[0];
462  }
463 
464  virtual const N* getNode(const std::string& name) const
465  {
466  std::vector<const N*> nodes;
467  TreeTemplateTools::searchNodeWithName<const N>(*root_, name, nodes);
468  if (nodes.size() > 1) throw NodeNotFoundException("TreeTemplate::getNode(): Non-unique name.", "" + name);
469  if (nodes.size() == 0) throw NodeNotFoundException("TreeTemplate::getNode(): Node with name not found.", "" + name);
470  return nodes[0];
471  }
472 
473  void rootAt(N* newRoot)
474  {
475  if (root_ == newRoot) return;
476  if (isRooted()) unroot();
477  std::vector<Node*> path = TreeTemplateTools::getPathBetweenAnyTwoNodes(*root_, *newRoot);
478  for (size_t i = 0; i < path.size() - 1; i++)
479  {
480  if (path[i + 1]->hasDistanceToFather()) {
481  path[i]->setDistanceToFather(path[i + 1]->getDistanceToFather());
482  }
483  else path[i]->deleteDistanceToFather();
484  path[i]->removeSon(path[i + 1]);
485  path[i + 1]->addSon(path[i]);
486 
487  std::vector<std::string> names = path[i + 1]->getBranchPropertyNames();
488  for (size_t j = 0; j < names.size(); j++)
489  {
490  path[i]->setBranchProperty(names[j], *path[i + 1]->getBranchProperty(names[j]));
491  }
492  path[i + 1]->deleteBranchProperties();
493  }
494 
495  newRoot->deleteDistanceToFather();
496  newRoot->deleteBranchProperties();
497  root_ = newRoot;
498  }
499 
500  void newOutGroup(N* outGroup)
501  {
502  if (root_ == outGroup) return;
503  int rootId;
504  if (isRooted())
505  {
506  for (size_t i = 0; i < root_->getNumberOfSons(); i++)
507  {
508  if (root_->getSon(i) == outGroup) return; // This tree is already rooted appropriately.
509  }
510  rootId = getRootId();
511  unroot();
512  }
513  else
514  {
515  rootId = getNextId();
516  }
517  rootAt(outGroup->getFather());
518  N* oldRoot = root_;
519  oldRoot->removeSon(outGroup);
520  root_ = new N();
521  root_->setId(rootId);
522  root_->addSon(oldRoot);
523  root_->addSon(outGroup);
524  // Check lengths:
525  if (outGroup->hasDistanceToFather())
526  {
527  double l = outGroup->getDistanceToFather() / 2.;
528  outGroup->setDistanceToFather(l);
529  oldRoot->setDistanceToFather(l);
530  }
531  }
532 
534 };
535 } // end of namespace bpp.
536 
537 #endif // _TREETEMPLATE_H_
538 
virtual N * getRootNode()
Definition: TreeTemplate.h:405
int getRootId() const
Definition: TreeTemplate.h:162
bool hasDistanceToFather(int nodeId) const
Definition: TreeTemplate.h:212
bool hasFather(int nodeId) const
Definition: TreeTemplate.h:190
virtual const N * getRootNode() const
Definition: TreeTemplate.h:407
virtual N * getNode(const std::string &name)
Definition: TreeTemplate.h:455
const Clonable * getNodeProperty(int nodeId, const std::string &name) const
Definition: TreeTemplate.h:220
void setVoidBranchLengths(double brLen)
Give a length to branches that don&#39;t have one in a tree.
Definition: TreeTemplate.h:369
void swapNodes(int parentId, size_t i1, size_t i2)
Definition: TreeTemplate.h:390
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::vector< Node * > getPathBetweenAnyTwoNodes(Node &node1, Node &node2, bool includeAncestor=true, bool includeAncestorAtEndOfPath=true)
static std::vector< int > getAncestorsId(const Node &node)
Retrieve all nodes ids that are ancestors of a node.
virtual std::vector< N * > getNodes()
Definition: TreeTemplate.h:415
void newOutGroup(N *outGroup)
Definition: TreeTemplate.h:500
std::string name_
Definition: TreeTemplate.h:101
void setName(const std::string &name)
Definition: TreeTemplate.h:160
static std::vector< N * > getNodes(N &node)
Retrieve all son nodes from a subtree.
void setNodeName(int nodeId, const std::string &name)
Definition: TreeTemplate.h:196
static void deleteSubtree(N *node)
Recursively delete a subtree structure.
static void scaleTree(Node &node, double factor)
Scale a given tree.
virtual int getRootId() const =0
virtual std::vector< const N * > getInnerNodes() const
Definition: TreeTemplate.h:417
std::vector< std::string > getBranchPropertyNames(int nodeId) const
Definition: TreeTemplate.h:236
virtual std::vector< const N * > getNodes() const
Definition: TreeTemplate.h:413
std::vector< double > getBranchLengths()
Get all the branch lengths of a tree.
Definition: TreeTemplate.h:345
void setDistanceToFather(int nodeId, double length)
Definition: TreeTemplate.h:208
bool isRoot(int nodeId) const
Definition: TreeTemplate.h:204
void scaleTree(double factor)
Scale a given tree.
Definition: TreeTemplate.h:377
The phylogenetic tree class.
static bool haveSameOrderedTopology(const Node &n1, const Node &n2)
Tells if two subtrees have the same topology.
std::vector< int > getAncestorsId(int nodeId) const
Definition: TreeTemplate.h:186
std::vector< int > getNodesId() const
Definition: TreeTemplate.h:174
static Node * searchFirstNodeWithId(Node &node, int id)
void rootAt(N *newRoot)
Definition: TreeTemplate.h:473
Interface for phylogenetic tree objects.
Definition: Tree.h:148
TreeTemplate(const Tree &t)
Definition: TreeTemplate.h:116
double getDistanceToFather(int nodeId) const
Definition: TreeTemplate.h:206
static std::vector< std::string > getLeavesNames(const Node &node)
Get the leaves names of a subtree defined by a particular node.
virtual std::vector< N * > getInnerNodes()
Definition: TreeTemplate.h:419
const Clonable * getBranchProperty(int nodeId, const std::string &name) const
Definition: TreeTemplate.h:232
std::vector< int > getLeavesId() const
Definition: TreeTemplate.h:172
Clonable * getBranchProperty(int nodeId, const std::string &name)
Definition: TreeTemplate.h:230
TreeTemplate< N > * clone() const
Definition: TreeTemplate.h:151
static unsigned int getNumberOfNodes(const Node &node)
Get the number of nodes of a subtree defined by a particular node.
void newOutGroup(int nodeId)
Root a tree by specifying an outgroup.
Definition: TreeTemplate.h:240
TreeTemplate< N > & operator=(const TreeTemplate< N > &t)
Definition: TreeTemplate.h:130
size_t getNumberOfBranches() const
Definition: TreeTemplate.h:168
virtual void setRootNode(N *root)
Definition: TreeTemplate.h:403
virtual std::vector< const N * > getLeaves() const
Definition: TreeTemplate.h:409
static std::vector< N * > searchNodeWithName(N &node, const std::string &name)
void deleteNodeName(int nodeId)
Definition: TreeTemplate.h:198
bool hasBranchProperty(int nodeId, const std::string &name) const
Definition: TreeTemplate.h:226
std::string getName() const
Definition: TreeTemplate.h:158
bool isRooted() const
Tell if the tree is rooted.
Definition: TreeTemplate.h:242
static std::vector< N * > getLeaves(N &node)
Retrieve all leaves from a subtree.
static int getMPNUId(const Tree &tree, int id)
Get the minimum positive non-used identifier in a (sub)tree.
Definition: TreeTools.cpp:762
int getFatherId(int parentId) const
Definition: TreeTemplate.h:188
void setNodeProperty(int nodeId, const std::string &name, const Clonable &property)
Definition: TreeTemplate.h:216
std::vector< std::string > getLeavesNames() const
Definition: TreeTemplate.h:182
static bool hasNodeWithId(const N &node, int id)
virtual const N * getNode(const std::string &name) const
Definition: TreeTemplate.h:464
bool isMultifurcating() const
Tell if the tree is multifurcating.
Definition: TreeTemplate.h:295
virtual std::vector< N * > getLeaves()
Definition: TreeTemplate.h:411
Clonable * getNodeProperty(int nodeId, const std::string &name)
Definition: TreeTemplate.h:218
std::vector< double > Vdouble
size_t getNumberOfNodes() const
Definition: TreeTemplate.h:166
TreeTemplate< N > * cloneSubtree(int newRootId) const
clones a Subtree rooted at given node Id
Definition: TreeTemplate.h:139
size_t getNumberOfLeaves() const
Definition: TreeTemplate.h:164
Exception thrown when something is wrong with a particular node.
Clonable * removeBranchProperty(int nodeId, const std::string &name)
Definition: TreeTemplate.h:234
void deleteDistanceToFather(int nodeId)
Definition: TreeTemplate.h:210
bool unroot()
Unroot a rooted tree.
Definition: TreeTemplate.h:244
bool isLeaf(int nodeId) const
Definition: TreeTemplate.h:202
static void setBranchLengths(Node &node, double brLen)
Set all the branch lengths of a subtree.
virtual ~TreeTemplate()
Definition: TreeTemplate.h:145
static unsigned int getNumberOfLeaves(const Node &node)
Get the number of leaves of a subtree defined by a particular node.
int getNextId()
Get an id.
Definition: TreeTemplate.h:385
static int getLeafId(const Node &node, const std::string &name)
Get the id of a leaf given its name in a subtree.
std::vector< int > getBranchesId() const
Definition: TreeTemplate.h:178
double getTotalLength()
Get the total length (sum of all branch lengths) of a tree.
Definition: TreeTemplate.h:356
void rootAt(int nodeId)
Change the root node.
Definition: TreeTemplate.h:238
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.
int getLeafId(const std::string &name) const
Definition: TreeTemplate.h:170
static double getTotalLength(const Node &node, bool includeAncestor=true)
Get the total length (sum of all branch lengths) of a subtree.
TreeTemplate(N *root)
Definition: TreeTemplate.h:124
static void setVoidBranchLengths(Node &node, double brLen)
Give a length to branches that don&#39;t have one in a subtree.
static std::vector< N * > getInnerNodes(N &node)
Retrieve all inner nodes from a subtree.
std::vector< int > getInnerNodesId() const
Definition: TreeTemplate.h:176
std::vector< double > getBranchLengths() const
Definition: TreeTemplate.h:180
virtual const N * getNode(int id, bool checkId=false) const
Definition: TreeTemplate.h:438
Clonable * removeNodeProperty(int nodeId, const std::string &name)
Definition: TreeTemplate.h:222
std::vector< std::string > getNodePropertyNames(int nodeId) const
Definition: TreeTemplate.h:224
void setBranchProperty(int nodeId, const std::string &name, const Clonable &property)
Definition: TreeTemplate.h:228
Exception thrown when a tree is expected to be rooted.
void resetNodesId()
Number nodes.
Definition: TreeTemplate.h:286
bool hasNode(int nodeId) const
Definition: TreeTemplate.h:200
std::string toString(T t)
void setBranchLengths(double brLen)
Set all the branch lengths of a tree.
Definition: TreeTemplate.h:361
static std::vector< int > getBranchesId(const Node &node)
Retrieve all branches ids from a subtree.
static std::vector< int > getInnerNodesId(const Node &node)
Retrieve all inner nodes ids from a subtree.
virtual N * getNode(int id, bool checkId=false)
Definition: TreeTemplate.h:421
bool hasNodeName(int nodeId) const
Definition: TreeTemplate.h:194
std::string getNodeName(int nodeId) const
Definition: TreeTemplate.h:192
bool hasNodeProperty(int nodeId, const std::string &name) const
Definition: TreeTemplate.h:214
std::vector< int > getSonsId(int parentId) const
Definition: TreeTemplate.h:184
bool hasSameTopologyAs(const TreeTemplate< N2 > &tree, bool ordered=false) const
Tells if this tree has the same topology as the one given for comparison.
Definition: TreeTemplate.h:318
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.
TreeTemplate(const TreeTemplate< N > &t)
Definition: TreeTemplate.h:108