# Drawing a Molecule Surface¶

The 2D representation of the molecule surface is calculated by first drawing circles around each atom of the molecule and then identifying the external arc segments that define a continuous surface around the molecule. This process is illustrated in the table Table: Calculation of the 2D representation of a molecule surface

The following Listing 1 example shows how to the display a molecule surface. After creating an image object and preparing the molecule for 2D depiction, an arc drawing functor is added to each atom of the molecule by using the OESetSurfaceArcFxn function. The surface then can be drawn by calling the OEDraw2DSurface function that calculates the arc segments that form a continuous curve around the molecule. The image created by Listing 1 is shown in Figure: Example of surface drawing.

Listing 1: Example of surface drawing

opts = OE2DMolDisplayOptions(imagewidth, imageheight, OEScale_AutoScale)
opts.SetTitleLocation(OETitleLocation_Hidden)
opts.SetScale(OEGetMoleculeSurfaceScale(mol, opts))

arcfxn = OEEyelashArcFxn(OEPen(OEGrey, OEGrey))

for atom in mol.GetAtoms():
OESetSurfaceArcFxn(mol, atom, arcfxn)

disp = OE2DMolDisplay(mol, opts)
OEDraw2DSurface(disp)


Example of surface drawing

The Listing 1 example uses the OESurfaceArcStyle_Eyelash style to draw the molecule surface. The following table lists all surface drawing styles that are available in Grapheme TK.

 OEAlphaRainbowArcFxn OEBrickRoadArcFxn OECastleArcFxn OECogArcFxn OEDefaultArcFxn OEEyelashArcFxn OEFlowerArcFxn OENecklaceArcFxn OENullArcFxn OEOliveBranchArcFxn OEPearlsArcFxn OERaceTrackArcFxn OEStitchArcFxn OESunArcFxn OEWreathArcFxn OERailroadTrackArcFxn OESawArcFxn OESimpsonArcFxn

User-defined surface drawing functors can be implemented by deriving from the OESurfaceArcFxnBase abstract base class. In the Listing 2 example, a user defined arc drawing functor is implemented that colors the eyelash arcs according to the atom they belong to. The image created by Listing 2 is shown in Figure: Example of user-defined surface drawing.

Listing 2: Example of user-defined surface drawing

class AtomColorArcFxn(OESurfaceArcFxnBase):
def __call__(self, image, arc):
return False

pen = OEPen(color, color, OEFill_Off, 1.0)

center = arc.GetCenter()
bgnAngle = arc.GetBgnAngle()
endAngle = arc.GetEndAngle()

OEDrawEyelashSurfaceArc(image, center, bgnAngle, endAngle, radius, pen)
return True

def Draw2DSurface(image, mol):

OEPrepareDepiction(mol)

opts = OE2DMolDisplayOptions(image.GetWidth(), image.GetHeight(), OEScale_AutoScale)
opts.SetTitleLocation(OETitleLocation_Hidden)
opts.SetScale(OEGetMoleculeSurfaceScale(mol, opts))

arcfxn = AtomColorArcFxn()
for atom in mol.GetAtoms():
OESetSurfaceArcFxn(mol, atom, arcfxn)

disp = OE2DMolDisplay(mol, opts)
OEDraw2DSurface(disp)
OERenderMolecule(image, disp)


Example of user-defined surface drawing

The Listing 3 example shows how to project atom properties, such as partial charges into the molecule surface. The image created by Listing 3 is shown in Figure: Example of depicting atom properties.

Listing 3: Example of depicting atom properties on the molecule surface

class AtomPartialChargeArcFxn(OESurfaceArcFxnBase):
def __init__(self, colorg):
OESurfaceArcFxnBase.__init__(self)
self.colorg = colorg

def __call__(self, image, arc):
return False

if atom is None:
return False

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

pen = OEPen()
pen.SetForeColor(color)
pen.SetLineWidth(2.0)

center = arc.GetCenter()
bAngle = arc.GetBgnAngle()
eAngle = arc.GetEndAngle()

edgeAngle = 5.0
dir = OEPatternDirection_Outside
patternAngle = 10.0
edgeAngle, dir, patternAngle)
return True

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

def Draw2DSurfacePartialCharge(image, mol):

OEPrepareDepiction(mol)

OEMMFFAtomTypes(mol)
OEMMFF94PartialCharges(mol)

