10.1 Using NewAtom and NewBond

While using SMILES is a convenient method of specifying a molecule, OEChem contains functions that allow molecules to be constructed from atoms and bonds explicitly. The following example shows how to create the molecule water.

Atoms are created by calling the OEMolBase method, NewAtom, and Bonds are created by calling the OEMolBase method, NewBond. OEMolBase NewAtom takes the atomic number of the atom to create and returns a pointer to the new OEAtomBase, and NewBond takes two OEAtomBases and a integer bond order as arguments, and returns a pointer to the new OEBondBase.

The atoms and bonds of a molecule are automatically deleted when their parent molecule is destroyed.

#!/usr/bin/env python
# ch10-1.py
from openeye.oechem import *

mol = OEGraphMol()

o  = mol.NewAtom(8)
h1 = mol.NewAtom(1)
h2 = mol.NewAtom(1)

b1 = mol.NewBond(o,h1,1)
b2 = mol.NewBond(o,h2,1)

print mol.NumAtoms()
print mol.NumBonds()

In the example source code, the atomic numbers of oxygen, 8, and hydrogen, 1, are explicitly encoded in the program. To make this code easier to read and less error prone, OEChem provides symbolic constants for the first 109 elements. This defines the atomic symbols in the C++ namespace OEElemNo with the appropriate values. The following example uses these constants instead of just numbers.

#!/usr/bin/env python
# ch10-2.py
from openeye.oechem import *

mol = OEGraphMol()

o  = mol.NewAtom(OEElemNo_O)
h1 = mol.NewAtom(OEElemNo_H)
h2 = mol.NewAtom(OEElemNo_H)

b1 = mol.NewBond(o,h1,1)
b2 = mol.NewBond(o,h2,1)

print mol.NumAtoms()
print mol.NumBonds()