Storage and RetrievalΒΆ

The OEFingerPrint does not store any reference to the molecule from which it was generated. The user has to keep track of which fingerprint corresponds to which molecule. One way to do this is to attach the fingerprint, as generic data, to the molecule. Listing 1 shows how store and retrieve fingerprints as generic data.

Listing 1: Storing and retrieving fingerprint as generic data

#!/usr/bin/env python
import sys
from openeye.oechem import *
from openeye.oegraphsim import *

tag = "FP_DATA"
mol = OEGraphMol()
OEParseSmiles(mol, "c1ccccc1")

fp = OEFingerPrint()
OEMakeLingoFP(fp, mol)
mol.SetData(tag, fp)

if mol.HasData(tag):
    f = mol.GetData(tag)
    if f.IsValid():
        fptype = f.GetFPTypeBase().GetFPTypeString() 
        print "%s fingerprint with `%s` identifier " % (fptype, tag)
It is good practice to check the validity of the fingerprint after retrieving it. The OEFingerPrint.IsValid method returns true if the fingerprint was successfully initialized.

Listing 2 demonstrates how to create a OEB binary file that stores molecules along with their fingerprints. When reading the OEB file that was generated by this program, the pre-calculated fingerprints can be rapidly accessed with the PATH_FP tag. This eliminates the on-the-fly generation of the fingerprints.

See also

Additional examples in Listing 2 and Listing 4 of the Fingerprint Database chapter.

Listing 2: Fingerprint generation and storage in OEB

#!/usr/bin/env python
import sys
from openeye.oechem import *
from openeye.oegraphsim import *

if len(sys.argv) != 3:
    OEThrow.Usage("%s <infile> <outfile>" % sys.argv[0])

ifs = oemolistream()
if not ifs.open(sys.argv[1]):
    OEThrow.Fatal("Unable to open %s for reading" % sys.argv[1])

ofs = oemolostream()
if not ofs.open(sys.argv[2]):
    OEThrow.Fatal("Unable to open %s for writing" % sys.argv[2])
if ofs.GetFormat() != OEFormat_OEB:
    OEThrow.Fatal("%s output file has to be an OEBinary file" % sys.argv[2])
     
fp = OEFingerPrint()
for mol in ifs.GetOEGraphMols():
    OEMakeFP(fp, mol, OEFPType_Path)
    mol.SetData(tag, "PATH_FP", fp)
    OEWriteMolecule(ofs, mol)

Previous topic

Fingerprint Generation

Next topic

Similarity Measures

Quick search