Predicate Functors¶
Functors that return bool are an important special case. A unary function whose return type is bool is called a predicate.
In OEChem TK, these functors are often passed into another function.
The functors are then called from inside the second
function. This is the concept of a callback, because the second
function provides the argument and ‘call’s back’ to the functor which
was passed into the function.
Generator method such as OEMolBase.GetAtoms
can take a functor
as an argument and use the callback mechanism to iterate over
atoms that satisfy the functor passed to it.
See example in Atom or Bond Subset Iteration section.
In the example below, the function CountAtoms
loops over the atoms
and performs a call-back to the predicate functor pred
for each
atom. If the predicate returns true, a counter is incremented.
The main
function passes OEIsOxygen predefined atom predicates
to the CountAtoms
function that counts the number of oxygen atoms in the molecule.
(Please note that this function is already implemented in OEChem TK and called
OECount
.)
Listing 1: Using functor callbacks
package openeye.docexamples.oechem;
import openeye.oechem.*;
public class Functors {
private static int CountAtoms(OEMolBase mol, OEUnaryAtomPred pred) {
int counts = 0;
for (OEAtomBase atom : mol.GetAtoms())
if (pred.constCall(atom))
counts++;
return counts;
}
public static void main(String argv[]) {
OEGraphMol mol = new OEGraphMol();
oechem.OESmilesToMol(mol, "c1cc[nH]c1CC2COCNC2");
System.out.println("Number of oxygen atoms = " + CountAtoms(mol,new OEIsOxygen()));
}
}
Built-in Functors¶
There are many useful functors already defined in OEChem TK. These can be used by programmers with little or no understanding of the details of how functors work. A programmer can simply pass them to one of the many OEChem TK functions and methods which take predicates as arguments.
Atom Functors¶
Access |
Functor Name |
---|---|
ring atoms |
|
chain atoms |
|
atom with specified atom index |
|
atom with selected atom index |
|
atom with specified atom name |
|
atoms with specified atom stereo |
|
atoms with specified formal charge |
|
atoms with specified number of heavy atom neighbors |
|
aromatic atoms |
|
atoms with specific hybridization |
|
chiral atoms |
|
atoms with anisotropic B-factor parameters |
|
atoms with specified map index |
|
atoms representing R-Groups |
|
valid atoms (by OpenEye valence conventions) |
|
valid atoms (by MDL valence conventions) |
|
\(n^{th}\) atom |
|
atoms that are both terminal and heavy |
|
atom membership in a set of atoms |
Listing 2: Using predefined atom functors
package openeye.docexamples.oechem;
import openeye.oechem.*;
public class FunctorsAtom {
public static void main(String argv[]) {
OEGraphMol mol = new OEGraphMol();
oechem.OESmilesToMol(mol,"c1cc[nH]c1CC2COCNC2");
System.out.println("Number of heavy atoms = " +
oechem.OECount(mol, new OEIsHeavy()));
System.out.println("Number of ring atoms = " +
oechem.OECount(mol, new OEAtomIsInRing()));
}
}
The output of the preceding program is the following:
Number of heavy atoms = 12
Number of ring atoms = 11
Atomic Number Functors¶
Access |
Functor Name |
---|---|
atoms with specified atomic number |
|
carbon atoms |
|
halogen atoms |
|
heavy atoms |
|
hetero atoms |
|
explicit hydrogen atoms |
|
metal atoms |
|
nitrogen atoms |
|
oxygen atoms |
|
sulfur atoms |
|
phosphorus atoms |
|
non-carbon atoms |
|
polar hydrogen atoms |
Please note that the following two lines produce the same result.
System.out.println("Number of oxygen atoms = " +
oechem.OECount(mol, new OEHasAtomicNum(OEElemNo.O)));
System.out.println("Number of oxygen atoms = " +
oechem.OECount(mol, new OEIsOxygen()));
Bond Functors¶
Access |
Functor Name |
---|---|
ring bonds |
|
chain bonds |
|
bond with specified bond index |
|
bond with selected bond index |
|
bonds with specified bond order |
|
rotatable bonds |
|
chiral bonds |
|
bonds with specific bond stereo |
|
aromatic bonds |
|
bond membership in a set of bonds |
Listing 3: Using predefined bond functors
package openeye.docexamples.oechem;
import openeye.oechem.*;
public class FunctorsBond {
public static void main(String argv[]) {
OEGraphMol mol = new OEGraphMol();
oechem.OESmilesToMol(mol,"CC(=O)Nc1c[nH]cc1");
System.out.println("Number of ring bonds = " +
oechem.OECount(mol, new OEBondIsInRing()));
System.out.println("Number of rotor bonds = " +
oechem.OECount(mol, new OEIsRotor()));
}
}
The output of the preceding program is the following:
Number of ring bonds = 5
Number of rotor bonds = 2
Group Functors¶
Access |
Functor Name |
---|---|
groups with a specific atom |
|
groups with a specific bond |
|
groups with a specific type |
|
groups that store MDL stereo information |
Reaction Component Functors¶
Access |
Functor Name |
---|---|
atoms of the catalysts or solvents of a reaction |
|
atoms of the product molecule(s) |
|
atoms of the reactant molecule(s) |
Conformer Functors¶
Access |
Functor Name |
---|---|
conformer with specified index |
|
conformer with selected index |
Residue Data Functors¶
Access |
Functor Name |
---|---|
atoms with specified residue properties |
|
atoms with specified chain id |
|
atoms with specified residue number |
|
atoms with an alternate location |
|
atoms with specified fragment number |
|
atoms with specified PDB index |
|
alpha carbon |
|
backbone atom |
|
water |
|
nucleic acid base |
|
nucleic acid sugar |
|
nucleic acid phosphate |
Composite Functors¶
Occasionally, one may want to use a logical operator to join two or more functors. The following table shows the composite functors defined in OEChem TK.
Logical Not |
Logical or |
Logical and |
Description |
---|---|---|---|
atom composite functors |
|||
bond composite functors |
|||
conformation composite functors |
|||
group composite functors |
|||
roleset composite functors |
Each composite functor takes the appropriate number of predicates as arguments and generates a single unary predicate. The following example demonstrates how to use composite functors to build expressions from OEChem TK’s predefined atom predicates.
Listing 4: Combining predefined atom predicates
package openeye.docexamples.oechem;
import openeye.oechem.*;
public class CompositionFunctorsAtom {
public static void main(String argv[]) {
OEGraphMol mol = new OEGraphMol();
oechem.OESmilesToMol(mol, "c1cnc(O)cc1CCCBr");
System.out.println("Number of chain atoms = " +
oechem.OECount(mol, new OENotAtom( new OEAtomIsInRing())));
System.out.println("Number of aromatic nitrogens = " +
oechem.OECount(mol, new OEAndAtom(new OEIsNitrogen(),
new OEIsAromaticAtom())));
System.out.println("Number of non-carbons = " +
oechem.OECount(mol, new OENotAtom(new OEHasAtomicNum(OEElemNo.C))));
System.out.println("Number of nitrogen and oxygen atoms = " +
oechem.OECount(mol, new OEOrAtom(new OEHasAtomicNum(OEElemNo.N),
new OEHasAtomicNum(OEElemNo.O))));
}
}
The OECount
function returns the number or objects (in this case atoms)
matching the given predicate argument.
The output of the preceding program is the following:
Number of chain atoms = 5
Number of aromatic nitrogens = 1
Number of non-carbons = 3
Number of nitrogen and oxygen atoms = 2
Composite functors can be used similarly to combine predefined bond predicates.
Listing 5: Combining predefined bond predicates
package openeye.docexamples.oechem;
import openeye.oechem.*;
public class CompositionFunctorsBond {
public static void main(String argv[]) {
OEGraphMol mol = new OEGraphMol();
oechem.OESmilesToMol(mol, "N#CCC1CCNC=C1");
System.out.println("Number of non-rotatable bonds = " +
oechem.OECount(mol,new OENotBond(new OEIsRotor())));
System.out.println("Number of ring double bonds = " +
oechem.OECount(mol,new OEAndBond(new OEBondIsInRing(),
new OEHasOrder(2))));
System.out.println("Number of double or triple bonds = " +
oechem.OECount(mol,new OEOrBond(new OEHasOrder(2),
new OEHasOrder(3))));
}
}
The output of the preceding program is the following:
Number of non-rotatable bonds = 8
Number of ring double bonds = 1
Number of double or triple bonds = 2
User Defined Functors¶
While many predefined functors exist in OEChem TK, it is not difficult to find a situation which calls for a new user-defined functor.
User-defined functor can be written by deriving from either
the OEUnaryAtomPred
or the OEUnaryBondPred
class.
The following example shows a user defined atom functor which returns true for aliphatic nitrogens.
Listing 6: User defined atom predicate
package openeye.docexamples.oechem;
import openeye.oechem.*;
public class UserPredAtom {
private static class PredAliphaticNitrogen extends OEUnaryAtomPred {
public boolean constCall(OEAtomBase atom) {
return atom.IsNitrogen() && ! atom.IsAromatic();
}
public OEUnaryAtomBoolFunc CreateCopy() {
OEUnaryAtomBoolFunc copy = new PredAliphaticNitrogen();
copy.swigReleaseOwnership();
return copy;
}
}
public static void main(String argv[]) {
OEGraphMol mol = new OEGraphMol();
oechem.OESmilesToMol(mol,"c1cc[nH]c1CC2COCNC2");
System.out.println("Number of aliphatic N atoms = " +
oechem.OECount(mol,new PredAliphaticNitrogen()));
}
}
The output of the preceding program is the following:
Number of aliphatic N atoms = 1
A bond predicate can be similarly defined by deriving from the
OEUnaryBondPred
class.
Listing 7: User defined bond predicate
package openeye.docexamples.oechem;
import openeye.oechem.*;
public class UserPredBond {
private static class PredHasDoubleBondO extends OEUnaryAtomPred {
public boolean constCall(OEAtomBase atom) {
for (OEBondBase bond : atom.GetBonds()) {
if (bond.GetOrder() == 2 && bond.GetNbr(atom).IsOxygen())
return true;
}
return false;
}
public OEUnaryAtomBoolFunc CreateCopy() {
OEUnaryAtomBoolFunc copy = new PredHasDoubleBondO();
copy.swigReleaseOwnership();
return copy;
}
}
private static class PredAmideBond extends OEUnaryBondPred {
public boolean constCall(OEBondBase bond) {
if (bond.GetOrder() != 1)
return false;
OEAtomBase atomB = bond.GetBgn();
OEAtomBase atomE = bond.GetEnd();
OEUnaryAtomBoolFunc pred = new PredHasDoubleBondO();
if (atomB.IsCarbon() && atomE.IsNitrogen() && pred. constCall(atomB))
return true;
if (atomB.IsNitrogen() && atomE.IsCarbon() && pred. constCall(atomE))
return true;
return false;
}
public OEUnaryBondBoolFunc CreateCopy() {
OEUnaryBondBoolFunc copy = new PredAmideBond();
copy.swigReleaseOwnership();
return copy;
}
}
public static void main(String argv[]) {
OEGraphMol mol = new OEGraphMol();
oechem.OESmilesToMol(mol,"CC(=O)Nc1c[nH]cc1");
System.out.println("Number of amide bonds = " +
oechem.OECount(mol,new PredAmideBond()));
}
}
The output of the preceding program is the following:
Number of amide bonds = 1
One advantage of functors over function pointers is that they can hold state. Since this state is held by the instance of the object it can be thread safe (unlike static-variables inside functions used with function pointers). The state of a functor can be initialized at construction. For instance, OEHasAtomicNum functor takes an argument on construction which defines which atomic number is required for the functor to return true.
Listing 8: User defined atom predicate with state
package openeye.docexamples.oechem;
import openeye.oechem.*;
public class UserPredAtomArg {
private static class PredAtomicNumList extends OEUnaryAtomPred {
public PredAtomicNumList(OEUIntArray a)
{
alist = new OEUIntArray(a);
}
public boolean constCall(OEAtomBase atom) {
for (int i =0; i < alist.len(); ++i) {
if (atom.GetAtomicNum() == alist.getItem(i))
return true;
}
return false;
}
public OEUnaryAtomBoolFunc CreateCopy() {
OEUnaryAtomBoolFunc copy = new PredAtomicNumList(alist);
copy.swigReleaseOwnership();
return copy;
}
private OEUIntArray alist;
}
public static void main(String argv[]) {
OEGraphMol mol = new OEGraphMol();
oechem.OESmilesToMol(mol,"c1cc[nH]c1CC2COCNC2");
OEUIntArray alist = new OEUIntArray(2);
alist.setItem(0,OEElemNo.O);
alist.setItem(1,OEElemNo.N);
System.out.println("Number of oxygen or nitrogen atoms = " +
oechem.OECount(mol, new PredAtomicNumList(alist)));
}
}
Functor substructure-based matching¶
The Listing 6
shows an example how to create a
user-defined atom predicate. OEChem TK also provides a functor template,
called OEMatchFunc, that allows convenient substructure-based
atom matching.
In the following example functors are initialized with a SMARTS string. These functors return true only if the atom matches the substructure pattern specified in construction.
Listing 9: Functor substructure-based matching
package openeye.docexamples.oechem;
import openeye.oechem.*;
public class FunctorsOEMatch {
public static void main(String argv[]) {
OEGraphMol mol = new OEGraphMol();
oechem.OESmilesToMol(mol,"C1(Cl)C(N)C(F)OC1C(=O)NCCCN");
System.out.println("Number of non-amide nitrogen = " +
oechem.OECount(mol,new OEMatchAtom("[N;!$(NC=O)]")));
System.out.println("Number of 5-membered ring oxygen = " +
oechem.OECount(mol,new OEMatchAtom("[O;r5]")));
System.out.println("Number of carbon attached to halogen = " +
oechem.OECount(mol,new OEMatchAtom("[#6][Cl,Br,F]")));
}
}
The output of Listing 9
is the following:
Number of non-amide nitrogen = 2
Number of 5-membered ring oxygen = 1
Number of carbon attached to halogen = 2
Molecule Partitioning¶
The OESubsetMol
function can take any atom predicate
as an argument and generate a subset molecule from only atoms for which the
specified predicate returns true. In the following example, ring atoms are
extracted from a molecule by using the OEAtomIsInRing
atom functor.
Listing 10: Ring system extraction
package openeye.docexamples.oechem;
import openeye.oechem.*;
public class FunctorsOESubsetMol {
public static void main(String argv[]) {
OEGraphMol mol = new OEGraphMol();
oechem.OESmilesToMol(mol,"c1cc[nH]c1CC2COCNC2");
OEGraphMol submol = new OEGraphMol();
oechem.OESubsetMol(submol,mol,new OEAtomIsInRing(),true);
System.out.println(oechem.OEMolToSmiles(submol));
}
}
The output of Listing 10
is the following:
c1cc[nH]c1.C1CNCOC1
In the following example, ring systems are extracted from a molecule
by using OEPartPred
functor.
Listing 11: Ring system extraction
package openeye.docexamples.oechem;
import openeye.oechem.*;
public class FunctorsOEPartPred {
public static void main(String argv[]) {
OEGraphMol mol = new OEGraphMol();
oechem.OESmilesToMol(mol,"c1cc[nH]c1CC2COCNC2");
int[] rings = new int[mol.GetMaxAtomIdx()];
int nrrings = oechem.OEDetermineRingSystems(mol,rings);
OEPartPredAtom pred = new OEPartPredAtom(rings);
System.out.println("Number of rings = " + nrrings);
for (int r = 1; r < nrrings+1; ++r) {
pred.SelectPart(r);
OEGraphMol ringmol = new OEGraphMol();
oechem.OESubsetMol(ringmol,mol,pred,true);
System.out.println(r + " -> " + oechem.OEMolToSmiles(ringmol));
}
}
}
The output of Listing 11
is the following:
Number of rings = 2
1 -> c1cc[nH]c1
2 -> C1CNCOC1