Annotating Atoms and BondsΒΆ

The Grapheme TK provides a framework to annotate atoms and bonds i.e. mark them based on their properties. The following Listing 1 example shows how to mark \(sp^2\) and \(sp^3\) hybridized atoms of depicted molecules. After preparing the molecule for 2D depiction and calculating the atom hybridizations, the molecule display object is created (OE2DMolDisplay) that stores the depiction information of a molecule The OEAddGlyph function is then called to add glyph to those atoms for which the given OEIsAtomHybridization functor returns true. In this example, the built-in OEAtomGlyphCircle class is used to mark atoms by drawings a circle around them with a specific style (OECircleStyle). Finally, the image is written out to a file by calling the OEWriteImage function. The image created by Listing 1 example is shown in Figure: Example of atom annotation.

Listing 1: Example of using a built-in atom annotation style

#!/usr/bin/env python
from openeye.oechem import *
from openeye.oedepict import *
from openeye.oegrapheme import *

mol = OEGraphMol()
OESmilesToMol(mol, "c1cc(N)cc(S(=O)(=O)O)c1")
OEAssignHybridization(mol)
OEPrepareDepiction(mol)

opts = OE2DMolDisplayOptions(350, 250, OEScale_AutoScale)
opts.SetTitleLocation(OETitleLocation_Hidden)
disp = OE2DMolDisplay(mol, opts)

sp2pen = OEPen(OEWhite, OEBlueTint, OEFill_Off, 1.5)
glyphSP2 = OEAtomGlyphCircle(sp2pen, OECircleStyle_Sun, 1.2)
OEAddGlyph(disp, glyphSP2, OEIsAtomHybridization(OEHybridization_sp2))

sp3pen = OEPen(OEWhite, OEPinkTint, OEFill_Off, 1.5)
glyphSP3 = OEAtomGlyphCircle(sp3pen, OECircleStyle_Eyelash, 1.2)
OEAddGlyph(disp, glyphSP3, OEIsAtomHybridization(OEHybridization_sp3))

OERenderMolecule("AnnotateAtomPredicate.png", disp)
OERenderMolecule("AnnotateAtomPredicate.pdf", disp)
../_images/AnnotateAtomPredicate.png

Example of atom annotation

See also

Similarly, the following example shows how to annotate bonds. The bonds being highlighted are specified by the bond predicate passed to the OEAddGlyph function. For each bond the predicate returns true, i.e. if it is a rotatable bond, the OEBondGlyphArrow.RenderGlyph method is invoked that draws an arrow across the middle of the specific bond. The image created by Listing 2 is shown in Figure: Example of bond annotation.

Listing 2: Example of using a built-in bond annotation style

#!/usr/bin/env python
from openeye.oechem import *
from openeye.oedepict import *
from openeye.oegrapheme import *

mol = OEGraphMol()
OESmilesToMol(mol, "c1cc(NCC)cc(CS(=O)(=O)O)c1")
OEPrepareDepiction(mol)

opts = OE2DMolDisplayOptions(400, 250, OEScale_AutoScale)
opts.SetTitleLocation(OETitleLocation_Hidden)
disp = OE2DMolDisplay(mol, opts)

pen = OEPen(OEDarkPurple, OEDarkPurple, OEFill_Off, 2.0)
glyph = OEBondGlyphArrow(pen, 0.5)
OEAddGlyph(disp, glyph, OEIsRotor())

OERenderMolecule("AnnotateBondPredicate.png", disp)
OERenderMolecule("AnnotateBondPredicate.pdf", disp)
../_images/AnnotateBondPredicate.png

Example of bond annotation

The following table lists the customizable bond glyphs that are currently available in Grapheme TK.

Supported bond glyphs
../_images/OEBondGlyphArrow_Default.png ../_images/OEBondGlyphCircle_Default.png ../_images/OEBondGlyphCross_Default.png ../_images/OEBondGlyphCurvedArrow_Default.png
OEBondGlyphArrow OEBondGlyphCircle OEBondGlyphCross OEBondGlyphCurvedArrow
../_images/OEBondGlyphScissors_Default.png ../_images/OEBondGlyphStitch_Default.png ../_images/OEBondGlyphZigZag_Default.png  
OEBondGlyphScissors OEBondGlyphStitch OEBondGlyphZigZag  

See also

The last example shows how to depict non-boolean properties by implementing a user-defined annotation style. User-defined atom and bond annotations can be implemented by deriving from the OEAtomGlyphBase and the OEBondGlyphBase abstract classes and implementing the OEAtomGlyphBase.RenderGlyph and the OEBondGlyphBase.RenderGlyph methods, respectively.

In the Listing 3 example, after calculating the MMFF charges, the atoms are annotated by their charge using the OELinearColorGradient class that interpolates colors. Atoms with negative and positive charge are highlighted by red and blue colors, respectively. The image created by Listing 3 is shown in Figure: Example of user-defined annotation.

Listing 3: Example of user-defined annotation

#!/usr/bin/env python
from openeye.oechem import *
from openeye.oedepict import *
from openeye.oegrapheme import *


class ColorCharge(OEAtomGlyphBase):
    def __init__(self, cg):
        OEAtomGlyphBase.__init__(self)
        self.colorg = cg

    def RenderGlyph(self, disp, atom):
        adisp = disp.GetAtomDisplay(atom)
        if adisp is None or not adisp.IsVisible():
            return False

        charge = atom.GetPartialCharge()
        if charge == 0.0:
            return True
        color = self.colorg.GetColorAt(charge)

        pen = OEPen()
        pen.SetForeColor(OEColor(color))
        color.SetA(100)
        pen.SetBackColor(OEColor(color))
        pen.SetFill(OEFill_On)
        radius = disp.GetScale() / 2.5

        layer = disp.GetLayer(OELayerPosition_Below)
        OEDrawCircle(layer, OECircleStyle_Simpson, adisp.GetCoords(), radius, pen)

        return True

    def CreateCopy(self):
        return ColorCharge(self.colorg).__disown__()

mol = OEGraphMol()
OESmilesToMol(mol, "Cc1cc(cc(c1[N+](=O)[O-])F)[N+]#C")
OEMMFFAtomTypes(mol)
OEMMFF94PartialCharges(mol)

OEPrepareDepiction(mol)

opts = OE2DMolDisplayOptions(350, 250, OEScale_AutoScale)
opts.SetAtomColorStyle(OEAtomColorStyle_WhiteMonochrome)
opts.SetTitleLocation(OETitleLocation_Hidden)
disp = OE2DMolDisplay(mol, opts)

coloranion = OEColorStop(-1.0, OEColor(OEDarkRed))
colorcation = OEColorStop(+1.0, OEColor(OEDarkBlue))
colorg = OELinearColorGradient(coloranion, colorcation)
colorg.AddStop(OEColorStop(0.0, OEColor(OEWhite)))

colorcharge = ColorCharge(colorg)

OEAddGlyph(disp, colorcharge, OEIsTrueAtom())

OERenderMolecule("AnnotatePartialCharge.png", disp)
OERenderMolecule("AnnotatePartialCharge.pdf", disp)
../_images/AnnotatePartialCharge.png

Example of user-defined annotation

See also