42 #ifndef _TREETEMPLATETOOLS_H_ 43 #define _TREETEMPLATETOOLS_H_ 46 #include <Bpp/Numeric/Random/RandomTools.h> 54 template<
class N>
class TreeTemplate;
84 std::vector<N*> leaves;
85 getLeaves<N>(node, leaves);
96 static void getLeaves(N& node, std::vector<N*>& leaves)
100 leaves.push_back(&node);
102 for (
size_t i = 0; i < node.getNumberOfSons(); i++)
104 getLeaves<N>(*node.getSon(i), leaves);
116 std::vector<int> ids;
131 ids.push_back(node.
getId());
147 std::vector<int> ids;
148 const Node* n = &node;
152 ids.push_back(n->
getId());
190 if (node.getName() == name)
192 id =
new int(node.getId());
196 for (
size_t i = 0; i < node.getNumberOfSons(); i++)
212 N* leaf = tree.getNode(leafName);
213 if (!leaf->hasFather())
214 throw Exception(
"TreeTemplateTools::dropLeaf(). Leaf is the only node in the tree, can't remove it.");
215 N* parent = leaf->getFather();
216 if (parent->getNumberOfSons() > 2)
219 parent->removeSon(leaf);
222 else if (parent->getNumberOfSons() == 2)
225 N* brother = parent->getSon(0);
226 if (brother == leaf) brother = parent->getSon(1);
227 if (!parent->hasFather())
230 if (leaf->hasDistanceToFather() && brother->hasDistanceToFather())
232 brother->setDistanceToFather(brother->getDistanceToFather() + leaf->getDistanceToFather());
234 brother->removeFather();
235 tree.setRootNode(brother);
241 N* gParent = parent->getFather();
242 if (brother->hasDistanceToFather() && parent->hasDistanceToFather())
244 brother->setDistanceToFather(brother->getDistanceToFather() + parent->getDistanceToFather());
246 size_t pos = gParent->getSonPosition(parent);
247 gParent->setSon(pos, brother);
255 throw Exception(
"TreeTemplateTools::dropLeaf. Parent node as only one child, I don't know what to do in that case :(");
269 if (!subtree->hasFather())
270 throw Exception(
"TreeTemplateTools::dropSubtree(). Trying to remove the full tree!");
271 N* parent = subtree->getFather();
272 if (parent->getNumberOfSons() > 2)
275 parent->removeSon(subtree);
278 else if (parent->getNumberOfSons() == 2)
281 N* brother = parent->getSon(0);
282 if (brother == subtree) brother = parent->getSon(1);
283 if (!parent->hasFather())
286 if (subtree->hasDistanceToFather() && brother->hasDistanceToFather())
288 brother->setDistanceToFather(brother->getDistanceToFather() + subtree->getDistanceToFather());
290 tree.setRootNode(brother);
296 N* gParent = parent->getFather();
297 if (brother->hasDistanceToFather() && parent->hasDistanceToFather())
299 brother->setDistanceToFather(brother->getDistanceToFather() + parent->getDistanceToFather());
301 size_t pos = gParent->getSonPosition(parent);
302 gParent->setSon(pos, brother);
310 throw Exception(
"TreeTemplateTools::dropSubtree. Parent node as only one child, I don't know what to do in that case :(");
324 std::vector<std::string> names = leaves;
325 for (
size_t n = names.size(); n > size; --n)
327 size_t i = RandomTools::giveIntRandomNumberBetweenZeroAndEntry(n);
329 names.erase(names.begin() +
static_cast<ptrdiff_t
>(i));
342 std::vector<N*> nodes;
343 getNodes<N>(node, nodes);
354 static void getNodes(N& node, std::vector<N*>& nodes)
356 for (
size_t i = 0; i < node.getNumberOfSons(); i++)
358 getNodes<N>(*node.getSon(i), nodes);
360 nodes.push_back(&node);
371 std::vector<int> ids;
384 std::vector<int> ids;
401 ids.push_back(node.
getId());
427 std::vector<N*> nodes;
428 getInnerNodes<N>(node, nodes);
443 for (
size_t i = 0; i < node.getNumberOfSons(); i++)
445 getInnerNodes<N>(*node.getSon(i), nodes);
448 nodes.push_back(&node);
461 std::vector<int> ids;
479 ids.push_back(node.
getId());
490 std::vector<N*> nodes;
491 searchNodeWithId<N>(node, id, nodes);
503 for (
size_t i = 0; i < node.getNumberOfSons(); ++i)
505 searchNodeWithId<N>(*node.getSon(i), id, nodes);
507 if (node.getId() == id) nodes.push_back(&node);
517 if (node.
getId() == id)
538 if (node.
getId() == id)
560 if (node.getId() == id)
return true;
563 for (
size_t i = 0; i < node.getNumberOfSons(); i++)
579 std::vector<N*> nodes;
580 searchNodeWithId<N>(node, name, nodes);
592 for (
size_t i = 0; i < node.getNumberOfSons(); i++)
594 searchNodeWithName<N>(*node.getSon(i), name, nodes);
596 if (node.hasName() && node.getName() == name) nodes.push_back(&node);
607 if (node.hasName() & node.getName() == name)
return true;
610 for (
size_t i = 0; i < node.getNumberOfSons(); i++)
692 static unsigned int getDepths(
const Node& node, std::map<const Node*, unsigned int>& depths);
718 static double getHeights(
const Node& node, std::map<const Node*, double>& heights);
759 N* clone =
new N(node);
766 clone->addSon(cloneSubtree<N>(*node[i]));
779 for (
size_t i = 0; i < node->getNumberOfSons(); ++i)
781 N* son = node->getSon(i);
797 std::vector<int> sonsId = tree.
getSonsId(nodeId);
798 for (
size_t i = 0; i < sonsId.size(); i++)
800 clone->addSon(cloneSubtree<N>(tree, sonsId[i]));
803 std::vector<std::string> names;
805 for (
size_t i = 0; i < names.size(); i++)
807 clone->setNodeProperty(names[i], *tree.
getNodeProperty(nodeId, names[i]));
810 for (
size_t i = 0; i < names.size(); i++)
952 static Element
getElement(
const std::string& elt)
throw (IOException);
1148 static
void orderTree(
Node& node,
bool downward = true,
bool orderLeaves = false)
1230 static OrderTreeData_
orderTree_(
Node& node,
bool downward,
bool orderLeaves);
1280 #endif // _TREETEMPLATETOOLS_H_
virtual Clonable * getNodeProperty(int nodeId, const std::string &name)=0
virtual std::vector< std::string > getBranchPropertyNames(int nodeId) const =0
virtual const Node * getSon(size_t pos) const
The phylogenetic tree class.
virtual const Node * getFather() const
Get the father of this node is there is one.
Interface for phylogenetic tree objects.
virtual bool hasFather() const
Tell if this node has a father node.
virtual bool isLeaf() const
virtual int getId() const
Get the node's id.
Exception thrown when something is wrong with a particular node.
The phylogenetic node class.
virtual std::vector< std::string > getNodePropertyNames(int nodeId) const =0
virtual double getDistanceToFather(int nodeId) const =0
virtual bool hasNodeName(int nodeId) const =0
virtual bool hasDistanceToFather(int nodeId) const =0
virtual size_t getNumberOfSons() const
General exception thrown when something is wrong with a particular node.
virtual Clonable * getBranchProperty(int nodeId, const std::string &name)=0
virtual std::vector< int > getSonsId(int parentId) const =0
virtual std::string getNodeName(int nodeId) const =0