Skip to content

Commit

Permalink
Merge pull request #39 from andrewjpage/reroot_around_edge
Browse files Browse the repository at this point in the history
Reroot around edge
  • Loading branch information
andrewjpage committed Jun 22, 2012
2 parents 52ad059 + 915c5c5 commit e4a0e86
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion run_gubbins.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,35 @@ def reroot_tree_with_outgroup(tree_name, outgroup):
tree.root_with_outgroup({'name': outgroup})
Phylo.write(tree, tree_name, 'newick')

def split_all_non_bi_nodes(node):
if node.is_leaf():
return None
elif len(node.child_nodes()) > 2:
split_child_nodes(node)

for child_node in node.child_nodes():
split_all_non_bi_nodes(child_node)

return None

def split_child_nodes(node):
all_child_nodes = node.child_nodes()
#skip over the first node
first_child = all_child_nodes.pop()
# create a placeholder node to hang everything else off
new_child_node = node.new_child(edge_length=0)
# move the subtree into the placeholder
new_child_node.set_child_nodes(all_child_nodes)
# probably not really nessisary
node.set_child_nodes((first_child,new_child_node))

def reroot_tree_at_midpoint(tree_name):
tree = dendropy.Tree.get_from_path(tree_name, 'newick')
tree.reroot_at_midpoint(update_splits=False, delete_outdegree_one=False)
split_all_non_bi_nodes(tree.seed_node)

tree.reroot_at_midpoint(update_splits=True, delete_outdegree_one=False)
tree.deroot()
tree.update_splits()
tree.write_to_path(
tree_name,
'newick',
Expand Down

0 comments on commit e4a0e86

Please sign in to comment.