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

###############################################################
# USED TO GENERATE CODE SNIPPETS FOR THE OEDEPICT DOCUMENTATION
###############################################################


# @ <SNIPPET-OEADDHIGHLIGHTING-PREDICATE>
class Pred6MemAromAtom(oechem.OEUnaryAtomPred):
    def __call__(self, atom):
        return atom.IsAromatic() and oechem.OEAtomIsInAromaticRingSize(atom, 6)


class Pred6MemAromBond(oechem.OEUnaryBondPred):
    def __call__(self, bond):
        return bond.IsAromatic() and oechem.OEBondIsInAromaticRingSize(bond, 6)


def OEAddHighlighting_Predicate(disp):
    highlightstyle = oedepict.OEHighlightByBallAndStick(oechem.OELightGreen)
    oedepict.OEAddHighlighting(disp, highlightstyle, Pred6MemAromAtom())
    oedepict.OEAddHighlighting(disp, highlightstyle, Pred6MemAromBond())


def OEAddHighlighting_AtomAndBondPredicate(disp):
    highlightstyle = oedepict.OEHighlightByBallAndStick(oechem.OELightGreen)
    oedepict.OEAddHighlighting(disp, highlightstyle, Pred6MemAromAtom(), Pred6MemAromBond())
# @ </SNIPPET-OEADDHIGHLIGHTING-PREDICATE>


# @ <SNIPPET-OEADDHIGHLIGHTING-OEMATCH>
def OEAddHighlighting_OEMatch(disp):
    subs = oechem.OESubSearch("a1aaaaa1")
    unique = True
    highlightstyle = oedepict.OEHighlightByBallAndStick(oechem.OELightGreen)
    for match in subs.Match(disp.GetMolecule(), unique):
        oedepict.OEAddHighlighting(disp, highlightstyle, match)
# @ </SNIPPET-OEADDHIGHLIGHTING-OEMATCH>


# @ <SNIPPET-OEADDHIGHLIGHTING-OEATOMBONDSET>
def OEAddHighlighting_OEAtomBondSet(disp):
    mol = disp.GetMolecule()
    abset = oechem.OEAtomBondSet(mol.GetAtoms(Pred6MemAromAtom()),
                                 mol.GetBonds(Pred6MemAromBond()))

    highlightstyle = oedepict.OEHighlightByBallAndStick(oechem.OELightGreen)
    oedepict.OEAddHighlighting(disp, highlightstyle, abset)
# @ </SNIPPET-OEADDHIGHLIGHTING-OEATOMBONDSET>


# @ <SNIPPET-OEADDHIGHLIGHTING-OESUBSEARCH>
def OEAddHighlighting_OESubSearch(disp):
    subsearch = oechem.OESubSearch("a1aaaaa1")
    highlightstyle = oedepict.OEHighlightByBallAndStick(oechem.OELightGreen)
    oedepict.OEAddHighlighting(disp, highlightstyle, subsearch)
# @ </SNIPPET-OEADDHIGHLIGHTING-OESUBSEARCH>

########################################################################
#
########################################################################


mol = oechem.OEGraphMol()
oechem.OESmilesToMol(mol, "c1ccnc2c1[nH]c3c2cccc3")
oedepict.OEPrepareDepiction(mol)

width, height = 250, 180
opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale)
opts.SetTitleLocation(oedepict.OETitleLocation_Hidden)

disp = oedepict.OE2DMolDisplay(mol, opts)
OEAddHighlighting_Predicate(disp)
oedepict.OERenderMolecule("OEAddHighlighting-OEHighlightBase-Predicate.png", disp)
oedepict.OERenderMolecule("OEAddHighlighting-OEHighlightBase-Predicate.pdf", disp)

disp = oedepict.OE2DMolDisplay(mol, opts)
OEAddHighlighting_AtomAndBondPredicate(disp)
oedepict.OERenderMolecule("OEAddHighlighting-OEHighlightBase-AtomAndBondPredicate.png", disp)
oedepict.OERenderMolecule("OEAddHighlighting-OEHighlightBase-AtomAndBondPredicate.pdf", disp)

disp = oedepict.OE2DMolDisplay(mol, opts)
OEAddHighlighting_OEMatch(disp)
oedepict.OERenderMolecule("OEAddHighlighting-OEHighlightBase-OEMatch.png", disp)
oedepict.OERenderMolecule("OEAddHighlighting-OEHighlightBase-OEMatch.pdf", disp)

disp = oedepict.OE2DMolDisplay(mol, opts)
OEAddHighlighting_OEAtomBondSet(disp)
oedepict.OERenderMolecule("OEAddHighlighting-OEHighlightBase-OEAtomBondSet.png", disp)
oedepict.OERenderMolecule("OEAddHighlighting-OEHighlightBase-OEAtomBondSet.pdf", disp)

disp = oedepict.OE2DMolDisplay(mol, opts)
OEAddHighlighting_OESubSearch(disp)
oedepict.OERenderMolecule("OEAddHighlighting-OEHighlightBase-OESubSearch.png", disp)
oedepict.OERenderMolecule("OEAddHighlighting-OEHighlightBase-OESubSearch.pdf", disp)
