bpp-phyl  2.2.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:
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:
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 
165 
167 
168  int getLeafId(const std::string& name) const throw (NodeNotFoundException) { return TreeTemplateTools::getLeafId(*root_, name); }
169 
170  std::vector<int> getLeavesId() const { return TreeTemplateTools::getLeavesId(*root_); }
171 
172  std::vector<int> getNodesId() const { return TreeTemplateTools::getNodesId(*root_); }
173 
174  std::vector<int> getInnerNodesId() const { return TreeTemplateTools::getInnerNodesId(*root_); }
175 
176  std::vector<int> getBranchesId() const { return TreeTemplateTools::getBranchesId(*root_); }
177 
178  std::vector<double> getBranchLengths() const { return TreeTemplateTools::getBranchLengths(*root_); }
179 
180  std::vector<std::string> getLeavesNames() const { return TreeTemplateTools::getLeavesNames(*const_cast<const N*>( root_)); }
181 
182  std::vector<int> getSonsId(int parentId) const throw (NodeNotFoundException) { return getNode(parentId)->getSonsId(); }
183 
184  std::vector<int> getAncestorsId(int nodeId) const throw (NodeNotFoundException) { return TreeTemplateTools::getAncestorsId(*getNode(nodeId)); }
185 
186  int getFatherId(int parentId) const throw (NodeNotFoundException) { return getNode(parentId)->getFatherId(); }
187 
188  bool hasFather(int nodeId) const throw (NodeNotFoundException) { return getNode(nodeId)->hasFather(); }
189 
190  std::string getNodeName(int nodeId) const throw (NodeNotFoundException) { return getNode(nodeId)->getName(); }
191 
192  bool hasNodeName(int nodeId) const throw (NodeNotFoundException) { return getNode(nodeId)->hasName(); }
193 
194  void setNodeName(int nodeId, const std::string& name) throw (NodeNotFoundException) { getNode(nodeId)->setName(name); }
195 
196  void deleteNodeName(int nodeId) throw (NodeNotFoundException) { return getNode(nodeId)->deleteName(); }
197 
198  bool hasNode(int nodeId) const { return TreeTemplateTools::hasNodeWithId(*root_, nodeId); }
199 
200  bool isLeaf(int nodeId) const throw (NodeNotFoundException) { return getNode(nodeId)->isLeaf(); }
201 
202  bool isRoot(int nodeId) const throw (NodeNotFoundException) { return TreeTemplateTools::isRoot(*getNode(nodeId)); }
203 
204  double getDistanceToFather(int nodeId) const { return getNode(nodeId)->getDistanceToFather(); }
205 
206  void setDistanceToFather(int nodeId, double length) { getNode(nodeId)->setDistanceToFather(length); }
207 
208  void deleteDistanceToFather(int nodeId) { getNode(nodeId)->deleteDistanceToFather(); }
209 
210  bool hasDistanceToFather(int nodeId) const { return getNode(nodeId)->hasDistanceToFather(); }
211 
212  bool hasNodeProperty(int nodeId, const std::string& name) const throw (NodeNotFoundException) { return getNode(nodeId)->hasNodeProperty(name); }
213 
214  void setNodeProperty(int nodeId, const std::string& name, const Clonable& property) throw (NodeNotFoundException) { getNode(nodeId)->setNodeProperty(name, property); }
215 
216  Clonable* getNodeProperty(int nodeId, const std::string& name) throw (NodeNotFoundException) { return getNode(nodeId)->getNodeProperty(name); }
217 
218  const Clonable* getNodeProperty(int nodeId, const std::string& name) const throw (NodeNotFoundException) { return getNode(nodeId)->getNodeProperty(name); }
219 
220  Clonable* removeNodeProperty(int nodeId, const std::string& name) throw (NodeNotFoundException) { return getNode(nodeId)->removeNodeProperty(name); }
221 
222  std::vector<std::string> getNodePropertyNames(int nodeId) const throw (NodeNotFoundException) { return getNode(nodeId)->getNodePropertyNames(); }
223 
224  bool hasBranchProperty(int nodeId, const std::string& name) const throw (NodeNotFoundException) { return getNode(nodeId)->hasBranchProperty(name); }
225 
226  void setBranchProperty(int nodeId, const std::string& name, const Clonable& property) throw (NodeNotFoundException) { getNode(nodeId)->setBranchProperty(name, property); }
227 
228  Clonable* getBranchProperty(int nodeId, const std::string& name) throw (NodeNotFoundException) { return getNode(nodeId)->getBranchProperty(name); }
229 
230  const Clonable* getBranchProperty(int nodeId, const std::string& name) const throw (NodeNotFoundException) { return getNode(nodeId)->getBranchProperty(name); }
231 
232  Clonable* removeBranchProperty(int nodeId, const std::string& name) throw (NodeNotFoundException) { return getNode(nodeId)->removeBranchProperty(name); }
233 
234  std::vector<std::string> getBranchPropertyNames(int nodeId) const throw (NodeNotFoundException) { return getNode(nodeId)->getBranchPropertyNames(); }
235 
236  void rootAt(int nodeId) throw (NodeNotFoundException) { rootAt(getNode(nodeId)); }
237 
238  void newOutGroup(int nodeId) throw (NodeNotFoundException) { newOutGroup(getNode(nodeId)); }
239 
240  bool isRooted() const { return root_->getNumberOfSons() == 2; }
241 
243  {
244  if (!isRooted()) throw UnrootedTreeException("Tree::unroot", this);
245  else
246  {
247  N* son1 = root_->getSon(0);
248  N* son2 = root_->getSon(1);
249  if (son1->isLeaf() && son2->isLeaf()) return false; // We can't unroot a single branch!
250 
251  // We manage to have a subtree in position 0:
252  if (son1->isLeaf())
253  {
254  root_->swap(0, 1);
255  son1 = root_->getSon(0);
256  son2 = root_->getSon(1);
257  }
258 
259  // Take care of branch lengths:
260  if (son1->hasDistanceToFather())
261  {
262  if (son2->hasDistanceToFather())
263  {
264  // Both nodes have lengths, we sum them:
265  son2->setDistanceToFather(son1->getDistanceToFather() + son2->getDistanceToFather());
266  }
267  else
268  {
269  // Only node 1 has length, we set it to node 2:
270  son2->setDistanceToFather(son1->getDistanceToFather());
271  }
272  son1->deleteDistanceToFather();
273  } // Else node 2 may or may not have a branch length, we do not care!
274 
275  // Remove the root:
276  root_->removeSons();
277  son1->addSon(son2);
278  delete root_;
279  setRootNode(son1);
280  return true;
281  }
282  }
283 
285  {
286  std::vector<N*> nodes = getNodes();
287  for (size_t i = 0; i < nodes.size(); i++)
288  {
289  nodes[i]->setId(static_cast<int>(i));
290  }
291  }
292 
293  bool isMultifurcating() const
294  {
295  if (root_->getNumberOfSons() > 3) return true;
296  for (size_t i = 0; i < root_->getNumberOfSons(); i++)
298  return true;
299  return false;
300  }
301 
315  template<class N2>
316  bool hasSameTopologyAs(const TreeTemplate<N2>& tree, bool ordered = false) const
317  {
318  const TreeTemplate<N>* t1 = 0;
319  const TreeTemplate<N2>* t2 = 0;
320  if (ordered)
321  {
322  t1 = this;
323  t2 = &tree;
324  }
325  else
326  {
327  TreeTemplate<N>* t1tmp = this->clone();
328  TreeTemplate<N2>* t2tmp = tree.clone();
329  TreeTemplateTools::orderTree(*t1tmp->getRootNode(), true, true);
330  TreeTemplateTools::orderTree(*t2tmp->getRootNode(), true, true);
331  t1 = t1tmp;
332  t2 = t2tmp;
333  }
334  bool test = TreeTemplateTools::haveSameOrderedTopology(*t1->getRootNode(), *t2->getRootNode());
335  if (!ordered)
336  {
337  delete t1;
338  delete t2;
339  }
340  return test;
341  }
342 
343  std::vector<double> getBranchLengths() throw (NodeException)
344  {
345  Vdouble brLen(1);
346  for (size_t i = 0; i < root_->getNumberOfSons(); i++)
347  {
348  Vdouble sonBrLen = TreeTemplateTools::getBranchLengths(*root_->getSon(i));
349  for (size_t j = 0; j < sonBrLen.size(); j++) { brLen.push_back(sonBrLen[j]); }
350  }
351  return brLen;
352  }
353 
354  double getTotalLength() throw (NodeException)
355  {
357  }
358 
359  void setBranchLengths(double brLen)
360  {
361  for (size_t i = 0; i < root_->getNumberOfSons(); i++)
362  {
363  TreeTemplateTools::setBranchLengths(*root_->getSon(i), brLen);
364  }
365  }
366 
367  void setVoidBranchLengths(double brLen)
368  {
369  for (size_t i = 0; i < root_->getNumberOfSons(); i++)
370  {
372  }
373  }
374 
375  void scaleTree(double factor) throw (NodeException)
376  {
377  for (size_t i = 0; i < root_->getNumberOfSons(); i++)
378  {
379  TreeTemplateTools::scaleTree(*root_->getSon(i), factor);
380  }
381  }
382 
383  int getNextId()
384  {
385  return TreeTools::getMPNUId(*this, root_->getId());
386  }
387 
388  void swapNodes(int parentId, size_t i1, size_t i2) throw (NodeNotFoundException, IndexOutOfBoundsException)
389  {
390  std::vector<N*> nodes = TreeTemplateTools::searchNodeWithId<N>(*root_, parentId);
391  if (nodes.size() == 0) throw NodeNotFoundException("TreeTemplate:swapNodes(): Node with id not found.", TextTools::toString(parentId));
392  for (size_t i = 0; i < nodes.size(); i++) { nodes[i]->swap(i1, i2); }
393  }
394 
395 
401  virtual void setRootNode(N* root) { root_ = root; }
402 
403  virtual N* getRootNode() { return root_; }
404 
405  virtual const N* getRootNode() const { return root_; }
406 
407  virtual std::vector<const N*> getLeaves() const { return TreeTemplateTools::getLeaves(*const_cast<const N*>(root_)); }
408 
409  virtual std::vector<N*> getLeaves() { return TreeTemplateTools::getLeaves(*root_); }
410 
411  virtual std::vector<const N*> getNodes() const { return TreeTemplateTools::getNodes(*const_cast<const N*>(root_)); }
412 
413  virtual std::vector<N*> getNodes() { return TreeTemplateTools::getNodes(*root_); }
414 
415  virtual std::vector<const N*> getInnerNodes() const { return TreeTemplateTools::getInnerNodes(*const_cast<const N*>(root_)); }
416 
417  virtual std::vector<N*> getInnerNodes() { return TreeTemplateTools::getInnerNodes(*root_); }
418 
419  virtual N* getNode(int id, bool checkId = false) throw (NodeNotFoundException, Exception)
420  {
421  if (checkId) {
422  std::vector<N*> nodes;
423  TreeTemplateTools::searchNodeWithId<N>(*root_, id, nodes);
424  if (nodes.size() > 1) throw Exception("TreeTemplate::getNode(): Non-unique id! (" + TextTools::toString(id) + ").");
425  if (nodes.size() == 0) throw NodeNotFoundException("TreeTemplate::getNode(): Node with id not found.", TextTools::toString(id));
426  return nodes[0];
427  } else {
428  N* node = dynamic_cast<N*>(TreeTemplateTools::searchFirstNodeWithId(*root_, id));
429  if (node)
430  return node;
431  else
432  throw NodeNotFoundException("TreeTemplate::getNode(): Node with id not found.", TextTools::toString(id));
433  }
434  }
435 
436  virtual const N* getNode(int id, bool checkId = false) const throw (NodeNotFoundException, Exception)
437  {
438  if (checkId) {
439  std::vector<const N*> nodes;
440  TreeTemplateTools::searchNodeWithId<const N>(*root_, id, nodes);
441  if (nodes.size() > 1) throw Exception("TreeTemplate::getNode(): Non-unique id! (" + TextTools::toString(id) + ").");
442  if (nodes.size() == 0) throw NodeNotFoundException("TreeTemplate::getNode(): Node with id not found.", TextTools::toString(id));
443  return nodes[0];
444  } else {
445  const N* node = dynamic_cast<const N*>(TreeTemplateTools::searchFirstNodeWithId(*root_, id));
446  if (node)
447  return node;
448  else
449  throw NodeNotFoundException("TreeTemplate::getNode(): Node with id not found.", TextTools::toString(id));
450  }
451  }
452 
453  virtual N* getNode(const std::string& name) throw (NodeNotFoundException, Exception)
454  {
455  std::vector<N*> nodes;
457  if (nodes.size() > 1) throw NodeNotFoundException("TreeTemplate::getNode(): Non-unique name.", "" + name);
458  if (nodes.size() == 0) throw NodeNotFoundException("TreeTemplate::getNode(): Node with name not found.", "" + name);
459  return nodes[0];
460  }
461 
462  virtual const N* getNode(const std::string& name) const throw (NodeNotFoundException, Exception)
463  {
464  std::vector<const N*> nodes;
465  TreeTemplateTools::searchNodeWithName<const N>(*root_, name, nodes);
466  if (nodes.size() > 1) throw NodeNotFoundException("TreeTemplate::getNode(): Non-unique name.", "" + name);
467  if (nodes.size() == 0) throw NodeNotFoundException("TreeTemplate::getNode(): Node with name not found.", "" + name);
468  return nodes[0];
469  }
470 
471  void rootAt(N* newRoot)
472  {
473  if (root_ == newRoot) return;
474  if (isRooted()) unroot();
475  std::vector<Node*> path = TreeTemplateTools::getPathBetweenAnyTwoNodes(*root_, *newRoot);
476 
477  for (size_t i = 0; i < path.size() - 1; i++)
478  {
479  // pathMatrix[i] -> _father = pathMatrix[i + 1];
480  // pathMatrix[i] -> setDistanceToFather(pathMatrix[i + 1] -> getDistanceToFather());
481  // typename vector<Node *>::iterator vec_iter;
482  // vec_iter = remove(pathMatrix[i] -> _sons.begin(), pathMatrix[i] -> _sons.end(), pathMatrix[i + 1]);
483  // pathMatrix[i] -> _sons.erase(vec_iter, pathMatrix[i] -> _sons.end()); // pg 1170, primer.
484  // pathMatrix[i+1] -> _sons.push_back(pathMatrix[i + 1] -> getFather());
485  // pathMatrix[i+1] -> _father = 0;
486  path[i]->removeSon(path[i + 1]);
487  if (path[i + 1]->hasDistanceToFather()) path[i]->setDistanceToFather(path[i + 1]->getDistanceToFather());
488  else path[i]->deleteDistanceToFather();
489  path[i + 1]->addSon(path[i]);
490 
491  std::vector<std::string> names = path[i + 1]->getBranchPropertyNames();
492  for (size_t j = 0; j < names.size(); j++)
493  {
494  path[i]->setBranchProperty(names[j], *path[i + 1]->getBranchProperty(names[j]));
495  }
496  path[i + 1]->deleteBranchProperties();
497  }
498  newRoot->deleteDistanceToFather();
499  newRoot->deleteBranchProperties();
500  root_ = newRoot;
501  }
502 
503  void newOutGroup(N* outGroup)
504  {
505  if (root_ == outGroup) return;
506  int rootId;
507  if (isRooted())
508  {
509  for (size_t i = 0; i < root_->getNumberOfSons(); i++)
510  {
511  if (root_->getSon(i) == outGroup) return; // This tree is already rooted appropriately.
512  }
513  rootId = getRootId();
514  unroot();
515  }
516  else
517  {
518  rootId = getNextId();
519  }
520  rootAt(outGroup->getFather());
521  N* oldRoot = root_;
522  oldRoot->removeSon(outGroup);
523  root_ = new N();
524  root_->setId(rootId);
525  root_->addSon(oldRoot);
526  root_->addSon(outGroup);
527  // Check lengths:
528  if (outGroup->hasDistanceToFather())
529  {
530  double l = outGroup->getDistanceToFather() / 2.;
531  outGroup->setDistanceToFather(l);
532  oldRoot->setDistanceToFather(l);
533  }
534  }
535 
537 };
538 } // end of namespace bpp.
539 
540 #endif // _TREETEMPLATE_H_
541 
double getDistanceToFather(int nodeId) const
Definition: TreeTemplate.h:204
size_t getNumberOfNodes() const
Definition: TreeTemplate.h:166
virtual N * getRootNode()
Definition: TreeTemplate.h:403
int getLeafId(const std::string &name) const
Definition: TreeTemplate.h:168
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:316
virtual N * getNode(const std::string &name)
Definition: TreeTemplate.h:453
void setVoidBranchLengths(double brLen)
Give a length to branches that don&#39;t have one in a tree.
Definition: TreeTemplate.h:367
void swapNodes(int parentId, size_t i1, size_t i2)
Definition: TreeTemplate.h:388
std::vector< int > getInnerNodesId() const
Definition: TreeTemplate.h:174
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.
const Clonable * getNodeProperty(int nodeId, const std::string &name) const
Definition: TreeTemplate.h:218
bool hasDistanceToFather(int nodeId) const
Definition: TreeTemplate.h:210
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:413
void newOutGroup(N *outGroup)
Definition: TreeTemplate.h:503
std::string name_
Definition: TreeTemplate.h:101
std::string getName() const
Definition: TreeTemplate.h:158
void setName(const std::string &name)
Definition: TreeTemplate.h:160
static std::vector< N * > getNodes(N &node)
Retrieve all son nodes from a subtree.
std::vector< int > getLeavesId() const
Definition: TreeTemplate.h:170
void setNodeName(int nodeId, const std::string &name)
Definition: TreeTemplate.h:194
std::vector< int > getSonsId(int parentId) const
Definition: TreeTemplate.h:182
static void deleteSubtree(N *node)
Recursively delete a subtree structure.
static void scaleTree(Node &node, double factor)
Scale a given tree.
std::vector< int > getNodesId() const
Definition: TreeTemplate.h:172
virtual int getRootId() const =0
bool hasFather(int nodeId) const
Definition: TreeTemplate.h:188
std::vector< double > getBranchLengths()
Get all the branch lengths of a tree.
Definition: TreeTemplate.h:343
void setDistanceToFather(int nodeId, double length)
Definition: TreeTemplate.h:206
void scaleTree(double factor)
Scale a given tree.
Definition: TreeTemplate.h:375
The phylogenetic tree class.
static bool haveSameOrderedTopology(const Node &n1, const Node &n2)
Tells if two subtrees have the same topology.
bool hasNode(int nodeId) const
Definition: TreeTemplate.h:198
static Node * searchFirstNodeWithId(Node &node, int id)
std::vector< int > getAncestorsId(int nodeId) const
Definition: TreeTemplate.h:184
void rootAt(N *newRoot)
Definition: TreeTemplate.h:471
Interface for phylogenetic tree objects.
Definition: Tree.h:148
TreeTemplate(const Tree &t)
Definition: TreeTemplate.h:116
std::string getNodeName(int nodeId) const
Definition: TreeTemplate.h:190
virtual const N * getRootNode() const
Definition: TreeTemplate.h:405
virtual const N * getNode(const std::string &name) const
Definition: TreeTemplate.h:462
bool isRooted() const
Tell if the tree is rooted.
Definition: TreeTemplate.h:240
virtual std::vector< const N * > getLeaves() const
Definition: TreeTemplate.h:407
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:417
Clonable * getBranchProperty(int nodeId, const std::string &name)
Definition: TreeTemplate.h:228
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:238
TreeTemplate< N > & operator=(const TreeTemplate< N > &t)
Definition: TreeTemplate.h:130
size_t getNumberOfLeaves() const
Definition: TreeTemplate.h:164
virtual void setRootNode(N *root)
Definition: TreeTemplate.h:401
static std::vector< N * > searchNodeWithName(N &node, const std::string &name)
void deleteNodeName(int nodeId)
Definition: TreeTemplate.h:196
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:767
std::vector< std::string > getNodePropertyNames(int nodeId) const
Definition: TreeTemplate.h:222
void setNodeProperty(int nodeId, const std::string &name, const Clonable &property)
Definition: TreeTemplate.h:214
bool hasNodeProperty(int nodeId, const std::string &name) const
Definition: TreeTemplate.h:212
static bool hasNodeWithId(const N &node, int id)
const Clonable * getBranchProperty(int nodeId, const std::string &name) const
Definition: TreeTemplate.h:230
virtual std::vector< N * > getLeaves()
Definition: TreeTemplate.h:409
Clonable * getNodeProperty(int nodeId, const std::string &name)
Definition: TreeTemplate.h:216
std::vector< int > getBranchesId() const
Definition: TreeTemplate.h:176
std::vector< std::string > getLeavesNames() const
Definition: TreeTemplate.h:180
Exception thrown when something is wrong with a particular node.
Clonable * removeBranchProperty(int nodeId, const std::string &name)
Definition: TreeTemplate.h:232
void deleteDistanceToFather(int nodeId)
Definition: TreeTemplate.h:208
virtual std::vector< const N * > getInnerNodes() const
Definition: TreeTemplate.h:415
virtual const N * getNode(int id, bool checkId=false) const
Definition: TreeTemplate.h:436
bool unroot()
Unroot a rooted tree.
Definition: TreeTemplate.h:242
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:383
bool isRoot(int nodeId) const
Definition: TreeTemplate.h:202
static int getLeafId(const Node &node, const std::string &name)
Get the id of a leaf given its name in a subtree.
double getTotalLength()
Get the total length (sum of all branch lengths) of a tree.
Definition: TreeTemplate.h:354
void rootAt(int nodeId)
Change the root node.
Definition: TreeTemplate.h:236
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.
TreeTemplate< N > * clone() const
Definition: TreeTemplate.h:151
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.
bool hasBranchProperty(int nodeId, const std::string &name) const
Definition: TreeTemplate.h:224
static std::vector< N * > getInnerNodes(N &node)
Retrieve all inner nodes from a subtree.
TreeTemplate< N > * cloneSubtree(int newRootId) const
clones a Subtree rooted at given node Id
Definition: TreeTemplate.h:139
static std::vector< Node * > getPathBetweenAnyTwoNodes(Node &node1, Node &node2, bool includeAncestor=true)
Clonable * removeNodeProperty(int nodeId, const std::string &name)
Definition: TreeTemplate.h:220
void setBranchProperty(int nodeId, const std::string &name, const Clonable &property)
Definition: TreeTemplate.h:226
Exception thrown when a tree is expected to be rooted.
int getRootId() const
Definition: TreeTemplate.h:162
bool isMultifurcating() const
Tell if the tree is multifurcating.
Definition: TreeTemplate.h:293
int getFatherId(int parentId) const
Definition: TreeTemplate.h:186
void resetNodesId()
Number nodes.
Definition: TreeTemplate.h:284
std::vector< std::string > getBranchPropertyNames(int nodeId) const
Definition: TreeTemplate.h:234
void setBranchLengths(double brLen)
Set all the branch lengths of a tree.
Definition: TreeTemplate.h:359
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:419
General exception thrown when something is wrong with a particular node.
bool isLeaf(int nodeId) const
Definition: TreeTemplate.h:200
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...
bool hasNodeName(int nodeId) const
Definition: TreeTemplate.h:192
virtual std::vector< const N * > getNodes() const
Definition: TreeTemplate.h:411
static std::vector< int > getNodesId(const Node &node)
Retrieve all nodes ids from a subtree.
std::vector< double > getBranchLengths() const
Definition: TreeTemplate.h:178
static std::vector< int > getLeavesId(const Node &node)
Retrieve all leaves ids from a subtree.
TreeTemplate(const TreeTemplate< N > &t)
Definition: TreeTemplate.h:108