Find immediate descendantsof root node of phylogenetic tree in Python or R
1
0
Entering edit mode
5.7 years ago

Hello.

I am seeking a way to find the immediate descendants of the root node in a phylogenetic tree such as with the function

get.desc.of.node()

from auteur or geiger libraries in R. However, this function doesn't seem to exist anymore, and I have struggled and failed to find ways to use the function from earlier versions.

For the immediate descendants, I eventually need to get the names of all the terminals in the two or more descendant clades. However, I can work up to that from something else, such as the node numbers, a list of all descendant node numbers on either side of the root, or something else.

I can think of plenty of rather forced ways to get what I want, such as finding the smallest node number in R, and then the next smallest two. This would work for bifurcating trees most of the time, though I am a little nervous about node numbers in R and worry that the smallest would not always be the root. Moreover, it would not work for trees that are not bifurcating at the root, unless I already knew how many nodes to expect.

I could also get a list of terminals in all clades using biopython and try to use the terminals within the largest clade to find its sister, where those terminals (and only those) should be missing. However that would not work if the tree is not bifurcating from the root.

I would like a way that's not so forced and more easily applicable to a wide breath of tree types.

Any help is appreciated, and thanks in advance.

R python phylogeny Geiger • 2.8k views
ADD COMMENT
1
Entering edit mode

Can't speak for R (though I imagine ape or phangorn will cut it), but you should be able to do this with dendropy in python fairly easily.

Finding the root node first should be fairly trivial, then you can feed that information to a descendent finding function easily enough I would imagine. I'll look in to some code, but that'll probably get you going in the mean time..

ADD REPLY
0
Entering edit mode

Thanks a lot. I agree that finding the root -should- be fairly easy. I've bveen invested in R for the last few hours. Maybe I'll put that on hold and try dendropy.

ADD REPLY
1
Entering edit mode

It's getting late here so I don't have a full code solution for you but it seems with dendropy its as simple as reading a tree in then accessing the 'seed node'

e.g.

from dendropy import Tree
import sys

tree = Tree.get(file=open(sys.argv[1], 'r'), schema="newick")    
print(tree.seed_node)

The .seed_node object has a lot of methods and data associated with it, so there's bound to be something in there you can use: http://dendropy.org/prerelease/library/treemodel.html?highlight=seed_node

There will be methods for accessing the child nodes.

ADD REPLY
1
Entering edit mode

Thanks again. I was also just playing around with seed_node. I can probably make that work. Your help in pointing me to dendropy is appreciated.

ADD REPLY
0
Entering edit mode
5.7 years ago

Looks like the following solution works:

import dendropy
trees = dendropy.TreeList.get(path="file.newick", schema="newick")
for tree in trees:
    rt = tree.seed_node
    deep_nds = rt.adjacent_nodes()

For each tree, this returns a list of nodes adjacent to the root. The list contains only child nodes. Notably, it would also contain parent nodes if adjacent_nodes() was called on any node other than the root (or seed_node).

The objects in the list are of an undesirable type, but they can be transformed.

Hallelujah, Python! And thanks so much to jrj.healey.

ADD COMMENT
0
Entering edit mode

That’s good, I did spot adjacent_nodes but didn’t finish playing with it ;p

ADD REPLY

Login before adding your answer.

Traffic: 2911 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6