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

import sys
from openeye import oechem
from openeye import oequacpac


def AssignChargesByName(mol, name):
    if name == "noop":
        return oequacpac.OEAssignCharges(mol, oequacpac.OEChargeEngineNoOp())
    elif name == "clear":
        return oequacpac.OEAssignCharges(mol, oequacpac.OEClearCharges())
    elif name == "formal":
        return oequacpac.OEAssignCharges(mol, oequacpac.OEFormalToPartialCharges())
    elif name == "initial":
        return oequacpac.OEAssignCharges(mol, oequacpac.OEInitialCharges())
    elif name == "gasteiger":
        return oequacpac.OEAssignCharges(mol, oequacpac.EGasteigerCharges())
    elif name == "mmff" or name == "mmff94":
        # @ <SNIPPET-ASSIGN-CHARGES-MMFF>
        if oequacpac.OEAssignCharges(mol, oequacpac.OEMMFF94Charges()):
            # ...
            # @ </SNIPPET-ASSIGN-CHARGES-MMFF>
            return True
        else:
            return False
    elif name == "am1":
        # @ <SNIPPET-ASSIGN-CHARGES-OPTIONS-PARAM>
        optimizationSetting = False
        if oequacpac.OEAssignCharges(mol, oequacpac.OEAM1Charges(optimizationSetting)):
            # ...
            # @ </SNIPPET-ASSIGN-CHARGES-OPTIONS-PARAM>
            return True
        else:
            return False
    elif name == "am1bcc":
        # @ <SNIPPET-ASSIGN-CHARGES-OPTIONS-METHOD>
        chargeEngine = oequacpac.OEAM1BCCCharges()
        chargeEngine.SetSymmetrize(False)
        if oequacpac.OEAssignCharges(mol, chargeEngine):
            # ...
            # @ </SNIPPET-ASSIGN-CHARGES-OPTIONS-METHOD>
            return True
        else:
            return False
    elif name == "amber" or name == "amberff94":
        return oequacpac.OEAssignCharges(mol, oequacpac.OEAmberFF94Charges())
    elif name == "am1bccelf10":
        return oequacpac.OEAssignCharges(mol, oequacpac.OEAM1BCCELF10Charges())
    return False


def main(argv=[__name__]):
    itf = oechem.OEInterface(InterfaceData)

    if not oechem.OEParseCommandLine(itf, argv):
        oechem.OEThrow.Fatal("Unable to interpret command line!")

    ims = oechem.oemolistream()

    inputFile = itf.GetString("-in")
    if not ims.open(inputFile):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % inputFile)

    mol = oechem.OEMol()
    if not oechem.OEReadMolecule(ims, mol):
        oechem.OEThrow.Fatal("Cannot read from %s" % inputFile)

    oms = oechem.oemolostream()

    outFile = itf.GetString("-out")
    if not oms.open(outFile):
        oechem.OEThrow.Fatal("Unable to open %s for writing" % outFile)

    chargeName = itf.GetString("-charge")

    if not AssignChargesByName(mol, chargeName):
        oechem.OEThrow.Fatal("Unable to assign %s charges to %s" % (chargeName, inputFile))
    else:
        oechem.OEWriteMolecule(oms, mol)

    oms.close()

#############################################################################
# INTERFACE
#############################################################################


InterfaceData = '''
!BRIEF AssignChargesDoc <inmol> [<outmol>]

!CATEGORY "input/output options :"
   !PARAMETER -in
      !ALIAS -i
      !TYPE string
      !BRIEF Input molecule
      !VISIBILITY simple
      !REQUIRED true
      !KEYLESS 1
   !END

   !PARAMETER -out
      !ALIAS -o
      !TYPE string
      !DEFAULT oeassigncharges.oeb.gz
      !BRIEF Output molecule (usually an oeb)
      !VISIBILITY simple
      !REQUIRED false
      !KEYLESS 2
   !END
!END

!CATEGORY "Charging options :"
   !PARAMETER -charge
      !ALIAS -c
      !TYPE string
      !LEGAL_VALUE noop
      !LEGAL_VALUE clear
      !LEGAL_VALUE formal
      !LEGAL_VALUE initial
      !LEGAL_VALUE gasteiger
      !LEGAL_VALUE mmff
      !LEGAL_VALUE mmff94
      !LEGAL_VALUE am1
      !LEGAL_VALUE am1bcc
      !LEGAL_VALUE amber
      !LEGAL_VALUE amberff94
      !LEGAL_VALUE am1bccelf10
      !DEFAULT mmff94
      !BRIEF which set of charges to apply
      !SIMPLE true
      !REQUIRED false
   !END
!END
'''

if __name__ == "__main__":
    sys.exit(main(sys.argv))
