Calculating Overlap

Overlap calculation is the simplest functionality offered through the Shape TK. These set of examples show how to calculate static overlap between two objects (molecules, grids or shape query). Note that static means that the two input species (ref and fit) are not moved at all. These examples simply calculate the overlap and/or other related quantities, given the input positions. Examples for performing calculations that actually optimize the alignment/overlap are considered in a separate section.

Simple Overlap

This example reads in a reference molecule and a few fit molecules and prints out the Exact overlap calculated.

Listing 1: Simple overlap using Exact Overlap

#!/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 __future__ import print_function

from openeye import oechem
from openeye import oeshape


def main(argv=[__name__]):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <reffile> <fitfile>" % argv[0])

    reffs = oechem.oemolistream(argv[1])
    fitfs = oechem.oemolistream(argv[2])

    refmol = oechem.OEGraphMol()
    oechem.OEReadMolecule(reffs, refmol)

    # Prepare reference molecule for calculation
    # With default options this will remove any explicit hydrogens present
    prep = oeshape.OEOverlapPrep()
    prep.Prep(refmol)

    # Get appropriate function to calculate exact shape
    shapeFunc = oeshape.OEExactShapeFunc()
    shapeFunc.SetupRef(refmol)

    res = oeshape.OEOverlapResults()
    for fitmol in fitfs.GetOEGraphMols():
        prep.Prep(fitmol)
        shapeFunc.Overlap(fitmol, res)
        print("title: %s  exact tanimoto = %.2f" %
              (fitmol.GetTitle(), res.GetTanimoto()))


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

Adding overlap to molecules

This next example program uses the Analytic overlap method and also uses a little extra OEChem to attach the overlap scores to each molecule as SD data. This can be used to rescore a set of ROCS hits or to measure the overlap of ROCS alignments against an exclusion region in the binding site.

Listing 2: Rescoring pre-aligned structures

#!/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 __future__ import print_function

from openeye import oechem
from openeye import oeshape


def main(argv=[__name__]):
    if len(argv) != 4:
        oechem.OEThrow.Usage("%s <reffile> <rocs_hits_file> <output.sdf>" % argv[0])

    reffs = oechem.oemolistream(argv[1])
    fitfs = oechem.oemolistream(argv[2])
    outfs = oechem.oemolostream(argv[3])

    refmol = oechem.OEGraphMol()
    oechem.OEReadMolecule(reffs, refmol)

    # Get appropriate function to calculate analytic shape
    shapeFunc = oeshape.OEAnalyticShapeFunc()
    shapeFunc.SetupRef(refmol)

    res = oeshape.OEOverlapResults()
    for fitmol in fitfs.GetOEGraphMols():
        shapeFunc.Overlap(fitmol, res)
        oechem.OESetSDData(fitmol, "AnalyticTanimoto", "%.2f" % res.GetTanimoto())
        oechem.OEWriteMolecule(outfs, fitmol)


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

Color Overlap

This example reads in a reference molecule and a few fit molecules and prints out the Exact color score calculated. By default all color calculations are performed using the ImplicitMillsDean force field.

Listing 3: Calculating color score

#!/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 __future__ import print_function

from openeye import oechem
from openeye import oeshape


def main(argv=[__name__]):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <reffile> <fitfile>" % argv[0])

    reffs = oechem.oemolistream(argv[1])
    fitfs = oechem.oemolistream(argv[2])

    refmol = oechem.OEGraphMol()
    oechem.OEReadMolecule(reffs, refmol)

    # Prepare reference molecule for calculation
    # With default options this will remove any explicit
    # hydrogens present, and add required color atoms
    prep = oeshape.OEOverlapPrep()
    prep.Prep(refmol)

    # Get appropriate function to calculate exact color
    colorFunc = oeshape.OEExactColorFunc()
    colorFunc.SetupRef(refmol)

    res = oeshape.OEOverlapResults()
    fitmol = oechem.OEGraphMol()
    while oechem.OEReadMolecule(fitfs, fitmol):
        prep.Prep(fitmol)
        colorFunc.Overlap(fitmol, res)
        print("title: %s  color score = %.2f" %
              (fitmol.GetTitle(), res.GetColorScore()))


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

Adding color score to molecules

This next example uses the ImplicitMillsDean force field and the Analytic color to rescore a set of ROCS hits and add the color scores to SD tags.

Listing 4: Using color to add scores to pre-aligned molecules.

#!/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 __future__ import print_function

from openeye import oechem
from openeye import oeshape


def main(argv=[__name__]):
    if len(argv) != 4:
        oechem.OEThrow.Usage("%s <reffile> <rocs_hits_file> <output.sdf>" % argv[0])

    reffs = oechem.oemolistream(argv[1])
    fitfs = oechem.oemolistream(argv[2])
    outfs = oechem.oemolostream(argv[3])

    refmol = oechem.OEGraphMol()
    oechem.OEReadMolecule(reffs, refmol)

    # Prepare reference molecule for calculation
    # With default options this will add required color atoms
    prep = oeshape.OEOverlapPrep()
    prep.Prep(refmol)

    # Get appropriate function to calculate analytic color
    colorFunc = oeshape.OEAnalyticColorFunc()
    colorFunc.SetupRef(refmol)

    res = oeshape.OEOverlapResults()
    for fitmol in fitfs.GetOEGraphMols():
        prep.Prep(fitmol)
        colorFunc.Overlap(fitmol, res)
        oechem.OESetSDData(fitmol, "AnalyticColorTanimoto", "%.2f" % res.GetColorTanimoto())
        oechem.OEWriteMolecule(outfs, fitmol)
        print("Fit Title: %s  Color Tanimoto: %.2f" %
              (fitmol.GetTitle(), res.GetColorTanimoto()))


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

