Appendix: Additional Examples in Python

These are full listings of programming examples that are excerpted or offered for download, elsewhere in this chapter. See the list in this chapter for the set of examples.

Listing 1: Using the string I/O functions.

#!/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])

    receptor = oechem.OEGraphMol()
    inputReceptorFilename = argv[1]
    oedocking.OEReadReceptorFile(receptor, inputReceptorFilename)

    outputReceptorFilename = argv[2]
    oedocking.OEWriteReceptorFile(receptor, outputReceptorFilename)

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

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

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

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

    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)
    print("SNIPPET-RECEPTOR-IO-7: .oeb      status={} nbytes={}".format(status, len(bytes)))

    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)
    print("SNIPPET-RECEPTOR-IO-8: .oeb.gz   status={} nbytes={}".format(status, len(bytes)))

    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)
    print("SNIPPET-RECEPTOR-IO-9: .json     status={} nbytes={}".format(status, len(bytes)))

    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)
    print("SNIPPET-RECEPTOR-IO-10: .json.gz status={} nbytes={}".format(status, len(bytes)))


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