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:

tag = OEGetTag("MolWeight")
mol.SetData(tag, OECalculateMolecularWeight(mol))
mol.SetData("MolWeight", OECalculateMolecularWeight(mol))

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

tag = OEGetTag("MolWeight")
print (mol.GetData(tag))
print (mol.GetData("MolWeight"))


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:

tag = OEGetTag("MolWeight")
weight = OECalculateMolecularWeight(mol)
mol.SetData(tag, float(weight))
mol.SetData(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

#!/usr/bin/env python
from __future__ import print_function
from openeye.oechem import *

def CalculateMoleculeWeight(mol):
    mol.SetData("MolWeight", OECalculateMolecularWeight(mol))

def PrintMoleculeWeight(mol):
    tag = OEGetTag("MolWeight")
    if mol.HasData(tag):
        print ("molecule weight =", mol.GetData(tag))
        print ("molecule weight is not calculated!")

mol = OEGraphMol()
OESmilesToMol(mol, "C1CCCC(C(=O)O)C1")



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.

The OEBase.GetData method returns only the first instance of data stored previously with the associated data tag. Data can also be accessed by using the OEBase.GetDataIter method that returns an iterator over all data stored.

mol = OEGraphMol()
OEParseSmiles(mol, "C1CCCC(C(=O)O)C1")
activitytag = OEGetTag("activity")
mol.AddData(activitytag, "antiarthritic")
mol.AddData(activitytag, "antiinflammatory")
mol.SetData("weight", OECalculateMolecularWeight(mol))

for gdata in mol.GetDataIter():
    print (OEGetTag(gdata.GetTag()), gdata.GetData())

The output of code snippet above is the following:

activity antiarthritic
activity antiinflammatory
weight 128.16898

The OEBase.GetDataIter method can also take a tag identifier. In this case it iterates over only data associated with the given tag. For example, the following code will only prints out the two piece of ‘activity’ data.

for gdata in mol.GetDataIter(OEGetTag("activity")):
    print (OEGetTag(gdata.GetTag()), gdata.GetData())

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

#!/usr/bin/env python
from __future__ import print_function
from openeye.oechem import *

IsDonorAtom = OEMatchAtom("[!H0;#7,#8]")

mol = OEGraphMol()
OESmilesToMol(mol, "c1c(Cl)cncc1C(=O)O")

for atom in mol.GetAtoms():
    atom.SetData("isdonor", IsDonorAtom(atom))

class IsDonorAtomPred(OEUnaryAtomPred):
    def __call__(self, atom):
        return atom.GetData("isdonor")

print ("Donor atoms:", end=" ")
for atom in mol.GetAtoms(IsDonorAtomPred()):
    print (atom.GetIdx(), OEGetAtomicSymbol(atom.GetAtomicNum()), end=" ")

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

#!/usr/bin/env python
from openeye.oechem import *
mol = OEGraphMol()
OESmilesToMol(mol, "c1ccccc1O")

frag = OEGraphMol()
OESubsetMol(frag, mol, OEIsCarbon())

mol.SetData("just_carbon", frag)
justCarbon = mol.GetData("just_carbon")

Sequences of objects can be stored as well. The following example shows how to attach a sequence of atoms into an OEAtomBase object.

Listing 4: Example of attaching list of atoms as generic data

#!/usr/bin/env python
from __future__ import print_function
from openeye.oechem import *

def CollectIncorrectStereo(mol):
    for atom in mol.GetAtoms():
        if atom.IsAromatic() and atom.HasStereoSpecified(OEAtomStereo_Tetrahedral):
            neighs = []
            for n in atom.GetAtoms():
            atom.SetData("incorrect_stereo_neighs", neighs)

def RemoveIncorrectStereo(mol):
    for atom in mol.GetAtoms():
        if atom.HasData("incorrect_stereo_neighs"):
            neighs = [mol.GetAtom(OEHasAtomIdx(a)) for a in atom.GetData("incorrect_stereo_neighs")]
            atom.SetStereo(neighs, OEAtomStereo_Tetrahedral, OEAtomStereo_Undefined)

mol = OEGraphMol()

OESmilesToMol(mol, "c1c[n@@H]cc1")

print (OEMolToSmiles(mol))


print (OEMolToSmiles(mol))

See also


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.