Skip to content

Instantly share code, notes, and snippets.

@flashton2003
Last active September 14, 2017 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flashton2003/98e756da23293645ec3bd8cabfdf9997 to your computer and use it in GitHub Desktop.
Save flashton2003/98e756da23293645ec3bd8cabfdf9997 to your computer and use it in GitHub Desktop.
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 = Phylo.read(tree_handle, 'newick')
res_dict = {}
for node in tree.find_clades():
## if the node is a leaf, the name will be in node.name
## if the node is internal, the name will be node.confidence
print node.name, 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