Molecule Layouts¶
The previous chapters described how to depict a single molecule in an image. This chapter provides examples for drawing multiple molecular diagrams into the same image.
Depicting Molecules in an Arbitrary Position¶
The OEImageFrame
class provides a general framework to
display a molecule in a specific region of the image by defining the following
parameters:
the reference of the parent image
the width and height of the generated frame
the offset of the frame relative to the top-left corner of its parent (
OE2DPoint
)
The Listing 1
examples generates an image where the same
molecule is depicted in different regions of the image.
The molecular diagrams are automatically resized in order to fit into
the specific frame, since the corresponding
OE2DMolDisplay
object is constructed with the width
and height of the OEImageFrame
object.
When the OERenderMolecule
function is called, the
offset of the OEImageFrame
object is added to the
coordinates stored in OE2DMolDisplay
in order to
transfer the molecule into the specific region of the image.
The image created by Listing 1
is shown in
Figure: Example of using OEImageFrame.
Listing 1: Example of using OEImageFrame
def DrawMolecule(image, mol, width, height, offset):
frame = oedepict.OEImageFrame(image, width, height, offset)
oedepict.OEDrawBorder(frame, oedepict.OELightGreyPen)
opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale)
disp = oedepict.OE2DMolDisplay(mol, opts)
clearbackground = True
oedepict.OERenderMolecule(frame, disp, not clearbackground)
image = oedepict.OEImage(400, 400)
mol = oechem.OEGraphMol()
oechem.OESmilesToMol(mol, "C1CC(O)CNC1")
oedepict.OEPrepareDepiction(mol)
DrawMolecule(image, mol, 60, 60, oedepict.OE2DPoint(50.0, 40.0))
DrawMolecule(image, mol, 180, 180, oedepict.OE2DPoint(180.0, 120.0))
DrawMolecule(image, mol, 80, 80, oedepict.OE2DPoint(300.0, 20.0))
DrawMolecule(image, mol, 50, 50, oedepict.OE2DPoint(150.0, 320.0))
DrawMolecule(image, mol, 20, 20, oedepict.OE2DPoint(360.0, 360.0))
oedepict.OEWriteImage("ImageFrame.png", image)

Example of using OEImageFrame¶
See also
OEImageFrame
classOEDrawBorder
functionOERenderMolecule
functionOEWriteImage
function
Depicting Molecules in a Grid¶
The OEImageGrid
class allows molecules to be aligned into
rows and columns.
After generating an OEImageGrid
object by specifying its number
of rows and columns, the cells of the grid can be accessed (from left to right, top
to bottom order) by calling the OEImageGrid.GetCells
method.
The image created by Listing 2
is shown in
Figure: Example of using OEImageGrid.
Listing 2: Example of using OEImageGrid
smiles = ["C1CC(C)CCC1", "C1CC(O)CCC1", "C1CC(Cl)CCC1"]
image = oedepict.OEImage(200, 200)
rows, cols = 2, 2
grid = oedepict.OEImageGrid(image, rows, cols)
opts = oedepict.OE2DMolDisplayOptions(grid.GetCellWidth(), grid.GetCellHeight(),
oedepict.OEScale_AutoScale)
for smi, cell in zip(smiles, grid.GetCells()):
mol = oechem.OEGraphMol()
oechem.OESmilesToMol(mol, smi)
oedepict.OEPrepareDepiction(mol)
disp = oedepict.OE2DMolDisplay(mol, opts)
oedepict.OERenderMolecule(cell, disp)
oedepict.OEWriteImage("ImageGridSimple.png", image)

Example of using OEImageGrid¶
See also
OEImageGrid
class
When an OE2DMolDisplay
object is initialized, the
dimensions of the depicted molecule is controlled by the width, height
and scale parameters of the OE2DMolDisplayOptions
object.
When only one molecule is depicted then auto scaling of this molecule is preferred
(i.e. scaling the molecule in order to maximally fit it to the given dimensions).
However, when more that one molecule, that are quite different in
sizes, are depicted next to each other, auto scaling of each molecule can be
visually misleading because it emphasizes molecules with greater scaling factors.
See example in Figure: Example of using OEImageGrid
that is generated by Listing 3
.
Listing 3: Example of auto scaling of molecules
smiles = ["c1ccccc1", "c1cccc2c1ccnc2", "c1ccc2c(c1)cc3ccccc3n2"]
image = oedepict.OEImage(400, 200)
rows, cols = 1, 3
grid = oedepict.OEImageGrid(image, rows, cols)
opts = oedepict.OE2DMolDisplayOptions(grid.GetCellWidth(), grid.GetCellHeight(),
oedepict.OEScale_AutoScale)
for smi, cell in zip(smiles, grid.GetCells()):
mol = oechem.OEGraphMol()
oechem.OESmilesToMol(mol, smi)
oedepict.OEPrepareDepiction(mol)
disp = oedepict.OE2DMolDisplay(mol, opts)
oedepict.OERenderMolecule(cell, disp)
oedepict.OEWriteImage("ImageGridAutoScale.png", image)

Example of auto scaling of molecules¶
See also
The following example (Listing 4
) shows
how to ensure that all molecules are depicted using the same scaling
factor.
After initializing the molecules, the minimum scaling factor is
determined by looping over the molecules being depicted and calling
the OEGetMoleculeScale
function.
This function returns the scaling factor of the molecule considering
the depiction options stored in the given
OE2DMolDisplayOptions
object.
After determining the minimum scaling factor, the molecules are
rendered to the cells of the grid by using this fixed scaling factor.
The image created by Listing 4
is shown in
Figure: Example of using OEImageGrid.
Listing 4: Example of identical scaling of molecules
smiles = ["c1ccccc1", "c1cccc2c1ccnc2", "c1ccc2c(c1)cc3ccccc3n2"]
molecules = []
for smi in smiles:
mol = oechem.OEGraphMol()
oechem.OESmilesToMol(mol, smi)
oedepict.OEPrepareDepiction(mol)
molecules.append(oechem.OEGraphMol(mol))
image = oedepict.OEImage(400, 200)
rows, cols = 1, 3
grid = oedepict.OEImageGrid(image, rows, cols)
opts = oedepict.OE2DMolDisplayOptions(grid.GetCellWidth(), grid.GetCellHeight(),
oedepict.OEScale_AutoScale)
minscale = float("inf")
for mol in molecules:
minscale = min(minscale, oedepict.OEGetMoleculeScale(mol, opts))
opts.SetScale(minscale)
for idx, cell in enumerate(grid.GetCells()):
disp = oedepict.OE2DMolDisplay(molecules[idx], opts)
oedepict.OERenderMolecule(cell, disp)
oedepict.OEWriteImage("ImageGridFixedScale.png", image)

Example of identical scaling of molecules¶
See also
OEImageGrid.GetCellHeight
methodOEImageGrid.GetCellWidth
method
Depicting Molecules in a Table¶
The following example demonstrates how to depict molecules and display data
alongside them by using the OEImageTable
.
The individual cells of an OEImageTable
object can be accessed
by calling the any of the following methods:
These methods return an image (OEImageBase
) on which
molecule can be drawn or text can be displayed using the
OEImageTable.DrawText
method.
The image created by Listing 5
is shown in
Figure: Example of depicting molecules and data using image table.
Listing 5: Example of depicting molecules and data using image table
from openeye import oechem
from openeye import oedepict
smiles = ["c1ccccc1 benzene", "c1cccnc1 pyridine", "c1cncnc1 pyrimidine"]
imagewidth, imageheight = 350, 250
image = oedepict.OEImage(imagewidth, imageheight)
mainrows, maincols = 3, 2
maintableopts = oedepict.OEImageTableOptions(mainrows, maincols, oedepict.OEImageTableStyle_NoStyle)
maintableopts.SetHeader(False)
maintableopts.SetColumnWidths([10, 20])
maintable = oedepict.OEImageTable(image, maintableopts)
datarows, datacols = 4, 2
datatableopts = oedepict.OEImageTableOptions(datarows, datacols,
oedepict.OEImageTableStyle_LightGreen)
datatableopts.SetStubColumn(True)
datatableopts.SetMargins(5.0)
datatableopts.SetColumnWidths([10, 20])
for r in range(min(len(smiles), maintable.NumRows())):
# depict molecule in first column
cell = maintable.GetBodyCell(r + 1, 1)
opts = oedepict.OE2DMolDisplayOptions(cell.GetWidth(), cell.GetHeight(),
oedepict.OEScale_AutoScale)
opts.SetTitleLocation(oedepict.OETitleLocation_Hidden)
mol = oechem.OEGraphMol()
oechem.OESmilesToMol(mol, smiles[r])
oedepict.OEPrepareDepiction(mol)
disp = oedepict.OE2DMolDisplay(mol, opts)
oedepict.OERenderMolecule(cell, disp)
# depicting data in table
cell = maintable.GetBodyCell(r + 1, 2)
table = oedepict.OEImageTable(cell, datatableopts)
table.DrawText(table.GetHeaderCell(1, False), "Property")
table.DrawText(table.GetHeaderCell(1), "Value")
table.DrawText(table.GetStubColumnCell(1), "Name")
table.DrawText(table.GetBodyCell(1, 1), mol.GetTitle())
table.DrawText(table.GetStubColumnCell(2), "SMILES")
table.DrawText(table.GetBodyCell(2, 1), oechem.OEMolToSmiles(mol))
table.DrawText(table.GetStubColumnCell(3), "MV")
table.DrawText(table.GetBodyCell(3, 1), "%.3f" % oechem.OECalculateMolecularWeight(mol))
oedepict.OEWriteImage("ImageTable.png", image)

Example of depicting molecules and data using image table¶