User Defined Color

As a step toward writing a complete color force field, it is possible to combine built-in rules for color atom assignment with user defined interactions. A new OEColorForceField object can be created using one of the built-in types, then the interactions can be cleared using OEColorForceField.ClearInteractions and subsequent user interactions added with OEColorForceField.AddInteraction.

For example, to use the ImplicitMillsDean atom typing rules, but to only consider donor-donor and acceptor-acceptor interactions, one can use the following:

Listing 5: Using ImplicitMillsDean with user interactions.

#!/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 __future__ import print_function

from openeye import oechem
from openeye import oeshape


def main(argv=[__name__]):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <reffile> <overlayfile>" % argv[0])

    reffs = oechem.oemolistream(argv[1])
    fitfs = oechem.oemolistream(argv[2])

    refmol = oechem.OEGraphMol()
    oechem.OEReadMolecule(reffs, refmol)

    # Modify ImplicitMillsDean color force field by
    # adding user defined color interactions
    cff = oeshape.OEColorForceField()
    cff.Init(oeshape.OEColorFFType_ImplicitMillsDean)
    cff.ClearInteractions()
    donorType = cff.GetType("donor")
    accepType = cff.GetType("acceptor")
    cff.AddInteraction(donorType, donorType, "gaussian", -1.0, 1.0)
    cff.AddInteraction(accepType, accepType, "gaussian", -1.0, 1.0)

    # Prepare reference molecule for calculation
    # With default options this will add required color atoms
    # Set the modified color force field for addignment
    prep = oeshape.OEOverlapPrep()
    prep.SetColorForceField(cff)
    prep.Prep(refmol)

    # Get appropriate function to calculate exact color
    # Set appropriate options to use the user defined color
    options = oeshape.OEColorOptions()
    options.SetColorForceField(cff)
    colorFunc = oeshape.OEExactColorFunc(options)
    colorFunc.SetupRef(refmol)

    res = oeshape.OEOverlapResults()
    for fitmol in fitfs.GetOEGraphMols():
        prep.Prep(fitmol)
        colorFunc.Overlap(fitmol, res)
        print("Fit Title: %s  Color Tanimoto: %.2f" %
              (fitmol.GetTitle(), res.GetColorTanimoto()))


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

Shape and Color Overlap

This example reads in a reference molecule and a few fit molecules and prints out both the shape overlap and the color score calculated. By default the OEOverlapFunc uses the Grid shape and Exact color.

Listing 6: Calculating both shape and color overlap

#!/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 __future__ import print_function

from openeye import oechem
from openeye import oeshape


def main(argv=[__name__]):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <reffile> <fitfile>" % argv[0])

    reffs = oechem.oemolistream(argv[1])
    fitfs = oechem.oemolistream(argv[2])

    refmol = oechem.OEGraphMol()
    oechem.OEReadMolecule(reffs, refmol)

    # Prepare reference molecule for calculation
    # With default options this will remove any explicit
    # hydrogens present and add color atoms
    prep = oeshape.OEOverlapPrep()
    prep.Prep(refmol)

    # Get appropriate function to calculate both shape and color
    # By default the OEOverlapFunc contains OEGridShapeFunc for shape
    # and OEExactColorFunc for color
    func = oeshape.OEOverlapFunc()
    func.SetupRef(refmol)

    res = oeshape.OEOverlapResults()
    for fitmol in fitfs.GetOEGraphMols():
        prep.Prep(fitmol)
        func.Overlap(fitmol, res)
        print("title: %s  tanimoto combo = %.2f shape tanimoto = %.2f color tanimoto = %.2f" %
              (fitmol.GetTitle(), res.GetTanimotoCombo(), res.GetTanimoto(), res.GetColorTanimoto()))


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

ROCS

The high-level OEROCS provides the simplest way to maximize shape and/or color between two objects. OEROCS uses the same shape and color functions as used for static overlap calculation, but optimizes the overlap between the reference and the fit objects. By default both shape and color optimization is performed, with shape estimated the Grid method and color estimated with the Exact method.

The fit object is not moved during the optimization. Part of the results returned from the calculation are the transform required to move the fit object into the final orientation. The results also contain a copy of the fit molecule conformer(s) in its final orientation.

A helper function OEROCSOverlay is created for the most simple usage of the functionality that performs optimizations between a set of conformers and returns the best hit.

Best Hit

This example reads in a reference molecule and a fit molecule, performs overlay optimization, finds the best hit conformer and prints it out in final orientation.

Listing 7: Finding best hit conformer pair

#!/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 __future__ import print_function

from openeye import oechem
from openeye import oeshape


def main(argv=[__name__]):
    if len(argv) != 4:
        oechem.OEThrow.Usage("%s <reffile> <fitfile> <outfile>" % argv[0])

    reffs = oechem.oemolistream(sys.argv[1])
    fitfs = oechem.oemolistream(sys.argv[2])
    outfs = oechem.oemolostream(sys.argv[3])

    refmol = oechem.OEMol()
    oechem.OEReadMolecule(reffs, refmol)

    fitmol = oechem.OEMol()
    oechem.OEReadMolecule(fitfs, fitmol)

    res = oeshape.OEROCSResult()
    oeshape.OEROCSOverlay(res, refmol, fitmol)
    outmol = res.GetOverlayConf()

    oechem.OEWriteMolecule(outfs, outmol)
    print("Tanimoto combo = %.2f" % res.GetTanimotoCombo())


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

Top Hits

This example reads in a reference molecule and a few fit molecules, performs overlay optimization, finds the few top hits, and prints them out in final orientation.

Listing 8: Finding top ROCS hits

#!/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 __future__ import print_function

