6.4 Looping over subsets of Atoms or Bonds

It can sometimes be useful to loop over a subset of the atoms or bonds of a molecule. Traditionally this can be done with "if" statements inside a loop, but it can sometimes be cleaner and more convenient to subset the members being looped over inside the iterator. To do this, many of OEChem's iterator generation functions (such as OEMolBase::GetAtoms) can take an argument which determines which subset of the object to loop over (these functions are called predicates as detailed in the chapter "Predicate Functions" below). The details of these functions are not important here. Instead, a programmer can simply use the predefined functors to control their loops.

The following example shows the use of the predicate HasAtomicNum() to loop over only carbon atoms in a molecule.

#!/usr/bin/env python
# ch6-5.py
from openeye.oechem import *

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

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

Some of the common predefined functors in OEChem are listed below. Predicate functions can be trivial, such as IsHydrogen(), or quite complex, such as Match(string), which returns atoms which match the SMARTS string passed to the constructor. For a complete listing, please see the chapter on predicate functions or the API manual. Many predicates take intuitive construction arguments. For instance, HasAtomName has a string argument which is the atom's name (e.g. mol.GetAtoms(HasAtomName("CA"))).

Atoms

HasAtomName(string)
HasAtomicNum(int)
IsHalogen
IsAromaticAtom
AtomIsInRing
IsChiralAtom
HasResidueNumber(int)
Match(string)
Bonds
HasBondIdx(int)
HasOrder(int)
BondIsInRing
IsRotor
Conformers
HasConfIdx(unsigned int)

These predicates can be particularly helpful when used in conjunction with functions which take OEIters as arguments as seen in the example below. This use of predicates allows factorization of the loop in a way not easily possible with if statements.