8.1 Looping over the Atoms and Bonds of a Molecule

Since the underlying storage mechanism for atoms and bonds is not exposed, there is no longer the notion of an array of atoms or and array of bonds inside a molecule. In order to traverse the atoms or bonds one uses iterators (OEAtomBaseIter or OEBondBaseIter).

The following example show how to traverse the atoms and print out their atomic numbers and then traverse the bonds and print their bond order.

 1 /**************************************************************
 2  * Copyright 2005, OpenEye Scientific Software, Inc.
 3  *************************************************************/
 4
 5 import openeye.oechem.*;
 6
 7 public class AtomAndBondLoops {
 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().GetAtomicNum());
15     }
16
17     for (OEBondBaseIter biter = mol.GetBonds();biter.hasNext();) {
18       System.out.println(biter.next().GetOrder());
19     }
20   }
21 }

Listing:8.1 Looping over the atoms and bonds

Note that this example also introduces a couple of new methods, an OEAtomBase member function: GetAtomicNum() and an OEBondBase member function: GetOrder(). These and other member functions of these two classes will be covered in more detail in subsequent chapters.