from openeye import oechem
from openeye import oeshape


def main(argv=[__name__]):
    if len(argv) != 5:
        oechem.OEThrow.Usage("%s <reffile> <fitfile> <outfile> <nhits>" % argv[0])

    reffs = oechem.oemolistream(sys.argv[1])
    fitfs = oechem.oemolistream(sys.argv[2])
    outfs = oechem.oemolostream(sys.argv[3])
    nhits = int(sys.argv[4])

    refmol = oechem.OEMol()
    oechem.OEReadMolecule(reffs, refmol)

    # Setup OEROCS with specified number of best hits
    options = oeshape.OEROCSOptions()
    options.SetNumBestHits(nhits)
    rocs = oeshape.OEROCS(options)
    rocs.SetDatabase(fitfs)
    for res in rocs.Overlay(refmol):
        outmol = res.GetOverlayConf()
        oechem.OEWriteMolecule(outfs, outmol)
        print("title: %s  tanimoto combo = %.2f" % (outmol.GetTitle(), res.GetTanimotoCombo()))


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

Top Hits with Multiple Conformers

This example repeats the Top Hits example above but fixes the number of hits to 3 and asks for multiple best conformers for each hit reported.

Listing 9: Finding top ROCS hits with multiple conformers

#!/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 __future__ import print_function

from openeye import oechem
from openeye import oeshape


def main(argv=[__name__]):
    if len(argv) != 5:
        oechem.OEThrow.Usage("%s <reffile> <fitfile> <outfile> <confsperhit>" % argv[0])

    reffs = oechem.oemolistream(sys.argv[1])
    fitfs = oechem.oemolistream(sys.argv[2])
    outfs = oechem.oemolostream(sys.argv[3])
    nconfs = int(sys.argv[4])

    refmol = oechem.OEMol()
    oechem.OEReadMolecule(reffs, refmol)

    # Setup OEROCS to provide 3 best hits
    # with the specified number of conformers per hit
    options = oeshape.OEROCSOptions()
    options.SetNumBestHits(3)
    options.SetConfsPerHit(nconfs)
    rocs = oeshape.OEROCS(options)
    rocs.SetDatabase(fitfs)
    for res in rocs.Overlay(refmol):
        outmol = res.GetOverlayConfs()
        oechem.OEWriteMolecule(outfs, outmol)
        print("title: %s  tanimoto combo = %.2f" % (outmol.GetTitle(), res.GetTanimotoCombo()))


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

Overlay Optimization

The OEOverlay and OEMultiRefOverlay, compared to OEROCS, allows setting up a reference system and process overlay optimization of multiple fit molecules. Another difference is that both OEOverlay and OEMultiRefOverlay, in addition to providing the best overlay score, also gives access to all the results obtained during a overlay optimization. Methods Overlay and Overlay, that provides all results should only be used when such rigorous amount of resulting data is desired.

Best Optimization Result

This example reads in a reference molecule and a few fit molecules, performs overlay optimization, and returns only the best result per reference to fit molecule overlay optimization.

Listing 10: Getting the best scores from OEMultiRefOverlay.

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

# This example reads in a reference molecule and a few fit
# molecules, performs overlay optimization, sorts the results
# in tanimoto order, and shows the single best result.
# With the default options, OEOverlay optimizes both shape and color.

from __future__ import print_function

from openeye import oechem
from openeye import oeshape


def main(argv=[__name__]):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <reffile> <fitfile>" % argv[0])

    reffs = oechem.oemolistream(argv[1])
    fitfs = oechem.oemolistream(argv[2])

    refmol = oechem.OEMol()
    oechem.OEReadMolecule(reffs, refmol)
    print("Ref. Title:", refmol.GetTitle(), "Num Confs:", refmol.NumConfs())

    # Prepare reference molecule for calculation
    # With default options this will remove any explicit
    # hydrogens present and add color atoms
    prep = oeshape.OEOverlapPrep()
    prep.Prep(refmol)

    overlay = oeshape.OEMultiRefOverlay()
    overlay.SetupRef(refmol)

    for fitmol in fitfs.GetOEMols():
        prep.Prep(fitmol)
        score = oeshape.OEBestOverlayScore()
        overlay.BestOverlay(score, fitmol, oeshape.OEHighestTanimoto())
        print("Fit Title: %-4s FitConfIdx: %-4d RefConfIdx: %-4d tanimoto combo: %.2f"
              % (fitmol.GetTitle(), score.GetFitConfIdx(), score.GetRefConfIdx(), score.GetTanimotoCombo()))

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

Listing 10b: Getting the best scores from OEMultiRefOverlay (multi-threaded version)

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

# This is a multi-thresded version of the example that reads in a 
# reference molecule and a few fit molecules, performs overlay optimization, 
# and shows the single best result.
# With the default options, OEOverlay optimizes both shape and color.

from __future__ import print_function
import sys
from openeye import oechem
from openeye import oeshape
from threading import Thread

class MultiCoreOverlay(Thread):
    def __init__(self, refmol, ifs):
        Thread.__init__(self)
        self._prep = oeshape.OEOverlapPrep()
        self._prep.Prep(refmol)
        self._overlay = oeshape.OEOverlay()
        self._overlay.SetupRef(refmol)
        self._ifs = ifs

    def run(self):
        for fitmol in self._ifs.GetOEMols():
            self._prep.Prep(fitmol)
            score = oeshape.OEBestOverlayScore()
            self._overlay.BestOverlay(score, fitmol, oeshape.OEHighestTanimoto())
            print("Fit Title: %-4s FitConfIdx: %-4d RefConfIdx: %-4d tanimoto combo: %.2f"
                  % (fitmol.GetTitle(), score.GetFitConfIdx(), score.GetRefConfIdx(), score.GetTanimotoCombo()))

def main(argv=[__name__]):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <reffile> <fitfile>" % argv[0])

    reffs = oechem.oemolistream(argv[1])
    refmol = oechem.OEMol()
    oechem.OEReadMolecule(reffs, refmol)
    print("Ref. Title:", refmol.GetTitle(), "Num Confs:", refmol.NumConfs())

    fitfs = oechem.oemolithread(sys.argv[2])

    thrds = []
    for i in range(oechem.OEGetNumProcessors()):
        thrd = MultiCoreOverlay(refmol, fitfs)
        thrd.start()
        thrds.append(thrd)

    for thrd in thrds:
        thrd.join()

    return 0

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

All Optimization Results

This example repeats the above, but uses Overlay to obtain all the results.

A single Overlay calculation will return N results, where N is the number of ref conformers times the number of fit conformers times the number of starting positions for each pair. So comparing 2 molecules with 10 conformers each could return 400 or more results.

There are two helper classes designed solely to contain these results and to make it easy to extract all or just the desired subset. OEBestOverlayResults holds the results of a single pair of conformers. It contains a set of OEBestOverlayScore objects, one for each starting position.

This example uses 2 iterators to show all the results.

Listing 11: Getting all the scores from OEMultiRefOverlay.

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

# This example reads in a reference molecule and a few fit
# molecules, performs overlay optimization, and uses 2 iterators to
# show all the results.
# With the default options, OEOverlay optimizes both shape and color.

from __future__ import print_function

from openeye import oechem
from openeye import oeshape


def main(argv=[__name__]):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <reffile> <fitfile>" % argv[0])

    reffs = oechem.oemolistream(argv[1])
    fitfs = oechem.oemolistream(argv[2])

    refmol = oechem.OEMol()
    oechem.OEReadMolecule(reffs, refmol)
    print("Ref. Title:", refmol.GetTitle(), "Num Confs:", refmol.NumConfs())

    # Prepare reference molecule for calculation
    # With default options this will remove any explicit
    # hydrogens present and add color atoms
    prep = oeshape.OEOverlapPrep()
    prep.Prep(refmol)

    overlay = oeshape.OEMultiRefOverlay()
    overlay.SetupRef(refmol)

    for fitmol in fitfs.GetOEMols():
        print("Fit Title:", fitmol.GetTitle(), "Num Confs:", fitmol.NumConfs())

        prep.Prep(fitmol)
        resCount = 0

        # double loop over results and scores to obtain all scores
        for res in overlay.Overlay(fitmol):
            for score in res.GetScores():
                print("FitConfIdx: %-4d RefConfIdx: %-4d tanimoto combo: %.2f"
                      % (score.GetFitConfIdx(), score.GetRefConfIdx(), score.GetTanimotoCombo()))
                resCount += 1

        print(resCount, "results returned")


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

Manipulating all results

This example repeats the above to access all overlay results, and shows how to navigate through the results to extract a specific set. Here, the OESortOverlayScores function is used to turn the double iterator as shown in the example above into a single iterator of OEBestOverlayScore. Note that the third argument is a functor used to sort the list, such that in this next example, we get one OEBestOverlayScore for each pair of conformers and they are returned in Tanimoto order.

OEOverlay or OEMultiRefOverlay does not actually move the fit molecule. Part of OEBestOverlayScore is the rotation matrix and translation matrix necessary to move the fit molecule into the final overlap position. (N.B. the rotation matrix is left-multiplied so can be regarded as left-handed.) This example also shows how to apply these transformations. The OpenEye standard is rotation then translation, but as a convenience, OEBestOverlayScore has OEBestOverlayScore.Transform method that will apply the transforms in the proper order.

This example keeps the user specified number of scores, aligns each fit conformer to the ref conformer it was overlaid on, and then write the pair to an output file. If SD or OEB is used as the output file type, then scores will also be stored in SD tags.

Listing 12: Writing few aligned structures from OEMultiRefOverlay.

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

# This example reads in a reference molecule and a few fit
# molecules, performs overlay optimization, sorts the results
# in tanimoto order, shows the user specified number of
# results, and saves the overlayed structures.
# With the default options, OEOverlay optimizes both shape and color.

from __future__ import print_function

from openeye import oechem
from openeye import oeshape


def main(argv=[__name__]):
    if len(argv) != 5:
        oechem.OEThrow.Usage("%s <reffile> <fitfile> <out.sdf> <keepsize>" % argv[0])

    reffs = oechem.oemolistream(argv[1])
    fitfs = oechem.oemolistream(argv[2])
    outfs = oechem.oemolostream(argv[3])
    keepsize = int(argv[4])

    refmol = oechem.OEMol()
    oechem.OEReadMolecule(reffs, refmol)
    print("Ref. Title:", refmol.GetTitle(), "Num Confs:", refmol.NumConfs())

    # Prepare reference molecule for calculation
    # With default options this will remove any explicit
    # hydrogens present and add color atoms
    prep = oeshape.OEOverlapPrep()
    prep.Prep(refmol)

    overlay = oeshape.OEMultiRefOverlay()
    overlay.SetupRef(refmol)

    for fitmol in fitfs.GetOEMols():
        print("Fit Title:", fitmol.GetTitle(), "Num Confs:", fitmol.NumConfs())

        prep.Prep(fitmol)
        resCount = 0

        # Sort all scores according to highest tanimoto
        scoreiter = oeshape.OEBestOverlayScoreIter()
        oeshape.OESortOverlayScores(scoreiter, overlay.Overlay(fitmol), oeshape.OEHighestTanimoto())

        for score in scoreiter:
            outmol = oechem.OEGraphMol(fitmol.GetConf(oechem.OEHasConfIdx(score.GetFitConfIdx())))
            score.Transform(outmol)

            oechem.OESetSDData(outmol, "RefConfIdx", "%-d" % score.GetRefConfIdx())
            oechem.OESetSDData(outmol, "tanimoto combo", "%-.3f" % score.GetTanimotoCombo())

            oechem.OEWriteMolecule(outfs, refmol.GetConf(oechem.OEHasConfIdx(score.GetRefConfIdx())))
            oechem.OEWriteMolecule(outfs, outmol)

            resCount += 1

            # Break at the user specified size
            if resCount == keepsize:
                break

        print(resCount, "results returned")


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

Working with Shape Query

This section of examples shows the use of a OEShapeQuery, and performs calculations where a shape query is used a the reference system instead of a molecule as the reference.

Creating a shape query

This examples shows how to create a simple shape query that combines a molecule with gaussians. In this example, the molecule is added in the query to define shape, and color is added separately in terms to gaussians. The created query is then saved in a file.

Listing 13: Creating a shape query.

#!/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 __future__ import print_function

from openeye import oechem
from openeye import oegrid
from openeye import oeshape


def main(argv=[__name__]):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <molfile> <outfile>" % argv[0])

    molfs = oechem.oemolistream(sys.argv[1])
    mol = oechem.OEGraphMol()
    oechem.OEReadMolecule(molfs, mol)

    # Use OEOverlapPrep to remove hydrogens and add
    # color atoms to the molecule
    prep = oeshape.OEOverlapPrep()
    prep.Prep(mol)

    # Get the color atoms, create gaussians and add them
    # to the shape query
    query = oeshape.OEShapeQuery()
    for atom in oeshape.OEGetColorAtoms(mol):
        coords = oechem.OEFloatArray(3)
        mol.GetCoords(atom, coords)
        gauss = oegrid.OEGaussian(1.0, 1.0, coords, oeshape.OEGetColorType(atom))
        query.AddColorGaussian(gauss)

    # Remove color atoms from the molecule and add to the query
    oeshape.OERemoveColorAtoms(mol)
    query.SetMolecule(mol)

    oeshape.OEWriteShapeQuery(sys.argv[2], query)
    print("shape query created")


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

Overlap with shape query

This example reads in a reference shape query and a few fit molecules and prints out both the shape overlap and the color score calculated. By default the OEOverlapFunc uses the Grid shape and Exact color.

Listing 14: Overlap with a shape query

#!/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 __future__ import print_function

from openeye import oechem
from openeye import oeshape


def main(argv=[__name__]):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <queryfile> <fitfile>" % argv[0])

    query = oeshape.OEShapeQuery()
    oeshape.OEReadShapeQuery(argv[1], query)

    # Get appropriate function to calculate both shape and color
    # By default the OEOverlapFunc contains OEGridShapeFunc for shape
    # and OEExactColorFunc for color
    func = oeshape.OEOverlapFunc()
    func.SetupRef(query)

    res = oeshape.OEOverlapResults()
    fitfs = oechem.oemolistream(argv[2])
    prep = oeshape.OEOverlapPrep()
    for fitmol in fitfs.GetOEGraphMols():
        prep.Prep(fitmol)
        func.Overlap(fitmol, res)
        print("title: %s  tanimoto combo = %.2f shape tanimoto = %.2f color tanimoto = %.2f" %
              (fitmol.GetTitle(), res.GetTanimotoCombo(), res.GetTanimoto(), res.GetColorTanimoto()))


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

Top hit against shape query

This example reads in a reference query and a few fit molecules, performs overlay optimization, finds the few top hits, and prints them out in final orientation.

Listing 15: Finding top ROCS hits against a query

#!/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 __future__ import print_function

from openeye import oechem
from openeye import oeshape


def main(argv=[__name__]):
    if len(argv) != 5:
        oechem.OEThrow.Usage("%s <reffile> <fitfile> <outfile> <nhits>" % argv[0])

    fitfs = oechem.oemolistream(sys.argv[2])
    outfs = oechem.oemolostream(sys.argv[3])
    nhits = int(sys.argv[4])

    query = oeshape.OEShapeQuery()
    oeshape.OEReadShapeQuery(sys.argv[1], query)

    # Setup OEROCS with specified number of best hits
    options = oeshape.OEROCSOptions()
    options.SetNumBestHits(nhits)
    rocs = oeshape.OEROCS(options)

    rocs.SetDatabase(fitfs)
    for res in rocs.Overlay(query):
        outmol = res.GetOverlayConf()
        oechem.OEWriteMolecule(outfs, outmol)
        print("title: %s  tanimoto combo = %.2f" % (outmol.GetTitle(), res.GetTanimotoCombo()))


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

Shape from Hermite expansion

Using Hermite expansion we can obtain two functionalities: expand a molecule into Hermite representation and compare two molecules that are in the Hermite representation by computing their shape-Tanimoto coefficient. We illustrate both functionalities below with two examples.

Hermite expansion

In this example we input a molecule, set the level of Hermite expansion using NPolyMax variable, determine the optimal parameters for \(\lambda_x, \lambda_y, \lambda_z\), and perform the computation of the Hermite expansion. At the end we output the resulting Hermite representation of the molecule as a grid, using CreateGrid method of the OEHermite class. The user can vary NPolyMax from low numbers, like 4 or 5, to higher numbers like 10-30 and observe convergence of the shape. Which value to use in practice depends on the size of the input molecule. Input molecule can be drug-like as well as a protein.

Listing 16: Expanding a molecular shape into Hermite polynomials

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

import sys
from openeye import oechem
from openeye import oeshape
from openeye import oegrid


def main(argv=[__name__]):
    itf = oechem.OEInterface(InterfaceData, argv)

    NPolyMax = itf.GetInt("-NPolyMax")
    gridspacing = itf.GetFloat("-gridspacing")

    ifname = itf.GetString("-inputfile")
    ofname = itf.GetString("-outputgrid")\

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

    if (not ofname.endswith(".grd")):
        oechem.OEThrow.Fatal("Output grid file extension hast to be '.grd' ")

    mol = oechem.OEMol()

    if (not oechem.OEReadMolecule(ifs, mol)):
        oechem.OEThrow.Fatal("Unable to read molecule in %s" % ifname)

    oechem.OESuppressHydrogens(mol)

    hermiteoptions = oeshape.OEHermiteOptions()
    hermiteoptions.SetNPolyMax(NPolyMax)
    hermiteoptions.SetUseOptimalLambdas(True)

    hermite = oeshape.OEHermite(hermiteoptions)

    if (not hermite.Setup(mol)):
        oechem.OEThrow.Fatal("Was not able to Setup the molecule for the OEHermite class.")

    hopts = hermite.GetOptions()
    print("Best lambdas found X=" + str(hopts.GetLambdaX()) + "  Y="
          + str(hopts.GetLambdaY()) + "  Z=" + str(hopts.GetLambdaZ()))

    print("Hermite self-overlap=", hermite.GetSelfOverlap())

    coeffs = hermite.GetCoefficients()
    NPolyMaxstring = str(NPolyMax)
    print("Hermite coefficients f_{l,m,n} in the following order l = 0..."
          + NPolyMaxstring + ", m = 0..." + NPolyMaxstring+"-l, n = " + NPolyMaxstring + "-l-m :")

    for x in coeffs:
        print(str(x) + " "),

    grid = oegrid.OEScalarGrid()

    hermite.CreateGrid(grid, gridspacing)

    if (not oegrid.OEWriteGrid(ofname, grid)):
        oechem.OEThrow.Fatal("Unable to write grid file")

    return 0


#############################################################################
InterfaceData = '''
!BRIEF [-inputfile] <InputFileName> [-outputgrid] <OutputGridFileName> [-NPolyMax] <NPolyMax> \
[-numgridpoints] <numgridpoints> [-gridspacing] <gridspacing>

!CATEGORY "input/output options :" 1

  !PARAMETER -inputfile 1
    !ALIAS -in
    !TYPE string
    !REQUIRED true
    !BRIEF Filename of the input molecule
    !KEYLESS 1
  !END

  !PARAMETER -outputgrid 2
  !ALIAS -out
    !TYPE string
    !REQUIRED true
    !BRIEF Filename of the output Hermite grid (needs to have .grd file extension)
    !KEYLESS 2
  !END

!END

!CATEGORY "Hermite options :" 2

  !PARAMETER -NPolyMax 3
    !ALIAS -NP
    !TYPE int
    !REQUIRED false
    !DEFAULT 5
    !LEGAL_RANGE 0 30
    !BRIEF Resolution parameter of Hermite Prep
    !KEYLESS 3
  !END


  !PARAMETER -gridspacing 4
    !ALIAS -gs
    !TYPE float
    !REQUIRED false
    !DEFAULT 1.0
    !LEGAL_RANGE 0.01 10.0
    !BRIEF Grid spacing of the output Hermite grid
    !KEYLESS 4
  !END

!END
'''

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

Hermite Tanimoto comparison of shape

In the second example below we setup two molecules and compute the Tanimoto-shape similarity coefficient, using Hermite expansion. The Hermite prep parameter NPolyMax_MAX is used to vary NPolyMax parameter and compute the shape-Tanimoto coefficient. First molecule in the reference input file is used versus all molecules in the fit input file. Results are attached as SD data to the moved fit molecule, as dictated by Hermite overlay, and sent to the output file. One can observe convergence of the Hermite Tanimoto coefficients as the level of expansion becomes more accurate.

Listing 17: Calculating shape Tanimoto using Hermite expansion

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

import sys
from openeye import oechem
from openeye import oeshape
from openeye import oeff

from openeye.oechem import (
        OEFloatArray, OEDoubleArray, OETrans,
        OESetSDData,
        OERotate, OETranslate,
        OEReadMolecule, OEWriteMolecule, oemolistream, oemolostream, OEMol,
        OEInterface,
        OEThrow,
        OESuppressHydrogens
    )

from openeye.oeshape import (
        OEOverlapPrep, OEMultiRefOverlay,
        OEHermiteOptions, OEHermite, OEHermiteShapeFunc,
        OEOrientByMomentsOfInertia, OEGetMomentsOfInertiaTransform

    )

from math import sin, cos



def CreateRMat(alpha, beta, gamma):
    rmat = OEFloatArray(9)

    rmat[0]=cos(beta)*cos(gamma)
    rmat[1]=cos(gamma)*sin(alpha)*sin(beta)-cos(alpha)*sin(gamma)
    rmat[2]=cos(alpha)*cos(gamma)*sin(beta)+sin(alpha)*sin(gamma)
    rmat[3]=cos(beta)*sin(gamma)
    rmat[4]=cos(alpha)*cos(gamma)+sin(alpha)*sin(beta)*sin(gamma)
    rmat[5]=-cos(gamma)*sin(alpha)+cos(alpha)*sin(beta)*sin(gamma)
    rmat[6]=-sin(beta)
    rmat[7]=cos(beta)*sin(alpha)
    rmat[8]=cos(alpha)*cos(beta)

    return rmat
  

def GetInertialRmatTransInverse(mol, final_mol):

   

    copy_mol = OEMol(mol)
    copy_mol_2 = OEMol(final_mol)
    
    transfm = OETrans()
    OEOrientByMomentsOfInertia(copy_mol, transfm)

    transfm.Transform(copy_mol_2)

    return OEMol(copy_mol_2)


def GetInertialRmatTransDirect(mol):

    copy_mol = OEMol(mol)
    transfm = OETrans()

    OEGetMomentsOfInertiaTransform(transfm, copy_mol)

    transfm.Transform(copy_mol)

    return OEMol(copy_mol)


def GetOptTransform(refmol, fitmol, xopt):

    ropt = OEFloatArray(9)
    topt = OEFloatArray(3)

    for i in range(3):
        topt[i] = xopt[i]

    ropt = CreateRMat(xopt[3], xopt[4], xopt[5])

    fitmol_moved = GetInertialRmatTransDirect(fitmol)
    
    OERotate(fitmol_moved, ropt)
    OETranslate(fitmol_moved, topt)    


    fitmol_moved_final = GetInertialRmatTransInverse(refmol, fitmol_moved)

    return fitmol_moved_final




def main(argv=[__name__]):
    itf = OEInterface(InterfaceData, argv)

    NPolyMax_MAX = itf.GetInt("-NPolyMax_MAX")

    ifrefname = itf.GetString("-inputreffile")
    iffitname = itf.GetString("-inputfitfile")
    ofname = itf.GetString("-outputfile")

    ifsref = oemolistream()
    ifsfit = oemolistream()
    ofs = oemolostream()

    if (not ifsref.open(ifrefname)):
        OEThrow.Fatal("Unable to open %s for reading" % ifrefname)

    if (not ifsfit.open(iffitname)):
        OEThrow.Fatal("Unable to open %s for reading" % iffitname)

    refmol = OEMol()
    fitmol = OEMol()

    newtonopt = oeff.OENewtonOpt()
    newtonopt.SetTolerance(0.0001)
    newtonopt.SetIterLimit(200)

    if (not OEReadMolecule(ifsref, refmol)):
        OEThrow.Fatal("Unable to read molecule in %s" % ifrefname)

    OESuppressHydrogens(refmol)

    prep = OEOverlapPrep()
    prep.SetAssignColor(False)
    prep.Prep(refmol)

    if (not ofs.open(ofname)):
        OEThrow.Fatal("Unable to open %s for writing" % ofname)
    OEWriteMolecule(ofs, refmol)

    idx = 0
    while (OEReadMolecule(ifsfit, fitmol)):

        OESuppressHydrogens(fitmol)
        prep.Prep(fitmol)
        print("Working on the fit molecule with idx = "+str(idx), "...")

        best = OEMultiRefOverlay()
        best.SetupRef(refmol)

        print("ROCS Taniomoto list= { "),
        for res in best.Overlay(fitmol):
             for score in res.GetScores():
                 print(score.tanimoto),
        print("}")

        for i in range(0, NPolyMax_MAX+1):
            print("Working on NPolyMax = " + str(i))

            hermiteoptionsref = OEHermiteOptions()
            hermiteoptionsref.SetNPolyMax(i)
            hermiteoptionsref.SetUseOptimalLambdas(True)

            hermiteref = OEHermite(hermiteoptionsref)
            if (not hermiteref.Setup(refmol)):
                OEThrow.Fatal("Was not able to Setup the reference molecule for the OEHermite class.")

            hermiteoptionsfit = OEHermiteOptions()
            hermiteoptionsfit.SetNPolyMax(i)
            hermiteoptionsfit.SetUseOptimalLambdas(True)

            hermitefit = OEHermite(hermiteoptionsfit)
            if (not hermitefit.Setup(fitmol)):
                OEThrow.Fatal("Was not able to Setup the fit molecule for the OEHermite class.")

            refvol = hermiteref.GetSelfOverlap()
            fitvol = hermitefit.GetSelfOverlap()

            hrefopts = hermiteref.GetOptions()
            hfitopts = hermitefit.GetOptions()

            hermiteshapeoverlap = OEHermiteShapeFunc(hrefopts, hfitopts)

            if (not hermiteshapeoverlap.SetupRef(refmol)):
                OEThrow.Fatal("Was not able to Setup the reference molecule for the OEHermiteShapeFunc class.")

            if (not hermiteshapeoverlap.Setup(fitmol)):
                OEThrow.Fatal("Was not able to Setup the fit molecule for the OEHermiteShapeFunc class.")

            x = OEDoubleArray(6)
            for j in range(0, 6):
                x[j] = 0.0
            xOpt = OEDoubleArray(6)

            bestoverlap = - newtonopt(hermiteshapeoverlap, x, xOpt)
            hermitetanimoto = bestoverlap/(refvol+fitvol-bestoverlap)

            print("Hermite Tanimoto = " + str(hermitetanimoto))
            
            fitmol_moved = GetOptTransform(refmol, fitmol, xOpt)

            OESetSDData(fitmol_moved, "HermiteTanimoto_"+str(i), str(hermitetanimoto))

            idx += 1
            OEWriteMolecule(ofs, fitmol_moved)

        if idx == 0:
            OEThrow.Warning("Fit molecules file does not contain valid molecules. The output file will be empty")

    ofs.flush()
    ofs.close()


