41 #ifndef _TREETEMPLATE_H_ 42 #define _TREETEMPLATE_H_ 113 root_ = TreeTemplateTools::cloneSubtree<N>(*t.getRootNode());
127 root_->removeFather();
134 root_ = TreeTemplateTools::cloneSubtree<N>(*t.getRootNode());
141 N* newRoot = TreeTemplateTools::cloneSubtree<N>(*
this, newRootId);
247 N* son1 =
root_->getSon(0);
248 N* son2 =
root_->getSon(1);
249 if (son1->isLeaf() && son2->isLeaf())
return false;
255 son1 =
root_->getSon(0);
256 son2 =
root_->getSon(1);
260 if (son1->hasDistanceToFather())
262 if (son2->hasDistanceToFather())
265 son2->setDistanceToFather(son1->getDistanceToFather() + son2->getDistanceToFather());
270 son2->setDistanceToFather(son1->getDistanceToFather());
272 son1->deleteDistanceToFather();
287 for (
size_t i = 0; i < nodes.size(); i++)
289 nodes[i]->setId(static_cast<int>(i));
295 if (
root_->getNumberOfSons() > 3)
return true;
296 for (
size_t i = 0; i <
root_->getNumberOfSons(); i++)
346 for (
size_t i = 0; i <
root_->getNumberOfSons(); i++)
349 for (
size_t j = 0; j < sonBrLen.size(); j++) { brLen.push_back(sonBrLen[j]); }
361 for (
size_t i = 0; i <
root_->getNumberOfSons(); i++)
369 for (
size_t i = 0; i <
root_->getNumberOfSons(); i++)
377 for (
size_t i = 0; i <
root_->getNumberOfSons(); i++)
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); }
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));
432 throw NodeNotFoundException(
"TreeTemplate::getNode(): Node with id not found.", TextTools::toString(
id));
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));
449 throw NodeNotFoundException(
"TreeTemplate::getNode(): Node with id not found.", TextTools::toString(
id));
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);
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);
473 if (
root_ == newRoot)
return;
477 for (
size_t i = 0; i < path.size() - 1; i++)
486 path[i]->removeSon(path[i + 1]);
488 else path[i]->deleteDistanceToFather();
489 path[i + 1]->addSon(path[i]);
491 std::vector<std::string> names = path[i + 1]->getBranchPropertyNames();
492 for (
size_t j = 0; j < names.size(); j++)
496 path[i + 1]->deleteBranchProperties();
498 newRoot->deleteDistanceToFather();
499 newRoot->deleteBranchProperties();
505 if (
root_ == outGroup)
return;
509 for (
size_t i = 0; i <
root_->getNumberOfSons(); i++)
511 if (
root_->getSon(i) == outGroup)
return;
520 rootAt(outGroup->getFather());
522 oldRoot->removeSon(outGroup);
524 root_->setId(rootId);
525 root_->addSon(oldRoot);
526 root_->addSon(outGroup);
528 if (outGroup->hasDistanceToFather())
530 double l = outGroup->getDistanceToFather() / 2.;
531 outGroup->setDistanceToFather(l);
532 oldRoot->setDistanceToFather(l);
540 #endif // _TREETEMPLATE_H_ double getDistanceToFather(int nodeId) const
size_t getNumberOfNodes() const
virtual N * getRootNode()
int getLeafId(const std::string &name) const
bool hasSameTopologyAs(const TreeTemplate< N2 > &tree, bool ordered=false) const
Tells if this tree has the same topology as the one given for comparison.
virtual N * getNode(const std::string &name)
void setVoidBranchLengths(double brLen)
Give a length to branches that don't have one in a tree.
void swapNodes(int parentId, size_t i1, size_t i2)
std::vector< int > getInnerNodesId() const
const Clonable * getNodeProperty(int nodeId, const std::string &name) const
bool hasDistanceToFather(int nodeId) const
virtual std::vector< N * > getNodes()
void newOutGroup(N *outGroup)
std::string getName() const
void setName(const std::string &name)
std::vector< int > getLeavesId() const
void setNodeName(int nodeId, const std::string &name)
std::vector< int > getSonsId(int parentId) const
std::vector< int > getNodesId() const
virtual int getRootId() const =0
bool hasFather(int nodeId) const
std::vector< double > getBranchLengths()
Get all the branch lengths of a tree.
void setDistanceToFather(int nodeId, double length)
void scaleTree(double factor)
Scale a given tree.
The phylogenetic tree class.
bool hasNode(int nodeId) const
std::vector< int > getAncestorsId(int nodeId) const
Interface for phylogenetic tree objects.
TreeTemplate(const Tree &t)
std::string getNodeName(int nodeId) const
virtual const N * getRootNode() const
virtual const N * getNode(const std::string &name) const
bool isRooted() const
Tell if the tree is rooted.
virtual std::vector< const N * > getLeaves() const
virtual std::vector< N * > getInnerNodes()
Clonable * getBranchProperty(int nodeId, const std::string &name)
void newOutGroup(int nodeId)
Root a tree by specifying an outgroup.
TreeTemplate< N > & operator=(const TreeTemplate< N > &t)
size_t getNumberOfLeaves() const
virtual void setRootNode(N *root)
void deleteNodeName(int nodeId)
std::vector< std::string > getNodePropertyNames(int nodeId) const
void setNodeProperty(int nodeId, const std::string &name, const Clonable &property)
bool hasNodeProperty(int nodeId, const std::string &name) const
const Clonable * getBranchProperty(int nodeId, const std::string &name) const
virtual std::vector< N * > getLeaves()
Clonable * getNodeProperty(int nodeId, const std::string &name)
std::vector< int > getBranchesId() const
std::vector< std::string > getLeavesNames() const
Exception thrown when something is wrong with a particular node.
Clonable * removeBranchProperty(int nodeId, const std::string &name)
void deleteDistanceToFather(int nodeId)
virtual std::vector< const N * > getInnerNodes() const
virtual const N * getNode(int id, bool checkId=false) const
bool unroot()
Unroot a rooted tree.
int getNextId()
Get an id.
bool isRoot(int nodeId) const
double getTotalLength()
Get the total length (sum of all branch lengths) of a tree.
void rootAt(int nodeId)
Change the root node.
TreeTemplate< N > * clone() const
bool hasBranchProperty(int nodeId, const std::string &name) const
TreeTemplate< N > * cloneSubtree(int newRootId) const
clones a Subtree rooted at given node Id
Clonable * removeNodeProperty(int nodeId, const std::string &name)
void setBranchProperty(int nodeId, const std::string &name, const Clonable &property)
Exception thrown when a tree is expected to be rooted.
bool isMultifurcating() const
Tell if the tree is multifurcating.
int getFatherId(int parentId) const
void resetNodesId()
Number nodes.
std::vector< std::string > getBranchPropertyNames(int nodeId) const
void setBranchLengths(double brLen)
Set all the branch lengths of a tree.
virtual N * getNode(int id, bool checkId=false)
General exception thrown when something is wrong with a particular node.
bool isLeaf(int nodeId) const
bool hasNodeName(int nodeId) const
virtual std::vector< const N * > getNodes() const
std::vector< double > getBranchLengths() const
TreeTemplate(const TreeTemplate< N > &t)