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); }
405 virtual N*
getNode(
int id,
bool checkId =
false)
409 std::vector<N*> nodes;
410 TreeTemplateTools::searchNodeWithId<N>(*
dynamic_cast<N*
>(
root_),
id, nodes);
425 virtual const N*
getNode(
int id,
bool checkId =
false)
const
429 std::vector<const N*> nodes;
430 TreeTemplateTools::searchNodeWithId<const N>(*
root_,
id, nodes);
447 std::vector<N*> nodes;
449 if (nodes.size() > 1)
throw NodeNotFoundException(
"TreeTemplate::getNode(): Non-unique name.",
"" + name);
450 if (nodes.size() == 0)
throw NodeNotFoundException(
"TreeTemplate::getNode(): Node with name not found.",
"" + name);
454 virtual const N*
getNode(
const std::string& name)
const
456 std::vector<const N*> nodes;
457 TreeTemplateTools::searchNodeWithName<const N>(*
root_, name, nodes);
458 if (nodes.size() > 1)
throw NodeNotFoundException(
"TreeTemplate::getNode(): Non-unique name.",
"" + name);
459 if (nodes.size() == 0)
throw NodeNotFoundException(
"TreeTemplate::getNode(): Node with name not found.",
"" + name);
465 if (
root_ == newRoot)
return;
468 for (
size_t i = 0; i < path.size() - 1; i++)
474 else path[i]->deleteDistanceToFather();
475 path[i]->removeSon(path[i + 1]);
476 path[i + 1]->addSon(path[i]);
478 std::vector<std::string> names = path[i + 1]->getBranchPropertyNames();
479 for (
size_t j = 0; j < names.size(); j++)
483 path[i + 1]->deleteBranchProperties();
486 newRoot->deleteDistanceToFather();
487 newRoot->deleteBranchProperties();
493 if (
root_ == outGroup)
return;
497 for (
size_t i = 0; i <
root_->getNumberOfSons(); i++)
499 if (
root_->getSon(i) == outGroup)
return;
508 rootAt(
dynamic_cast<N*
>(outGroup->getFather()));
510 oldRoot->removeSon(outGroup);
512 root_->setId(rootId);
513 root_->addSon(oldRoot);
514 root_->addSon(outGroup);
516 if (outGroup->hasDistanceToFather())
518 double l = outGroup->getDistanceToFather() / 2.;
519 outGroup->setDistanceToFather(l);
520 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 * > getLeaves()
double getDistanceToFather(int nodeId) const
virtual void setRootNode(N *root)
bool unroot()
Unroot a rooted tree.
std::vector< double > getBranchLengths()
Get all the branch lengths of a tree.
double getTotalLength()
Get the total length (sum of all branch lengths) of a tree.
void swapNodes(int parentId, size_t i1, size_t i2)
size_t getNumberOfLeaves() const
bool isMultifurcating() const
Tell if the tree is multifurcating.
virtual const N * getNode(int id, bool checkId=false) const
std::vector< std::string > getNodePropertyNames(int nodeId) const
virtual std::vector< N * > getNodes()
std::vector< int > getLeavesId() const
virtual std::vector< const N * > getNodes() 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.
TreeTemplate< N > * clone() const
bool hasNodeName(int nodeId) const
TreeTemplate< N > * cloneSubtree(int newRootId) const
clones a Subtree rooted at given node Id
std::vector< std::string > getLeavesNames() const
std::vector< int > getAncestorsId(int nodeId) const
std::vector< std::string > getBranchPropertyNames(int nodeId) const
void rootAt(int nodeId)
Change the root node.
TreeTemplate(const TreeTemplate< N > &t)
void resetNodesId()
Number nodes.
TreeTemplate< N > & operator=(const TreeTemplate< N > &t)
bool hasNodeProperty(int nodeId, const std::string &name) const
bool isLeaf(int nodeId) const
Clonable * getBranchProperty(int nodeId, const std::string &name)
int getNextId()
Get an id.
Clonable * removeBranchProperty(int nodeId, const std::string &name)
std::vector< double > getBranchLengths() const
int getLeafId(const std::string &name) const
const Clonable * getBranchProperty(int nodeId, 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)
std::vector< int > getSonsId(int parentId) const
virtual N * getNode(int id, bool checkId=false)
void setNodeName(int nodeId, const std::string &name)
virtual std::vector< const N * > getLeaves() const
virtual std::vector< const N * > getInnerNodes() const
virtual std::vector< N * > getInnerNodes()
bool hasNoSon(int nodeId) const
void setNodeProperty(int nodeId, const std::string &name, const Clonable &property)
Clonable * getNodeProperty(int nodeId, const std::string &name)
std::string getName() const
bool hasDistanceToFather(int nodeId) const
std::vector< int > getBranchesId() const
std::vector< int > getInnerNodesId() const
std::vector< int > getNodesId() const
virtual N * getRootNode()
virtual const N * getRootNode() 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
virtual N * getNode(const std::string &name)
void newOutGroup(N *outGroup)
virtual const N * getNode(const std::string &name) const
size_t getNumberOfNodes() const
Clonable * removeNodeProperty(int nodeId, const std::string &name)
size_t getNumberOfBranches() 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
void setVoidBranchLengths(double brLen)
Give a length to branches that don't have one in a tree.
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