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(1); // 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!