Generic Data

Previous chapters Molecule Properties, Atom Properties, Bond Properties have described how common global properties of molecule, atoms, and bonds can be modified and accessed. There are applications, however, when associating arbitrary data with objects such as molecules, atoms and bonds is necessary. OEChem provides a framework to solve this problem by allowing to attach generic data to an object by association either with an integer or character string, called tag identifier.

The following two snippets demonstrate how generic data (for example molecule weight) can be attached to a molecule:

int tag = oechem.OEGetTag("MolWeight");
mol.SetDoubleData(tag, oechem.OECalculateMolecularWeight(mol));
mol.SetDoubleData("MolWeight", oechem.OECalculateMolecularWeight(mol));

After annotation, the data can be accessed with the same integer or character string identifier:

tag = oechem.OEGetTag("MolWeight");
System.out.println(mol.GetDoubleData(tag));
System.out.println(mol.GetDoubleData("MolWeight"));

Warning

The integer tag of a generic data should always be allocated using the OEGetTag function.

The following table shows the basic methods of the OEBase class that allow the manipulation of generic data.

Methods to manipulate generic data
Method Description
OEBase.SetData sets a generic data associating it with the given tag
OEBase.AddData adds a generic data associating it with the given tag
OEBase.HasData determines whether a molecule has any generic data with a given tag
OEBase.GetData returns the generic data associated with the given tag
OEBase.DeleteData deletes all generic data with the given tag
OEBase.Clear clears all stored generic data

The main difference between the OEBase.SetData method and the OEBase.AddData method is that if a data with the same identifier is already attached to an object then:

Furthermore, OEBase.SetData does not allow replacing an existing tag with a different data type:

int tag = oechem.OEGetTag("MolWeight");
double weight =  oechem.OECalculateMolecularWeight(mol);
mol.SetDoubleData(tag, weight);
mol.SetIntData(tag, (int)weight);

The above code will throw the following warning:

Warning: data type mismatch found when using generic data

Attaching plain old data

The following simple code demonstrate how data calculated and attached to a molecule in one function can be accessed later on through the tag identifier.

Listing 1: Example of using generic data

package openeye.docexamples.oechem;

import openeye.oechem.*;

public class GenericDataMolWeight {
    private static void calculateMoleculeWeight(OEMolBase mol) {
        mol.SetDoubleData("MolWeight", oechem.OECalculateMolecularWeight(mol));
    }

    private static void printMoleculeWeight(OEMolBase mol) {
        int tag = oechem.OEGetTag("MolWeight");
        if (mol.HasData(tag)) {
            System.out.println("molecule weight = " + mol.GetDoubleData(tag));
        }
        else {
            System.out.println("molecule weight is not calculated!");
        }
    }

    public static void main(String argv[]) {
        OEGraphMol mol = new OEGraphMol();
        oechem.OESmilesToMol(mol, "C1CCCC(C(=O)O)C1");

        calculateMoleculeWeight(mol);
        printMoleculeWeight(mol);
    }
}

Note

It is a good programming practice to call OEBase.HasData in order to check whether or not a data exists before trying to access it by the OEBase.GetData method.

Attaching data to atoms

Generic data can be attached to any object that derives from the OEBase class. The following program shows an example where hydrogen bonding donor property is attached as a bool value to the corresponding OEAtomBase object.

Listing 2: Example of attaching generic data to atoms

package openeye.docexamples.oechem;

import openeye.oechem.*;

public class GenericDataToAtom {
    private static class IsDonorAtomPred extends OEUnaryAtomPred {
        public boolean constCall(OEAtomBase atom) {
            return atom.GetBoolData("isdonor");
        }
        public OEUnaryAtomBoolFunc CreateCopy() {
            OEUnaryAtomBoolFunc copy = new IsDonorAtomPred();
            copy.swigReleaseOwnership();
            return copy;
        }
    }

    public static void main(String argv[]) {
        OEGraphMol mol = new OEGraphMol();
        oechem.OESmilesToMol(mol, "c1c(Cl)cncc1C(=O)O");

        OEMatchAtom IsDonorAtom = new OEMatchAtom("[!H0;#7,#8]");
        for (OEAtomBase atom : mol.GetAtoms()) {
            atom.SetBoolData("isdonor", IsDonorAtom.constCall(atom));
        }

        System.out.print("Donor atoms: ");
        for (OEAtomBase atom : mol.GetAtoms(new IsDonorAtomPred())) {
            System.out.print(atom.GetIdx() + " " +  oechem.OEGetAtomicSymbol(atom.GetAtomicNum()));
        }
        System.out.println("");
    }
}

See also

Attaching other objects

The type of the generic data is not restricted to fundamental data types of the programming language. High-level OEChem objects such as OEMolBase, OEAtomBase, OEBondBase OEScalarGrid, OESkewGrid and OESurface can also be stored through this mechanism. The following program demonstrates how to attach a subset of a molecule to the original molecule as generic data.

Listing 3: Example of attaching a molecule as generic data

package openeye.docexamples.oechem;

import openeye.oechem.*;

public class GenericDataMol {

    public static void main(String argv[]) {
        OEGraphMol mol = new OEGraphMol();
        oechem.OESmilesToMol(mol, "c1ccccc1O");

        OEGraphMol frag = new OEGraphMol();
        oechem.OESubsetMol(frag, mol, new OEIsCarbon());

        oechem.OESetOEGraphMol(mol, "just_carbon", frag);

        OEGraphMol justCarbon = oechem.OEGetOEGraphMol(mol, "just_carbon");
    }
}

Note

Generic data attached to a molecule or any of its atoms or bonds is automatically saved when the molecule is written into an .oeb file.