How to do it...

Take a look at the following steps:

  1. First, let's load the RAxML-generated tree for all Ebola viruses, as follows:
import dendropyebola_raxml = dendropy.Tree.get_from_path('my_ebola.nex', 'nexus')
  1. Then, we need to compute the level of each node (the distance to the root node):
def compute_level(node, level=0):    for child in node.child_nodes():        compute_level(child, level + 1)    if node.taxon is not None:        print("%s: %d %d" % (node.taxon, node.level(), level))compute_level(ebola_raxml.seed_node)

DendroPy's node representation has a level method (which is used for comparison), but the point here is to introduce a recursive algorithm, so we will implement it anyway.

Note how the function works; it's called with the seed_node (which ...

Get Bioinformatics with Python Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.