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 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

from openeye.oechem import *
from openeye.oedepict import *


def DrawMolecule(image, mol, width, height, offset):

    frame = OEImageFrame(image, width, height, offset)

    OEDrawBorder(frame, OELightGreyPen)

    opts = OE2DMolDisplayOptions(width, height, OEScale_AutoScale)
    disp = OE2DMolDisplay(mol, opts)

    clearbackground = True
    OERenderMolecule(frame, disp, not clearbackground)


image = OEImage(400, 400)

mol = OEGraphMol()
OESmilesToMol(mol, "C1CC(O)CNC1")
OEPrepareDepiction(mol)

DrawMolecule(image, mol, 60, 60, OE2DPoint(50.0, 40.0))
DrawMolecule(image, mol, 180, 180, OE2DPoint(180.0, 120.0))
DrawMolecule(image, mol, 80, 80, OE2DPoint(300.0, 20.0))
DrawMolecule(image, mol, 50, 50, OE2DPoint(150.0, 320.0))
DrawMolecule(image, mol, 20, 20, OE2DPoint(360.0, 360.0))

OEWriteImage("ImageFrame.png", image)
../_images/ImageFrame.png

Example of using OEImageFrame

See also

Depicting Molecules in 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

from openeye.oechem import *
from openeye.oedepict import *

smiles = ["C1CC(C)CCC1", "C1CC(O)CCC1", "C1CC(Cl)CCC1"]

image = OEImage(200, 200)

rows, cols = 2, 2
grid = OEImageGrid(image, rows, cols)

opts = OE2DMolDisplayOptions(grid.GetCellWidth(), grid.GetCellHeight(), OEScale_AutoScale)

for smi, cell in zip(smiles, grid.GetCells()):
    mol = OEGraphMol()
    OESmilesToMol(mol, smi)
    OEPrepareDepiction(mol)
    disp = OE2DMolDisplay(mol, opts)
    OERenderMolecule(cell, disp)

OEWriteImage("ImageGridSimple.png", image)
../_images/ImageGridSimple.png

Example of using OEImageGrid

See also

The cells of an OEImageGrid object are evenly spaced and their dimensions (width and height) are calculated based on the width and height of the grid, its margins and gap parameter set between the cells. The spacing between the cells and the margins of the grid can be modified by calling the following functions:

Warning

After accessing the cells of the OEImageGrid object, the dimensions and position of the cells become immutable and modifying the spacing between the cells or the margin of the grid is not allowed.

The image created by Listing 3 is shown in Figure: Example of customizing OEImageGrid.

Listing 3: Example of customizing OEImageGrid

from openeye.oechem import *
from openeye.oedepict import *


def DrawCellNumber(cell, idx):
    font = OEFont()
    font.SetAlignment(OEAlignment_Center)
    yoffset = font.GetSize()
    xoffset = font.GetSize()
    cell.DrawText(OE2DPoint(xoffset, yoffset), str(idx), font)


smiles = ["C1CC(C)CCC1", "C1CC(O)CCC1", "C1CC(Cl)CCC1"]

image = OEImage(200, 200)
image.Clear(OEColor(OELightGrey))

rows, cols = 2, 2
grid = OEImageGrid(image, rows, cols)
grid.SetMargins(5)
grid.SetMargin(OEMargin_Left, 30)
grid.SetCellGap(5)

opts = OE2DMolDisplayOptions(grid.GetCellWidth(), grid.GetCellHeight(), OEScale_AutoScale)

for idx, cell in zip(range(0, len(smiles)), grid.GetCells()):
    mol = OEGraphMol()
    OESmilesToMol(mol, smiles[idx])
    OEPrepareDepiction(mol)

    disp = OE2DMolDisplay(mol, opts)
    OERenderMolecule(cell, disp)

    OEDrawBorder(cell, OEBlackPen)
    DrawCellNumber(cell, idx + 1)

OEWriteImage("ImageGridBoxed.png", image)
../_images/ImageGridBoxed.png

Example of customizing OEImageGrid

