Depicting Atom Properties

Problem

You want to depict arbitrary atom properties calculated by another application or script. See examples in Table 1..

Table 1. Example of depicting partial charges with various styles
property map atom glyph molecule surface
../_images/atomprop2img-partialcharge-propmap.png ../_images/atomprop2img-partialcharge-atomglyph.png ../_images/atomprop2img-partialcharge-molsurface.png

Ingredients

  • Grapheme TK - molecule and property visualization toolkit

Note

Requires OpenEye toolkits version 2014.Feb or later.

Difficulty Level

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

Solution

The OEChem TK provides a framework to associate arbitrary data with objects such as molecules, atoms and bonds. Generic data can be attached to an object by association either with an integer or string, called a tag identifier. When a molecule is written into an OEBinary (oeb) file, the generic data attached to the molecule and its atoms and bonds is written to the binary file as well. This provides a convenient way to transfer data along with the molecules from one application to another.

The DepictAtomProperty function visualizes properties attached to atom as generic data. First an image is divided into two frames, one for rendering a molecule with the atom properties and one for depicting the corresponding color gradient that shows the range of the property (lines 3-7). The scaling of the depiction options is then adjusted by utilizing the OEGetMoleculeSurfaceScale function. A OELinearColorGradient object is then initialized by calling the GetColorGradient function. This color gradient is used to associate the atom properties with colors that will represent them in the image. After constructing the molecule display, the atom properties are depicted on the molecular graph based on the user defined depiction style (lines 16-23). The molecule display and the color gradient are then rendered into the image frames along with drawing the label of the atom property into the image (lines 25-30).

 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
def DepictAtomProperty(image, mol, opts, tagname, negcolor, poscolor, style):

    mwidth, mheight = image.GetWidth(), image.GetHeight() * 0.9
    cwidth, cheight = image.GetWidth(), image.GetHeight() * 0.1

    mframe = OEImageFrame(image, mwidth, mheight, OE2DPoint(0.0, 0.0))
    cframe = OEImageFrame(image, cwidth, cheight, OE2DPoint(0.0, mheight))

    opts.SetDimensions(mwidth, mheight, OEScale_AutoScale)
    opts.SetScale(OEGetMoleculeSurfaceScale(mol, opts))

    colorg = GetColorGradient(mol, tagname, negcolor, poscolor)

    disp = OE2DMolDisplay(mol, opts)

    if style == "propmap":
        DepictAtomPropertyPropMap(disp, tagname, negcolor, poscolor)

    if style == "atomglyph":
        DepictAtomPropertyAtomGlyph(disp, tagname, colorg)

    if style == "molsurface":
        DepictAtomPropertyMolSurface(disp, tagname, colorg)

    OERenderMolecule(mframe, disp)

    OEDrawColorGradient(cframe, colorg)

    font = OEFont(OEFontFamily_Default, OEFontStyle_Default, 14, OEAlignment_Left, OEBlack)
    cframe.DrawText(OE2DPoint(10.0, -10.0), tagname, font)

The GetMinMaxAtomProperty function loops over the atoms of the molecule to find the minimum and maximum values attached to the atoms by the given tag identifier.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def GetMinMaxAtomProperty(mol, tagname):

    minvalue = float("inf")
    maxvalue = float("-inf")

    tag = OEGetTag(tagname)

    for atom in mol.GetAtoms():
        if atom.HasData(tag):
            val = atom.GetData(tag)
            minvalue = min(minvalue, val)
            maxvalue = max(maxvalue, val)

    return minvalue, maxvalue

The GetColorGradient function creates a OELinearColorGradient object based on the minimum and maximum atom property values.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def GetColorGradient(mol, tagname, ncolor, pcolor):

    minvalue, maxvalue = GetMinMaxAtomProperty(mol, tagname)

    colorg = OELinearColorGradient(OEColorStop(0.0, OEWhite))
    if minvalue < 0.0:
        colorg.AddStop(OEColorStop(minvalue, ncolor))
    if maxvalue > 0.0:
        colorg.AddStop(OEColorStop(maxvalue, pcolor))

    return colorg

The DepictAtomPropertyPropMap function shows how to project atom properties into a property map.

