Painless Installation of Ruby Open Babel 3
Open Babel 2.1.0 has just been released. Among its new features is a Ruby interface containing most of the functionality of the C++ library. Installation is quick and easy, as shown in this article.
Prerequisites
In addition to a working build system, you'll need Ruby and the Ruby development libraries. Although any recent version should do, this tutorial was written with version 1.8.5.
Step 0: Compile and Install Open Babel
Given the right tools on your system, compiling and and installing Open Babel from source is trivial. This page gives instructions for doing so on Linux, Windows, and Mac OS X.
Step 1: Create the Wrapper's Makefile
After unpacking, compiling, and installing Open Babel, change into the scripts/ruby directory of your source distribution. Next, run the extconf.rb script:
$ ruby extconf.rb checking for main() in -lopenbabel... yes creating Makefile
As you've probably guessed, the purpose of this script is to generate a Makefile specific to your platform. This script uses the standard Ruby library mkmf.
Step 2: Compile the Wrapper
After creating a Makefile, we're ready to compile the C++ Ruby wrapper, contained in openbabel_ruby.cpp:
$ make g++ -I. -I. -I/usr/lib/ruby/1.8/x86_64-linux-gnu -I. -I../../include -fPIC -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -Wall -fPIC -c openbabel_ruby.cpp
This output will be followed by other lines as the compiler builds the wrapper library.
Step 3: Install the Wrapper
After compiling the wrapper, we're ready to install it. You can probably guess that the next command will be (as root):
# make install /usr/bin/install -c -m 0755 openbabel.so /usr/lib/ruby/site_ruby/1.8/x86_64-linux-gnu
Your install directory is chosen by Ruby to be appropriate for your platform and Ruby version.
Hello, Benzene!
Congratulations, you've installed Ruby Open Babel! You can verify that your new library works with interactive Ruby (irb):
$ irb irb(main):001:0> require 'openbabel' => true irb(main):002:0> c=OpenBabel::OBConversion.new => #<OpenBabel::OBConversion:0x2acedbadd020> irb(main):003:0> c.set_in_format 'smi' => true irb(main):004:0> benzene=OpenBabel::OBMol.new => #<OpenBabel::OBMol:0x2acedbacfa10> irb(main):005:0> c.read_string benzene, 'c1ccccc1' => true irb(main):006:0> benzene.num_atoms => 6
OBRuby: A Ruby Interface to Open Babel

And the LORD said, Behold, the people is one, and they have all one language; and this they begin to do: and now nothing will be restrained from them, which they have imagined to do.
-Genesis 11:6
Open Babel is a widely-used Open Source chemical informatics toolkit written in C++. Although originally designed as a molecular language translator, Open Babel also supports SMARTS pattern recognition, molecular fingerprints, molecular superposition, and other features as well.
Open Babel currently offers interfaces for two scripting languages: Python and Perl. Recently, Geoff Hutchison and I have been working to add Ruby to that list. This article reports our success in doing so and provides a glimpse of what might now be possible.
OBRuby
The upcoming release of Open Babel (version 2.1.0) will come complete with a Ruby interface. For those interested in trying it out sooner, a package called OBRuby can be downloaded now. OBRuby compiles against revision 1577 of the Open Babel SVN trunk. It has been tested with Linux and Mac OS X, and will probably work on Windows with minor modifications. The approach outlined here is known to fail with Open Babel 2.0.2.
OBRuby is a technology demonstration. The Ruby scripting support included with Open Babel 2.1.0 may differ in some details from OBRuby. My purpose in this article is simply to demonstrate what is now possible. Please read through the install scripts (they're short) to be sure you're comfortable with what they do.
Here was my OBRuby installation process:
- Download the Open Babel SVN trunk revision 1577 or later.
- cd trunk
- configure, make, (as root) make install
- (as root) ldconfig (necessary on my system - perhaps not on yours)
- cd OBRUBY_DIR
- ruby build.rb
- (as root) make install
One last wrinkle: the build.rb script included with OBRuby is something of a hack. It hardcodes the location of the Open Babel library on line 6:
@@ob_dir='/usr/local'$ irb irb(main):001:0> require 'openbabel' => true
A return value of true shows that the installation was successful. An error message about libopenbabel.so not being found indicates that your system can't find your Open Babel libraries. Be sure you've installed Open Babel and either run ldconfig or set LD_LIBRARY_PATH.
The majority of OBRuby was autogenerated by SWIG. A future article will detail how this was done - with an eye toward developing a Java interface to Open Babel.
Building an OBMol From SMILES
With installation out of the way, let's fire up OBRuby and take her for a test drive. The following code can either be entered with IRB or saved to a file and executed with the ruby interpreter:
require 'openbabel'
include OpenBabel
smi2mol = OBConversion.new
smi2mol.set_in_format("smi")
mol = OBMol.new
smi2mol.read_string(mol, 'CC(C)CCCC(C)C1CCC2C1(CCC3C2CC=C4C3(CCC(C4)O)C)C') # cholesterol, no chirality
mol.add_hydrogens
puts "Cholesterol has #{mol.num_atoms} atoms, including hydrogens."
puts "Its molecular weight is #{mol.get_mol_wt} and its molecular formula is #{mol.get_formula}."SMARTS Matching
One of the most useful features of Open Babel is its SMARTS pattern matching capability. This can conveniently be accessed from OBRuby by first instantiating an OBSmartsPattern, passing the SMARTS pattern of interest to the instance's init method, and retrieving the hit set:require 'openbabel'
include OpenBabel
smi2mol = OBConversion.new
smi2mol.set_in_format("smi")
mol = OBMol.new
smiles = 'CC(C)CCCC(C)C1CCC2C1(CCC3C2CC=C4C3(CCC(C4)O)C)C' # cholesterol, no chirality
smi2mol.read_string(mol, smiles)
mol.add_hydrogens
pattern=OBSmartsPattern.new
smarts = 'C1CCCCC1'
pattern.init(smarts)
pattern.match(mol)
hits = pattern.get_umap_list # => indicies of two cyclohexane rings
puts "Found #{hits.size} instances of the SMARTS pattern '#{smarts}' in the SMILES string #{smiles}. Here are the atom indices:"
hits.each_with_index do |hit, index|
print "Hit #{index}: [ "
hit.each do |atom_index|
print "#{atom_index} "
end
puts "]"
endFound 2 instances of the SMARTS pattern 'C1CCCCC1' in the SMILES string CC(C)CCCC(C)C1CCC2C1(CCC3C2CC=C4C3(CCC(C4)O)C)C. Here are the atom indices: Hit 0: [ 12 17 16 15 14 13 ] Hit 1: [ 20 25 24 23 22 21 ]
Finding Your Way
Using a new library like OBRuby can take some getting used to. An excellent source of information is OpenBabel's online API documentation. Another source is Ruby itself.
For example, let's say you've instantiated an OBMol, but can't remember the exact name of the method that counts the number of atoms. Just use Object.methods.sort:
require 'openbabel'
mol = OpenBabel::OBMol.new
mol.methods.sort # => see output belowConclusions
OBRuby combines the dynamic programming language Ruby with the highly-functional toolkit Open Babel. Further augmenting OBRuby's capabilities with the web application framework Rails and/or Ruby Chemistry Development Kit offers even more possibilities. Future articles will bring some of them to life.

