5 #ifndef BPP_PHYL_TREE_TREETEMPLATETOOLS_H
6 #define BPP_PHYL_TREE_TREETEMPLATETOOLS_H
20 template<
class N>
class TreeTemplate;
51 std::vector<N*> leaves;
52 getLeaves<N>(node, leaves);
63 static void getLeaves(N& node, std::vector<N*>& leaves)
67 leaves.push_back(&node);
69 for (
size_t i = 0; i < node.getNumberOfSons(); i++)
71 getLeaves<N>(*
dynamic_cast<N*
>(node.getSon(i)), leaves);
133 static void searchLeaf(
const Node& node,
const std::string& name,
int*&
id);
146 N* leaf = tree.
getNode(leafName);
147 if (!leaf->hasFather())
148 throw Exception(
"TreeTemplateTools::dropLeaf(). Leaf is the only node in the tree, can't remove it.");
149 N* parent = leaf->getFather();
150 if (parent->getNumberOfSons() > 2)
153 parent->removeSon(leaf);
156 else if (parent->getNumberOfSons() == 2)
159 N* brother = parent->getSon(0);
160 if (brother == leaf) brother = parent->getSon(1);
161 if (!parent->hasFather())
164 if (leaf->hasDistanceToFather() && brother->hasDistanceToFather())
166 brother->setDistanceToFather(brother->getDistanceToFather() + leaf->getDistanceToFather());
168 brother->removeFather();
175 N* gParent = parent->getFather();
176 if (brother->hasDistanceToFather() && parent->hasDistanceToFather())
178 brother->setDistanceToFather(brother->getDistanceToFather() + parent->getDistanceToFather());
180 size_t pos = gParent->getSonPosition(parent);
181 gParent->setSon(pos, brother);
189 throw Exception(
"TreeTemplateTools::dropLeaf. Parent node as only one child, I don't know what to do in that case :(");
204 throw Exception(
"TreeTemplateTools::dropSubtree(). Trying to remove the full tree!");
206 if (parent->getNumberOfSons() > 2)
212 else if (parent->getNumberOfSons() == 2)
215 N* brother = parent->getSon(0);
216 if (brother == subtree) brother = parent->getSon(1);
217 if (!parent->hasFather())
222 brother->setDistanceToFather(brother->getDistanceToFather() + subtree->
getDistanceToFather());
230 N* gParent = parent->getFather();
231 if (brother->hasDistanceToFather() && parent->hasDistanceToFather())
233 brother->setDistanceToFather(brother->getDistanceToFather() + parent->getDistanceToFather());
235 size_t pos = gParent->getSonPosition(parent);
236 gParent->setSon(pos, brother);
244 throw Exception(
"TreeTemplateTools::dropSubtree. Parent node as only one child, I don't know what to do in that case :(");
258 std::vector<std::string> names = leaves;
259 for (
size_t n = names.size(); n > size; --n)
263 names.erase(names.begin() +
static_cast<ptrdiff_t
>(i));
276 std::vector<N*> nodes;
277 getNodes<N>(node, nodes);
288 static void getNodes(N& node, std::vector<N*>& nodes)
290 for (
size_t i = 0; i < node.getNumberOfSons(); i++)
292 getNodes<N>(*
dynamic_cast<N*
>(node.getSon(i)), nodes);
294 nodes.push_back(&node);
305 std::vector<int> ids;
322 ids.push_back(node.
getId());
334 std::vector<N*> nodes;
335 getInnerNodes<N>(node, nodes);
350 for (
size_t i = 0; i < node.getNumberOfSons(); i++)
352 getInnerNodes<N>(*
dynamic_cast<N*
>(node.getSon(i)), nodes);
355 nodes.push_back(&node);
368 std::vector<int> ids;
386 ids.push_back(node.
getId());
397 std::vector<N*> nodes;
398 searchNodeWithId<N>(node,
id, nodes);
410 for (
size_t i = 0; i < node.getNumberOfSons(); ++i)
412 searchNodeWithId<N>(*
dynamic_cast<N*
>(node.getSon(i)),
id, nodes);
414 if (node.getId() ==
id) nodes.push_back(&node);
424 if (node.
getId() ==
id)
445 if (node.
getId() ==
id)
467 if (node.getId() ==
id)
return true;
470 for (
size_t i = 0; i < node.getNumberOfSons(); i++)
486 std::vector<N*> nodes;
487 searchNodeWithId<N>(node, name, nodes);
499 for (
size_t i = 0; i < node.getNumberOfSons(); i++)
501 searchNodeWithName<N>(*
dynamic_cast<N*
>(node.getSon(i)), name, nodes);
503 if (node.hasName() && node.getName() == name) nodes.push_back(&node);
514 if (node.hasName() & node.getName() == name)
return true;
517 for (
size_t i = 0; i < node.getNumberOfSons(); i++)
542 size_t nbBranches = 0;
624 static unsigned int getDepths(
const Node& node, std::map<const Node*, unsigned int>& depths);
650 static double getHeights(
const Node& node, std::map<const Node*, double>& heights);
676 static std::vector<const Node*>
getPathBetweenAnyTwoNodes(
const Node& node1,
const Node& node2,
bool includeAncestor =
true,
bool includeAncestorAtEndOfPath =
true);
691 N* clone =
new N(node);
698 clone->addSon(cloneSubtree<N>(*node[i]));
711 for (
size_t i = 0; i < node->getNumberOfSons(); ++i)
713 N* son =
dynamic_cast<N*
>(node->getSon(i));
729 std::vector<int> sonsId = tree.
getSonsId(nodeId);
730 for (
size_t i = 0; i < sonsId.size(); i++)
732 clone->addSon(cloneSubtree<N>(tree, sonsId[i]));
735 std::vector<std::string> names;
737 for (
size_t i = 0; i < names.size(); i++)
739 clone->setNodeProperty(names[i], *tree.
getNodeProperty(nodeId, names[i]));
742 for (
size_t i = 0; i < names.size(); i++)
884 static Element
getElement(
const std::string& elt);
899 static Node*
parenthesisToNode(
const std::string& description,
unsigned int& nodeCounter,
bool bootstrap =
true,
const std::string& propertyName =
TreeTools::BOOTSTRAP,
bool withId =
false,
bool verbose =
true);
914 static std::unique_ptr<TreeTemplate<Node>>
parenthesisToTree(
const std::string& description,
bool bootstrap =
true,
const std::string& propertyName =
TreeTools::BOOTSTRAP,
bool withId =
false,
bool verbose =
true);
939 static std::string
nodeToParenthesis(
const Node& node,
bool bootstrap,
const std::string& propertyName);
983 static std::unique_ptr<TreeTemplate<Node>>
getRandomTree(std::vector<std::string>& leavesNames,
bool rooted =
true);
1032 static void getNodeProperties(
const Node& node,
const std::string& propertyName, std::map<int, const Clonable*>& properties);
1044 static void getNodeProperties(
Node& node,
const std::string& propertyName, std::map<int, Clonable*>& properties);
1072 static void getBranchProperties(
const Node& node,
const std::string& propertyName, std::map<int, const Clonable*>& properties);
1084 static void getBranchProperties(
Node& node,
const std::string& propertyName, std::map<int, Clonable*>& properties);
1104 static void orderTree(
Node& node,
bool downward =
true,
bool orderLeaves =
false)
1185 static OrderTreeData_
orderTree_(
Node& node,
bool downward,
bool orderLeaves);
Exception thrown when something is wrong with a particular node.
The phylogenetic node class.
virtual Node * removeSon(size_t pos)
virtual int getId() const
Get the node's id.
virtual const Node * getSon(size_t pos) const
virtual const Node * getFather() const
Get the father of this node is there is one.
virtual bool hasDistanceToFather() const
Tell is this node has a distance to the father.
virtual bool isLeaf() const
virtual bool hasFather() const
Tell if this node has a father node.
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
virtual size_t getNumberOfSons() const
The phylogenetic tree class.
virtual void setRootNode(N *root)
virtual N * getNode(int id, bool checkId=false)
Interface for phylogenetic tree objects.
virtual std::vector< int > getSonsId(int parentId) const =0
virtual std::string getNodeName(int nodeId) const =0
virtual Clonable * getBranchProperty(int nodeId, const std::string &name)=0
virtual Clonable * getNodeProperty(int nodeId, const std::string &name)=0
virtual std::vector< std::string > getBranchPropertyNames(int nodeId) const =0
virtual bool hasDistanceToFather(int nodeId) const =0
virtual bool hasNodeName(int nodeId) const =0
virtual std::vector< std::string > getNodePropertyNames(int nodeId) const =0
virtual double getDistanceToFather(int nodeId) const =0
Defines the basic types of data flow nodes.
std::vector< double > Vdouble