Depicting Molecules In a Grid

A program that converts molecular structures into an image with a grid layout. By default the input molecules are depicted in a 3x2 grid.

Command Line Interface

A description of the command line interface can be obtained by executing the program with the –help argument.

prompt> python mols2img.py --help

will generate the following output:

Simple parameter list
 -height : Height of output image
 -width : Width of output image

 image grid options :
   -cols : Number of columns
   -rows : Number of rows

 input/output options :
   -in : Input filename(s)
   -out : Output filename

 molecule display options :
   -aromstyle : Aromatic ring display style
   -atomcolor : Atom coloring style
   -atomlabelfontscale : Atom label font scale
   -atomprop : Atom property display
   -atomstereostyle : Atom stereo display style
   -bondcolor : Bond coloring style
   -bondprop : Bond property display
   -bondstereostyle : Bond stereo display style
   -hydrstyle : Hydrogen display style
   -linewidth : Default bond line width
   -protgroupdisp : Protective group display style
   -scale : Scaling of the depicted molecule
   -superdisp : Super atom display style
   -titleloc : Location of the molecule title

 prepare depiction options :
   -clearcoords : Clear and regenerate 2D coordinates of molecule(s)
   -orientation : Set the preferred orientation of 2D coordinates
   -suppressH : Suppress explicit hydrogens of molecule(s)

Code

Download code

mols2img.py

#!/usr/bin/env python
# (C) 2022 Cadence Design Systems, Inc. (Cadence) 
# All rights reserved.
# TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
# provided to current licensees or subscribers of Cadence products or
# SaaS offerings (each a "Customer").
# Customer is hereby permitted to use, copy, and modify the Sample Code,
# subject to these terms. Cadence claims no rights to Customer's
# modifications. Modification of Sample Code is at Customer's sole and
# exclusive risk. Sample Code may require Customer to have a then
# current license or subscription to the applicable Cadence offering.
# THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED.  OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall Cadence be
# liable for any damages or liability in connection with the Sample Code
# or its use.

#############################################################################
# Converts molecules into an image with grid layout.
# The output file format depends on its file extension.
#############################################################################

import sys
from openeye import oechem
from openeye import oedepict


def main(argv=[__name__]):

    itf = oechem.OEInterface()
    oechem.OEConfigure(itf, InterfaceData)
    oedepict.OEConfigureImageWidth(itf, 400.0)
    oedepict.OEConfigureImageHeight(itf, 400.0)
    oedepict.OEConfigureImageGridParams(itf)
    oedepict.OEConfigurePrepareDepictionOptions(itf)
    oedepict.OEConfigure2DMolDisplayOptions(itf)

    if not oechem.OEParseCommandLine(itf, argv):
        oechem.OEThrow.Fatal("Unable to interpret command line!")

    oname = itf.GetString("-out")

    ext = oechem.OEGetFileExtension(oname)
    if not oedepict.OEIsRegisteredImageFile(ext):
        oechem.OEThrow.Fatal("Unknown image type!")

    ofs = oechem.oeofstream()
    if not ofs.open(oname):
        oechem.OEThrow.Fatal("Cannot open output file!")

    width, height = oedepict.OEGetImageWidth(itf), oedepict.OEGetImageHeight(itf)
    image = oedepict.OEImage(width, height)

    rows = oedepict.OEGetImageGridNumRows(itf)
    cols = oedepict.OEGetImageGridNumColumns(itf)
    grid = oedepict.OEImageGrid(image, rows, cols)

    popts = oedepict.OEPrepareDepictionOptions()
    oedepict.OESetupPrepareDepictionOptions(popts, itf)

    dopts = oedepict.OE2DMolDisplayOptions()
    oedepict.OESetup2DMolDisplayOptions(dopts, itf)
    dopts.SetDimensions(grid.GetCellWidth(), grid.GetCellHeight(), oedepict.OEScale_AutoScale)

    celliter = grid.GetCells()
    for iname in itf.GetStringList("-in"):
        ifs = oechem.oemolistream()
        if not ifs.open(iname):
            oechem.OEThrow.Warning("Cannot open %s input file!" % iname)
            continue

        for mol in ifs.GetOEGraphMols():
            if not celliter.IsValid():
                break

            oedepict.OEPrepareDepiction(mol, popts)
            disp = oedepict.OE2DMolDisplay(mol, dopts)
            oedepict.OERenderMolecule(celliter.Target(), disp)
            celliter.Next()

    oedepict.OEWriteImage(ofs, ext, image)

    return 0


#############################################################################
# INTERFACE
#############################################################################

InterfaceData = '''
!BRIEF -in <input1> [ <input2> .. ] -out <output image>

!CATEGORY "input/output options :"

    !PARAMETER -in
      !ALIAS -i
      !TYPE string
      !LIST true
      !REQUIRED true
      !VISIBILITY simple
      !BRIEF Input filename(s)
    !END

    !PARAMETER -out
      !ALIAS -o
      !TYPE string
      !REQUIRED true
      !VISIBILITY simple
      !BRIEF Output filename
    !END

!END
'''

if __name__ == "__main__":
    sys.exit(main(sys.argv))

Examples

prompt> python mols2img.py -in amino.ism -out image.png

will generate the image shown in Figure: Example of using the program with default parameters.

../_images/mols2img-01.png

Example of using the program with default parameters

Note

By default the program depicts molecules in a 3x2 grid

prompt> python mols2img.py -in amino.ism -out image.png -rows 2 -cols 3

will generate the image shown in Figure: Example of using the program with user-defined number of rows and columns.

../_images/mols2img-02.png

Example of using the program with user-defined number of rows and columns

prompt> python mols2img.py -in amino.ism -out image.png  -rows 2 -cols 2 -titleloc Bottom

will generate the image shown in Figure: Example of using the program with molecule titles displayed at the bottom.

../_images/mols2img-03.png

Example of using the program with molecule titles displayed at the bottom