Depicting Atom Contributions of XLogP

Problem

You want to depict the contribution of each atom to the total XLogP on your molecule diagram. See example in Figure 1.

../_images/xlogp2img.svg

Figure 1. Example of depicting the atom contributions of XLogP

Ingredients

Difficulty Level

../_images/chilly.png ../_images/chilly.png

Download

Download code

xlogp2img.py and xlogp2pdf.py

See also the Usage (xlogp2img) and Usage (xlogp2pdf) subsections.

Solution

The code snippet below shows how to calculate the total XLogP of a molecule along with the atom contributions by calling the OEGetXLogP function. Each atom contribution is then attached to the relevant atom as generic data with the given tag.

 1def set_atom_properties(mol, tag):
 2    """
 3    Attaches the XLogP atom contribution to each atom with the given tag.
 4
 5    :type mol: oechem.OEMolBase
 6    :type tag: string
 7    """
 8
 9    oequacpac.OERemoveFormalCharge(mol)
10
11    avals = oechem.OEFloatArray(mol.GetMaxAtomIdx())
12    logp = oemolprop.OEGetXLogP(mol, avals)
13
14    mol.SetTitle(mol.GetTitle() + " -- OEXLogP = %.2f" % logp)
15
16    for atom in mol.GetAtoms():
17        val = avals[atom.GetIdx()]
18        atom.SetData(tag, val)

The depict_molecule_xlogp function below shows how to project the atom contributions of the total XLogP into a 2D molecular diagram using the OE2DPropMap class. After constructing the OE2DMolDisplay object to depict a molecule, an OE2DPropMap object is initialized and its properties are set that determine how the data is going to be visualized (lines 19-22). For example in this case the negative color xlogp atom contributions are going to be represented by dark green, while the positive values are visualized by using dark purple.

When the OE2DPropMap.Render method is called with same tag that was used in the set_atom_properties function, the properties that were attached to the atoms as generic data are retrieved and a 2D grid is generated underneath the molecular diagram. The OELinearColorGradient object that is used to assign colors to the cells of the grid is also rendered based on the option set by the OE2DPropMap.SetLegendLocation method . You can see the result in Figure 1.

 1def depict_molecule_xlogp(image, mol, opts):
 2    """
 3    Generates an image of a molecule depicting the atom contribution of XLogP.
 4
 5    :type image oedepict.OEImageBase
 6    :type mol: list[oechem.OEMolBase]
 7    :type opts: oedepict.OE2DMolDiplayOptions
 8    """
 9
10    scale = oegrapheme.OEGetMoleculeSurfaceScale(mol, opts)
11    opts.SetScale(scale)
12
13    stag = "XLogP"
14    itag = oechem.OEGetTag(stag)
15    set_atom_properties(mol, itag)
16
17    disp = oedepict.OE2DMolDisplay(mol, opts)
18
19    propmap = oegrapheme.OE2DPropMap(opts.GetBackgroundColor())
20    propmap.SetNegativeColor(oechem.OEDarkGreen)
21    propmap.SetPositiveColor(oechem.OEDarkPurple)
22    propmap.SetLegendLocation(oegrapheme.OELegendLocation_Left)
23    propmap.Render(disp, stag)
24
25    oedepict.OERenderMolecule(image, disp)

Hint

You can easily adapt this example to visualize other atom properties by writing your own set_atom_properties function.

Usage (xlogp2img)

Usage

xlogp2img.py

The following commands will generate the image shown in Figure 1.

prompt > echo "SCCNC(=O)c2ccc3c(c2)sc(n3)NC(=O)NCC"  > molecule.ism
prompt > python3 xlogp2img.py molecule.ism xlogp.png

Command Line Parameters

Simple parameter list
    -height : Height of output image
    -width : Width of output image

    molecule display options :
      -aromstyle : Aromatic ring display style

    input/output options
      -in : Input molecule filename
      -out : Output filename of the generated image

Discussion

