Skip to content

Instantly share code, notes, and snippets.

@mtholder
Last active May 17, 2016 19:10
Show Gist options
  • Select an option

  • Save mtholder/8c5a4079d04a0129c652e431575add01 to your computer and use it in GitHub Desktop.

Select an option

Save mtholder/8c5a4079d04a0129c652e431575add01 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import dendropy
def get_tree(r):
tree_str = "((C:1.3,D:4.)34:0.034,(A:1.1,(B:1.2,X:1.6)26:0.026)12:0.0126,E:1.5);"
tree = dendropy.Tree.get(
data=tree_str,
schema="newick",
rooting=r,
)
return tree
def node_label_method():
tree = get_tree("force-rooted")
outgroup_node = tree.find_node_with_taxon_label("X")
new_root = outgroup_node.parent_node
tree.reseed_at(new_root)
return tree
def rooted_bipartition_method(r):
tree = get_tree(r)
benc = tree.encode_bipartitions()
support_values = {}
for nd in tree:
support_values[nd.bipartition] = float(nd.label) if nd.label is not None else 1.0
outgroup_node = tree.find_node_with_taxon_label("X")
new_root = outgroup_node.parent_node
tree.reseed_at(new_root)
tree.encode_bipartitions()
for nd in tree:
nd.label = support_values.get(nd.bipartition, "Not_specified_in_original_tree")
tree.seed_node.edge.length = None
return tree
print("\n[Original Tree:]\n ")
print(get_tree("force-rooted").as_string("newick").strip())
print("\n[Labels-attached-to-nodes Tree:]\n ")
print(node_label_method().as_string("newick").strip())
print("\n[Labels-attached-to-unrooted-bipartition-method Tree:]\n ")
print(rooted_bipartition_method("force-unrooted").as_string("newick").strip())
print("\n[labels-attached-to-rooted-bipartition-method Tree:]\n")
print(rooted_bipartition_method("force-rooted").as_string("newick").strip())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment