Developer:Cpp Tutorial

From Open Babel
Revision as of 10:51, 6 May 2006 by Ghutchis (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This tutorial is aimed at developers looking to use Open Babel as a core chemistry library. 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 wrappers to Perl and Python programming languages. See the developer tutorial for more on using those programming languages.

Please feel free to comment on or improve this tutorial by e-mailing to openbabel-devel

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.

#include <iostream>

#include <openbabel/obconversion.h>
#include <openbabel/obmol.h>

OBConversion obconv;
obconversion.SetInFormat("sdf");
OBMol mol;

bool notatend = obconversion.ReadFile(mol,"../xsaa.sdf");
while (notatend)
{
    std::cout << "Molecular Weight: " << mol.GetMolWt() << std::endl;
    mol.Clear();
    notatend = obconversion.Read(mol);
}