The example above shows how to visualize the atom contributions for a single molecule, however you might want to visualize the XLogP data for a set of molecules. In this case the set_atom_properties function not only attaches the XLogP contributions to the relevant atom, but also calculates the minimum and maximum atom contributions for the whole molecule set.

 1def set_atom_properties(mol, tag, minvalue, maxvalue):
 2    """
 3    Attaches the XLogP atom contribution to each atom with the given tag.
 4
 5    :type mol: oechem.OEMolBase
 6    :type datatag: string
 7    :type minvalue: float
 8    :type maxvalue: float
 9    """
10
11    oequacpac.OERemoveFormalCharge(mol)
12
13    avals = oechem.OEFloatArray(mol.GetMaxAtomIdx())
14    logp = oemolprop.OEGetXLogP(mol, avals)
15
16    mol.SetTitle(mol.GetTitle() + " -- OEXLogP = %.2f" % logp)
17
18    for atom in mol.GetAtoms():
19        val = avals[atom.GetIdx()]
20        atom.SetData(tag, val)
21        minvalue = min(minvalue, val)
22        maxvalue = max(maxvalue, val)
23    return minvalue, maxvalue

These minimum and maximum values are used to initialize the value range of the linear color gradient of the property map (see lines 27-28 of the depict_molecules_xlogp function below). Each molecule, along with its property map, is then rendered into a cell of an OEReport object. The OEReport class is a layout manager allowing generation of multi-page images in a convenient way. You can see the generated multi-page PDF in Table 1. While the value range of the color gradients depicted alongside the molecules represents the range for the whole set, the black box rendered on each color gradient represents the minimum and maximum XLogP atom contributions for the corresponding molecule.

 1def depict_molecules_xlogp(report, mollist, opts):
 2    """
 3    Generates a report of molecules depicting the atom contribution of XLogP.
 4
 5    :type report: oedepict.OEReport
 6    :type mollist: list[oechem.OEMolBase]
 7    :type opts: oedepict.OE2DMolDiplayOptions
 8    """
 9
10    molscale = float("inf")
11    for mol in mollist:
12        molscale = min(molscale, oegrapheme.OEGetMoleculeSurfaceScale(mol, opts))
13    opts.SetScale(molscale)
14
15    stag = "XLogP"
16    itag = oechem.OEGetTag(stag)
17
18    minvalue = float("inf")
19    maxvalue = float("-inf")
20    for mol in mollist:
21        minvalue, maxvalue = set_atom_properties(mol, itag, minvalue, maxvalue)
22
23    propmap = oegrapheme.OE2DPropMap(opts.GetBackgroundColor())
24    propmap.SetNegativeColor(oechem.OEDarkGreen)
25    propmap.SetPositiveColor(oechem.OEDarkPurple)
26    propmap.SetLegendLocation(oegrapheme.OELegendLocation_Left)
27    propmap.SetMinValue(minvalue)
28    propmap.SetMaxValue(maxvalue)
29
30    for mol in mollist:
31
32        disp = oedepict.OE2DMolDisplay(mol, opts)
33        propmap.Render(disp, stag)
34
35        cell = report.NewCell()
36        oedepict.OERenderMolecule(cell, disp)
Table 1. Example of depicting atom contributions of XLogP for a set of molecules (The pages are reduced here for visualization convenience)

page 1

page 2

page 3

../_images/xlogp2pdf-01.png ../_images/xlogp2pdf-02.png ../_images/xlogp2pdf-03.png

Usage (xlogp2pdf)

Usage

xlogp2pdf.py and supporting data examples.ism

The following command will generate the report shown in Table 1.

prompt > python3 xlogp2pdf.py molecules.ism xlogp.pdf

Command Line Parameters

Simple parameter list
    molecule display options :
      -aromstyle : Aromatic ring display style

    report options
      -pagebypage : Write individual numbered separate pages

    report options :
      -colsperpage : Number of columns per page
      -pageheight : Page height
      -pageorientation : Page orientation
      -pagesize : Page size
      -pagewidth : Page width
      -rowsperpage : Number of rows per page

    input/output options
      -in : Input molecule filename
      -out : Output filename of the generated image

See also in OEChem TK manual

Theory

API

See also in MolProp TK manual

API

See also in OEDepict TK manual

Theory

API

See also in GraphemeTM TK manual

Theory

API