Difference between revisions of "Python"

From Open Babel
Jump to: navigation, search
(Using iterators)
(Changed main Python page to a signpost to other pages)
Line 1: Line 1:
This page describes how to use the OpenBabel library from Python. First of all, if you haven't already installed the Python bindings, you should do so now:
+
This page provides links to all the information you need to use OpenBabel from the Python programming language.
# '''Windows users''' should download the OpenBabel Python module from the [[Install]] page
+
# '''Linux and MacOSX users''' should compile OpenBabel and the Python bindings themselves as described on the [[Install]] page. Alternatively, you can check your distribution's package manager for something like 'openbabel-python' or 'python-openbabel'.
+
  
If you have any problems or want to ask a question, please send an email to the '''[mailto:[email protected]sourceforge.net openbabel-scripting] mailing list'''.  
+
* '''[[Install Python bindings | How to install]]''' the Python bindings on Windows, Linux or MacOSX.
 
+
* '''[[Using OpenBabel from Python | How to use]]''' the Python bindings
The most important thing that you need to understand is that there are '''two ways''' to access the Open Babel library using Python:
+
* If you have any problems or want to ask a question, please '''[mailto:[email protected]sf.net send an email]''' to the [email protected].sf.net
# The <code>[[#The openbabel module|openbabel]]</code> module, a direct binding of the Open Babel C++ library, created using the SWIG package.
+
* Read the '''[http://dx.doi.org/10.1186/1752-153X-2-5 Pybel paper]''' for background and examples of use. Don't forget to '''cite the Pybel paper''' if you use Pybel to obtain results for publication:
# <code>[[#Pybel|Pybel]]</code>, a set of convenience functions and classes that simplifies access to the <code>openbabel</code> module.
+
 
+
You should probably read the Pybel paper now. Remember to '''cite the Pybel paper''' if you use Pybel to obtain results for publication:
+
 
: N. M. O'Boyle, C. Morley and G. R. Hutchison. [http://dx.doi.org/10.1186/1752-153X-2-5 Pybel: a Python wrapper for the OpenBabel cheminformatics toolkit] ''Chem. Cent. J.'' '''2008''', ''2'', 5.
 
: N. M. O'Boyle, C. Morley and G. R. Hutchison. [http://dx.doi.org/10.1186/1752-153X-2-5 Pybel: a Python wrapper for the OpenBabel cheminformatics toolkit] ''Chem. Cent. J.'' '''2008''', ''2'', 5.
 +
* See further '''[[Developer:Python_Tutorial | examples]]''' of the use of the Python bindings
  
For '''examples''' of the use of Pybel, see the paper above and the [[Developer:Python_Tutorial | tutorial page]].
 
 
The following sections describe these Python modules in detail.
 
 
== The openbabel module ==
 
 
The <code>openbabel</code> module provides direct access to the C++ Open Babel library from Python. This binding is generated using the SWIG package and provides access to almost all of the Open Babel interfaces via Python, including the base classes OBMol, OBAtom, OBBond, and OBResidue, as well as the conversion framework OBConversion. As such, essentially any call in the C++ API is available to Python scripts with very little difference in syntax. As a result, the principal documentation is the [[API | Open Babel C++ API documentation]].
 
 
==== Examples ====
 
 
Here we give some examples of common Python syntax for the <code>openbabel</code> module and pointers to the appropriate sections of the API documentation.
 
 
The example script below creates atoms and bonds one-by-one using the [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBMol.shtml OBMol], [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBAtom.shtml OBAtom], and [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBBond.shtml OBBond] classes.
 
 
<pre>
 
import openbabel
 
 
mol = openbabel.OBMol()
 
print 'Should print 0 (atoms)'
 
print mol.NumAtoms()
 
 
a = mol.NewAtom()
 
a.SetAtomicNum(6)  # carbon atom
 
a.SetVector(0.0, 1.0, 2.0) # coordinates
 
 
b = mol.NewAtom()
 
mol.AddBond(1, 2, 1)  # atoms indexed from 1
 
print 'Should print 2 (atoms)'
 
print mol.NumAtoms()
 
print 'Should print 1 (bond)'
 
print mol.NumBonds()
 
 
mol.Clear();
 
</pre>
 
 
More commonly, Open Babel can be used to read in molecules using the [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBConversion.shtml OBConversion] framework. The following script reads in molecular information (a SMI file) from a string, adds hydrogens, and writes out an MDL file as a string.
 
 
<pre>
 
import openbabel
 
 
obConversion = openbabel.OBConversion()
 
obConversion.SetInAndOutFormats("smi", "mdl")
 
 
mol = openbabel.OBMol()
 
obConversion.ReadString(mol, "C1=CC=CS1")
 
 
print 'Should print 5 (atoms)'
 
print mol.NumAtoms()
 
 
mol.AddHydrogens()
 
print 'Should print 9 (atoms) after adding hydrogens'
 
print mol.NumAtoms()
 
 
outMDL = obConversion.WriteString(mol)
 
</pre>
 
 
The following script writes out a file using a filename, rather than reading and writing to a Python string.
 
 
<pre>
 
import openbabel
 
 
obConversion = openbabel.OBConversion()
 
obConversion.SetInAndOutFormats("pdb", "mol2")
 
 
mol = openbabel.OBMol()
 
obConversion.ReadFile(mol, "1ABC.pdb.gz")  # Open Babel will uncompress automatically
 
 
mol.AddHydrogens()
 
 
print mol.NumAtoms()
 
print mol.NumBonds()
 
print mol.NumResidues()
 
 
obConversion.WriteFile(mol, '1abc.mol2')
 
</pre>
 
 
That's it! There's more information on particular calls in the [[API | library API]]. Feel free to address questions to the [mailto:[email protected] openbabel-scripting] mailing list.
 
 
==== Using iterators  ====
 
 
A number of Open Babel toolkit classes provide iterators over various objects; these classes are identifiable by the suffix "Iter" in the [http://openbabel.sourceforge.net/api/current/annotated.shtml list of toolkit classes] in the API:
 
* [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBAtomAtomIter.shtml OBAtomAtomIter] and [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBAtomBondIter.shtml OBAtomBondIter] - given an OBAtom, iterate over all neighboring OBAtoms or OBBonds
 
* [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBMolAtomIter.shtml OBMolAtomIter], [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBMolBondIter.shtml OBMolBondIter], [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBMolAngleIter.shtml OBMolAngleIter], [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBMolTorsionIter.shtml OBMolTorsionIter], [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBMolRingIter.shtml OBMolRingIter] - given an OBMol, iterate over all OBAtoms, OBBonds, OBAngles, OBTorsions or OBRings.
 
* [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBMolAtomBFSIter.shtml OBMolAtomBFSIter] - given an OBMol and the index of an atom, OBMolAtomBFSIter iterates over all the neighbouring atoms in a breadth-first manner. It differs from the other iterators in that it returns two values - an OBAtom, and the 'depth' of the OBAtom in the breadth-first search (this is useful, for example, when creating circular fingerprints)
 
* [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBMolPairIter.shtml OBMolPairIter] - given an OBMol, iterate over all pairs of OBAtoms separated by more than three bonds
 
* [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBResidueIter.shtml OBResidueIter] - given an OBMol representing a protein, iterate over all OBResidues
 
* [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBResidueAtomIter.shtml OBResidueAtomIter] - given an OBResidue, iterate over all OBAtoms
 
 
These iterator classes can be used using the typical Python syntax for iterators:
 
<pre>
 
for obatom in openbabel.OBMolAtomIter(obmol):
 
    print obatom.GetAtomicMass()
 
</pre>
 
 
Note that OBMolTorsionIter returns atom IDs which are off by one. That is, you need to add one to each ID to get the correct ID. Also, if you add or remove atoms, you will need to delete the existing TorsionData before using OBMolTorsionIter. This is done as follows:<pre>
 
mol.DeleteData(openbabel.TorsionData)
 
</pre>
 
 
==== Calling a method requiring an array of C doubles====
 
 
Some Open Babel toolkit methods, for example OBMol::Rotate(), require an array of doubles. It's not possible to directly use a list of floats when calling such a function from Python. Instead, you need to first explicitly create a C array using the ''double_array()'' function:
 
<pre>
 
obMol.Rotate([1.0, -54.7, 3])
 
# Error!
 
myarray = openbabel.double_array([1.0, -54.7, 3])
 
obMol.Rotate(myarray)
 
# Works!
 
</pre>
 
 
==== Accessing OBPairData and OBUnitCell ====
 
 
If you want to access any [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBPairData.shtml OBPairData] or [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBUnitCell.shtml OBUnitCell] associated with a molecule, you need to 'cast' the OBGenericData returned by OBMol.GetData using the ''toPairData()'' or ''toUnitCell()'' functions:
 
<pre>
 
pairdata = [openbabel.toPairData(x) for x in obMol.GetData() if x.GetDataType()==openbabel.PairData]
 
print pairdata[0].GetAttribute(), pairdata[0].GetValue()
 
 
unitcell = openbabel.toUnitCell(obMol.GetData(openbabel.UnitCell))
 
print unitcell.GetAlpha(), unitcell.GetSpaceGroup()
 
</pre>
 
 
==== Combining numpy with Open Babel ====
 
 
If you are using the Python numerical extension, numpy, and you try to pass values from a numpy array to Open Babel, it may not work unless you convert the values to Python built-in types first:
 
<pre>
 
import numpy, openbabel
 
mol = openbabel.OBMol()
 
atom = mol.NewAtom()
 
 
coord = numpy.array([1.2, 2.3, 4.6], "float32")
 
atom.SetVector(coord[0], coord[1], coord[2])
 
# Error
 
 
atom.SetVector(float(coord[0]), float(coord[1]), float(coord[2]))
 
# No error
 
 
coord = numpy.array([1.2, 2.3, 4.6], "float64")
 
atom.SetVector(coord[0], coord[1], coord[2])
 
# No error either - not all numpy arrays will cause an error
 
</pre>
 
 
== Pybel ==
 
 
Pybel provides convenience functions and classes that make it simpler to use the Open Babel libraries from Python, especially for file input/output and for accessing the attributes of atoms and molecules. The Atom and Molecule classes used by Pybel can be converted to and from the OBAtom and OBMol used by the <code>openbabel</code> module. These features are discussed in more detail below.
 
 
Information on the Pybel API can be found at the interactive Python prompt using the <code>help()</code> function, and is also available here: [http://openbabel.sourceforge.net/pybel.html Pybel API].
 
 
To use Pybel, use "<code>import pybel</code>" or "<code>from pybel import *</code>".
 
 
==== Atoms and Molecules ====
 
 
A [http://openbabel.sourceforge.net/pybel.html#Molecule Molecule] can be created in any of three ways:
 
# From an OBMol, using <code>Molecule(myOBMol)</code>
 
# By reading from a file (see [[#Input/Output | Input/Output]] below)
 
# By reading from a string (see [[#Input/Output | Input/Output]] below)
 
 
An [http://openbabel.sourceforge.net/pybel.html#Atom Atom] can be created in two different ways:
 
# From an OBAtom, using <code>Atom(myOBAtom)</code>
 
# By accessing the <code>.atoms</code> attribute of a Molecule
 
 
It is always possible to access the OBMol or OBAtom on which a Molecule or Atom is based, by accessing the appropriate attribute, either <code>.OBMol</code> or <code>.OBAtom</code>. In this way, it is easy to combine the convenience of <code>pybel</code> with the many additional capabilities present in <code>openbabel</code>. See [[#Combining Pybel with openbabel.py | Combining Pybel with openbabel.py]] below.
 
 
Molecules have the following attributes: atoms, charge, data, dim, energy, exactmass, flags, formula, mod, molwt, spin, sssr, title and unitcell (if crystal data). The <code>.atoms</code> attribute provides a list of the Atoms in a Molecule. The <code>.data</code> attribute returns a dictionary-like object for accessing and editing the data fields associated with the molecule (technically, it's a [http://openbabel.sourceforge.net/pybel.html#MoleculeData MoleculeData] object, but you can use it like it's a regular dictionary). The <code>.unitcell</code> attribute gives access to any unit cell data associated with the molecule (see [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBUnitCell.shtml OBUnitCell]). The remaining attributes correspond directly to attributes of OBMols: e.g. <code>Molecule.formula</code> is equivalent to <code>OBMol.GetFormula()</code>. For more information on what these attributes are, please see the Open Babel Library documentation for [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBMol.shtml OBMol].
 
 
For example, let's suppose we have an SD file containing descriptor values in the data fields:
 
<pre>
 
>>> mol = readfile("sdf", "calculatedprops.sdf").next() # (readfile is described below)
 
>>> print mol.molwt
 
100.1
 
>>> print len(mol.atoms)
 
16
 
>>> print mol.data.keys()
 
{'Comment': 'Created by CDK', 'NSC': 1, 'Hydrogen Bond Donors': 3, 'Surface Area': 342.43, .... }
 
>>> print mol.data['Hydrogen Bond Donors']
 
3
 
>>> mol.data['Random Value'] = random.randint(0,1000) # Add a descriptor containing noise
 
</pre>
 
 
Molecules have a <code>[http://openbabel.sourceforge.net/pybel.html#Molecule-write .write()]</code> method that writes a representation of a Molecule to a file or to a string. See [[#Input/Output | Input/Output]] below. They also have a <code>[http://openbabel.sourceforge.net/pybel.html#Molecule-calcfp .calcfp()]</code> method that calculates a molecular fingerprint. See [[#Fingerprints | Fingerprints]] below.
 
 
The <code>[http://openbabel.sourceforge.net/pybel.html#Molecule-draw .draw()]</code> method of a Molecule generates 2D coordinates and a 2D depiction of a molecule. It uses the [http://bkchem.zirael.org/oasa_en.html OASA library] by Beda Kosata to do this (see the section below on [[Python#Installing OASA | Installing OASA]]). The default options are to show the image on the screen (<code>show=True</code>), not to write to a file (<code>filename=None</code>), to calculate 2D coordinates (<code>usecoords=False</code>) but not to store them (<code>update=False</code>).
 
 
The <code>[http://openbabel.sourceforge.net/pybel.html#Molecule-addh .addh()]</code> and <code>[http://openbabel.sourceforge.net/pybel.html#Molecule-removeh .removeh()]</code> methods allow hydrogens to be added and removed.
 
 
If a molecule does not have 3D coordinates, they can be generated using the <code>[http://openbabel.sourceforge.net/pybel.html#Molecule-make3D .make3D()]</code> method. By default, this includes 50 steps of a geometry optimisation using the MMFF94 forcefield. The list of available forcefields is stored in the [http://openbabel.sourceforge.net/pybel.html#forcefields forcefields] variable. To further optimise the structure, you can use the <code>[http://openbabel.sourceforge.net/pybel.html#Molecule-localopt .localopt()]</code> method, which by default carries out 500 steps of an optimisation using MMFF94. Note that hydrogens need to be added before calling <code>localopt()</code>.
 
 
The <code>[http://openbabel.sourceforge.net/pybel.html#Molecule-calcdesc .calcdesc()]</code> method of a Molecule returns a dictionary containing descriptor values for LogP, Polar Surface Area ("TPSA") and Molar Refractivity ("MR"). A list of the available descriptors is contained in the variable <code>[http://openbabel.sourceforge.net/pybel.html#descs descs]</code>. If only one or two descriptor values are required, you can specify the names as follows: <code>calcdesc(["LogP", "TPSA"])</code>. Since the <code>.data</code> attribute of a Molecule is also a dictionary, you can easily add the result of <code>calcdesc()</code> to an SD file (for example) as follows:
 
<pre>
 
mol = readfile("sdf", "without_desc.sdf").next()
 
descvalues = mol.calcdesc()
 
# In Python, the update method of a dictionary allows you
 
# to add the contents of one dictionary to another
 
mol.data.update(descvalues)
 
output = Outputfile("sdf", "with_desc.sdf")
 
output.write(mol)
 
output.close()
 
</pre>
 
 
For convenience, a Molecule provides an iterator over its Atoms. This is used as follows:
 
<pre>
 
for atom in myMolecule:
 
  # do something with atom
 
</pre>
 
 
Atoms have the following attributes: atomicmass, atomicnum, cidx, coords, coordidx, exactmass, formatcharge, heavyvalence, heterovalence, hyb, idx, implicitvalence, index, isotope, partialcharge, spin, type, valence, vector. The <code>.coords</code> attribute provides a tuple (x, y, z) of the atom's coordinates. The remaining attributes are as for [http://openbabel.sourceforge.net/api/current/classOpenBabel_1_1OBAtom.shtml OBAtom].
 
 
==== Input/Output ====
 
 
One of the strengths of Open Babel is the number of chemical file formats that it can handle. Pybel provides a dictionary of the input and output formats in the variables [http://openbabel.sourceforge.net/pybel.html#formats informats] and [http://openbabel.sourceforge.net/pybel.html#formats outformats], where the keys are the three-letter codes for each format (e.g. 'pdb') and the values are the descriptions (e.g. 'Protein Data Bank format').
 
 
Pybel greatly simplifies the process of reading and writing molecules to and from strings or files. There are two functions for reading Molecules:
 
# <code>[http://openbabel.sourceforge.net/pybel.html#-readstring readstring(format, string)]</code> reads a Molecule from a string
 
# <code>[http://openbabel.sourceforge.net/pybel.html#-readfile readfile(format, filename)]</code> provides an iterator over the Molecules in a file
 
 
Here are some examples of their use. Note in particular the use of <code>.next()</code> to access the first (and possibly only) molecule in a file:
 
<pre>
 
>>> mymol = readstring("smi", "CCCC")
 
>>> print mymol.molwt
 
58
 
>>> for mymol in readfile("sdf", "largeSDfile.sdf")
 
... print mymol.molwt
 
>>> singlemol = readfile("pdb", "1CRN.pdb").next()
 
</pre>
 
 
If a single molecule is to be written to a molecule or string, the <code>[http://openbabel.sourceforge.net/pybel.html#Molecule-write .write()]</code> method of the Molecule should be used:
 
# <code>mymol.write(format)</code> returns a string
 
# <code>mymol.write(format, filename)</code> writes the Molecule to a file. An optional additional parameter, <code>overwrite</code>, should be set to <code>True</code> if you wish to overwrite an existing file.
 
 
For files containing multiple molecules, the [http://openbabel.sourceforge.net/pybel.html#Outputfile Outputfile] class should be used instead. This is initialised with a format and filename (and optional <code>overwrite</code> parameter). To write a Molecule to the file, the <code>[http://openbabel.sourceforge.net/pybel.html#Outputfile-write .write()]</code> method of the Outputfile is called with the Molecule as a parameter. When all molecules have been written, the <code>[http://openbabel.sourceforge.net/pybel.html#Outputfile-close .close()]</code> method of the Outputfile should be called.
 
 
Here are some examples of output using the Pybel methods and classes:
 
<pre>
 
>>> print mymol.write("smi")
 
'CCCC'
 
>>> mymol.write("smi", "outputfile.txt")
 
>>> largeSDfile = Outputfile("sdf", "multipleSD.sdf")
 
>>> largeSDfile.write(mymol)
 
>>> largeSDfile.write(myothermol)
 
>>> largeSDfile.close()
 
</pre>
 
 
==== Fingerprints ====
 
 
A [http://openbabel.sourceforge.net/pybel.html#Fingerprint Fingerprint] can be created in either of two ways:
 
# From a vector returned by the OpenBabel GetFingerprint() method, using <code>Fingerprint(myvector)</code>
 
# By calling the <code>[http://openbabel.sourceforge.net/pybel.html#Molecule-calcfp .calcfp()]</code> method of a Molecule
 
 
The <code>.calcfp()</code> method takes an optional argument, <code>fptype</code>, which should be one of the fingerprint types supported by OpenBabel (see [[Tutorial:Fingerprints]]). The list of supported fingerprints is stored in the variable <code>[http://openbabel.sourceforge.net/pybel.html#fps fps]</code>. If unspecified, the default fingerprint ("FP2") is calculated.
 
 
Once created, the Fingerprint has two attributes: <code>fp</code> gives the original OpenBabel vector corresponding to the fingerprint, and <code>bits</code> gives a list of the bits that are set.
 
 
The Tanimoto coefficient of two Fingerprints can be calculated using the "|" operator.
 
 
Here is an example of its use:
 
<pre>
 
>>> import pybel
 
>>> smiles = ['CCCC', 'CCCN']
 
>>> mols = [pybel.readstring("smi", x) for x in smiles] # Create two molecules from the SMILES
 
>>> fps = [x.calcfp() for x in mols] # Calculate their fingerprints
 
>>> print fps[0].bits, fps[1].bits
 
[261, 385, 671] [83, 261, 349, 671, 907]
 
>>> print fps[0] | fps[1] # Print the Tanimoto coefficient
 
0.3333
 
</pre>
 
 
==== SMARTS matching ====
 
 
Pybel also provides a simplified API to the Open Babel SMARTS pattern matcher. A [http://openbabel.sourceforge.net/pybel.html#Smarts Smarts] object is created, and the <code>[http://openbabel.sourceforge.net/pybel.html#Smarts-findall .findall()]</code> method is then used to return a list of the matches to a given Molecule.
 
 
Here is an example of its use:
 
<pre>
 
>>> mol = readstring("smi","CCN(CC)CC") # triethylamine
 
>>> smarts = Smarts("[#6][#6]") # Matches an ethyl group
 
>>> print smarts.findall(mol)
 
[(1, 2), (4, 5), (6, 7)]
 
</pre>
 
 
==== Combining Pybel with openbabel.py ====
 
 
It is easy to combine the ease of use of Pybel, with the comprehensive coverage of the Open Babel toolkit that <code>openbabel.py</code> provides. Pybel is really a wrapper around <code>openbabel.py</code>, with the result that the OBAtom and OBMol used by openbabel.py can be interconverted to the Atom and Molecule used by Pybel.
 
 
The following example shows how to read a molecule from a PDB file using Pybel, and then how to use openbabel.py to add hydrogens. It also illustrates how to find out information on what methods and classes are available, while at the interactive Python prompt.
 
<pre>
 
>>> import pybel
 
>>> mol = pybel.readfile("pdb", "1PYB").next()
 
>>> help(mol)
 
Help on Molecule in module pybel object:
 
...
 
|  Attributes:
 
|    atoms, charge, dim, energy, exactmass, flags, formula,
 
|    mod, molwt, spin, sssr, title.
 
...
 
|  The original Open Babel molecule can be accessed using the attribute:
 
|    OBMol
 
...
 
>>> print len(mol.atoms), mol.molwt
 
3430 49315.2
 
>>> dir(mol.OBMol) # Show the list of methods provided by openbabel.py
 
['AddAtom', 'AddBond', 'AddConformer', 'AddHydrogens', 'AddPolarHydrogens', ... ]
 
>>> mol.OBMol.AddHydrogens()
 
>>> print len(mol.atoms), mol.molwt
 
7244 49406.0
 
</pre>
 
 
The next example is an extension of one of the <code>openbabel.py</code> examples at the top of this page. It shows how a molecule could be created using <code>openbabel.py</code>, and then written to a file using Pybel:
 
<pre>
 
import openbabel, pybel
 
 
mol = openbabel.OBMol()
 
a = mol.NewAtom()
 
a.SetAtomicNum(6)  # carbon atom
 
a.SetVector(0.0, 1.0, 2.0) # coordinates
 
b = mol.NewAtom()
 
mol.AddBond(1, 2, 1)  # atoms indexed from 1
 
 
pybelmol = pybel.Molecule(mol)
 
pybelmol.write("sdf", "outputfile.sdf")
 
</pre>
 
 
For more examples of using Open Babel from Python, see the [[Developer:Python_Tutorial| developer Python tutorial]].
 
 
==== Installing OASA ====
 
 
To draw 2D depictions using Pybel, you need the [http://bkchem.zirael.org/oasa_en.html OASA library] by Beda Kosata.
 
 
===== Windows =====
 
 
* Install [http://www.pythonware.com/products/pil/#pil116 Python Imaging Library (PIL)] for your version of Python
 
* Download and unzip the Windows package of [http://bkchem.zirael.org/oasa_en.html OASA]
 
* Copy the two folders 'oasa' and 'cairo' to the site-packages folder of your Python distribution (on my system, this is C:\Program Files\Python25\Lib\site-packages)
 
* If you are using Python 2.5, you are finished!
 
* If you are using Python 2.4, go into the 'site-packages/cairo' folder, delete '_cairo.pyd', and rename '_cairo.pyd2.4' to '_cairo.pyd'
 
 
===== Linux =====
 
  
* Download [http://bkchem.zirael.org/oasa_en.html OASA 0.12.1], unzip it, and add the oasa-0.12.1 directory to the PYTHONPATH.
+
If you find Pybel useful, you may also be interested in '''[http://cinfony.googlecode.com Cinfony]''' which allows you to access several cheminformatics toolkits using the same Python API as Pybel, from both regular Python and Jython.
* OASA requires Cairo and its Python bindings which are included in Debian as 'libcairo2' and 'python-cairo' respectively.
+
* To display images on the screen (rather than just writing to a file), you also need:
+
** the [http://www.pythonware.com/products/pil/ Python Imaging Library], available as the Debian packages 'python-imaging' and 'python-imaging-tk',
+
** the Python Tkinter library. This should already be installed as part of a standard Python distribution. If not it's available as the Debian package 'python-tk'.
+
  
 
[[Category:Developer]]
 
[[Category:Developer]]

Revision as of 05:15, 1 January 2009

This page provides links to all the information you need to use OpenBabel from the Python programming language.

  • How to install the Python bindings on Windows, Linux or MacOSX.
  • How to use the Python bindings
  • If you have any problems or want to ask a question, please send an email to the [email protected]
  • Read the Pybel paper for background and examples of use. Don't forget to cite the Pybel paper if you use Pybel to obtain results for publication:
N. M. O'Boyle, C. Morley and G. R. Hutchison. Pybel: a Python wrapper for the OpenBabel cheminformatics toolkit Chem. Cent. J. 2008, 2, 5.
  • See further examples of the use of the Python bindings


If you find Pybel useful, you may also be interested in Cinfony which allows you to access several cheminformatics toolkits using the same Python API as Pybel, from both regular Python and Jython.