def _build_tree(self, tree_file, tree_format): if tree_format == 'newick_string': self.tree_str = tree_file return ete3.Tree(self.tree_str, format=1) elif tree_format == 'newick': with open(tree_file, 'r') as nwk_file: self.tree_str = nwk_file.read() return ete3.Tree(self.tree_str, format=1) elif tree_format == 'phyloxml': from ete3 import Phyloxml project = Phyloxml() project.build_from_file(tree_file) self.tree_str = None tree = project.get_phylogeny()[0] for node in tree.traverse(): # assign name to extant species if node.is_leaf(): node.name = self._get_name_phyloxml( node, self.phyloxml_leaf_name_tag) # assign name to ancestral species elif self.use_internal_name: node.name = self._get_name_phyloxml( node, self.phyloxml_internal_name_tag) return tree
def getTreeFromPhyloxml(xml, saveToFile="default.xml", delFile=True): """ Read a phylogeny tree from a phyloxml string and return a TreeClass object or a list of TreeClass object """ project = Phyloxml() fo = open(saveToFile, "w+") fo.write(xml) fo.close() project.build_from_file(saveToFile) treeList = [] for tree in project.get_phylogeny(): treeList.append(TreeClass.import_from_PhyloxmlTree(tree)) if (delFile): os.remove(saveToFile) if len(treeList) == 1: return treeList[0] return treeList
def getTreeFromPhyloxml(xml, saveToFile="default.xml", delFile=True): """ Read a phylogeny tree from a phyloxml string and return a TreeClass object or a list of TreeClass object """ project = Phyloxml() fo = open(saveToFile, "w+") fo.write(xml) fo.close() project.build_from_file(saveToFile) treeList = [] for tree in project.get_phylogeny(): treeList.append(TreeClass.import_from_PhyloxmlTree(tree)) if(delFile): os.remove(saveToFile) if len(treeList) == 1: return treeList[0] return treeList
def get_ete_tree(filename): from ete3 import Tree, Phyloxml p = Phyloxml() p.build_from_file(filename) t = p.get_phylogeny()[0] return t
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"> # <phy:phylogeny>
from ete3 import Phyloxml project = Phyloxml() project.build_from_file("apaf.xml") # Each tree contains the same methods as a PhyloTree object for tree in project.get_phylogeny(): print tree # you can even use rendering options tree.show() # PhyloXML features are stored in the phyloxml_clade attribute for node in tree: print "Node name:", node.name for seq in node.phyloxml_clade.get_sequence(): for domain in seq.domain_architecture.get_domain(): domain_data = [domain.valueOf_, domain.get_from(), domain.get_to()] print " Domain:", '\t'.join(map(str, domain_data))
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">