Skip to content

Instantly share code, notes, and snippets.

Last active September 14, 2017 08:25
What would you like to do?
from Bio import Phylo
I want to get a dictionary where the keys are every leaf name
and the value is the parental (internal) node of that leaf
tree =, 'newick')
res_dict = {}
for node in tree.find_clades():
## if the node is a leaf, the name will be in
## if the node is internal, the name will be node.confidence
print, node.confidence
## iterate through the descendet clades (should only be 2)
for c in node.clades:
## if one of them is a leaf aka terminal
if c.is_terminal():
## print leaf name and parent node
print c, node.confidence
assert c not in res_dict
res_dict[c] = node.confidence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment