class BiopaxLevel2Handler(handler.ContentHandler): """ Class to handle content in Biopax Level2 XML files """ def _identity(a): return a def _entrez_funct(a): return str(a).replace("gi|", "") datatype_operations = { "uniprot": _identity, "ncbi_taxonomy": _identity, "tigr": _identity, "reactome": _identity, "chebi": _identity, "go": _identity, "pubchem compound": _identity, "glycan": _identity, "compound": _identity, "entrez": _entrez_funct } biopax_objects_dict = { "bp:unificationxref": BiopaxXREF, "bp:relationshipxref": BiopaxXREF, "bp:publicationxref": BiopaxXREF, "bp:opencontrolledvocabulary": BiopaxOpenControlledVocabulary, "bp:biosource": BiopaxBioSource, "bp:protein": BiopaxProtein, "bp:complex": BiopaxComplex, "bp:dna": BiopaxDNA, "bp:rna": BiopaxRNA, "bp:smallmolecule": BiopaxSmallMolecule, "bp:physicalentity": BiopaxSmallMolecule, "bp:pathway": BiopaxPathway, "bp:interaction": BiopaxInteraction, "bp:physcialinteraction": BiopaxPhysicalInteraction, "bp:conversion": BiopaxConversion, "bp:control": BiopaxControl, "bp:biochemicalreaction": BiopaxBiochemicalReaction, "bp:complexassembly": BiopaxComplexAssembly, "bp:transport": BiopaxTransport, "bp:catalysis": BiopaxCatalysis, "bp:modulation": BiopaxModulation, "bp:sequenceparticipant": BiopaxPhysicalEntityParticipant, "bp:physicalentityparticipant": BiopaxPhysicalEntityParticipant, "bp:pathwaystep": BiopaxPathwayStep } #"bp:transportwithbiochemicalreaction":, # "bp:physicalentityparticipant":, # "bp:proteinparticipant":, # "bp:complexparticipant":, # "bp:rnaparticipant":, # "bp:dnaparticipant":, # "bp:smallmoleculeparticipant": } def get_biana_data_type(self, type): return BiopaxLevel2Parser.BiopaxLevel2Handler.datatype_to_biana_type[ type.lower()] def __init__(self): print "initalizing BiopaxLevel2Handler" self.current_XMLNode = None self.step = 0 self.xmlnode_hierarchylist = [] self.biopaxElements = {} BiopaxEntity.resources = self.biopaxElements BiopaxEntity.controlled_relations = {} handler.ContentHandler.__init__(self) def _get_cross_ref(self, xref_id): if xref_id[0] == '#': xref_id = xref_id[1:] try: return self.unification_xrefs[xref_id] except: return self._get_cross_ref( xref_id=self.recursive_xref[xref_id]) def _get_link(self, xref_id): if xref_id[0] == '#': xref_id = xref_id[1:] return self.links[xref_id] # ContentHandler methods def startDocument(self): return def endDocument(self): self.step += 1 def startElement(self, name, attrs): if self.current_XMLNode is None: self.current_XMLNode = XMLNode(name=name, attrs=attrs) else: t = XMLNode(name=name, attrs=attrs) self.current_XMLNode.addChild(t) self.xmlnode_hierarchylist.append(self.current_XMLNode) self.current_XMLNode = t def endElement(self, name): name = name.lower() if name != self.current_XMLNode.name.lower(): raise ValueError("ERROR IN XML FILE") if BiopaxLevel2Parser.BiopaxLevel2Handler.biopax_objects_dict.has_key( name): self.biopaxElements['#' + self.current_XMLNode.attrs[ "rdf:ID"]] = BiopaxLevel2Parser.BiopaxLevel2Handler.biopax_objects_dict[ name](self.current_XMLNode) # Sets the current object the parent if len(self.xmlnode_hierarchylist) > 0: self.current_XMLNode = self.xmlnode_hierarchylist.pop() else: self.current_XMLNode = None return def characters(self, text): #print text self.current_XMLNode.addValue( text.replace('.<', '<').replace('br>', '>').encode("ascii", "ignore")) #self.current_XMLNode.addValue(text.replace('.<','<').replace('br>','>').decode("ascii","ignore")) def toBiana(self): for current_element in self.biopaxElements.values(): current_element.toBiana()
class BiopaxLevel2Handler(handler.ContentHandler): """ Class to handle content in Biopax Level2 XML files """ def _identity(a): return a def _entrez_funct(a): return str(a).replace("gi|","") datatype_operations = { "uniprot": _identity, "ncbi_taxonomy": _identity, "tigr": _identity, "reactome": _identity, "chebi": _identity, "go": _identity, "pubchem compound" : _identity, "glycan" : _identity, "compound" : _identity, "entrez": _entrez_funct } biopax_objects_dict = { "bp:unificationxref": BiopaxXREF, "bp:relationshipxref": BiopaxXREF, "bp:publicationxref": BiopaxXREF, "bp:opencontrolledvocabulary": BiopaxOpenControlledVocabulary, "bp:biosource": BiopaxBioSource, "bp:protein": BiopaxProtein, "bp:complex": BiopaxComplex, "bp:dna": BiopaxDNA, "bp:rna": BiopaxRNA, "bp:smallmolecule": BiopaxSmallMolecule, "bp:physicalentity": BiopaxSmallMolecule, "bp:pathway": BiopaxPathway, "bp:interaction": BiopaxInteraction, "bp:physcialinteraction": BiopaxPhysicalInteraction, "bp:conversion": BiopaxConversion, "bp:control": BiopaxControl, "bp:biochemicalreaction": BiopaxBiochemicalReaction, "bp:complexassembly": BiopaxComplexAssembly, "bp:transport": BiopaxTransport, "bp:catalysis": BiopaxCatalysis, "bp:modulation": BiopaxModulation, "bp:sequenceparticipant": BiopaxPhysicalEntityParticipant, "bp:physicalentityparticipant": BiopaxPhysicalEntityParticipant, "bp:pathwaystep": BiopaxPathwayStep } #"bp:transportwithbiochemicalreaction":, # "bp:physicalentityparticipant":, # "bp:proteinparticipant":, # "bp:complexparticipant":, # "bp:rnaparticipant":, # "bp:dnaparticipant":, # "bp:smallmoleculeparticipant": } def get_biana_data_type(self, type): return BiopaxLevel2Parser.BiopaxLevel2Handler.datatype_to_biana_type[type.lower()] def __init__(self): print "initalizing BiopaxLevel2Handler" self.current_XMLNode = None self.step = 0 self.xmlnode_hierarchylist = [] self.biopaxElements = {} BiopaxEntity.resources = self.biopaxElements BiopaxEntity.controlled_relations = {} handler.ContentHandler.__init__(self) def _get_cross_ref(self, xref_id): if xref_id[0] == '#': xref_id = xref_id[1:] try: return self.unification_xrefs[xref_id] except: return self._get_cross_ref( xref_id = self.recursive_xref[xref_id] ) def _get_link(self, xref_id): if xref_id[0] == '#': xref_id = xref_id[1:] return self.links[ xref_id ] # ContentHandler methods def startDocument(self): return def endDocument(self): self.step += 1 def startElement(self, name, attrs): if self.current_XMLNode is None: self.current_XMLNode = XMLNode(name = name, attrs = attrs) else: t = XMLNode(name = name, attrs = attrs) self.current_XMLNode.addChild(t) self.xmlnode_hierarchylist.append(self.current_XMLNode) self.current_XMLNode = t def endElement(self, name): name = name.lower() if name!=self.current_XMLNode.name.lower(): raise ValueError("ERROR IN XML FILE") if BiopaxLevel2Parser.BiopaxLevel2Handler.biopax_objects_dict.has_key(name): self.biopaxElements['#'+self.current_XMLNode.attrs["rdf:ID"]] = BiopaxLevel2Parser.BiopaxLevel2Handler.biopax_objects_dict[name](self.current_XMLNode) # Sets the current object the parent if len(self.xmlnode_hierarchylist)>0: self.current_XMLNode = self.xmlnode_hierarchylist.pop() else: self.current_XMLNode = None return def characters(self, text): #print text self.current_XMLNode.addValue(text.replace('.<','<').replace('br>','>').encode("ascii","ignore")) #self.current_XMLNode.addValue(text.replace('.<','<').replace('br>','>').decode("ascii","ignore")) def toBiana(self): for current_element in self.biopaxElements.values(): current_element.toBiana()