#!/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.

import sys
import math
from openeye import oechem

if len(sys.argv) != 3:
    oechem.OEThrow.Usage("{} <protein> <ligand>".format(sys.argv[0]))

prot = oechem.OEGraphMol()
pifs = oechem.oemolistream(sys.argv[1])
if not oechem.OEReadMolecule(pifs, prot):
    oechem.OEThrow.Fatal("Cannot read protein from file %s!", sys.argv[1])

lig = oechem.OEGraphMol()
lifs = oechem.oemolistream(sys.argv[2])
if not oechem.OEReadMolecule(lifs, lig):
    oechem.OEThrow.Fatal("Cannot read ligand from file %s!", sys.argv[2])

asite = oechem.OEInteractionHintContainer(prot, lig)
if not oechem.OEIsValidActiveSite(asite):
    oechem.OEThrow.Fatal("Cannot initialize active site!")

# @ <SNIPPET-INTER>
oechem.OEPerceiveInteractionHints(asite)
itype = oechem.OEHBondInteractionHint(oechem.OEHBondInteractionHintType_LigandDonates)
ipred = oechem.OEHasInteractionHintType(itype)
for inter in asite.GetInteractions(ipred):
    itype = inter.GetInteractionType()
    print("Calculated geometries for '{}' interaction type:".format(itype.GetName()))
    for gname in inter.GetCalculatedGeometries():
        print("{0:30} : {1:.3f}".format(gname, inter.GetCalculatedGeometry(gname)))
# @ </SNIPPET-INTER>
print("")

iter = asite.GetInteractions(ipred)
inter = iter.Target()
# @ <SNIPPET-HAS-GEOMETRY>
if inter.HasCalculatedGeometry("distance"):
    distance = inter.GetCalculatedGeometry("distance")
    print("distance: {:.3f}  Angstrom(s)".format(distance))
if inter.HasCalculatedGeometry("acceptor-angle"):
    angle = math.degrees(inter.GetCalculatedGeometry("acceptor-angle"))
    print("acceptor angle: {:.3f} degree(s)".format(angle))
# @ </SNIPPET-HAS-GEOMETRY>
print("")

# @ <SNIPPET-INTER-TYPE>
hbondacc = oechem.OEHBondInteractionHint(oechem.OEHBondInteractionHintType_LigandAccepts)
print("Calculated geometries for '{}' interaction type:".format(hbondacc.GetName()))
for geom in oechem.OEGetCalculatedInteractionGeometries(hbondacc):
    print(geom)
#  </SNIPPET-INTER-TYPE>
