ElementTree and SBML
I'll start this by going to my "Useful and New Modules" talk [key.tgz| PPT| PDF], which has a section on using ElementTree to parse XML files. ElementTree is a pythonic XML parser. The word "pythonic" is like the word "beautiful". It's hard to quantify and is mostly a matter of personal esthetics. The general idea is pythonic APIs should feel like other pythonic APIs; experience should help guide you in how to use the API. Like art, it's hard to train. There are several pythonic XML parsers besides ElementTree. ET was the most widely used of these and is now part of Python 2.5.
I talked about ElementTree a few months ago and last year so I'll not go into details here. Instead I'll show how to use ElementTree to parse an SBML file I got from Brett.
I'll start by parsing the file into an ElementTree object. The tree has a method called "getroot()" which returns the root node of the document.
>>> from xml.etree import ElementTree >>> ElementTree.parse(open("BIOMD0000000023.xml")) <xml.etree.ElementTree.ElementTree instance at 0x585aa8> >>> tree = _ >>> tree.getroot() <Element {}sbml at 595490> >>>Each node in the tree has a few properties. "tag" is the element tag, in Clark notation. (That's the {curly braces} part, containing the namespace.) "attrib" is a dictionary containing the attributes of the start element. It's the empty dictionary if there are no attributes.
>>> root = tree.getroot() >>> root.tag '{}sbml' >>> root.attrib {'version': '1', 'metaid': 'metaid_0000001', 'level': '2'} >>>
An SBML file contains models. I can iterate over models in a couple ways. If the root node only has models then I iterate over all the children of the root. ElementTree maps this to Pythons list and iteration protocols, so the following work:
>>> len(root) 1 >>> for node in root: ... print node.tag ... {}model >>> root[0] <Element {}model at 595468> >>> del root[0] >>> len(root) 0 >>>Of course as I deleted the root I'll need to reload it. The other way to get the children elements is to "find" it. This is a method on every node (and the tree) which implements a small subset of the XPath language
>>> root = ElementTree.parse(open("BIOMD0000000023.xml")).getroot() >>> root.find("{}model") <Element {}model at 739e18> >>> root.findall("{}model") [<Element {}model at 739e18>] >>>That 'findall' wasn't very interesting. Here I'll get the species element
>>> root.findall("*/*/{}species") [<Element {}species at 741238>, <Element {}species at 7415f8>, <Element {}species at 741918>, <Element {}species at 741e68>, <Element {}species at 10081c0>, <Element {}species at 1008580>, <Element {}species at 1008828>, <Element {}species at 1008af8>, <Element {}species at 1008e18>, <Element {}species at 100f170>, <Element {}species at 100f490>, <Element {}species at 100f7b0>, <Element {}species at 100fad0>] >>> for ele in root.findall("*/*/{}species"): ... print "%s has initial concentration %s" % (ele.attrib["id"], ... ele.attrib["initialConcentration"]) ... Fru has initial concentration 1 Glc has initial concentration 1 HexP has initial concentration 1 Suc6P has initial concentration 1 Suc has initial concentration 1 Sucvac has initial concentration 0 glycolysis has initial concentration 0 phos has initial concentration 5.1 UDP has initial concentration 0.2 ADP has initial concentration 0.2 ATP has initial concentration 1 Glcex has initial concentration 5 Fruex has initial concentration 5 >>>I prefer using the correct path rather than the wildcard "*"
>>> root.find("{}model/" ... "{}listOfSpecies/" ... "{}species" ) <Element {}species at 741238> >>> len(root.findall( ... "{}model/" ... "{}listOfSpecies/" ... "{}species")) 13 >>>
Another option is to use the getiterator method, which returns all elements which match the given tag.
>>> len(root.getiterator("{}species")) 13 >>>For a more complicated example, the kineticLaw section has an embedded MathML element which looks like this
<math xmlns=""> <apply> <times/> <ci> compartment </ci> <ci> compartment </ci> <apply> <divide/> <apply> <times/> <ci> Vmax11 </ci> <ci> Suc </ci> </apply> <apply> <plus/> <ci> Km11Suc </ci> <ci> Suc </ci> </apply> </apply> </apply> </math>Here's a bit of code to get the text of the "ci" elements, uniquely. It uses the generator comprehension new to Python 2.5 and the set object added a few releases ago.
>>> set(ele.text for ele in root.getiterator("{}ci")) set([' Keq6 ', ' Vmax8r ', ' Km4Fru ', ' ADP ', ' Ki8UDP ', ' Km6UDP ', ' Km9Suc ', ' Vmax2 ', ' Km10F6P ', ' Ki8Fru ', ' Km5Fru ', ' Km3Glc ', ' Vmax9 ', ' Km11Suc ', ' Km8Fru ', ' Km1Fruex ', ' Ki1Fru ', ' UDP ', ' Glcex ', ' Vmax4 ', ' Vmax11 ', ' Km6F6P ', ' compartment ', ' Vmax1 ', ' Ki6Pi ', ' phos ', ' ATP ', ' Ki3G6P ', ' Km7Suc6P ', ' Ki5ADP ', ' Km3ATP ', ' Km8Suc ', ' Km6Suc6P ', ' Vmax5 ', ' Km8UDP ', ' Ki4F6P ', ' Km5ATP ', ' Vmax7 ', ' Suc ', ' Km2Glcex ', ' Vmax3 ', ' Vmax6r ', ' Vmax8f ', ' Km6UDPGlc ', ' Glc ', ' Ki5Fru ', ' Ki9Glc ', ' Fru ', ' Fruex ', ' Vmax6f ', ' Ki2Glc ', ' Keq8 ', ' Suc6P ', ' Ki9Fru ', ' Ki8Suc ', ' Ki6UDPGlc ', ' HexP ', ' Vmax10 ', ' Km8UDPGlc ', ' Km4ATP ', ' Ki6F6P ', ' Ki6Suc6P ']) >>>
For the last example, I'll export the notes body as XHTML. First I'll make a new Element containing the XHTML "html" element. and put in a head and title
>>> html = ElementTree.Element("{}html") >>> head = ElementTree.SubElement(html, "{}head") >>> title = ElementTree.SubElement(head, "{}title") >>> title.>> head.>>Next I'll get the body element from the existing root element and stick it into the new HTML document. The 'tail = "\n"' above and below are to make the output look nice.
>>> body = root.getiterator("{}body")[0] >>> body <Element {}body at 739f08> >>> html.append(body) >>> import sys >>> html.>>The last step is to write the new html element inside of an ElementTree and write the results to stdout.
>>> tree = ElementTree.ElementTree(html) >>> tree.write(sys.stdout) <html:html xmlns:<html:head><html:title>This is a note</html:title></html:head> <html:body> <html:p> <html:font <html:b> <html:aSBML</html:a> level 2 code generated for the JWS Online project by Jacky Snoep using <html:aPySCeS</html:a> <html:br /> Run this model online at <html:a</html:a> <html:br /> To cite JWS Online please refer to: Olivier, B.G. and Snoep, J.L. (2004) <html:aWeb-based modelling using JWS Online</html:a>, Bioinformatics, 20:2143-2144 </html:b> </html:font> </html:p> </html:body> </html:html> >>>