opts = OE2DMolDisplayOptions(image.GetWidth(), image.GetHeight(), OEScale_AutoScale)
opts.SetTitleLocation(OETitleLocation_Hidden)
opts.SetScale(OEGetMoleculeSurfaceScale(mol, opts))

coloranion = OEColorStop(-1.0, OEColor(OEDarkRed))
colorcation = OEColorStop(+1.0, OEColor(OEDarkBlue))

arcfxn = AtomPartialChargeArcFxn(colorg)

for atom in mol.GetAtoms():
OESetSurfaceArcFxn(mol, atom, arcfxn)

disp = OE2DMolDisplay(mol, opts)
OEDraw2DSurface(disp)
OERenderMolecule(image, disp)


Example of depicting atom properties

It is also possible to draw multiple 2D surfaces arc-by-arc. In the Listing 4 example below the arcs returned by the OEGet2DSurfaceArcs function for the given atom display and radius are directly rendered below the molecule diagram. In this case it is important to call the OEGetMoleculeSurfaceScale function with the largest radius scale of the 2D surfaces drawn. This reduces the scaling of the molecule in order to able to fit the molecule diagram and all of the arcs of the 2D surfaces into the image. The image created by Listing 4 is shown in Figure: Example of drawing multiple 2D surfaces.

Listing 4: Example of drawing multiple 2D surfaces

def Draw2DSurface(disp, atompred, radius, color):

penA = OEPen(color, color, OEFill_Off, 2.0)
arcfxnA = OEDefaultArcFxn(penA)

penB = OEPen(OELightGrey, OELightGrey, OEFill_Off, 2.0, OEStipple_ShortDash)
arcfxnB = OEDefaultArcFxn(penB)

layer = disp.GetLayer(OELayerPosition_Below)

arcfxnA(layer, arc)
else:
arcfxnB(layer, arc)

def Draw2DSurfaces(image, mol):

OEPrepareDepiction(mol)

opts = OE2DMolDisplayOptions(image.GetWidth(), image.GetHeight(), OEScale_AutoScale)
opts.SetTitleLocation(OETitleLocation_Hidden)
opts.SetScale(OEGetMoleculeSurfaceScale(mol, opts, 1.50))

disp = OE2DMolDisplay(mol, opts)

Draw2DSurface(disp, OEHasAtomicNum(OEElemNo_C), 1.00, OEBlack)
Draw2DSurface(disp, OEHasAtomicNum(OEElemNo_N), 1.25, OEDarkBlue)
Draw2DSurface(disp, OEHasAtomicNum(OEElemNo_O), 1.50, OEDarkRed)

OERenderMolecule(image, disp)


Example of drawing multiple 2D surfaces

OESurfaceArcScale namespace

The last Listing 5 example shows how to draw a 2D surface with various radii. It draws two surfaces one with a minimum radius scale and one with various radius depending on the covalent radius of the atoms. The OEGetMoleculeSurfaceScale function has to be called in this example too with the largest radius scale in order to able to fit the molecule diagram and all of the arcs of the 2D surfaces into the image. The image created by Listing 5 is shown in Figure: Example of drawing 2D surface with various radii.

Listing 5: Example of drawing 2D surface with various radii

def DrawSurfaces(image, mol):

maxrscale = float("-inf")

for atom in mol.GetAtoms():
maxrscale = max(maxrscale, rscale)

opts = OE2DMolDisplayOptions(image.GetWidth(), image.GetHeight(), OEScale_AutoScale)
opts.SetTitleLocation(OETitleLocation_Hidden)
opts.SetScale(OEGetMoleculeSurfaceScale(mol, opts, maxrscale))

disp = OE2DMolDisplay(mol, opts)

layer = disp.GetLayer(OELayerPosition_Below)

penA = OEPen(OELightGrey, OELightGrey, OEFill_Off, 2.0, OEStipple_ShortDash)
arcfxnA = OEDefaultArcFxn(penA)

for arc in OEGet2DSurfaceArcs(disp, OESurfaceArcScale_Minimum):
arcfxnA(layer, arc)

penB = OEPen(OEGrey, OEGrey, OEFill_Off, 2.0)
arcfxnB = OEDefaultArcFxn(penB)

arcfxnB(layer, arc)

OERenderMolecule(image, disp)


Example of drawing 2D surface with various radii