5#ifndef BPP_PHYL_TREE_TREETEMPLATE_H
6#define BPP_PHYL_TREE_TREETEMPLATE_H
92 root_->removeFather();
106 N* newRoot = TreeTemplateTools::cloneSubtree<N>(*
this, newRootId);
147 std::vector<int>::iterator rit(vRes.begin());
148 while (rit < vRes.end())
151 vRes.erase(rit, rit + 1);
231 N* son1 =
dynamic_cast<N*
>(
root_->getSon(0));
232 N* son2 =
dynamic_cast<N*
>(
root_->getSon(1));
233 if (son1->isLeaf() && son2->isLeaf())
return false;
239 son1 =
dynamic_cast<N*
>(
root_->getSon(0));
240 son2 =
dynamic_cast<N*
>(
root_->getSon(1));
244 if (son1->hasDistanceToFather())
246 if (son2->hasDistanceToFather())
249 son2->setDistanceToFather(son1->getDistanceToFather() + son2->getDistanceToFather());
254 son2->setDistanceToFather(son1->getDistanceToFather());
256 son1->deleteDistanceToFather();
271 for (
size_t i = 0; i < nodes.size(); i++)
273 nodes[i]->setId(
static_cast<int>(i));
279 if (
root_->getNumberOfSons() > 3)
return true;
280 for (
size_t i = 0; i <
root_->getNumberOfSons(); i++)
332 for (
size_t i = 0; i <
root_->getNumberOfSons(); i++)
335 for (
size_t j = 0; j < sonBrLen.size(); j++) { brLen.push_back(sonBrLen[j]); }
347 for (
size_t i = 0; i <
root_->getNumberOfSons(); i++)
355 for (
size_t i = 0; i <
root_->getNumberOfSons(); i++)
363 for (
size_t i = 0; i <
root_->getNumberOfSons(); i++)
376 std::vector<N*> nodes = TreeTemplateTools::searchNodeWithId<N>(*
root_, parentId);
378 for (
size_t i = 0; i < nodes.size(); i++) { nodes[i]->swap(i1, i2); }
429 virtual N*
getNode(
int id,
bool checkId =
false)
433 std::vector<N*> nodes;
434 TreeTemplateTools::searchNodeWithId<N>(*
dynamic_cast<N*
>(
root_),
id, nodes);
449 virtual const N*
getNode(
int id,
bool checkId =
false)
const
453 std::vector<const N*> nodes;
454 TreeTemplateTools::searchNodeWithId<const N>(*
root_,
id, nodes);
471 std::vector<N*> nodes;
473 if (nodes.size() > 1)
throw NodeNotFoundException(
"TreeTemplate::getNode(): Non-unique name.",
"" + name);
474 if (nodes.size() == 0)
throw NodeNotFoundException(
"TreeTemplate::getNode(): Node with name not found.",
"" + name);
478 virtual const N*
getNode(
const std::string& name)
const
480 std::vector<const N*> nodes;
481 TreeTemplateTools::searchNodeWithName<const N>(*
root_, name, nodes);
482 if (nodes.size() > 1)
throw NodeNotFoundException(
"TreeTemplate::getNode(): Non-unique name.",
"" + name);
483 if (nodes.size() == 0)
throw NodeNotFoundException(
"TreeTemplate::getNode(): Node with name not found.",
"" + name);
489 if (
root_ == newRoot)
return;
492 for (
size_t i = 0; i < path.size() - 1; i++)
498 else path[i]->deleteDistanceToFather();
499 path[i]->removeSon(path[i + 1]);
500 path[i + 1]->addSon(path[i]);
502 std::vector<std::string> names = path[i + 1]->getBranchPropertyNames();
503 for (
size_t j = 0; j < names.size(); j++)
507 path[i + 1]->deleteBranchProperties();
510 newRoot->deleteDistanceToFather();
511 newRoot->deleteBranchProperties();
517 if (
root_ == outGroup)
return;
521 for (
size_t i = 0; i <
root_->getNumberOfSons(); i++)
523 if (
root_->getSon(i) == outGroup)
return;
532 rootAt(
dynamic_cast<N*
>(outGroup->getFather()));
534 oldRoot->removeSon(outGroup);
536 root_->setId(rootId);
537 root_->addSon(oldRoot);
538 root_->addSon(outGroup);
540 if (outGroup->hasDistanceToFather())
542 double l = outGroup->getDistanceToFather() / 2.;
543 outGroup->setDistanceToFather(l);
544 oldRoot->setDistanceToFather(l);
Exception thrown when something is wrong with a particular node.
The phylogenetic tree class.
void setDistanceToFather(int nodeId, double length)
virtual std::vector< N * > getNodes()
double getDistanceToFather(int nodeId) const
Clonable * getBranchProperty(int nodeId, const std::string &name)
std::vector< std::string > getBranchPropertyNames(int nodeId) const
virtual void setRootNode(N *root)
bool unroot()
Unroot a rooted tree.
virtual const N * getNode(int id, bool checkId=false) const
std::vector< int > getAncestorsId(int nodeId) const
Clonable * getNodeProperty(int nodeId, const std::string &name)
double getTotalLength()
Get the total length (sum of all branch lengths) of a tree.
const Clonable * getBranchProperty(int nodeId, const std::string &name) const
std::vector< int > getNodesId() const
void swapNodes(int parentId, size_t i1, size_t i2)
size_t getNumberOfLeaves() const
bool isMultifurcating() const
Tell if the tree is multifurcating.
virtual std::vector< N * > getLeaves()
std::vector< int > getLeavesId() const
void setName(const std::string &name)
void scaleTree(double factor)
Scale a given tree.
bool isRoot(int nodeId) const
bool isRooted() const
Tell if the tree is rooted.
bool hasNodeName(int nodeId) const
virtual N * getNode(const std::string &name)
std::vector< std::string > getNodePropertyNames(int nodeId) const
TreeTemplate< N > & operator=(const TreeTemplate< N > &t)
std::vector< double > getBranchLengths()
Get all the branch lengths of a tree.
std::vector< int > getBranchesId() const
void rootAt(int nodeId)
Change the root node.
TreeTemplate(const TreeTemplate< N > &t)
virtual N * getRootNode()
void resetNodesId()
Number nodes.
std::vector< int > getSonsId(int parentId) const
virtual std::vector< N * > getInnerNodes()
TreeTemplate< N > * clone() const
bool hasNodeProperty(int nodeId, const std::string &name) const
Clonable * removeBranchProperty(int nodeId, const std::string &name)
bool isLeaf(int nodeId) const
int getNextId()
Get an id.
int getLeafId(const std::string &name) const
TreeTemplate(const Tree &t)
bool hasNode(int nodeId) 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.
void deleteNodeName(int nodeId)
TreeTemplate< N > * cloneSubtree(int newRootId) const
clones a Subtree rooted at given node Id
void setNodeName(int nodeId, const std::string &name)
virtual const N * getNode(const std::string &name) const
std::vector< double > getBranchLengths() const
bool hasNoSon(int nodeId) const
void setNodeProperty(int nodeId, const std::string &name, const Clonable &property)
std::string getName() const
bool hasDistanceToFather(int nodeId) const
std::vector< int > getInnerNodesId() const
virtual std::vector< const N * > getNodes() const
virtual std::vector< const N * > getInnerNodes() const
const Clonable * getNodeProperty(int nodeId, const std::string &name) const
void deleteDistanceToFather(int nodeId)
std::string getNodeName(int nodeId) const
int getFatherId(int parentId) const
bool hasFather(int nodeId) const
void newOutGroup(N *outGroup)
size_t getNumberOfNodes() const
virtual N * getNode(int id, bool checkId=false)
size_t getNumberOfBranches() const
virtual const N * getRootNode() const
void newOutGroup(int nodeId)
Root a tree by specifying an outgroup.
void setBranchProperty(int nodeId, const std::string &name, const Clonable &property)
bool hasBranchProperty(int nodeId, const std::string &name) const
virtual std::vector< const N * > getLeaves() const
void setVoidBranchLengths(double brLen)
Give a length to branches that don't have one in a tree.
virtual const N & rootNode() const
Clonable * removeNodeProperty(int nodeId, const std::string &name)
std::vector< std::string > getLeavesNames() const
void setBranchLengths(double brLen)
Set all the branch lengths of a tree.
Interface for phylogenetic tree objects.
virtual int getRootId() const =0
Exception thrown when a tree is expected to be rooted.
std::string toString(T t)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble