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

#############################################################################
# Simple superimposition of a fit protein on to a reference protein
#############################################################################
import sys
import os
from openeye import oechem

def main(argv=[__name__]):
    if len(argv) < 3:
        oechem.OEThrow.Usage(f"{argv[0]} <input CIF> <reference cif oeb> <output CIF>")  # noqa

    file1 = os.path.basename(argv[1])
    _, ext1 = os.path.splitext(argv[1])
    if ext1 != '.cif':
        oechem.OEThrow.Usage(f"Input file {argv[1]} should be a CIF file.")

    file2 = os.path.basename(argv[2])

    _, ext3 = os.path.splitext(argv[3])
    if ext3 != '.cif':
        oechem.OEThrow.Usage(f"Output file {argv[3]} should be a CIF file.")

    ifs = oechem.oemolistream()
    if not ifs.open(argv[1]):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[1])

    mol = oechem.OEMol()
    if not oechem.OEReadMolecule(ifs, mol):
        oechem.OEThrow.Fatal("Unable to read molecule from %s" % argv[1])

    ifs = oechem.oemolistream()
    if not ifs.open(argv[2]):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[2])

    rmol = oechem.OEMol()
    if not oechem.OEReadMolecule(ifs, rmol):
        oechem.OEThrow.Fatal("Unable to read molecule from %s" % argv[2])

    if not oechem.OEHasResidues(mol) or not oechem.OEHasResidues(rmol):
        oechem.OEThrow.Fatal("Input molecule(s) do not contain any residues.")

    cifData = oechem.OECIFData(mol)
    opts = oechem.OECIFOptions()
    opts.SetPerceiveChemComp(True)
    if not cifData.Update(mol, opts):
        oechem.OEThrow.Warning("Molecule missing header information and could not update.")

    if not cifData.SetMMCIFChemCompData(mol, rmol, False):
        oechem.OEThrow.Fatal(f"Could not apply reference molecule {file2} to _chem_comp header in {file1}.")
    oechem.OESetMMCIFData(mol, cifData)

    ofs = oechem.oemolostream(argv[3])
    if not ofs.open(argv[3]):
        oechem.OEThrow.Fatal("Unable to open %s for writing" % argv[3])
    oechem.OEWriteMolecule(ofs, mol)
    ofs.close()

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