9. Atom, Bond and Conformer Indices

OEChem assigns each OEAtomBase and each OEBondBase of an OEMolBase an index when it is created. This index can be used to distinguish one OEAtomBase from another, as that it is unique among OEAtomBases of the same molecule. Atom and bond indices are also stable. A given OEAtomBase will have the same index throughout its lifetime independent of the reordering of atoms of a molecule, or the creation or deletion of other atoms (with the single exception of the Sweep method of OEMolBases and the SweepConfs method of OEMCMolBases).

Note that atom and bond indices are not guaranteed to be sequential, or even created sequentially, hence atom indices can't easily be used to retrieve all of the atoms of a molecule. They can however be assumed to be dense small integers greater than or equal to zero and less than GetMaxAtomIdx() for atoms (or GetMaxBondIdx() for bonds)

OEAtomBases and OEBondBases both implement the GetIdx method to return their unique index in the molecule and also a SetIdx method for setting a unique number. SetIdx should probably only be used in writing file readers or other low-level methods. The following example shows loops over the atoms and bonds and prints their indices.

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

mol = OEGraphMol()
OEParseSmiles(mol, "c1ccccc1")

print "atoms"
for atom in mol.GetAtoms():
    print atom.GetIdx()

print "bonds"
for bond in mol.GetBonds():
    print bond.GetIdx()

Even typing the following idiom may invalidate any chance of support and eliminate any glimmer of respect from OpenEye Scientific Software or the computational chemistry/chemoinformatics community.

# Never ever, ever do this!

for i in xrange(mol.NumAtoms()):
    atom = mol.GetAtom(HasAtomIdx(i))
    # pretend atom is valid

There are far more efficient methods of crashing computer software that should be used instead. OpenEye reserves the right to increase and/or demand additional maintenance and support fees on discovering such abuses, and reserves the right to publish the names of known offenders on our web site and at annual user group meetings. This information is also available to HR departments for aiding hiring and firing decisions upon request.