from ete3 import Phyloxml, phyloxml import random project = Phyloxml() # Creates a random tree phylo = phyloxml.PhyloxmlTree() phylo.populate(5, random_branches=True) phylo.phyloxml_phylogeny.set_name("test_tree") # Add the tree to the phyloxml project project.add_phylogeny(phylo) print project.get_phylogeny()[0] # /-iajom # /---| # | \-wiszh #----| # | /-xrygw # \---| # | /-gjlwx # \---| # \-ijvnk # Trees can be operated as normal ETE trees phylo.show() # Export the project as phyloXML format project.export() # <phy:Phyloxml xmlns:phy="http://www.phyloxml.org/1.10/phyloxml.xsd"> # <phy:phylogeny>
def _generate_internal_node_name(self, tree): if self.use_internal_name is False: for node in tree.traverse("postorder"): if node.is_leaf() is False: self.set_taxon_name(node) if self.tree_format == 'phyloxml': #### IMPORTANT #### ''' This code section is an horrible fix due to incompatibility of Phyloxml().export() with python3. We overwrite the export module (and its functions) here to deal with both byte and unicode. TODO: this should be replace as ASAP ''' def showIndent(outfile, level): for idx in range(level): g = ' ' g = g.encode('UTF-8') outfile.write(g) namespace_ = 'phy:' name_ = 'Phyloxml' namespacedef_ = '' outfile = BytesIO() level = 0 def export(xxxx, outfile, level, namespace_='phy:', name_='Phyloxml', namespacedef_=''): showIndent(outfile, level) x = '<%s%s%s' % ( namespace_, name_, namespacedef_ and ' ' + namespacedef_ or '', ) x = x.encode('UTF-8') outfile.write(x) already_processed = [] exportAttributes(xxxx, outfile, level, already_processed, namespace_, name_='Phyloxml') if hasContent_(xxxx): y = '>\n' y = y.encode('UTF-8') outfile.write(y) exportChildren(xxxx, outfile, level + 1, namespace_, name_) showIndent(outfile, level) z = '</%s%s>\n' % (namespace_, name_) z = z.encode('UTF-8') outfile.write(z) else: v = '/>\n' v = v.encode('UTF-8') outfile.write(v) def exportAttributes(xxxx, outfile, level, already_processed, namespace_='phy:', name_='Phyloxml'): pass def exportChildren(xxxx, outfile, level, namespace_='phy:', name_='Phyloxml', fromsubclass_=False): for phylogeny_ in xxxx.phylogeny: export(phylogeny_, outfile, level, namespace_, name_='phylogeny') def hasContent_(xxxx): if hasattr(xxxx, 'phylogeny'): return True else: return False #### IMPORTANT #### # build phyloxml project project = Phyloxml() project.add_phylogeny(tree) # Export phyloxml document export(project, outfile, level, namespace_='phy:', name_='Phyloxml', namespacedef_='') OUTPUT = outfile # Some ad-hoc changes to the phyloxml formatted document to meet the schema definition text = OUTPUT.read().decode('UTF-8') text = text.replace('phy:', '') text = re.sub('branch_length_attr="[^"]+"', "", text) header = """ <phyloxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.phyloxml.org" xsi:schemaLocation="http://www.phyloxml.org http://www.phyloxml.org/1.20/phyloxml.xsd"> """ text = re.sub('<Phyloxml[^>]+>', header, text) text = text.replace('Phyloxml', 'phyloxml') text = text.replace('\n', '').replace("'", '') self.tree_str = text else: self.tree_str = self.tree.write(format=8, format_root_node=True)
from ete3 import Phyloxml, phyloxml import random project = Phyloxml() # Creates a random tree phylo = phyloxml.PhyloxmlTree() phylo.populate(5, random_branches=True) phylo.phyloxml_phylogeny.set_name("test_tree") # Add the tree to the phyloxml project project.add_phylogeny(phylo) print project.get_phylogeny()[0] # /-iajom # /---| # | \-wiszh #----| # | /-xrygw # \---| # | /-gjlwx # \---| # \-ijvnk # Trees can be operated as normal ETE trees phylo.show() # Export the project as phyloXML format project.export() # <phy:Phyloxml xmlns:phy="http://www.phyloxml.org/1.10/phyloxml.xsd">