Highlighting

Atoms and bonds of a molecule can be highlighted by using the OEAddHighlighting functions. OEDepict TK provides four built-in highlighting styles:

color

This style is associated with the OEHighlightStyle_Color constant. See Figure: Example of highlighting using the ‘color’ style.

../_images/OEHighlightStyle_Color.png

Example of highlighting using the ‘color’ style

stick

This style is associated with the OEHighlightStyle_Stick constant. See Figure: Example of highlighting using the ‘stick’ style.

../_images/OEHighlightStyle_Stick.png

Example of highlighting using the ‘stick’ style

ball and stick

This style is associated with the OEHighlightStyle_BallAndStick constant. See Figure: Example of highlighting using the ‘ball and stick’ style.

../_images/OEHighlightStyle_BallAndStick.png

Example of highlighting using the ‘ball and stick’ style

cogwheel

This style is associated with the OEHighlightStyle_Cogwheel constant. See Figure: Example of highlighting using the ‘cogwheel’ style.

../_images/OEHighlightStyle_Cogwheel.png

Example of highlighting using the ‘cogwheel’ style

lasso

This style is associated with the OEHighlightStyle_Lasso constant. See Figure: Example of highlighting using the ‘lasso’ style.

../_images/OEHighlightStyle_Lasso.png

Example of highlighting using the ‘lasso’ style

Using Highlighting Styles

The following example (Listing 1) shows how to use the built-in highlighting styles. The OEAddHighlighting function takes an OEMatchBase object and highlights each of its target atoms and bonds using the given style. In this case, the two pyridine rings in the target structure are highlighted by red color using the stick style. The image created by Listing 1 is shown in Figure: Example of using ‘stick’ highlighting style.

Listing 1: Example of using highlighting style

mol = oechem.OEGraphMol()
oechem.OESmilesToMol(mol, "c1cncc2c1cc3c(cncc3)c2")
oedepict.OEPrepareDepiction(mol)

subs = oechem.OESubSearch("c1ncccc1")
disp = oedepict.OE2DMolDisplay(mol)

unique = True
for match in subs.Match(mol, unique):
    oedepict.OEAddHighlighting(disp, oechem.OEColor(oechem.OERed),
                               oedepict.OEHighlightStyle_Stick, match)

oedepict.OERenderMolecule("HighlightSimple.png", disp)
../_images/HighlightSimple.png

Example of using ‘stick’ highlighting style

Using Highlighting Classes

OEDepict TK also provides the highlighting classes that correspond to the highlighting styles. Each highlighting style can be customized by using the corresponding class:

The next example (Listing 2) shows how the stick style can be customized by using the corresponding OEHighlightByStick class. The image created by Listing 2 is shown in Figure: Example of customizing the ‘stick’ highlighting style.

Listing 2: Example of using highlighting class

mol = oechem.OEGraphMol()
oechem.OESmilesToMol(mol, "c1cncc2c1cc3c(cncc3)c2")
oedepict.OEPrepareDepiction(mol)

subs = oechem.OESubSearch("c1ncccc1")

opts = oedepict.OE2DMolDisplayOptions(240.0, 100.0, oedepict.OEScale_AutoScale)
opts.SetMargins(10.0)
disp = oedepict.OE2DMolDisplay(mol, opts)

stickWidthScale = 6.0
monochrome = True
highlight = oedepict.OEHighlightByStick(oechem.OEColor(oechem.OERed),
                                        stickWidthScale, not monochrome)

unique = True
for match in subs.Match(mol, unique):
    oedepict.OEAddHighlighting(disp, highlight, match)

oedepict.OERenderMolecule("HighlightStyle.png", disp)
../_images/HighlightStyle.png

Example of customizing the ‘stick’ highlighting style

See also

Highlighting Overlapped Patterns

If more than one part of a molecule is highlighted, these highlights can overlap resulting in loss of information. For example, highlighting the matches of the c1cc[c,n]cc1 SMARTS pattern will produce the following image.

../_images/HighlightAllInOne.png

Example of highlighting multiple matches in the same image

There are two ways how this can be avoided and correctly highlight overlapping parts of a molecule. The first example demonstrates how to depict multiple matches in different image cells one by one. The image created by Listing 3 is shown in Figure: Example of highlighting multiple matches one by one.

Listing 3: Example of highlighting multiple matches one by one

mol = oechem.OEGraphMol()
oechem.OESmilesToMol(mol, "c1cncc2c1cc3c(cncc3)c2")
oedepict.OEPrepareDepiction(mol)

subs = oechem.OESubSearch("c1cc[c,n]cc1")

width, height = 350, 250
image = oedepict.OEImage(width, height)

rows, cols = 2, 2
grid = oedepict.OEImageGrid(image, rows, cols)
opts = oedepict.OE2DMolDisplayOptions(grid.GetCellWidth(), grid.GetCellHeight(),
                                      oedepict.OEScale_AutoScale)

unique = True
for match, cell in zip(subs.Match(mol, unique), grid.GetCells()):
    disp = oedepict.OE2DMolDisplay(mol, opts)
    oedepict.OEAddHighlighting(disp, oechem.OEColor(oechem.OEPink),
                               oedepict.OEHighlightStyle_Color, match)
    oedepict.OERenderMolecule(cell, disp)

oedepict.OEWriteImage("HighlightMulti.png", image)
../_images/HighlightMulti.png

Example of highlighting multiple matches one by one

The second example (Listing 4) illustrates how to highlight overlapping matches at once using the OEHighlightOverlayByBallAndStick class. In this example, the OEAddHighlightOverlay function takes all matches being highlighted and colors the overlapped atoms and bonds using the colors by turn. The colors used for highlighting are determined when the OEHighlightOverlayByBallAndStick object is constructed. The image created by Listing 4 is shown in Figure: Example of highlighting overlapping matches at once.

Listing 4: Example of highlighting multiple matches simultaneously

mol = oechem.OEGraphMol()
oechem.OESmilesToMol(mol, "c1cncc2c1cc3c(cncc3)c2")
oedepict.OEPrepareDepiction(mol)

subs = oechem.OESubSearch("c1cc[c,n]cc1")

opts = oedepict.OE2DMolDisplayOptions(240.0, 100.0, oedepict.OEScale_AutoScale)
opts.SetMargins(10.0)
disp = oedepict.OE2DMolDisplay(mol, opts)

highlight = oedepict.OEHighlightOverlayByBallAndStick(oechem.OEGetContrastColors())

unique = True
oedepict.OEAddHighlightOverlay(disp, highlight, subs.Match(mol, unique))

oedepict.OERenderMolecule("HighlightOverlay.png", disp)
../_images/HighlightOverlay.png

Example of highlighting overlapping matches at once