#!/usr/bin/env python
# (C) 2022 Cadence Design Systems, Inc. (Cadence) 
# All rights reserved.
# TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
# provided to current licensees or subscribers of Cadence products or
# SaaS offerings (each a "Customer").
# Customer is hereby permitted to use, copy, and modify the Sample Code,
# subject to these terms. Cadence 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 Cadence 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 Cadence be
# liable for any damages or liability in connection with the Sample Code
# or its use.

from openeye import oechem
from openeye import oegraphsim
import sys

if len(sys.argv) != 3:
    oechem.OEThrow.Usage("%s <queryfile> <targetfile>" % 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])

qmol = oechem.OEGraphMol()
oechem.OEReadMolecule(ifs, qmol)
qfp = oegraphsim.OEFingerPrint()
oegraphsim.OEMakeFP(qfp, qmol, oegraphsim.OEFPType_Path)

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


fpdb = oegraphsim.OEFPDatabase(qfp.GetFPTypeBase())
for tmol in ifs.GetOEGraphMols():
    fpdb.AddFP(tmol)

print("Tanimoto scores:")
# @ <SNIPPET-GET-SCORES-1>
descending = True
fpdb.SetSimFunc(oegraphsim.OESimMeasure_Tanimoto, descending)
fpdb.SetCutoff(0.1)
for score in fpdb.GetScores(qfp, 0, 100):
    print("%.3f" % score.GetScore())
# @ </SNIPPET-GET-SCORES-1>

fpdb.ClearCutoff()
print("Tversky scores:")
# @ <SNIPPET-GET-SCORES-2>
fpdb.SetSimFunc(oegraphsim.OETverskySim(0.9))
for score in fpdb.GetScores(qfp):
    print("%.3f" % score.GetScore())
# @ </SNIPPET-GET-SCORES-2>

print("Dice scores:")
# @ <SNIPPET-GET-SCORES-3>
descending = True
fpdb.SetSimFunc(oegraphsim.OESimMeasure_Dice, not descending)
fpdb.SetCutoff(0.5)
for score in fpdb.GetScores(qfp, 100):
    print("%.3f" % score.GetScore())
# @ </SNIPPET-GET-SCORES-3>

print("Cosine scores with option:")
# @ <SNIPPET-GET-SCORES-OPTIONS>
opts = oegraphsim.OEFPDatabaseOptions()
opts.SetCutoff(0.3)
opts.SetSimFunc(oegraphsim.OESimMeasure_Cosine)
for score in fpdb.GetScores(qfp, opts):
    print("%.3f" % score.GetScore())
# @ </SNIPPET-GET-SCORES-OPTIONS>

print("Tanimoto sorted scores:")
fpdb.ClearCutoff()  # default
descending = True
fpdb.SetSimFunc(oegraphsim.OESimMeasure_Tanimoto, descending)  # default
# @ <SNIPPET-GET-SORTED-SCORES-1>
for score in fpdb.GetSortedScores(qfp, 10):
    print("%.3f" % score.GetScore())
# @ </SNIPPET-GET-SORTED-SCORES-1>

print("Dice sorted scores:")
# @ <SNIPPET-GET-SORTED-SCORES-2>
descending = True
fpdb.SetSimFunc(oegraphsim.OESimMeasure_Dice, descending)
fpdb.SetCutoff(0.5)
for score in fpdb.GetSortedScores(qfp, 0, 0, 100):
    print("%.3f" % score.GetScore())
# @ </SNIPPET-GET-SORTED-SCORES-2>

print("Manhattan sorted scores:")
# @ <SNIPPET-GET-SORTED-SCORES-3>
descending = True
fpdb.SetSimFunc(oegraphsim.OESimMeasure_Manhattan, not descending)
fpdb.SetCutoff(0.3)
for score in fpdb.GetSortedScores(qfp, 5, 100):
    print("%.3f" % score.GetScore())
# @ </SNIPPET-GET-SORTED-SCORES-3>

print("Tversky sorted scores with options:")
# @ <SNIPPET-GET-SORTED-SCORES-OPTIONS>
opts = oegraphsim.OEFPDatabaseOptions()
opts.SetDescendingOrder(True)
opts.SetCutoff(0.3)
opts.SetSimFunc(oegraphsim.OESimMeasure_Tversky)
opts.SetTverskyCoeffs(0.9, 0.1)
opts.SetLimit(10)
for score in fpdb.GetSortedScores(qfp, opts):
    print("%.3f" % score.GetScore())
# @ </SNIPPET-GET-SORTED-SCORES-OPTIONS>
