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


def main(argv=[__name__]):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <input receptor> <output receptor>" % argv[0])

    # @ <SNIPPET-RECEPTOR-IO-1>
    receptor = oechem.OEGraphMol()
    inputReceptorFilename = argv[1]
    oedocking.OEReadReceptorFile(receptor, inputReceptorFilename)
    # @ </SNIPPET-RECEPTOR-IO-1>

    # @ <SNIPPET-RECEPTOR-IO-2>
    outputReceptorFilename = argv[2]
    oedocking.OEWriteReceptorFile(receptor, outputReceptorFilename)
    # @ </SNIPPET-RECEPTOR-IO-2>

    # @<SNIPPET-RECEPTOR-IO-3>
    bytes = oedocking.OEWriteReceptorToBytes(".oeb", receptor)
    receptorIO = oechem.OEGraphMol()
    status = oedocking.OEReadReceptorFromBytes(receptorIO, ".oeb", bytes)
    # @</SNIPPET-RECEPTOR-IO-3>
    print("SNIPPET-RECEPTOR-IO-3: .oeb      status={} nbytes={}".format(status, len(bytes)))

    # @<SNIPPET-RECEPTOR-IO-4>
    bytes = oedocking.OEWriteReceptorToBytes(".oeb.gz", receptor)
    receptorIO = oechem.OEGraphMol()
    status = oedocking.OEReadReceptorFromBytes(receptorIO, ".oeb.gz", bytes)
    # @</SNIPPET-RECEPTOR-IO-4>
    print("SNIPPET-RECEPTOR-IO-4: .oeb.gz   status={} nbytes={}".format(status, len(bytes)))

    # @<SNIPPET-RECEPTOR-IO-5>
    bytes = oedocking.OEWriteReceptorToBytes(".json", receptor)
    receptorIO = oechem.OEGraphMol()
    status = oedocking.OEReadReceptorFromBytes(receptorIO, ".json", bytes)
    # @</SNIPPET-RECEPTOR-IO-5>
    print("SNIPPET-RECEPTOR-IO-5: .json     status={} nbytes={}".format(status, len(bytes)))

    # @<SNIPPET-RECEPTOR-IO-6>
    bytes = oedocking.OEWriteReceptorToBytes(".json.gz", receptor)
    receptorIO = oechem.OEGraphMol()
    status = oedocking.OEReadReceptorFromBytes(receptorIO, ".json.gz", bytes)
    # @</SNIPPET-RECEPTOR-IO-6>
    print("SNIPPET-RECEPTOR-IO-6: .json.gz  status={} nbytes={}".format(status, len(bytes)))

    # @<SNIPPET-RECEPTOR-IO-7>
    gzip = False
    bytes = oedocking.OEWriteReceptorToBytes(oechem.OEFormat_OEB,
                                             oechem.OEGetDefaultOFlavor(oechem.OEFormat_OEB),
                                             gzip, receptor)
    receptorIO = oechem.OEGraphMol()
    status = oedocking.OEReadReceptorFromBytes(receptorIO, oechem.OEFormat_OEB,
                                               oechem.OEGetDefaultIFlavor(oechem.OEFormat_OEB),
                                               gzip, bytes)
    # @</SNIPPET-RECEPTOR-IO-7>
    print("SNIPPET-RECEPTOR-IO-7: .oeb      status={} nbytes={}".format(status, len(bytes)))

    # @<SNIPPET-RECEPTOR-IO-8>
    gzip = True
    bytes = oedocking.OEWriteReceptorToBytes(oechem.OEFormat_OEB,
                                             oechem.OEGetDefaultOFlavor(oechem.OEFormat_OEB),
                                             gzip, receptor)
    receptorIO = oechem.OEGraphMol()
    status = oedocking.OEReadReceptorFromBytes(receptorIO, oechem.OEFormat_OEB,
                                               oechem.OEGetDefaultIFlavor(oechem.OEFormat_OEB),
                                               gzip, bytes)
    # @</SNIPPET-RECEPTOR-IO-8>
    print("SNIPPET-RECEPTOR-IO-8: .oeb.gz   status={} nbytes={}".format(status, len(bytes)))

    # @<SNIPPET-RECEPTOR-IO-9>
    gzip = False
    bytes = oedocking.OEWriteReceptorToBytes(oechem.OEFormat_JSON,
                                             oechem.OEGetDefaultOFlavor(oechem.OEFormat_JSON),
                                             gzip, receptor)
    receptorIO = oechem.OEGraphMol()
    status = oedocking.OEReadReceptorFromBytes(receptorIO, oechem.OEFormat_JSON,
                                               oechem.OEGetDefaultIFlavor(oechem.OEFormat_JSON),
                                               gzip, bytes)
    # @</SNIPPET-RECEPTOR-IO-9>
    print("SNIPPET-RECEPTOR-IO-9: .json     status={} nbytes={}".format(status, len(bytes)))

    # @<SNIPPET-RECEPTOR-IO-10>
    gzip = True
    bytes = oedocking.OEWriteReceptorToBytes(oechem.OEFormat_JSON,
                                             oechem.OEGetDefaultOFlavor(oechem.OEFormat_JSON),
                                             gzip, receptor)
    receptorIO = oechem.OEGraphMol()
    status = oedocking.OEReadReceptorFromBytes(receptorIO, oechem.OEFormat_JSON,
                                               oechem.OEGetDefaultIFlavor(oechem.OEFormat_JSON),
                                               gzip, bytes)
    # @</SNIPPET-RECEPTOR-IO-10>
    print("SNIPPET-RECEPTOR-IO-10: .json.gz status={} nbytes={}".format(status, len(bytes)))


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