Getting Started

Not surprisingly, the Open Babel library is a full chemical toolbox. So to start out, the first example is to read in molecular file data and uses the OBMol, and OBConversion classes. The former is designed to store the basic information in a molecule and to perceive information and chemical properties. The latter is designed to handle conversion of a variety of data (i.e., not just molecules) and import and export.

This example program shows how to read in a molecule, check the number of atoms, and write a SMILES string.

      #include <iostream.h>

      // Include Open Babel classes for OBMol and OBConversion
      #include <openbabel/mol.h>
      #include <openbabel/obconversion.h>

      int main(int argc,char **argv)
      {
         // Read from STDIN (cin) and Write to STDOUT (cout)
         OBConversion conv(&cin,&cout);

         // Try to set input format to MDL SD file
         // and output to SMILES
         if(conv.SetInAndOutFormats("SDF","SMI"))
         { 
            OBMol mol;
            if(conv.Read(&mol))
            {
               //  ...manipulate molecule 
               cerr << " Molecule has: " << mol.NumAtoms() 
                    << " atoms." << endl;
            }

            // Write SMILES to the standard output
            conv->Write(&mol);
         }
         return 0; // exit with success
      }

All of the main classes, including OBMol and OBConversion, include example code designed to facilitate using the Open Babel code in real-world chemistry.

For a further list of example code, see the developer tutorials. This section includes examples in C++, Perl, Python, and other porgramming languages.

Also, the tools directory of the Open Babel source releases include a variety of programs which are intended to be more advanced examples (although usually still under 300 lines of code).

Please e-mail the [email protected] mailing list if you have more questions!