Molecule Depiction¶
The previous chapter introduced how to create images and draw basic geometric elements (such as text, lines rectangles, etc.) Drawing these basic graphical elements provides a framework to construct more complex images such as molecular diagrams.
The molecule depiction is divided into two separate steps (see Figure: Process of Molecule Depiction ):
Generating 2D coordinates
Rendering molecule that involves
transforming and scaling coordinates in order to fit the molecule into an image with a specific width and height
setting atom and bond display properties based on depiction style
creating an image (such as drawing lines for bonds, drawing text for atomic labels)
The next example (Listing 1
) demonstrates
how easy is to depict a molecule using the OEDepict TK.
After creating a molecule (for example by parsing a SMILES string) the
image can be generated by calling two functions.
The
OEPrepareDepiction
function prepares the molecule for depiction. This process may involve perceiving atom and bond stereo information and suppressing or adding hydrogens. It also calls theOEDepictCoordinates
function to generate the 2D coordinates of the molecule.The
OERenderMolecule
function generates the image of the molecule using the default depiction styles and then writes the image into the given file.
The image created by Listing 1
is shown in
Figure: Example of depicting a molecule.
Listing 1: Example of molecule depiction
public class DepictMolSimple
{
public static void Main(string[] args)
{
OEGraphMol mol = new OEGraphMol();
OEChem.OESmilesToMol(mol, "c1cc(N)cc(S(=O)(=O)O)c1 3-aminobenzenesulfonic acid");
OEDepict.OEPrepareDepiction(mol);
OEDepict.OERenderMolecule("DepictMolSimple.svg", mol);
}
}
See also
OEPrepareDepiction
functionOERenderMolecule
function
Customizing Molecule Depiction¶
The previous example showed how to generate a molecule diagram with
default parameters.
OEDepict TK provides the ability to customize molecule depiction.
The Listing 2
example shows how to depict a
molecule width specific width and height parameters.
Listing 2: Example of depicting a molecule
public class DepictMolSize
{
public static int Main(string[] args)
{
OEGraphMol mol = new OEGraphMol();
OEChem.OESmilesToMol(mol, "c1cc(N)cc(S(=O)(=O)O)c1 3-aminobenzenesulfonic acid");
OEDepict.OEPrepareDepiction(mol);
double width = 300;
double height = 300;
double scale = OEScale.AutoScale;
OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(width, height, scale);
OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);
OEDepict.OERenderMolecule("DepictMolSize.svg", disp);
return 0;
}
}
See also
OE2DMolDisplayOptions class
OE2DMolDisplay class
Apart from defining the dimensions of a depicted molecule, the
OE2DMolDisplayOptions class stores all properties
that determine how a molecule is depicted.
The following example shows how to set various depiction styles before
initializing the OE2DMolDisplay object.
The image created by Listing 3
is shown in
Figure: Example of customizing depiction style.
Listing 3: Example of customizing the depiction style
public class DepictMolStyle
{
public static void Main(string [] argv)
{
OEGraphMol mol = new OEGraphMol();
OEChem.OESmilesToMol(mol, "c1cc(N)cc(S(=O)(=O)O)c1 3-aminobenzenesulfonic acid");
OEDepict.OEPrepareDepiction(mol);
double width = 200;
double height = 200;
double scale = OEScale.AutoScale;
OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(width, height, scale);
opts.SetAromaticStyle(OEAromaticStyle.Circle);
opts.SetAtomColorStyle(OEAtomColorStyle.BlackCPK);
opts.SetTitleLocation(OETitleLocation.Bottom);
OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);
OEDepict.OERenderMolecule("DepictMolStyle.svg", disp);
}
}
Apart from controlling the depiction style through the
OE2DMolDisplayOptions class, OEDepict TK also provides
access to manipulate individual atom and bond 2D display properties.
The OE2DMolDisplay.GetAtomDisplays
method
returns an iterator over OE2DAtomDisplay objects
that store atom display information.
Similarly, the OE2DMolDisplay.GetBondDisplays
method
returns an iterator over OE2DBondDisplay objects
that store bond display information.
The following example demonstrates how to change atom and bond display
properties and depict the graph of the molecule by ignoring atom
labels and setting bond display styles.
The image created by Listing 4
is shown in
Figure: Example of molecule graph depiction.
Listing 4: Example of molecule graph depiction
public class DepictMolGraph
{
public static int Main(string[] args)
{
OEGraphMol mol = new OEGraphMol();
OEChem.OESmilesToMol(mol, "c1cc(N)cc(S(=O)(=O)O)c1");
OEDepict.OEPrepareDepiction(mol);
double width = 200;
double height = 200;
double scale = OEScale.AutoScale;
OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(width, height, scale);
opts.SetAtomColorStyle(OEAtomColorStyle.WhiteMonochrome);
opts.SetTitleLocation(OETitleLocation.Hidden);
opts.SetAtomColor(OEElemNo.C, OEChem.OEDarkGreen);
OEPen pen = new OEPen(OEChem.OEBlack, OEChem.OEBlack, OEFill.On, 3.0);
opts.SetDefaultBondPen(pen);
OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);
foreach (OE2DAtomDisplay adisp in disp.GetAtomDisplays())
{
adisp.SetLabel("");
}
foreach (OE2DBondDisplay bdisp in disp.GetBondDisplays())
{
bdisp.SetDisplayType(OEBondDisplayType.Single);
}
OEDepict.OERenderMolecule("DepictMolGraph.svg", disp);
return 0;
}
}
Hint
Even though OEDepict TK provides the ability to manipulate atom and bond display properties of after the OE2DMolDisplay object is constructed, it is highly recommended to set the depiction properties using the OE2DMolDisplayOptions class. Only by knowing all properties (such as labels, font styles and sizes etc.) in advance can one ensure the best depiction layout i.e. that the molecule diagram is rendered without any label clippings and the labels are displayed with the minimum number of overlaps.
See also
OE2DAtomDisplay class
OE2DBondDisplay class
Controlling the Size of Depicted Molecules¶
The dimensions of a depicted molecule are controlled by the width,
height and scale parameters of the OE2DMolDisplayOptions
that is used to initialized the OE2DMolDisplay object.
(See example in Listing 2
)
See also
If the width and the height of the image is not specified (set to be
zero), then scaling value determine the dimensions of the image.
(See examples in Examples of image scaling).
The default scale is defined to be OEScale.Default
constant, and the image may be enlarged or shrunk by specifying a
floating point scaling value.
For example, the scaling OEScale.Default
*
0.5
generates an image a half of the default size, and scaling
OEScale.Default
* 1.5
generates an image one
and a half times the default.
OEScale::Default * 0.5 |
OEScale::Default |
OEScale::Default * 1.5 |
---|---|---|
If an OE2DMolDisplayOptions object is initialized with zero width and / or height, then the real dimensions of the image can be accessed after creating an OE2DMolDisplay object.
double width = 0.0;
double height = 0.0;
double scale = 50.0;
OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(width, height, scale);
OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);
Console.WriteLine("{0:width 0.0}", disp.GetWidth());
Console.WriteLine("{0:height 0.0}", disp.GetHeight());
Console.WriteLine("{0:scale 0.0}", disp.GetScale());
The output of the preceding snippet is the following:
width 137.0
height 181.0
scale 50.0
See also
OE2DMolDisplay.GetHeight
method
OE2DMolDisplay.GetWidth
method
OE2DMolDisplay.GetScale
method
If both the width and the height of are specified, then
OEScale.AutoScale
scaling indicates
that the molecule is scaled to maximally fit the given dimensions.
The specified width and height will not be altered, not even if the
given scaling would require it i.e if necessary the scaling will be
reduced in order to fit the molecule into the given dimension.
(See examples in Examples of image scaling).
OEScale::Default * 0.5 |
OEScale::Default |
OEScale::Default * 1.5 |
---|---|---|
Note
Specifying a width and / or height larger than what is required by the scale, causes the molecule to float in the middle of the image of the requested size.
If only either the width or the height of the image is specified, then the unspecified dimension will be set based on the scaling and the specified width / height, respectively.
OEScale::Default, width = 250.0, height = 0.0 |
OEScale::Default, width = 0.0, height = 250.0 |
---|---|
Molecule Depiction with Transparent Background¶
The following code snippet shows how to generate a molecule depiction with transparent
background.
First, the OEImage object, onto which the depiction is going to be rendered,
has to be constructed with the OETransparentColor
color.
Then the OERenderMolecule
function has to be called with a
parameter that ensures that the background in not cleared prior to rendering the molecule.
double width = 200;
double height = 200;
double scale = OEScale.AutoScale;
OEImage image = new OEImage(width, height, OEChem.OETransparentColor);
OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(width, height, scale);
OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);
bool clearbackground = true;
OEDepict.OERenderMolecule("DepictMolTransparent.png", disp, !clearbackground);
Displaying Atom Properties¶
OEDepict TK provides ability the display atom properties as strings. When a OE2DMolDisplay object is created, the positions where atom properties can be displayed are calculated. (see Figure: Example of generating atom property positions.)
The following example shows how to display the atom indices
by using the OEDisplayAtomIdx functor.
The font used to depict the atom property strings also can be
modified by using the
OE2DMolDisplayOptions.SetAtomPropLabelFont
method.
The image created by Listing 5
is shown in
Figure: Example of displaying atom indices.
Listing 5: Example of depicting atom indices
public class AtomPropIndex
{
public static void Main(string[] argv)
{
OEGraphMol mol = new OEGraphMol();
OEChem.OESmilesToMol(mol, "c1cc(N)cc(S(=O)(=O)O)c1");
OEDepict.OEPrepareDepiction(mol);
double width = 300;
double height = 200;
double scale = OEScale.AutoScale;
OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(width, height, scale);
opts.SetAtomPropertyFunctor(new OEDisplayAtomIdx());
opts.SetAtomPropLabelFont(new OEFont(OEChem.OEDarkGreen));
OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);
OEDepict.OERenderMolecule("AtomPropIndex.svg", disp);
}
}
Hint
It is recommended to use the OE2DMolDisplayOptions.SetAtomPropertyFunctor
method to define the atom property labels when the OE2DMolDisplay
object is constructed.
All labels displayed on the molecule diagram have to be known in advance in order to be
able to minimize the number of label clashes and clippings when calculating
the positions of the atom property labels.
See also
OEDepict TK provides the following built-in atom property functors:
OEDisplayAtomIdx class
OEDisplayAtomMapIdx class
OEDisplayNoAtomProp class
In order to display user-defined atom properties, the functor that is
passed to the
OE2DMolDisplayOptions.SetAtomPropertyFunctor
method has to be derived from the OEDisplayAtomPropBase
abstract base class.
The Listing 6
example shows
how to implement a user-defined functor that marks aromatic atoms.
The image created by Listing 6
is shown in
Figure: Example of displaying user-defined atom properties.
Listing 6: Example of depicting user-defined atom properties
public class AtomPropUser
{
private class LabelAromaticAtoms : OEDisplayAtomPropBase
{
public override String Call(OEAtomBase atom)
{
return (atom.IsAromatic() ? "(A)" : "");
}
public override OEDisplayAtomPropBase CreateCopy()
{
LabelAromaticAtoms copy = new LabelAromaticAtoms();
copy.ReleaseOwnership();
return copy;
}
}
public static void Main(string[] argv)
{
OEGraphMol mol = new OEGraphMol();
OEChem.OESmilesToMol(mol, "c1cc(N)cc(S(=O)(=O)O)c1");
OEDepict.OEPrepareDepiction(mol);
double width = 300;
double height = 200;
double scale = OEScale.AutoScale;
OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(width, height, scale);
LabelAromaticAtoms atomlabel = new LabelAromaticAtoms();
opts.SetAtomPropertyFunctor(atomlabel);
OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);
OEDepict.OERenderMolecule("AtomPropUser.svg", disp);
}
}
See also
OEDisplayAtomPropBase class
Displaying Bond Properties¶
Similarly to atom properties, bond properties can be displayed as strings. (see Figure: Example of generating bond property positions.)
The Listing 7
demonstrates how to
display bond indices by using the OEDisplayBondIdx functor.
The font used to depict the bond property strings can be
set by using the OE2DMolDisplayOptions.SetBondPropLabelFont
method.
The image created by Listing 7
is shown in
Figure: Example of displaying bond indices.
Listing 7: Example of depicting bond indices
public class BondPropIndex
{
public static void Main(String[] argv)
{
OEGraphMol mol = new OEGraphMol();
OEChem.OESmilesToMol(mol, "c1cc(N)cc(S(=O)(=O)O)c1");
OEDepict.OEPrepareDepiction(mol);
double width = 300;
double height = 200;
double scale = OEScale.AutoScale;
OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(width, height, scale);
opts.SetBondPropertyFunctor(new OEDisplayBondIdx());
opts.SetBondPropLabelFont(new OEFont(OEChem.OEDarkBlue));
OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);
OEDepict.OERenderMolecule("BondPropIndex.svg", disp);
}
}
Hint
It is recommended to use the OE2DMolDisplayOptions.SetBondPropertyFunctor
method to define the bond property labels when the OE2DMolDisplay
object is constructed.
All labels displayed on the molecule diagram have to be known in advance in order to be
able to minimize the number of label clashes and clippings when calculating
the positions of the bond property labels.
See also
OEDepict TK provides the following built-in bond property functors:
OEDisplayBondIdx class
OEDisplayNoBondProp class
In order to display user-defined bond properties, the functor that is
passed to the
OE2DMolDisplayOptions.SetBondPropertyFunctor
method has to be derived from the OEDisplayBondPropBase
abstract base class.
The Listing 8
example shows how to implement a
user-defined functor that marks chain and ring bonds.
The image created by Listing 8
is shown in
Figure: Example of displaying user-defined bond properties.
Listing 8: Example of depicting user-defined bond properties
public class BondPropUser
{
private class LabelRingChainBonds : OEDisplayBondPropBase
{
public override String Call(OEBondBase bond)
{
return (bond.IsInRing() ? "R" : "C");
}
public override OEDisplayBondPropBase CreateCopy()
{
OEDisplayBondPropBase copy = new LabelRingChainBonds();
copy.ReleaseOwnership();
return copy;
}
}
public static void Main(string[] argv)
{
OEGraphMol mol = new OEGraphMol();
OEChem.OESmilesToMol(mol, "c1cc(N)cc(S(=O)(=O)O)c1");
OEDepict.OEPrepareDepiction(mol);
double width = 300;
double height = 200;
double scale = OEScale.AutoScale;
OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(width, height, scale);
LabelRingChainBonds bondlabel = new LabelRingChainBonds();
opts.SetBondPropertyFunctor(bondlabel);
OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);
OEDepict.OERenderMolecule("BondPropUser.svg", disp);
}
}
See also
OEDisplayBondPropBase class
Hiding Atoms and Bonds¶
OEDepict TK provides ability the suppress the display of specific atom and bonds. When a OE2DMolDisplay object is created, the visibility of every atom is evaluated. Bonds with both atom endpoints visible are also displayed. (see Figure: Example of hiding a portion of a structure.)
The following example shows how to selectively display only
a portion of the structure by using a custom class derived from
the OEUnaryAtomPred
functor.
The images created by Listing 9
is shown in
Figure: Example of hiding a portion of a structure.
Hint
It is recommended to use the OE2DMolDisplayOptions.SetAtomVisibilityFunctor
method to identify the visible atoms when the OE2DMolDisplay
object is constructed. All objects displayed on the molecule diagram have to be known in advance in order to be
able to correctly size and scale the molecule.
Listing 9: Example of selectively displaying atoms
public class AtomVisibility
{
private class OEAtomVisibilityShowRxnRole : OEUnaryAtomPred
{
private uint rxnrole = OERxnRole.None;
public OEAtomVisibilityShowRxnRole(uint role)
{ rxnrole = role; }
public override bool Call(OEAtomBase atom) {
if (rxnrole == OERxnRole.None) {
return true;
}
return (rxnrole == atom.GetRxnRole());
}
public override OEUnaryAtomBoolFunc CreateCopy() {
OEUnaryAtomBoolFunc copy = new OEAtomVisibilityShowRxnRole(rxnrole);
copy.ReleaseOwnership();
return copy;
}
}
public static void Main(string[] args)
{
if (args.Length != 2)
{
OEChem.OEThrow.Usage("AtomVisibility <rxnfile> <imagefile>");
}
oemolistream ifs = new oemolistream(args[0]);
OEGraphMol mol = new OEGraphMol();
if (!OEChem.OEReadRxnFile(ifs,mol)) {
OEChem.OEThrow.Fatal("error reading rxnfile");
}
OEDepict.OEPrepareDepiction(mol);
OEImage image = new OEImage(900, 100);
uint rows = 1;
uint cols = 3;
OEImageGrid grid = new OEImageGrid(image, rows, cols);
OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(grid.GetCellWidth(),
grid.GetCellHeight(),
OEScale.AutoScale);
OEImageBase cell = grid.GetCell(1,1);
mol.SetTitle("Reaction display");
OEUnaryAtomPred allatoms = new OEIsTrueAtom(); // explicitly set the default
opts.SetAtomVisibilityFunctor(allatoms);
OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);
double rxnscale = disp.GetScale();
OEDepict.OERenderMolecule(cell, disp);
cell = grid.GetCell(1,2);
mol.SetTitle("Reactant display");
OEAtomVisibilityShowRxnRole showreacs = new OEAtomVisibilityShowRxnRole(OERxnRole.Reactant);
opts.SetAtomVisibilityFunctor(showreacs);
opts.SetScale(rxnscale);
disp = new OE2DMolDisplay(mol, opts);
OEDepict.OERenderMolecule(cell, disp);
cell = grid.GetCell(1,3);
mol.SetTitle("Product display");
OEAtomVisibilityShowRxnRole showprods = new OEAtomVisibilityShowRxnRole(OERxnRole.Product);
opts.SetAtomVisibilityFunctor(showprods);
opts.SetScale(rxnscale);
disp = new OE2DMolDisplay(mol, opts);
OEDepict.OERenderMolecule(cell, disp);
OEDepict.OEWriteImage(args[1], image);
}
}
Configuring Molecule Display¶
OEDepict TK provides a mechanism to generate a standard interface that
allows configuration of molecule displays.
By calling the OEConfigureImageOptions
function,
“-height” and “width” parameters are added to the interface
(OEInterface), at run-time the value of these
parameters can be obtained by calling the
OEGetImageHeight
and
OEGetImageWidth
functions, respectively.
(See the standard interface displayed when calling the
Listing 10
example
with the “–help” parameter)
The OEConfigure2DMolDisplayOptions
function
generates an interface that allows setting of various molecule display
options (such as aromatic style, atom and bond coloring etc.)
The OE2DMolDisplayOptions object, that stores
properties determine the molecule depiction style, can initialized
based on to the command line parameters by calling the
OESetup2DMolDisplayOptions
function.
Listing 10: Example of configuring mol display
public class DepictMolConfigure
{
static string InterfaceData =
"!CATEGORY \"input/output options :\"\n" +
"\n" +
" !PARAMETER -in\n" +
" !ALIAS -i\n" +
" !TYPE string\n" +
" !REQUIRED true\n" +
" !BRIEF Input filename\n" +
" !END\n" +
"\n" +
" !PARAMETER -out\n" +
" !ALIAS -o\n" +
" !TYPE string\n" +
" !REQUIRED true\n" +
" !BRIEF Output filename\n" +
" !END\n" +
"\n" +
"!END\n";
public static int Main(string[] args)
{
OEInterface itf = new OEInterface();
// import configuration file
OEChem.OEConfigure(itf, InterfaceData);
// add configuration for image size and display options
OEDepict.OEConfigureImageOptions(itf);
OEDepict.OEConfigure2DMolDisplayOptions(itf);
if (!OEChem.OEParseCommandLine(itf, args, "DepictMolConfigure"))
{
OEChem.OEThrow.Fatal("Unable to interpret command line!");
}
string ifname = itf.GetString("-in");
string ofname = itf.GetString("-out");
oemolistream ifs = new oemolistream(ifname);
OEGraphMol mol = new OEGraphMol();
OEChem.OEReadMolecule(ifs, mol);
OEDepict.OEPrepareDepiction(mol);
double width = OEDepict.OEGetImageWidth(itf);
double height = OEDepict.OEGetImageHeight(itf);
double scale = OEScale.AutoScale;
OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(width, height, scale);
// set up display options from command line parameters
OEDepict.OESetup2DMolDisplayOptions(opts, itf);
OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);
OEDepict.OERenderMolecule(ofname, disp);
return 0;
}
}
prompt> DepictMolConfigure --help
will generate the following standard interface:
Simple parameter list
image options
-height : Height of output image
-width : Width of output image
input/output
-in : Input filename
-out : Output filename
molecule display options
-aromstyle : Aromatic ring display style
-atomcolor : Atom coloring style
-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
See also
OEInterface class in the OEChem TK manual
OEDepict Examples chapter
OEDepict TK provides the following high-level functions to build standard interface of depiction applications:
Function name |
Builds standard interface for .. |
---|---|
molecule depiction style |
|
highlighting |
|
image grid |
|
image |
|
multi-page image |
|
preparing molecules for 2D depiction |
|
multi-page report |