1
2
3
4
5
6
7
8
def DepictAtomPropertyPropMap(disp, tagname, negcolor, poscolor):

    opts = disp.GetOptions()
    propmap = OE2DPropMap(opts.GetBackgroundColor())
    propmap.SetLegendLocation(OELegendLocation_Hidden)
    propmap.SetNegativeColor(negcolor)
    propmap.SetPositiveColor(poscolor)
    propmap.Render(disp, tagname)

The DepictAtomPropertyAtomGlyph function shows how to visualize atom properties using atom glyphs.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def DepictAtomPropertyAtomGlyph(disp, tagname, colorg):

    tag = OEGetTag(tagname)
    mol = disp.GetMolecule()

    for atom in mol.GetAtoms():
        if atom.HasData(tag):
            value = atom.GetDoubleData(tag)
            color = colorg.GetColorAt(value)
            pen = OEPen(color, color, OEFill_Off, 3.0)
            glyph = OEAtomGlyphCircle(pen, OECircleStyle_Default, 1.2)
            OEAddGlyph(disp, glyph, OEHasAtomIdx(atom.GetIdx()))

The DepictAtomPropertyMolSurface function shows how to project atom properties into the molecule surface.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def DepictAtomPropertyMolSurface(disp, tagname, colorg):

    tag = OEGetTag(tagname)
    mol = disp.GetMolecule()

    for atom in mol.GetAtoms():
        if atom.HasData(tag):
            value = atom.GetDoubleData(tag)
            color = colorg.GetColorAt(value)
            pen = OEPen(color, color, OEFill_Off, 4.0)
            OESetSurfaceArcFxn(mol, atom, OEDefaultArcFxn(pen))

    OEDraw2DSurface(disp)

Download code

atomprop2img.py

Discussion

The following section shows examples how to generate the input oeb file for the atomprop2img.py script.

Atom Partial Charge

The following program generates an OEBinary oeb file in which the partial charge is attached to each atom by the given tag string identifier.

Download code

addpartialcharge.py

After running the addpartialcharge.py script, the atom partial charges can be visualized by calling the atomprop2img.py script with the same tag identifier.

Usage:

Running the above commands will generate the images shown in Table 1.

prompt > python3 addpartialcharge.py molecule.ism molecule.oeb partialcharge
prompt > python3 atomprop2img.py -in molecule.oeb -out partialcharge-propmap.png -tagname partialcharge -style propmap
prompt > python3 atomprop2img.py -in molecule.oeb -out partialcharge-atomglyph.png -tagname partialcharge -style atomglyph
prompt > python3 atomprop2img.py -in molecule.oeb -out partialcharge-molsurface.png -tagname partialcharge -style molsurface

Atom XLogP Contribution

The following program generates an OEBinary oeb file in which the contribution of XLogP is attached to each atom by the given tag string identifier.

Download code

addxlogp.py

After running the addxlogp.py script, the atom contributions of XLogP can be visualized by calling the atomprop2img.py script with the same tag identifier.

Usage:

Running the above commands will generate the images shown in Table 2.

prompt > python3 addxlogp.py molecule.ism molecule.oeb xlogp
prompt > python3 atomprop2img.py -in molecule.oeb -out xlogp-propmap.png -tagname xlogp -style propmap -negcolor darkgreen -poscolor darkpurple
prompt > python3 atomprop2img.py -in molecule.oeb -out xlogp-atomglyph.png -tagname xlogp -style atomglyph -negcolor darkgreen -poscolor darkpurple
prompt > python3 atomprop2img.py -in molecule.oeb -out xlogp-molsurface.png -tagname xlogp -style molsurface -negcolor darkgreen -poscolor darkpurple
Table 2. Example of depicting atom contributions of XLogP with various styles
property map atom glyph molecule surface
../_images/atomprop2img-xlogp-propmap.png ../_images/atomprop2img-xlogp-atomglyph.png ../_images/atomprop2img-xlogp-molsurface.png

See Also in OEChem TK Manual

Theory

API

See Also in OEDepict TK Manual

Theory

API

See Also in Grapheme TK Manual

Theory

API