def parse(self): """Parse the input elements.""" def _parse_pathway(attrib): for k, v in attrib.items(): self.pathway.__setattr__(k, v) def _parse_entry(element): new_entry = Entry() for k, v in element.attrib.items(): new_entry.__setattr__(k, v) for subelement in element.getchildren(): if subelement.tag == 'graphics': _parse_graphics(subelement, new_entry) elif subelement.tag == 'component': _parse_component(subelement, new_entry) self.pathway.add_entry(new_entry) def _parse_graphics(element, entry): new_graphics = Graphics(entry) for k, v in element.attrib.items(): new_graphics.__setattr__(k, v) entry.add_graphics(new_graphics) def _parse_component(element, entry): new_component = Component(entry) for k, v in element.attrib.items(): new_component.__setattr__(k, v) entry.add_component(new_component) def _parse_reaction(element): new_reaction = Reaction() for k, v in element.attrib.items(): new_reaction.__setattr__(k, v) for subelement in element.getchildren(): if subelement.tag == 'substrate': new_reaction.add_substrate(int(subelement.attrib['id'])) elif subelement.tag == 'product': new_reaction.add_product(int(subelement.attrib['id'])) self.pathway.add_reaction(new_reaction) def _parse_relation(element): new_relation = Relation() new_relation.entry1 = int(element.attrib['entry1']) new_relation.entry2 = int(element.attrib['entry2']) new_relation.type = element.attrib['type'] for subtype in element.getchildren(): name, value = subtype.attrib['name'], subtype.attrib['value'] if name in ('compound', 'hidden compound'): new_relation.subtypes.append((name, int(value))) else: new_relation.subtypes.append((name, value)) self.pathway.add_relation(new_relation) # ========== # Initialize Pathway self.pathway = Pathway() # Get information about the pathway itself _parse_pathway(self.entry.attrib) for element in self.entry.getchildren(): if element.tag == 'entry': _parse_entry(element) elif element.tag == 'reaction': _parse_reaction(element) elif element.tag == 'relation': _parse_relation(element) # Parsing of some elements not implemented - no examples yet else: # This should warn us of any unimplemented tags import warnings from Bio import BiopythonParserWarning warnings.warn( "Warning: tag %s not implemented in parser" % element.tag, BiopythonParserWarning) return self.pathway
def parse(self): """Parse the input elements.""" def _parse_pathway(attrib): for k, v in attrib.items(): self.pathway.__setattr__(k, v) def _parse_entry(element): new_entry = Entry() for k, v in element.attrib.items(): new_entry.__setattr__(k, v) for subelement in element: if subelement.tag == "graphics": _parse_graphics(subelement, new_entry) elif subelement.tag == "component": _parse_component(subelement, new_entry) self.pathway.add_entry(new_entry) def _parse_graphics(element, entry): new_graphics = Graphics(entry) for k, v in element.attrib.items(): new_graphics.__setattr__(k, v) entry.add_graphics(new_graphics) def _parse_component(element, entry): new_component = Component(entry) for k, v in element.attrib.items(): new_component.__setattr__(k, v) entry.add_component(new_component) def _parse_reaction(element): new_reaction = Reaction() for k, v in element.attrib.items(): new_reaction.__setattr__(k, v) for subelement in element: if subelement.tag == "substrate": new_reaction.add_substrate(int(subelement.attrib["id"])) elif subelement.tag == "product": new_reaction.add_product(int(subelement.attrib["id"])) self.pathway.add_reaction(new_reaction) def _parse_relation(element): new_relation = Relation() new_relation.entry1 = int(element.attrib["entry1"]) new_relation.entry2 = int(element.attrib["entry2"]) new_relation.type = element.attrib["type"] for subtype in element: name, value = subtype.attrib["name"], subtype.attrib["value"] if name in ("compound", "hidden compound"): new_relation.subtypes.append((name, int(value))) else: new_relation.subtypes.append((name, value)) self.pathway.add_relation(new_relation) # ========== # Initialize Pathway self.pathway = Pathway() # Get information about the pathway itself _parse_pathway(self.entry.attrib) for element in self.entry: if element.tag == "entry": _parse_entry(element) elif element.tag == "reaction": _parse_reaction(element) elif element.tag == "relation": _parse_relation(element) # Parsing of some elements not implemented - no examples yet else: # This should warn us of any unimplemented tags import warnings from Bio import BiopythonParserWarning warnings.warn( "Warning: tag %s not implemented in parser" % element.tag, BiopythonParserWarning, ) return self.pathway