#!/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 oebioisostere
from openeye import oemolprop

class ChompOptions(oechem.OEOptions):
    def __init__(self):
        oechem.OEOptions.__init__(self, "ChompOptions")

        dbParam = oechem.OEStringParameter("-out")
        dbParam.SetVisibility(oechem.OEParamVisibility_Simple)
        dbParam.SetBrief("Output database folder name")
        dbParam.SetRequired(True)
        dbParam.SetKeyless(2)
        self._dbParam = self.AddParameter(dbParam)

        self._fragOpts = oebioisostere.ToFragmentOptions(self.AddOption(oebioisostere.OEFragmentOptions()))
        self._screenOpts = oebioisostere.ToDBScreenOptions(self.AddOption(oebioisostere.OEDBScreenOptions()))
        pass

    def CreateCopy(self):
        return self

    def GetDBName(self):
        return self._dbParam.GetStringValue()

    def GetFragOpts(self):
        return self._fragOpts

    def GetScreenOpts(self):
        return self._screenOpts


def main(argv=[__name__]):
    chompOpts = ChompOptions()
    opts = oechem.OESimpleAppOptions(chompOpts, "BroodDataBase", oechem.OEFileStringType_Mol)
    if oechem.OEConfigureOpts(opts, argv, False) == oechem.OEOptsConfigureStatus_Help:
        return 0
    chompOpts.UpdateValues(opts)

    ifs = oechem.oemolistream()
    if not ifs.open(opts.GetInFile()):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % opts.GetInFile())

    print("Generating fragments...")
    builder = oebioisostere.OEDBBuilder(chompOpts.GetFragOpts())
    for mol in ifs.GetOEMols():
        builder.Generate(mol)

    print("Building 2D fragments library...")
    builder.Filter(oebioisostere.OECreateFragFilter())
    builder.Expand(oebioisostere.OECreateFlipperOptions())
    builder.Screen(chompOpts.GetScreenOpts())

    print("Generating fragment conformers...")
    writer = oebioisostere.OEDBWriter()
    writer.Init(chompOpts.GetDBName())
    count = 0
    for frag in builder.GetFrags():
        if oebioisostere.OEGenerateConformers(frag):
            writer.Write(frag)
            count += 1
    writer.Finish()
    print("Generated fragments: %d" % count)


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