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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.