def new_clade(self, parent=None, id=None): """Returns a new Newick.Clade, optionally with a temporary reference to its parent clade.""" try: parentPath = parent.find("property[@ref='Lm:path']").text except: # doesn't have a path, means it's the root ? pass if parent is not None: clade = SubElement(parent, "clade", namespace=None) if id is not None: SubElement(clade, "property", value=id, namespace=None, attribs=[('ref', 'Lm:pathId'), ('datatype', 'xsd:integer'), ('applies_to', 'clade')]) path = str(id) + ',' + parentPath SubElement(clade, "property", value=path, namespace=None, attribs=[('ref', 'Lm:path'), ('datatype', 'xsd:string'), ('applies_to', 'clade')]) self.parentMap[clade] = parent #if path is not None: #SubElement(cladeEl, "path", value=path, namespace=None) #SubElement(cladeEl, "id", value=id, namespace=None) #return cladeEl else: clade = Element("clade", namespace=None) # new lines for LmXml SubElement(clade, "property", value=id, namespace=None, attrib={'ref': 'Lm:pathId', # 'datatype': 'xsd:integer', # 'applies_to': 'clade'}) SubElement(clade, "property", value=id, namespace=None, attribs=[('ref', 'Lm:pathId'), ('datatype', 'xsd:integer'), ('applies_to', 'clade')]) SubElement(clade, "property", value=id, namespace=None, attribs=[('ref', 'Lm:path'), ('datatype', 'xsd:string'), ('applies_to', 'clade')]) #clade = Newick.Clade() #clade = None #if parent: # clade.parent = parent return clade
def processNexusForPhyloXml(nexStr): if nexStr.startswith('TREE'): parts = nexStr.strip('TREE ').split(' = ') name = parts[0] phylo = parts[1] print "name ", name t = Element( "phyloxml", namespace=None, attribs= [("xmlns", "http://www.phyloxml.org"), ("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"), ("xsi:schemaLocation", "http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd" )]) phylogenyEl = SubElement(t, "phylogeny", namespace=None, attribs=[('rooted', 'true')]) SubElement(phylogenyEl, 'name', value=name, namespace=None) xmlify(phylogenyEl, phylo.strip('[&R] ')) return t
def createClade(parent, depth, name=None): cladeEl = SubElement(parent, "clade", namespace=None) SubElement(cladeEl, "branch_length", value=depth, namespace=None) if name is not None: SubElement(cladeEl, "name", value=name, namespace=None) return cladeEl
phylogenyEl = SubElement(t, "phylogeny", namespace=None, attribs=[('rooted', 'true')]) SubElement(phylogenyEl, 'name', value=name, namespace=None) xmlify(phylogenyEl, phylo.strip('[&R] ')) return t #outStr = tostring(processNexusForPhyloXml(nexusTree)) t = open('/home/jcavner/PhyloXM_Examples/amphibiansFromNexus.nhx','r').read() t = nexusTreeMod sh = Parser.from_string(t) #nexusTreeMod- for bats, works p = Parser(sh) eTree,phyloDict,parentDicts = p.parse() t = Element("phyloxml", namespace=None, attribs=[ ("xmlns", "http://www.phyloxml.org"), ("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"), ("xsi:schemaLocation", "http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd") ]) phylo = SubElement(t, "phylogeny", namespace=None, attribs=[('rooted', 'false')]) phylo.append(eTree) outStr = tostring(t) with open('/home/jcavner/PhyloXM_Examples/nexusTreeMod.xml', 'w') as f: f.write(outStr) with open('/home/jcavner/PhyloXM_Examples/amphibianJSONDirectTest.json','w') as f: f.write(simplejson.dumps(phyloDict,sort_keys=True, indent=4))
def add_name(self, clade, name): SubElement(clade, "name", name, namespace=None)
#outStr = tostring(processNexusForPhyloXml(nexusTree)) t = open('/home/jcavner/PhyloXM_Examples/amphibiansFromNexus.nhx', 'r').read() t = nexusTreeMod sh = Parser.from_string(t) #nexusTreeMod- for bats, works p = Parser(sh) eTree, phyloDict, parentDicts = p.parse() t = Element( "phyloxml", namespace=None, attribs=[ ("xmlns", "http://www.phyloxml.org"), ("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"), ("xsi:schemaLocation", "http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd") ]) phylo = SubElement(t, "phylogeny", namespace=None, attribs=[('rooted', 'false')]) phylo.append(eTree) outStr = tostring(t) with open('/home/jcavner/PhyloXM_Examples/nexusTreeMod.xml', 'w') as f: f.write(outStr) with open('/home/jcavner/PhyloXM_Examples/amphibianJSONDirectTest.json', 'w') as f: f.write(simplejson.dumps(phyloDict, sort_keys=True, indent=4))