ete icon indicating copy to clipboard operation
ete copied to clipboard

How can we get leaf and non-leaf node from NCBITaxa?

Open YojanaGadiya opened this issue 1 year ago • 1 comments

Dear @jhcepas,

I want to list the terminal and non-terminal nodes for the NCBITaxa tree. Since the tree is an independent object and not part of the PhyloTree class, there is no function to do so. Please can you direct me to an example on where or how this can be done?

Thank You.

YojanaGadiya avatar Jul 28 '22 11:07 YojanaGadiya

Tree objects have the .get_leaves(), .iter_leaves(), .get_leaf_names(), .iter_leaf_names() methods:

tree = ete3.Tree('(a,b)x,c)r;', format=1)
tree.get_leaf_names()
>>> ['a', 'b', 'c']

tree.get_leaves()
>>> [Tree node 'a' (0x7f54c416a14), Tree node 'b' (0x7f54c416900), Tree node 'c' (0x7f54c416a71)]

To list the internal nodes, iterate all nodes and test if they are (not) a leaf:

for node in tree.traverse():
    if not node.is_leaf():
        print(node)

In case of NCBITaxa module, you may be interested in species nodes, but not subspecies, strains, etc. Use the rank node attribute:

taxatree.get_leaves(is_leaf_fn=lambda node: node.rank == 'species')

Gullumluvl avatar Apr 17 '23 20:04 Gullumluvl