Highlighting Fragments

Problem

You want to depict a molecule with highlighting of its fragments. See example in Table 1.

Table 1. Example of highlighting fragments of a molecule
OEGetFuncGroupFragments OEGetRingChainFragments OEGetRingLinkerSideChainFragments
../_images/OEGetFuncGroupFragments1.png ../_images/OEGetRingChainFragments1.png ../_images/OEGetRingLinkerSideChainFragments1.png

Ingredients

Note

Requires OpenEye toolkits version 2014.Feb or later.

Difficulty Level

../_images/chilly1.png ../_images/chilly1.png

Solution

The OEMedChem TK currently provides three ways to partition a molecule into fragments:

The DepictMoleculeWithFragmentHighlights function, that takes the following parameters, shows how to depict molecules with fragment highlighting.

image
An OEImage object which is a layout manager that allows generation of multi-page images.
mol
The molecule being fragmented.
fragfunc
The fragmentation function either OEGetRingChainFragments , OEGetRingLinkerSideChainFragments or OEGetFuncGroupFragments .
opts
An OE2DMolDisplayOptions object that defines the style of the molecule depiction.

First, the molecule is fragmented by invoking the ‘fragfunc’ fragmentation function (line 3). The fragmentation function partitions the molecule and returns an iterator over OEAtomBondSet objects each storing the atoms and bonds of a fragment. An OELinearColorGradient object is then created, based on the number of returned fragments (line 6-8), and is used to highlight each fragment with a distinct color. After constructing the molecule display, the fragments are looped over and highlighted using the OEAddHighlighting function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def DepictMoleculeWithFragmentHighlights(image, mol, fragfunc, opts):

    fraglist = [f for f in fragfunc(mol)]
    nrfrags = len(fraglist)

    colorg = OELinearColorGradient()
    colorg.AddStop(OEColorStop(0, OEYellowTint))
    colorg.AddStop(OEColorStop(nrfrags, OEDarkOrange))

    disp = OE2DMolDisplay(mol, opts)

    for fidx, frag in enumerate(fraglist):
        color = colorg.GetColorAt(fidx)
        OEAddHighlighting(disp, color, OEHighlightStyle_BallAndStick, frag)

    OERenderMolecule(image, disp)

Download code

frags2img.py

Usage:

prompt > python3 frags2img.py -in molecule.ism -out fragments.png -fragtype ring-chain

Discussion

The example below generates a multi-page PDF document. At the top of each page an input molecule is rendered highlighting its fragments. These fragments are then depicted one by one on the page. See example in Figure: Example of depiction of molecules with their fragments.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def DepictMoleculesWithFragments(report, mollist, fragfunc, opts):

    for mol in mollist:

        body = report.NewBody()
        OEPrepareDepiction(mol)
        header = report.GetHeader(report.NumPages())

        # loop over input molecule and fragment

        fragsets = [f for f in fragfunc(mol)]
        fragmols = []
        for fset in fragsets:
            fragment = OEGraphMol()
            fragpred = OEIsAtomMember(fset.GetAtoms())
            adjustHCount = True
            OESubsetMol(fragment, mol, fragpred, adjustHCount)
            fragmols.append(OEGraphMol(fragment))

        nrfrags = len(fragmols)
        colorg = OELinearColorGradient(OEColorStop(0, OEYellowTint),
                                       OEColorStop(nrfrags - 1, OEDarkOrange))

        # render molecule with fragment highlights

        cellwidth, cellheight = report.GetHeaderWidth(), report.GetHeaderHeight()
        opts.SetDimensions(cellwidth, cellheight, OEScale_AutoScale)

        disp = OE2DMolDisplay(mol, opts)
        for fidx, fset in enumerate(fragsets):
            color = colorg.GetColorAt(fidx)
            OEAddHighlighting(disp, color, OEHighlightStyle_BallAndStick, fset)

        OERenderMolecule(header, disp)

        # create fragment grid

        rows = max(2, int(math.sqrt(nrfrags + 1)))
        cols = max(2, int(nrfrags / rows) + 1)
        grid = OEImageGrid(body, rows, cols)
        grid.SetCellGap(8.0)

        cellwidth, cellheight = grid.GetCellWidth(), grid.GetCellHeight()
        opts.SetDimensions(cellwidth, cellheight, OEScale_AutoScale)
        opts.SetTitleLocation(OETitleLocation_Hidden)

        # determine the scale factor to depict fragments with equal size

        minscale = OEGetMoleculeScale(mol, opts) * 1.25
        for frag in fragmols:
            minscale = min(minscale, OEGetMoleculeScale(frag, opts))
        opts.SetScale(minscale)

        # render each fragments

        for fidx, (cell, fmol) in enumerate(zip(grid.GetCells(), fragmols)):
            OEPrepareDepiction(fmol)
            disp = OE2DMolDisplay(fmol, opts)
            OERenderMolecule(cell, disp)

            color = colorg.GetColorAt(fidx)
            OEDrawBorder(cell, OEPen(OEWhite, color, OEFill_Off, 3.0))

Download code

frags2pdf.py

Usage:

prompt > python3 frags2pdf.py -in molecules.ism -out fragments.pdf -fragtype func-group
Example of depiction of molecules with their fragments (The pages are reduced here for visualization convenience)
page 1 page 2 page 3 ..
../_images/frags2pdf-1.png ../_images/frags2pdf-2.png ../_images/frags2pdf-3.png ..

See Also in OEChem TK Manual

Theory

API

See Also in OEMedChem TK Manual

Theory

API

See Also in OEDepict TK Manual

Theory

API