Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Extend the leaf branch lengths of a Newick tree so all the paths are the same length
from Bio import Phylo
from io import StringIO
import sys
treedata = sys.argv[1]
handle = StringIO(treedata)
tree =, "newick")
out = StringIO()
def path_length(tree, node):
return sum(y.branch_length if y.branch_length else 0 for y in tree.get_path(node))
max_length = max([path_length(tree, x) for x in tree.get_terminals()])
for leaf in tree.get_terminals():
q = path_length(tree, leaf)
leaf.branch_length += max_length - q
Phylo.write([tree], out, "newick")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.