Skip to content

Instantly share code, notes, and snippets.

@radaniba
Created February 14, 2013 17:47
Show Gist options
  • Save radaniba/4954622 to your computer and use it in GitHub Desktop.
Save radaniba/4954622 to your computer and use it in GitHub Desktop.
Ete2 is a mighty useful toolkit for working with phylogenies. Now, one of the problems of phylogenetic data is the variety of formats it can come in, some programs needing one rather than the other. Ete2 is fairly good in this regard is that it can read and write Newick and PhyloXML (and NeXML). However, it cannot inter-convert between the two, …
import ete2
def tree_to_phyloxml (ete_tree):
"""
Convert an Ete2 tree to PhyloXML.
:Parameters:
ete_tree
An Ete2 format tree
:Returns:
PhyloXML markup as text
"""
from cStringIO import StringIO
buffer = StringIO()
def visit_node (node, buf, indent=0):
buf.write (" " * indent)
buf.write ("<phy:clade>\n")
buf.write (" " * (indent+1))
buf.write ("<phy:name>%s</phy:name>\n" % node.name )
buf.write (" " * (indent+1))
buf.write ("<phy:branch_length>%s</phy:branch_length>\n" % node.dist)
buf.write (" " * (indent+1))
buf.write ("<phy:confidence type='branch_support'>%s</phy:confidence>\n" % node.support)
for c in node.get_children():
visit_node (c, buf, indent=indent+1)
buf.write (" " * indent)
buf.write ("</phy:clade>\n")
buffer.write ("<phy:Phyloxml xmlns:phy='http://www.phyloxml.org/1.10/phyloxml.xsd'>\n")
buffer.write ("<phy:phylogeny>\n")
buffer.write ("<phy:name>test_tree</phy:name>\n")
visit_node (ete_tree.get_tree_root(), buffer)
buffer.write ("</phy:phylogeny>\n")
buffer.write ("</phy:Phyloxml>\n")
return buffer.getvalue()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment