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 /**************************************************************
2 * Copyright 2005, OpenEye Scientific Software, Inc.
3 *************************************************************/
4
5 import openeye.oechem.*;
6
7 public class AtomAndBondIndices {
8 public static void main(String argv[]) {
9 OEGraphMol mol = new OEGraphMol();
10 oechem.OEParseSmiles(mol, "c1cocc1");
11 oechem.OEAssignAromaticFlags(mol);
12
13 for (OEAtomBaseIter aiter = mol.GetAtoms();aiter.hasNext();) {
14 System.out.println(aiter.next().GetIdx());
15 }
16
17 for (OEBondBaseIter biter = mol.GetBonds();biter.hasNext();) {
18 System.out.println(biter.next().GetIdx());
19 }
20 }
21 }
Please never, never, ever do this:
// Never ever, ever do this!
for (int i=0;i<=mol.NumAtoms();++i) {
OEAtomBase atom = mol.GetAtom(new OEHasAtomIdx(i));
// pretend atom is valid
}