22.4 Composition Functors in OEChem

Occasionally, one may want to use a logical operator to join two or more functors. While it is certainly possible to write a quick functor which wraps two or more functors, but this is not necessary. The functors OEAndAtom, OEOrAtom and OENotAtom are already defined. The each have constructors which take the appropriate number of predicates as arguments and generate a single unary predicate. Similar logical predicates are defined for bonds, residues, etc.

The following example demonstrates use of the OEAndAtom and OENotAtom composition predicates with two of the predefined atom predicates

 1 /**************************************************************
 2  * Copyright 2005,,2008 OpenEye Scientific Software, Inc.
 3  *************************************************************/
 4
 5 import openeye.oechem.*;
 6
 7 public class CompositePred {
 8   private static int Count(OEUnaryAtomPred func, OEMolBase mol) {
 9     int count = 0;
10     for (OEAtomBaseIter aiter=mol.GetAtoms();aiter.hasNext();) {
11       OEAtomBase atom = aiter.next();
12       if (func.constCall(atom)) count++;
13     }
14     return count;
15   }
16   public static void main(String argv[]) {
17     OEMol mol = new OEMol();
18     oechem.OEParseSmiles(mol, "c1c(O)c(O)c(Cl)cc1CCCBr");
19     oechem.OEAssignAromaticFlags(mol);
20
21     String smiles = oechem.OECreateCanSmiString(mol);
22     System.out.println(smiles);
23
24     System.out.println("Number of Aromatic Carbons "+
25         Count(new OEAndAtom(new OEIsCarbon(), new OEIsAromaticAtom()), mol));
26
27     System.out.println("Number of Non-Carbons "+
28         Count(new OENotAtom(new OEHasAtomicNum(6)), mol));
29   }
30 }

Listing:22.3 Composition Functors in OEChem