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 TK 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:

uint 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");
Console.WriteLine(mol.GetDoubleData(tag));
Console.WriteLine(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:

uint 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

using System;

using OpenEye.OEChem;

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

    private static void PrintMoleculeWeight(OEMolBase mol)
    {
        uint tag = OEChem.OEGetTag("MolWeight");
        if (mol.HasData(tag))
        {
            Console.WriteLine("molecule weight = {0}", mol.GetDoubleData(tag));
        }
        else
        {
            Console.WriteLine("molecule weight is not calculated!");
        }
    }

    public static int Main(String[] args)
    {
        OEGraphMol mol = new OEGraphMol();
        OEChem.OESmilesToMol(mol, "C1CCCC(C(=O)O)C1");

        CalculateMoleculeWeight(mol);
        PrintMoleculeWeight(mol);
        return 0;
    }
}

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

using System;

using OpenEye.OEChem;

public class GenericDataToAtom
{
    private class IsDonorAtomPred : OEUnaryAtomPred
    {
        public override bool Call(OEAtomBase atom)
        {
            return atom.GetBoolData("isdonor");
        }
        public override OEUnaryAtomBoolFunc CreateCopy()
        {
            OEUnaryAtomBoolFunc copy = new IsDonorAtomPred();
            copy.ReleaseOwnership();
            return copy;
        }
    }

    public static void Main(String[] args)
    {
        OEGraphMol mol = new OEGraphMol();
        OEChem.OESmilesToMol(mol, "c1c(Cl)cncc1C(=O)O");

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

        Console.Write("Donor atoms: ");
        foreach (OEAtomBase atom in mol.GetAtoms(new IsDonorAtomPred()))
        {
            Console.Write(atom.GetIdx() + " " +  OEChem.OEGetAtomicSymbol(atom.GetAtomicNum()));
        }
        Console.WriteLine("");
    }
}

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 TK 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

using System;

using OpenEye.OEChem;

public class GenericDataMol
{
    public static void Main(String[] args)
    {
        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.