Monday, January 08, 2007

A simple CML parser written in Python

Python is a powerful programming language. It can be easily extended by importing modules.
Below is a simple module for parsing CML file. As you can see, it's really easy to add the support for another tags.



import xml.sax.handler

class CMLHandler(xml.sax.handler.ContentHandler):
def __init__(self):
self.id = ""
self.formula = ""
self.inIdentifier = False
self.inchi = "InChI/1="
self.inInChI = False
self.inBasic = False
self.name = ""
self.inName = False
self.weight = ""
self.inWeight = False
self.mpt = ""
self.mptSet = False
self.inMpt = False
self.bpt = ""
self.inBpt = False
self.bptSet = False

def startElement(self, name, attributes):
if name == "molecule":
self.id = attributes["id"]

if name == "formula":
self.formula = attributes["concise"]

if name == "identifier":
self.inIdentifier = True
if attributes.has_key("version") and attributes["version"] == "InChI/1":
self.inInChI = True

if name == "basic":
self.inBasic = True

if name == "name":
self.inName = True

if name == "scalar":
if attributes["dictRef"] == "cml:molwt":
self.inWeight = True
elif attributes["dictRef"] == "cml:mpt":
self.inMpt = True
elif attributes["dictRef"] == "cml:bpt":
self.inBpt = True

def characters(self, data):
if self.inName:
self.name += data

if self.inWeight:
self.weight += data

if self.inMpt:
self.mpt += data

if self.inBpt:
self.bpt += data

if self.inBasic and self.inInChI:
self.inchi += data

def endElement(self,name):
if name == "identifier":
self.inIdenfitier = False

if name == "basic":
self.inBasic = False
if self.inInChI:
self.inInChI = False

if name == "name":
self.inName = False

if name == "inchi":
self.inInChI = False

if name == "scalar":
if self.inWeight:
self.inWeight = False
elif self.inMpt:
self.inMpt = False
self.mptSet = True
elif self.inBpt:
self.inBpt = False
self.bptSet = True

Thursday, June 15, 2006

CML, a powerful chemical file type

CML (Chemical Markup Language) is a chemistry-oriented file format, based on a markup language. It's capable of holding many informations, like chemical structures, chemical properties or reactions.

By using the right style sheet, it's possible to transform or to render easily the information. For example, it's used in the chemical-structures project to generate html datasheet of each compound.

Some papers relevant to this topic have been published.

Here is an example of molecule (D-Alanine) in cml format:
<?xml version="1.0"?>
<molecule xmlns="http://www.xml-cml.org/schema/cml2/core"
id="D-alanine">
<name convention="IUPAC">(2R)-2-Aminopropanoic acid</name>
<atomArray>
<atom id="a1" elementType="H" x3="-3.496" y3="1.763" z3="-2.499"/>
<atom id="a2" elementType="C" x3="-3.687" y3="0.754" z3="-2.120"/>
<atom id="a3" elementType="N" x3="-2.411" y3="0.067" z3="-1.892"/>
<atom id="a4" elementType="H" x3="-1.649" y3="0.745" z3="-1.865"/>
<atom id="a5" elementType="H" x3="-2.208" y3="-0.558" z3="-2.672"/>
<atom id="a6" elementType="C" x3="-4.450" y3="0.847" z3="-0.803"/>
<atom id="a7" elementType="H" x3="-5.398" y3="1.380" z3="-0.932"/>
<atom id="a8" elementType="H" x3="-3.872" y3="1.391" z3="-0.048"/>
<atom id="a9" elementType="H" x3="-4.682" y3="-0.144" z3="-0.401"/>
<atom id="a10" elementType="C" x3="-4.453" y3="-0.004" z3="-3.198"/>
<atom id="a11" elementType="O" x3="-4.044" y3="-0.991" z3="-3.791"/>
<atom id="a12" elementType="O" x3="-5.639" y3="0.577" z3="-3.501"/>
<atom id="a13" elementType="H" x3="-6.093" y3="0.057" z3="-4.197"/>
</atomArray>
<bondArray>
<bond atomRefs2="a1 a2" order="1"/>
<bond atomRefs2="a2 a3" order="1"/>
<bond atomRefs2="a2 a6" order="1"/>
<bond atomRefs2="a2 a10" order="1"/>
<bond atomRefs2="a3 a4" order="1"/>
<bond atomRefs2="a3 a5" order="1"/>
<bond atomRefs2="a6 a7" order="1"/>
<bond atomRefs2="a6 a8" order="1"/>
<bond atomRefs2="a6 a9" order="1"/>
<bond atomRefs2="a10 a11" order="2"/>
<bond atomRefs2="a10 a12" order="1"/>
<bond atomRefs2="a12 a13" order="1"/>
</bondArray>
</molecule>