#!/usr/bin/env python
# (C) 2017 OpenEye Scientific Software Inc. All rights reserved.
#
# TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
# provided to current licensees or subscribers of OpenEye products or
# SaaS offerings (each a "Customer").
# Customer is hereby permitted to use, copy, and modify the Sample Code,
# subject to these terms. OpenEye claims no rights to Customer's
# modifications. Modification of Sample Code is at Customer's sole and
# exclusive risk. Sample Code may require Customer to have a then
# current license or subscription to the applicable OpenEye offering.
# THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED.  OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall OpenEye be
# liable for any damages or liability in connection with the Sample Code
# or its use.

from openeye import oechem
from openeye import oegraphsim


# @ <SNIPPET-SIM-FUNC-1>
class SimpsonSimFunc(oegraphsim.OESimFuncBase):

    def __call__(self, fpA, fpB):
        onlyA, onlyB, bothAB, neitherAB = oechem.OEGetBitCounts(fpA, fpB)
        if onlyA + onlyB == 0:
            return 1.0
        if bothAB == 0:
            return 0.0
        sim = float(bothAB)
        sim /= min(float(onlyA + bothAB), float(onlyB + bothAB))
        return sim

    def GetSimTypeString(self):
        return "Simpson"

    def CreateCopy(self):
        return SimpsonSimFunc().__disown__()
# @ </SNIPPET-SIM-FUNC-1>


smiles = ["c1ccc2c(c1)c(c(oc2=O)OCCSC(=N)N)Cl",
          "COc1cc2ccc(cc2c(=O)o1)NC(=N)N"]

# @ <SNIPPET-SIM-FUNC-2>
fpdb = oegraphsim.OEFPDatabase(oegraphsim.OEFPType_Path)
fpdb.SetSimFunc(SimpsonSimFunc())
# @ </SNIPPET-SIM-FUNC-2>

mol = oechem.OEGraphMol()
for smi in smiles:
    mol.Clear()
    oechem.OESmilesToMol(mol, smi)
    fpdb.AddFP(mol)

query = oechem.OEGraphMol()
oechem.OESmilesToMol(query, "COc1c(c2ccc(cc2c(=O)o1)NC(=N)N)Cl")
for score in fpdb.GetScores(query):
    print("%.3f" % score.GetScore())
