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

# @ <SNIPPET-MCS-NO-ALIGNMENT>
refmol = oechem.OEGraphMol()
oechem.OESmilesToMol(refmol, "c1cc(c2cc(cnc2c1)CCCO)C(=O)CCO")
oedepict.OEPrepareDepiction(refmol)

fitmol = oechem.OEGraphMol()
oechem.OESmilesToMol(fitmol, "c1cc2ccc(cc2c(c1)C(=O)O)CCO")
oedepict.OEPrepareDepiction(fitmol)

mcss = oechem.OEMCSSearch(oechem.OEMCSType_Approximate)
atomexpr = oechem.OEExprOpts_DefaultAtoms
bondexpr = oechem.OEExprOpts_DefaultBonds
mcss.Init(refmol, atomexpr, bondexpr)
mcss.SetMCSFunc(oechem.OEMCSMaxBondsCompleteCycles())

image = oedepict.OEImage(400, 200)

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

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

refscale = oedepict.OEGetMoleculeScale(refmol, opts)
fitscale = oedepict.OEGetMoleculeScale(fitmol, opts)
opts.SetScale(min(refscale, fitscale))

refdisp = oedepict.OE2DMolDisplay(mcss.GetPattern(), opts)
fitdisp = oedepict.OE2DMolDisplay(fitmol, opts)

unique = True
miter = mcss.Match(fitmol, unique)
if miter.IsValid():
    match = miter.Target()

    abset = oechem.OEAtomBondSet(match.GetPatternAtoms(), match.GetPatternBonds())
    oedepict.OEAddHighlighting(refdisp, oechem.OEBlueTint,
                               oedepict.OEHighlightStyle_BallAndStick, abset)

    abset = oechem.OEAtomBondSet(match.GetTargetAtoms(), match.GetTargetBonds())
    oedepict.OEAddHighlighting(fitdisp, oechem.OEBlueTint,
                               oedepict.OEHighlightStyle_BallAndStick, abset)

refcell = grid.GetCell(1, 1)
oedepict.OERenderMolecule(refcell, refdisp)

fitcell = grid.GetCell(1, 2)
oedepict.OERenderMolecule(fitcell, fitdisp)

oedepict.OEWriteImage("MCSNoAlignment.png", image)
# @ </SNIPPET-MCS-NO-ALIGNMENT>
oedepict.OEWriteImage("MCSNoAlignment.pdf", image)