The following example demonstrates how to depict molecules and display data using OEImageGrid. The individual cells of an OEImageGrid object can be accessed by calling the OEImageGrid.GetCell method that returns an image (OEImageBase pointer) on which molecule can be drawn or text can be displayed. The image created by Listing 4 is shown in Figure: Example of depicting molecules and data.

Listing 4: Example of depicting molecules and data

from openeye.oechem import *
from openeye.oedepict import *


def DrawMoleculeData(cell, mol):

    gap = cell.GetHeight() / 4.0

    font = OEFont()
    font.SetAlignment(OEAlignment_Left)

    data = "title = " + mol.GetTitle()
    offset = gap
    cell.DrawText(OE2DPoint(0, offset), data, font)

    offset += gap
    data = "smiles = " + OEMolToSmiles(mol)
    cell.DrawText(OE2DPoint(0, offset), data, font)

    offset += gap
    data = "MW = %.3f" % OECalculateMolecularWeight(mol)
    cell.DrawText(OE2DPoint(0, offset), data, font)


smiles = ["c1ccccc1 benzene", "c1cccnc1 pyridine", "c1cncnc1 pyrimidine"]

image = OEImage(250, 250)
image.Clear(OEColor(OELightGrey))

rows, cols = 3, 2
grid = OEImageGrid(image, rows, cols)
grid.SetMargins(5)
grid.SetCellGap(5)

opts = OE2DMolDisplayOptions(grid.GetCellWidth(), grid.GetCellHeight(), OEScale_AutoScale)
opts.SetTitleLocation(OETitleLocation_Hidden)

for r in range(0, grid.NumRows()):
    #depict molecule in first column
    cell = grid.GetCell(r + 1, 1)

    mol = OEGraphMol()
    OESmilesToMol(mol, smiles[r])
    OEPrepareDepiction(mol)

    disp = OE2DMolDisplay(mol, opts)
    OERenderMolecule(cell, disp)

    # depict data in second column
    cell = grid.GetCell(r + 1, 2)
    DrawMoleculeData(cell, mol)

OEWriteImage("ImageGridData.png", image)
../_images/ImageGridData.png

Example of depicting molecules and data

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 5.

Listing 5: Example of auto scaling of molecules

from openeye.oechem import *
from openeye.oedepict import *

smiles = ["c1ccccc1", "c1cccc2c1ccnc2", "c1ccc2c(c1)cc3ccccc3n2"]

image = OEImage(400, 200)

rows, cols = 1, 3
grid = OEImageGrid(image, rows, cols)

opts = OE2DMolDisplayOptions(grid.GetCellWidth(), grid.GetCellHeight(), OEScale_AutoScale)

for smi, cell in zip(smiles, grid.GetCells()):
    mol = OEGraphMol()
    OESmilesToMol(mol, smi)
    OEPrepareDepiction(mol)

    disp = OE2DMolDisplay(mol, opts)
    OERenderMolecule(cell, disp)

OEWriteImage("ImageGridAutoScale.png", image)
../_images/ImageGridAutoScale.png

Example of auto scaling of molecules

The following example (Listing 6) 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 6 is shown in Figure: Example of using OEImageGrid.

Listing 6: Example of identical scaling of molecules

from openeye.oechem import *
from openeye.oedepict import *

smiles = ["c1ccccc1", "c1cccc2c1ccnc2", "c1ccc2c(c1)cc3ccccc3n2"]

molecules = []
for smi in smiles:
    mol = OEGraphMol()
    OESmilesToMol(mol, smi)
    OEPrepareDepiction(mol)
    molecules.append(OEGraphMol(mol))

image = OEImage(400, 200)

rows, cols = 1, 3
grid = OEImageGrid(image, rows, cols)

opts = OE2DMolDisplayOptions(grid.GetCellWidth(), grid.GetCellHeight(), OEScale_AutoScale)

minscale = float("inf")
for mol in molecules:
    minscale = min(minscale, OEGetMoleculeScale(mol, opts))

opts.SetScale(minscale)
for idx, cell in enumerate(grid.GetCells()):
    disp = OE2DMolDisplay(molecules[idx], opts)
    OERenderMolecule(cell, disp)

OEWriteImage("ImageGridFixedScale.png", image)
../_images/ImageGridFixedScale.png

Example of identical scaling of molecules