Extract or print subtree

231 views
Skip to first unread message

Yan Wong

unread,
Sep 16, 2015, 11:57:21 AM9/16/15
to DendroPy Users
I've probably just missed it when perusing the docs, but is there a function to create a new tree out of the descendants of a particular node. Something like tree.get_from_node() . Ot a way to just print out a newick / whatever file for the subtree descending from that node? I guess I could simply reroot at that node and prune the sister taxa, but that seems a bit messy.

Yan Wong

unread,
Sep 16, 2015, 5:50:05 PM9/16/15
to DendroPy Users
I've just realised that I can copy the tree, then set the seed_node to the target subnode and set the subnode's parent_node to None, which will (presumably) end up rendering inaccessible the parts of the tree that don't descend from that node. If I do this, are all the now-unlinked nodes garbage collected? I ask as I'm likely to be doing this with big trees.

Yan

Jeet Sukumaran

unread,
Sep 16, 2015, 5:57:19 PM9/16/15
to dendrop...@googlegroups.com
Hi Yan,

First of all, you did not miss anything in the documentation. There is
currently no pre-rolled method to do this.

Secondly, the approach you describe below is how I would go about doing
it, but it can be done more simply:

subtree = dendropy.Tree(seed_node=node_of_interest)

BUT bear in mind that the original tree is now in an invalid state, as
the node links are disrupted. In DendroPy's data model, nodes cannot be
shared between trees. If you need the original tree intact, you will
have to clone the entire structure first and use the nodes from the clone:

tree2 = dendropy.Tree(tree1)
node_of_interest = tree2.find_node(...)
subtree = dendropy.Tree(seed_node=node_of_interest)

In response to your second question, garbage collection is, of course,
the purview of the Python virtual machine. So as long as you do not
maintain any references to the original tree and its component nodes,
the Python VM GC will kick in. To be sure, you may want to delete the
original tree reference after extracting the subtree:

node_of_interest = tree1.find_node(...)
subtree = dendropy.Tree(seed_node=node_of_interest)
del tree1
> --
> You received this message because you are subscribed to the Google
> Groups "DendroPy Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to dendropy-user...@googlegroups.com
> <mailto:dendropy-user...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

--



--------------------------------------
Jeet Sukumaran
--------------------------------------
jeetsu...@gmail.com
--------------------------------------
Blog/Personal Pages:
http://jeetworks.org/
GitHub Repositories:
http://github.com/jeetsukumaran
Photographs (as stream):
http://www.flickr.com/photos/jeetsukumaran/
Photographs (by galleries):
http://www.flickr.com/photos/jeetsukumaran/sets/
--------------------------------------

Yan Wong

unread,
Sep 17, 2015, 6:11:37 AM9/17/15
to DendroPy Users
On Wednesday, 16 September 2015 22:57:19 UTC+1, Jeet Sukumaran wrote:
Hi Yan,

First of all, you did not miss anything in the documentation. There is
currently no pre-rolled method to do this.

Secondly, the approach you describe below is how I would go about doing
it, but it can be done more simply:

        subtree = dendropy.Tree(seed_node=node_of_interest)

What a good idea. Thanks. 

On the topic of subtrees, would it be sensible for prune_subtree() to return the top node of the subtree just pruned? Then it can be used to copy / paste parts of the tree, e.g. clipboard = tree.prune_subtree(mynode); other_node.add_child(clipboard).

Jeet Sukumaran

unread,
Sep 17, 2015, 1:28:06 PM9/17/15
to dendrop...@googlegroups.com
In the example you give, could you not just:

tree.prune_subtree(mynode)
other_node.add_child(mynode)





> On the topic of subtrees, would it be sensible for prune_subtree() to
> return the top node of the subtree just pruned? Then it can be used to
> copy / paste parts of the tree, e.g. clipboard =
> tree.prune_subtree(mynode); other_node.add_child(clipboard).
>


Reply all
Reply to author
Forward
0 new messages