Developer:Python Tutorial
From Open Babel
This tutorial is aimed at developers looking to use Open Babel with Python. It will focus on the object-oriented application programmers' interface (API). There is also documentation on the full API. Although Open Babel is written in C++, it also has bindings to Perl and Python programming languages.
The tutorial is also available in other programming languages.
Please feel free to comment on or improve this tutorial by e-mailing to openbabel-scripting
Contents |
Output Molecular Weight for a Multi-Molecule SDF File
Let's say we want to print out the molecular weights of every molecule in an SD file. Why? Well, we might want to plot a histogram of the distribution, or see whether the average of the distribution is significantly different (in the statistical sense) compared to another SD file.
openbabel.py
from openbabel import *
obconversion = OBConversion()
obconversion.SetInFormat("sdf")
obmol = OBMol()
notatend = obconversion.ReadFile(obmol,"../xsaa.sdf")
while notatend:
print obmol.GetMolWt()
obmol = OBMol()
notatend = obconversion.Read(obmol)
Pybel
from pybel import *
for molecule in readfile("sdf","../xsaa.sdf"):
print molecule.molwt
Find information on all of the atoms and bonds connected to a particular atom
First of all, look at all of the Classes in the Open Babel API that end with "Iter". You should use these whenever you need to do something like iterate over all of the atoms or bonds connected to a particular atom, iterate over all the atoms in a molecule, iterate over all of the residues in a protein, and so on.
As an example, let's say we want to find information on all of the bond orders and atoms connected to a particular OBAtom called 'obatom'. The idea is that we iterate over the neighbouring atoms using OBAtomAtomIter, and then find the bond between the neighbouring atom and 'obatom'. Alternatively, we could have iterated over the bonds (OBAtomBondIter), but we would need to look at the indices of the two atoms at the ends of the bond to find out which is the neighbouring atom:
for neighbour_atom in openbabel.OBAtomAtomIter(obatom): print neighbour_atom.GetAtomicNum() bond = obatom.GetBond(neighbour_atom) print bond.GetBondOrder()
Examples from around the web
- Noel O'Blog - Hack that SD file, Just How Unique are your Molecules Part I and Part II, and Calculate circular fingerprints with Pybel.
- Filter erroneous structures from the ZINC database
- Quantum Pharmaceuticals - Investigation of datasets for hERG binding
- cclib - Given the coordinates, charge, and multiplicity, how to create the corresponding OBMol
Invert a particular stereocenter in a series of molecules
The following was a request on the CCL.net list:
I am looking for any program which can specifically change or invert the stereocenter. I have a lot of compounds to work with. All those compounds have more than one stereocenters. I want to invert one stereocenter which is common in all compounds. So, precisely, my problem is to change a carbon's stereocenter from "S" to "R" and need to do this thing for all of compounds in database.
OBAtom has methods to interrogate and alter an atom's stereochemistry. If you use a SMARTS query to find the target atom, it's easy to change it.:
import pybel
smarts = pybel.Smarts("C[C@](O)CC(=O)O")
inverse = pybel.Smarts("C[C@@](O)CC(=O)O")
outputfile = pybel.Outputfile("sdf", "output.sdf")
for mol in pybel.readfile("smi", "3_p0.smi"):
matches = smarts.findall(mol)
if matches:
firstmatch = matches[0]
matchingatom = firstmatch[1]
mol.OBMol.GetAtom(matchingatom).SetClockwiseStereo()
assert inverse.findall(mol), "Hasn't been inverted!"
outputfile.write(mol)
outputfile.close()

