#!/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 oedepict
import sys


# @ <SNIPPET-ATOM-VISIBILITY>
class OEAtomVisibilityShowRxnRole(oechem.OEUnaryAtomPred):
    def __init__(self, role):
        self.rxnrole = role
        oechem.OEUnaryAtomPred.__init__(self)

    def __call__(self, atom):
        # return True if the atom should be visible, otherwise return False
        if self.rxnrole == oechem.OERxnRole_None:
            return True
        return (self.rxnrole == atom.GetRxnRole())

    def CreateCopy(self):
        # __disown__ is required to allow C++ to take
        # ownership of this object and its memory
        return OEAtomVisibilityShowRxnRole(self.rxnrole).__disown__()


if len(sys.argv) != 3:
    oechem.OEThrow.Usage("%s <rxnfile> <imagefile>" % sys.argv[0])

ifs = oechem.oemolistream(sys.argv[1])

mol = oechem.OEGraphMol()
if not oechem.OEReadRxnFile(ifs, mol):
    oechem.OEThrow.Fatal("%s error reading reaction file" % sys.argv[0])

oedepict.OEPrepareDepiction(mol)

image = oedepict.OEImage(900, 100)

rows, cols = 1, 3
grid = oedepict.OEImageGrid(image, rows, cols)

opts = oedepict.OE2DMolDisplayOptions(grid.GetCellWidth(), grid.GetCellHeight(),
                                      oedepict.OEScale_AutoScale)

cell = grid.GetCell(1, 1)
mol.SetTitle("Reaction display")
opts.SetAtomVisibilityFunctor(oechem.OEIsTrueAtom())  # explicitly set the default
disp = oedepict.OE2DMolDisplay(mol, opts)
rxnscale = disp.GetScale()
oedepict.OERenderMolecule(cell, disp)

cell = grid.GetCell(1, 2)
mol.SetTitle("Reactant display")
opts.SetAtomVisibilityFunctor(OEAtomVisibilityShowRxnRole(oechem.OERxnRole_Reactant))
opts.SetScale(rxnscale)
disp = oedepict.OE2DMolDisplay(mol, opts)
oedepict.OERenderMolecule(cell, disp)

cell = grid.GetCell(1, 3)
mol.SetTitle("Product display")
opts.SetAtomVisibilityFunctor(OEAtomVisibilityShowRxnRole(oechem.OERxnRole_Product))
opts.SetScale(rxnscale)
disp = oedepict.OE2DMolDisplay(mol, opts)
oedepict.OERenderMolecule(cell, disp)

# @ </SNIPPET-ATOM-VISIBILITY>
oedepict.OEWriteImage(sys.argv[2], image)
