Difference between revisions of "Python"

From Open Babel
Jump to: navigation, search
m (Removed spaces: otherwise cannot cut+paste into Python interpreter)
m
 
(54 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= Accessing Open Babel with Python =
+
This page provides links to all the information you need to use OpenBabel from the Python programming language.
  
There are two ways to access the Open Babel library using Python:
+
* '''[http://openbabel.org/docs/current/UseTheLibrary/Python.html Documentation]''' on using Open Babel from Python, including:
# The <code>[[#The openbabel module|openbabel]]</code> module, a direct wrapper of the Open Babel C++ library, created using the SWIG package.
+
** '''[http://openbabel.org/docs/current/UseTheLibrary/PythonInstall.html Installation]'''
# The <code>[[#The pyopenbabel module|pyopenbabel]]</code> module, a Pythonic wrapper around the <code>openbabel</code> module.
+
** Using the '''[http://openbabel.org/docs/current/UseTheLibrary/PythonDoc.html openbabel module]'''
 +
** Using '''[http://openbabel.org/docs/current/UseTheLibrary/Python_Pybel.html Pybel]''' and its '''[http://openbabel.org/docs/current/UseTheLibrary/Python_PybelAPI.html API]'''
 +
** '''[http://openbabel.org/docs/current/UseTheLibrary/PythonExamples.html Examples]''' of use
  
== The openbabel module ==
+
* If you have any problems or questions, '''[mailto:openbabel[email protected].sf.net send an email]''' to openbabel-[email protected].sf.net
 
+
* 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:
The <code>openbabel</code> module provides direct access to the C++ Open Babel library from Python. This wrapper 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.
+
: 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.
 
+
This guide is designed to give 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 OBMol, OBAtom, and 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 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 [http://openbabel.sourceforge.net/api/ library API]. Feel free to address questions to the [mailto:openbabel-[email protected]ists.sourceforge.net openbabel-scripting] mailing list.
+
 
+
=== Note on Compiling the SWIG wrapper ===
+
 
+
If you need to recompile the SWIG wrapper for the library, make sure you have the latest version of SWIG installed and run 'configure' as follows:
+
 
+
<pre>
+
./configure --enable-maintainer-mode
+
</pre>
+
 
+
Running 'make' as usual should recreate the SWIG wrappers.
+
 
+
== The pyopenbabel module ==
+
 
+
The motivation behind the <code>pyopenbabel</code> module is to provide a more Pythonic wrapper for the Open Babel C++ library. By 'more Pythonic', it means that it should behave like a typical Python library so that it should:
+
# provide easy access to attributes and methods (no need for GetVar())
+
# keep things simple (only one good way to do things)
+
# things should work 'as expected' in Python (iterators, etc.)
+
 
+
The API for pyopenbabel will soon be available here. Until then, here are some examples of its use at an interactive Python prompt:
+
 
+
<pre>
+
>>> from pyopenbabel import *
+
 
+
>>> mymol = readstring("smi","C1=CC=CS1")
+
 
+
>>> print "The number of atoms is %d" % len(mymol.atoms)
+
The number of atoms is 5
+
 
+
>>> for atom in mymol:
+
...    print "Atomic number:%d" % atom.atomicnum
+
...
+
Atomic number:6
+
Atomic number:6
+
Atomic number:6
+
Atomic number:6
+
Atomic number:16
+
 
+
>>> print "This molecule's InChI representation is %s" % mymol.write("inchi")
+
This molecule's InChI representation is InChI=1/C4H4S/c1-2-4-5-3-1/h1-4H
+
 
+
</pre>
+
  
 +
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.
  
 
[[Category:Developer]]
 
[[Category:Developer]]

Latest revision as of 23:08, 10 October 2012

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

  • If you have any problems or questions, send an email to [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.

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.