#############################################################################
InterfaceData = '''
!BRIEF [-inputreffile] <InputReferenceFileName> [-inputfitfile] <InputFitFileName> \
[-outputfile] <OutputFileName> [-NPolyMax_MAX] <NPolyMax_MAX>

!CATEGORY "input/output options :" 1

  !PARAMETER -inputreffile 1
    !ALIAS -inref
    !TYPE string
    !REQUIRED true
    !BRIEF Input file name with reference molecule (will only read the first molecule).
    !KEYLESS 1
  !END
  !PARAMETER -inputfitfile 2
    !ALIAS -infit
    !TYPE string
    !REQUIRED true
    !BRIEF Input file name with fit molecules
    !KEYLESS 2
  !END
  !PARAMETER -outputfile 3
    !ALIAS -out
    !TYPE string
    !REQUIRED true
    !BRIEF Output file name
    !KEYLESS 3
  !END

!END

!CATEGORY "Hermite options :" 2

  !PARAMETER -NPolyMax_MAX 4
    !ALIAS -NP_MAX
    !TYPE int
    !REQUIRED false
    !DEFAULT 5
    !LEGAL_RANGE 0 30
    !BRIEF Maximum value of the parameter of the NPolyMax parameter of the Hermite prep \
    (NPolyMax will be varied from 0 to this value)
    !KEYLESS 4
  !END

!END
'''

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

Advanced Examples

Calculating NxN scores

There are times when you want to have all the pairwise scores among a set of molecules. Maintaining this 2D matrix of scores is more complicated than the single set of scores from the ref vs. set of fit molecules examples shown above.

This example takes a set of molecules and performs all the pairwise shape optimizations with OEBestOverlay. It outputs a spreadsheet of the scores.

Listing 18: Calculating NxN scores.

#!/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 __future__ import print_function

from openeye import oechem
from openeye import oeshape


def oneConf(conf, prep, filename, csvfile):
    csvfile.write("%s_%d" % (conf.GetTitle(), conf.GetIdx()))
    refmol = oechem.OEGraphMol(conf)

    options = oeshape.OEOverlayOptions()
    options.SetOverlapFunc(oeshape.OEGridShapeFunc())
    overlay = oeshape.OEOverlay(options)
    overlay.SetupRef(refmol)

    bfs = oechem.oemolistream(filename)
    fitmol = oechem.OEMol()
    while oechem.OEReadMolecule(bfs, fitmol):
        prep.Prep(fitmol)
        scoreiter = oeshape.OEBestOverlayScoreIter()
        oeshape.OESortOverlayScores(scoreiter, overlay.Overlay(fitmol), oeshape.OEHighestTanimoto())

        for score in scoreiter:
            csvfile.write(",%.2f" % score.GetTanimoto())
    csvfile.write("\n")


def genHeader(filename, csvfile):
    csvfile.write("name")
    ifs = oechem.oemolistream(filename)
    mol = oechem.OEMol()
    while oechem.OEReadMolecule(ifs, mol):
        for conf in mol.GetConfs():
            csvfile.write(",%s_%d" % (conf.GetTitle(), conf.GetIdx()))
    csvfile.write("\n")


def main(argv=[__name__]):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <infile> <csvfile>" % argv[0])

    csvfile = open(argv[2], "w")
    genHeader(sys.argv[1], csvfile)

    prep = oeshape.OEOverlapPrep()
    prep.SetAssignColor(False)
    afs = oechem.oemolistream(argv[1])
    for molA in afs.GetOEMols():
        prep.Prep(molA)
        print(molA.GetTitle())
        for conf in molA.GetConfs():
            oneConf(conf, prep, sys.argv[1], csvfile)


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

Calculating shape characteristics

While most functionality in the Shape Toolkit involves comparison of pairs of molecules, there are a few fundamental properties that can be calculated for a single molecule. All of these calculations are done using the same basic Gaussian description of a molecule as described above.

The simplest property to calculate is volume, using OECalcVolume.

In addition to simple volume calculations, the steric multipoles of a molecule can also be calculated. See section OECalcShapeMultipoles.

This next example demonstrates the calculation of volume and shape multipoles.

Listing 19: Calculating shape properties.

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


def main(argv=[__name__]):
    if len(argv) != 2:
        oechem.OEThrow.Usage("%s <infile>" % argv[0])

    ifs = oechem.oemolistream(argv[1])

    for mol in ifs.GetOEGraphMols():
        oechem.OEThrow.Info("              Title: %s" % mol.GetTitle())
        oechem.OEThrow.Info("             Volume: %8.2f" % oeshape.OECalcVolume(mol))
        oechem.OEThrow.Info("Volume: (without H): %8.2f\n" % oeshape.OECalcVolume(mol, False))

        smult = oeshape.OECalcShapeMultipoles(mol)

        oechem.OEThrow.Info("  Steric multipoles:")
        oechem.OEThrow.Info("           monopole: %8.2f" % smult[0])
        oechem.OEThrow.Info("        quadrupoles: %8.2f %8.2f %8.2f" % (smult[1], smult[2], smult[3]))
        oechem.OEThrow.Info("          octopoles:")
        oechem.OEThrow.Info("                xxx: %8.2f  yyy: %8.2f  zzz: %8.2f" % (smult[4], smult[5], smult[6]))
        oechem.OEThrow.Info("                xxy: %8.2f  xxz: %8.2f  yyx: %8.2f" % (smult[7], smult[8], smult[9]))
        oechem.OEThrow.Info("                yyz: %8.2f  zzx: %8.2f  zzy: %8.2f" % (smult[10], smult[11], smult[12]))
        oechem.OEThrow.Info("                xyz: %8.2f\n" % smult[13])


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

Calculating excluded volume

This code demonstrates how to use the shape toolkit to do an overlay to a crystallographic ligand, then calculate the overlap of the database molecule to the cognate protein. The output includes a new score Rescore which is the ShapeTanimotoCombo penalized by the fraction overlap.

Command Line Interface

A description of the command line interface can be obtained by executing the program with the –help argument.

prompt> python excludevolume.py --help

will generate the following output:

-q : Query file name
-e : Protein to use as exclusion volume
-d : Database file name
-o : Output file name

Code

Download code

excludevolume.py