Friday, October 03, 2008

OpenBabel from IronPython

Noel O'Blog reports on the recent OpenBabel release that explicitly supports IronPython.
OpenBabel is:

"a chemical toolbox designed to speak the many languages of chemical data. It's an open, collaborative project allowing anyone to search, convert, analyze, or store data from molecular modeling, chemistry, solid-state materials, biochemistry, or related areas."

The OBDotNet release comes with instructions on how to use it from IronPython:

First of all, unzip the OBDotNet distribution, and set the environment variable BABEL_DATADIR to point to the data folder contained within.

If running IronPython in the unzipped OBDotNet folder, you can use the following to initialise OBDotNet:

>>> import clr
>>> clr.AddReference("OBDotNet")

Otherwise, you need to need to:
(1) add the OBDotNet distribution folder to the PATH environment variable
(2) include the full path to the OBDotNet.dll when adding the reference, as follows (note the 'r' at the start):

>>> import clr
>>> path = r"C:\Tools\OpenBabel\ob-22x\windows-vc2005\OBCSharp\OBDotNet-0.1\OBDotNet.dll"
>>> clr.AddReferenceToFileAndPath(path)

Once the reference to OBDotNet has been added, you can import and use the OpenBabel bindings just like from CPython:

>>> import OpenBabel as ob
>>> conv = ob.OBConversion()
>>> conv.SetInFormat("smi")
>>> mol = ob.OBMol()
>>> conv.ReadString(mol, "CCC")
>>> mol.GetMolWt()

No comments:

Post a Comment

Note: only a member of this blog may post a comment.