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 3 shows how store and retrieve fingerprints as generic data.

Listing 3: Storing and retrieving fingerprint as generic data

tag = "FP_DATA"
mol = oechem.OEGraphMol()
oechem.OESmilesToMol(mol, "c1ccccc1")

fp = oegraphsim.OEFingerPrint()
oegraphsim.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 4 demonstrates how to create an 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 accessed rapidly with the PATH_FP tag. This eliminates the on-the-fly generation of the fingerprints.

See also

Additional examples in Listing 10 and Listing 12 of the Fingerprint Database chapter.

Listing 4: Fingerprint generation and storage in OEB

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

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

ofs = oechem.oemolostream()
if not ofs.open(sys.argv[2]):
    oechem.OEThrow.Fatal("Unable to open %s for writing" % sys.argv[2])
if ofs.GetFormat() != oechem.OEFormat_OEB:
    oechem.OEThrow.Fatal("%s output file has to be an OEBinary file" % sys.argv[2])

fp = oegraphsim.OEFingerPrint()
for mol in ifs.GetOEGraphMols():
    oegraphsim.OEMakeFP(fp, mol, oegraphsim.OEFPType_Path)
    mol.SetData("PATH_FP", fp)
    oechem.OEWriteMolecule(ofs, mol)

The following code snippets shows how to generate a bitstring from an OEFingerPrint object.

Listing 5: Accessing a fingerprint as a bitstring

def GetBitString(fp):
    bitstring = ''
    for b in range(0, fp.GetSize()):
        if fp.IsBitOn(b):
            bitstring += '1'
        else:
            bitstring += '0'

    return bitstring

See also

Warning

Even though the GraphSim TK library provides a fingerprint API for the LINGO similarity search method, it is not implemented as a real fingerprint, so bitstrings that are generated from LINGO fingerprints are meaningless.

Fingerprints can also be stored in SDF files. The Listing 6 demonstrates how to create an SDF and store fingerprints as hexadecimal strings.

After the fingerprint is generated, it is attached to the molecule as an SD data tag. The identifier of the fingerprint in the SDF file will be the string representation of the fingerprint type. The bitvector of the fingerprint is converted to a hexadecimal string with the OEBitVector.ToHexString method.

Listing 6: Storing fingerprint in SDF

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

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

ofs = oechem.oemolostream()
if not ofs.open(sys.argv[2]):
    oechem.OEThrow.Fatal("Unable to open %s for writing" % sys.argv[2])
if ofs.GetFormat() != oechem.OEFormat_SDF:
    oechem.OEThrow.Fatal("%s output file has to be an SDF file" % sys.argv[2])

fp = oegraphsim.OEFingerPrint()
for mol in ifs.GetOEGraphMols():
    oegraphsim.OEMakeFP(fp, mol, oegraphsim.OEFPType_Circular)
    fptypestr = fp.GetFPTypeBase().GetFPTypeString()
    fphexdata = fp.ToHexString()
    oechem.OESetSDData(mol, fptypestr, fphexdata)
    oechem.OEWriteMolecule(ofs, mol)

The following example (Listing 7) shows how to retrieve fingerprints from an SDF file. When looping over the SD data the OEIsValidFPTypeString functions can be used to identify SD data that stores a fingerprint. The tag of the data is the string representation of the fingerprint type. This string representation can be used to generate the corresponding OEFPTypeBase object by using the OEGetFPType function. The type of the OEFingerPrint object then has to be set by the OEFingerPrint.SetFPTypeBase method. Finally, the bitvector of the fingerprint then can be initialized from the hexadecimal string by using the OEBitVector.FromHexString method.

Listing 7: Retrieving fingerprint from SDF

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

ifs = oechem.oemolistream()
if not ifs.open(sys.argv[1]):
    oechem.OEThrow.Fatal("Unable to open %s for reading" % sys.argv[1])
if ifs.GetFormat() != oechem.OEFormat_SDF:
    oechem.OEThrow.Fatal("%s input file has to be an SDF file" % sys.argv[1])

molcounter = 0
fpcounter = 0
for mol in ifs.GetOEGraphMols():
    molcounter += 1
    for dp in oechem.OEGetSDDataPairs(mol):
        if oegraphsim.OEIsValidFPTypeString(dp.GetTag()):
            fpcounter += 1
            fptypestr = dp.GetTag()
            fphexdata = dp.GetValue()

            fp = oegraphsim.OEFingerPrint()
            fptype = oegraphsim.OEGetFPType(fptypestr)
            fp.SetFPTypeBase(fptype)
            fp.FromHexString(fphexdata)

print("Number of molecules = %d" % molcounter)
print("Number of fingerprints = %d" % fpcounter)

See also