#!/usr/bin/env python
# (C) 2017 OpenEye Scientific Software Inc. All rights reserved.
#
# TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
# provided to current licensees or subscribers of OpenEye products or
# SaaS offerings (each a "Customer").
# Customer is hereby permitted to use, copy, and modify the Sample Code,
# subject to these terms. OpenEye 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 OpenEye 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 OpenEye 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-OEADDLABEL-PREDICATE>
def OEAddLabel_Predicate(disp):
    ringhighlight = oedepict.OEHighlightByBallAndStick(oechem.OELightGreen)
    oedepict.OEAddHighlighting(disp, ringhighlight, oechem.OEAtomIsInRing(), oechem.OEBondIsInRing())
    ringlabel = oedepict.OEHighlightLabel("ring", oechem.OELightGreen)
    oedepict.OEAddLabel(disp, ringlabel, oechem.OEAtomIsInRing())

    chainhighlight = oedepict.OEHighlightByBallAndStick(oechem.OEBlueTint)
    oedepict.OEAddHighlighting(disp, chainhighlight, oechem.OEAtomIsInChain(), oechem.OEBondIsInChain())
    chainlabel = oedepict.OEHighlightLabel("chain", oechem.OEBlueTint)
    oedepict.OEAddLabel(disp, chainlabel, oechem.OEAtomIsInChain())
# @ </SNIPPET-OEADDLABEL-PREDICATE>


# @ <SNIPPET-OEADDLABEL-OEMATCH>
def OEAddLabel_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)
        label = oedepict.OEHighlightLabel("aromatic", oechem.OELightGreen)
        oedepict.OEAddLabel(disp, label, match)
# @ </SNIPPET-OEADDLABEL-OEMATCH>


# @ <SNIPPET-OEADDLABEL-OEATOMBONDSET>
def OEAddLabel_OEAtomBondSet(disp):
    mol = disp.GetMolecule()

    ringset = oechem.OEAtomBondSet(mol.GetAtoms(oechem.OEAtomIsInRing()), mol.GetBonds(oechem.OEBondIsInRing()))
    ringhighlight = oedepict.OEHighlightByBallAndStick(oechem.OELightGreen)
    oedepict.OEAddHighlighting(disp, ringhighlight, ringset)
    ringlabel = oedepict.OEHighlightLabel("ring", oechem.OELightGreen)
    oedepict.OEAddLabel(disp, ringlabel, ringset)

    chainset = oechem.OEAtomBondSet(mol.GetAtoms(oechem.OEAtomIsInChain()), mol.GetBonds(oechem.OEBondIsInChain()))
    chainhighlight = oedepict.OEHighlightByBallAndStick(oechem.OEBlueTint)
    oedepict.OEAddHighlighting(disp, chainhighlight, chainset)
    chainlabel = oedepict.OEHighlightLabel("chain", oechem.OEBlueTint)
    oedepict.OEAddLabel(disp, chainlabel, chainset)
# @ </SNIPPET-OEADDLABEL-OEATOMBONDSET>


# @ <SNIPPET-OEADDLABEL-TO-IMAGE>
def OEAddLabel_OEImage(image):

    label = oedepict.OEHighlightLabel("Hello!")
    oedepict.OEAddLabel(image, oedepict.OE2DPoint(50, 50), label)

    label.SetBoundingBoxPen(oedepict.OETransparentPen)
    oedepict.OEAddLabel(image, oedepict.OE2DPoint(100, 50), label)

    label.SetBoundingBoxPen(oedepict.OELightGreyPen)
    oedepict.OEAddLabel(image, oedepict.OE2DPoint(150, 50), label)
# @ </SNIPPET-OEADDLABEL-TO-IMAGE>

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


mol = oechem.OEGraphMol()
oechem.OESmilesToMol(mol, "CC(=O)C1Cc2ccccc2N1")
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)
OEAddLabel_Predicate(disp)
oedepict.OERenderMolecule("OEAddLabel-Predicate.png", disp)
oedepict.OERenderMolecule("OEAddLabel-Predicate.pdf", disp)

disp = oedepict.OE2DMolDisplay(mol, opts)
OEAddLabel_OEMatch(disp)
oedepict.OERenderMolecule("OEAddLabel-OEMatch.png", disp)
oedepict.OERenderMolecule("OEAddLabel-OEMatch.pdf", disp)

disp = oedepict.OE2DMolDisplay(mol, opts)
OEAddLabel_OEAtomBondSet(disp)
oedepict.OERenderMolecule("OEAddLabel-OEAtomBondSet.png", disp)
oedepict.OERenderMolecule("OEAddLabel-OEAtomBondSet.pdf", disp)

image = oedepict.OEImage(200, 100)
OEAddLabel_OEImage(image)
oedepict.OEWriteImage("OEAddLabel-OEImageBase.png", image)
oedepict.OEWriteImage("OEAddLabel-OEImageBase.pdf", image)
