10. 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).

Molecules instantiated via copy constructors will have indices which correspond with those of the original molecule.

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 or GetMaxConfIdx for conformers).

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.

 1 #!/usr/bin/env python
 2 # ch9-1.py
 3 from openeye.oechem import *
 4
 5 mol = OEGraphMol()
 6 OEParseSmiles(mol, "c1ccccc1")
 7
 8 print "atoms"
 9 for atom in mol.GetAtoms():
10     print atom.GetIdx()
11
12 print "bonds"
13 for bond in mol.GetBonds():
14     print bond.GetIdx()

Listing:10.1 Atom and bond indices

Please never, never, ever do this:

# Never ever, ever do this!

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