Wednesday 26 August 2009

Using OpenBabel from Java

OpenBabel 2.2.3 has just been released and with that, a new release of the Java bindings. The full details on using OpenBabel from Java are available on our wiki. On Windows openbabel.jar is included with the OpenBabel GUI so no additional installation is necessary. You just start Eclipse, add the jar file and away you go.

The following example shows how to use OpenBabel from Java. It includes an example of file format conversion, iteration over atoms, and using the SMARTS matcher.
import org.openbabel.*;

public class Test {

public static void main(String[] args) {
// Initialise
System.loadLibrary("openbabel_java");

// Read molecule from SMILES string
OBConversion conv = new OBConversion();
OBMol mol = new OBMol();
conv.SetInFormat("smi");
conv.ReadString(mol, "C(Cl)(=O)CCC(=O)Cl");

// Print out some general information on the molecule, atoms
conv.SetOutFormat("can");
System.out.print("Canonical SMILES: " + conv.WriteString(mol));
System.out.println("The molecular weight of the molecule is "
+ mol.GetMolWt());
for(OBAtom atom : new OBMolAtomIter(mol)) {
System.out.println("Atom " + atom.GetIdx() +
": atomic number = " + atom.GetAtomicNum() +
", hybridisation = " + atom.GetHyb());
}

// What are the indices of the carbon atoms
// of the acid chloride groups?
OBSmartsPattern acidpattern = new OBSmartsPattern();
acidpattern.Init("C(=O)Cl");
acidpattern.Match(mol);

vvInt matches = acidpattern.GetUMapList();
System.out.println("There are " + matches.size() +
" acid chloride groups");
System.out.print("The carbon atoms of the matches are: ");
for(int i=0; i<matches.size(); i++)
System.out.print(matches.get(i).get(0) + " ");
}
}
The output is as follows:
Canonical SMILES: ClC(=O)CCC(=O)Cl
The molecular weight of the molecule is 154.97935999999999
Atom 1: atomic number = 6, hybridisation = 2
Atom 2: atomic number = 17, hybridisation = 0
Atom 3: atomic number = 8, hybridisation = 2
Atom 4: atomic number = 6, hybridisation = 3
Atom 5: atomic number = 6, hybridisation = 3
Atom 6: atomic number = 6, hybridisation = 2
Atom 7: atomic number = 8, hybridisation = 2
Atom 8: atomic number = 17, hybridisation = 0
There are 2 acid chloride groups
The carbon atoms of the matches are: 1 6
Note: although using OpenBabel from Eclipse on Windows works fine, some users have reported problems on Linux with the default OpenBabel build. You probably need to build OpenBabel statically on Linux if you want to use it from Eclipse, but I haven't tested this. In any case, you can just compile it from the command line.

4 comments:

AndreRamme said...

Hi, I'm using openbabel.jar inside and java application (swing) developing using NetBeans 6.8 In windows environment its runs fine.

When I run it on Linux, application still running fine, but, any call to openbabel functions return an java.lang.StackOverflowError.

I installed in OpenSuse 32 bits 11.2 the last openbabel (version 2.2.3). I copied the openbabel.jar from windows environment to linux.

I also tried to install netbeans in open suse an recompile entire application. Running from development I got the same error.

Any directions / ideas ?

Here is the error message:

Exception occurred during event dispatching: java.lang.StackOverflowError at java.beans.ReflectionUtils$Signature.hashCode(ReflectionUtils.java:341) at java.util.HashMap.get(HashMap.java:317) at java.beans.ReflectionUtils.getMethod(ReflectionUtils.java:373) at java.beans.Statement.invoke(Statement.java:226) at java.beans.Expression.getValue(Expression.java:115) at java.beans.Encoder.getValue(Encoder.java:105) at java.beans.Encoder.get(Encoder.java:225) at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:110) at java.beans.Encoder.writeObject(Encoder.java:74) at java.beans.XMLEncoder.writeObject(XMLEncoder.java:274) at java.beans.Encoder.writeExpression(Encoder.java:304) at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:389) at java.beans.DefaultPersistenceDelegate.doProperty(DefaultPersistenceDelegate.java:229) at java.beans.DefaultPersistenceDelegate.initBean(DefaultPersistenceDelegate.java:264) at java.beans.DefaultPersistenceDelegate.initialize(DefaultPersistenceDelegate.java:408) at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:116) at java.beans.Encoder.writeObject(Encoder.java:74) at java.beans.XMLEncoder.writeObject(XMLEncoder.java:274) at java.beans.Encoder.writeExpression(Encoder.java:304) at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:389) at java.beans.DefaultPersistenceDelegate.doProperty(DefaultPersistenceDelegate.java:229) at java.beans.DefaultPersistenceDelegate.initBean(DefaultPersistenceDelegate.java:264) at java.beans.DefaultPersistenceDelegate.initialize(DefaultPersistenceDelegate.java:408) at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:116) at java.beans.Encoder.writeObject(Encoder.java:74) at java.beans.XMLEncoder.writeObject(XMLEncoder.java:274) at java.beans.Encoder.writeExpression(Encoder.java:304) at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:389) at java.beans.DefaultPersistenceDelegate.doProperty(DefaultPersistenceDelegate.java:229) at java.beans.DefaultPersistenceDelegate.initBean(DefaultPersistenceDelegate.java:264) at java.beans.DefaultPersistenceDelegate.initialize(DefaultPersistenceDelegate.java:408) at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:116) at java.beans.Encoder.writeObject(Encoder.java:74) at java.beans.XMLEncoder.writeObject(XMLEncoder.java:274) at java.beans.Encoder.writeExpression(Encoder.java:304) at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:389) at java.beans.DefaultPersistenceDelegate.doProperty(DefaultPersistenceDelegate.java:229) at java.beans.DefaultPersistenceDelegate.initBean(DefaultPersistenceDelegate.java:264) at ......

Noel O'Boyle said...

Are you using the OpenBabel package provided by OpenSUSE? They probably don't provide the Java bindings. We are not responsible for this, so you should email the package maintainer over at OpenSUSE and request that they include the Java bindings in the next release.

Until they do this, you will need to compile OpenBabel and its Java bindings yourself. See our wiki for instructions.

AndreRamme said...

Hi Noel,
Thank you very much for quick response.

I'm not using the openbabel distributed with OpenSuse.
I downloaded the openbabel-2.2.3.tar file for linux, extracted it and compiled it.

If you have any idea about other item to investigate, I appreciate.

Thank you,

Noel O'Boyle said...

Did you follow the instructions for compiling the Java bindings at http://openbabel.org/wiki/Java. (The website is currently down, but if you look in Google's cache, you'll see it)