RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
MolDraw2DUtils.h
Go to the documentation of this file.
1//
2// Copyright (C) 2016-2021 Greg Landrum and other RDKit contributors
3//
4// @@ All Rights Reserved @@
5// This file is part of the RDKit.
6// The contents are covered by the terms of the BSD license
7// which is included in the file license.txt, found at the root
8// of the RDKit source tree.
9//
10
11#include <RDGeneral/export.h>
12#ifndef MOLDRAW2DUTILS_H
13#define MOLDRAW2DUTILS_H
15#include <GraphMol/RWMol.h>
16
17#include <tuple>
18
19// ****************************************************************************
20
21namespace RDKit {
22class MolDraw2D;
23
24namespace MolDraw2DUtils {
25
26//! Does some cleanup operations on the molecule to prepare it to draw nicely
27/*
28The operations include: kekulization, addition of chiral Hs (so that we can draw
29wedges to them), wedging of bonds at chiral centers, and generation of a 2D
30conformation if the molecule does not already have a conformation
31
32\param mol: the molecule to be modified
33\param kekulize: toggles kekulization (this can fail, see below)
34\param addChiralHs: adds Hs to the graph on chiral atoms
35\param wedgeBonds: calls WedgeMolBonds()
36\param forceCoords: generates a 2D conformation even if one is present already
37\param wavyBonds: calls addWavyBondsForStereoAny() and clears other markers that
38 double bond stereo is unknown
39
40NOTE: the kekulization step can fail, throwing a MolSanitizeExecption. If this
41happens the molecule will be in an inconsistent, partially kekulized, state.
42This isn't normally a problem for molecules that have been sanitized, but can be
43problematic if the molecules have been modified post santitization.
44*/
46 RWMol &mol, bool kekulize = true, bool addChiralHs = true,
47 bool wedgeBonds = true, bool forceCoords = false, bool wavyBonds = false);
48
49//! prepare a molecule for drawing and draw it
50/*
51 \param mol: the molecule to draw
52 \param legend: (optional) the legend (to be drawn under the molecule)
53 \param highlight_atoms: (optional) vector of atom ids to highlight
54 \param highlight_atoms: (optional) vector of bond ids to highlight
55 \param highlight_atom_map: (optional) map from atomId -> DrawColour
56 providing the highlight colors. If not provided the default
57 highlight colour from \c drawOptions() will be used.
58 \param highlight_bond_map: (optional) map from bondId -> DrawColour
59 providing the highlight colors. If not provided the default
60 highlight colour from \c drawOptions() will be used.
61 \param highlight_radii: (optional) map from atomId -> radius (in molecule
62 coordinates) for the radii of atomic highlights. If not provided
63 the default value from \c drawOptions() will be used.
64 \param confId: (optional) conformer ID to be used for atomic coordinates
65
66*/
68 MolDraw2D &drawer, const ROMol &mol, const std::string &legend = "",
69 const std::vector<int> *highlight_atoms = nullptr,
70 const std::vector<int> *highlight_bonds = nullptr,
71 const std::map<int, DrawColour> *highlight_atom_map = nullptr,
72 const std::map<int, DrawColour> *highlight_bond_map = nullptr,
73 const std::map<int, double> *highlight_radii = nullptr, int confId = -1,
74 bool kekulize = true, bool addChiralHs = true, bool wedgeBonds = true,
75 bool forceCoords = false, bool wavyBonds = false);
76
78 const char *json);
80 const std::string &json);
82 const char *json);
84 MolDrawOptions &opts, const std::string &json);
85
87 bool setScale = true; // assumes the grid is drawn first
88 bool dashNegative = true; // use dashed lines for negative contours
89 bool fillGrid = false; // shade the grid
90 double gridResolution = 0.15; // spacing between elements of the grid
91 double contourWidth = 1.0; // linewidth for drawing contours
92 double extraGridPadding = 0.0; // extra padding (in molecule coordinates)
93 DrawColour contourColour = {0.5, 0.5, 0.5,
94 0.5}; // color for drawing contours
95 std::vector<DrawColour> colourMap = {
96 {0.557, 0.004, 0.322, 0.5},
97 {1, 1, 1, 0.5},
98 {0.153, 0.392, 0.098, 0.5}}; // similarity map color scheme
100 true; // draws the contours as continuous lines instead of line segments.
102 1000.; // caling factor used to convert coordinates to ints when forming
103 // the continuous lines
105 1e6; // scaling factor used to convert isovalues to ints when forming the
106 // continuous lines
108 false; // use a magnitude threshold to determine if a grid box is filled
109 double fillThreshold = 0.01; // magnitude threshold for filling grid boxes
110 bool fillThresholdIsFraction = true; // if true, the fill threshold is a
111 // fraction of the range of the data
112};
113
114//! Generates and draws contours for data on a grid
115/*
116 \param drawer: the MolDraw2D object to use
117 \param grid: the data to be contoured
118 \param xcoords: x positions of the grid points
119 \param ycoords: y positions of the grid points
120 \param nContours: the number of contours to draw
121 \param levels: the contours to use
122 \param ps: additional parameters controlling the contouring.
123 \param mol: molecule to be used to adjust the scale of the drawing.
124 If the \c levels argument is empty, the contour levels will be determined
125 automatically from the max and min values on the grid and \c levels will
126 be updated to include the contour levels.
127
128 If \c ps.fillGrid is set, the data on the grid will also be drawn using
129 the color scheme in \c ps.colourMap
130
131 if the \c mol argument is given, it will be used to adjust the scale of
132 drawing. This is because a common use is to draw the molecule onto
133 the contour, and it makes sense if it fits.
134
135*/
137 MolDraw2D &drawer, const double *grid, const std::vector<double> &xcoords,
138 const std::vector<double> &ycoords, size_t nContours,
139 std::vector<double> &levels, const ContourParams &ps = ContourParams(),
140 const ROMol *mol = nullptr);
141//! \overload
143 MolDraw2D &drawer, const double *grid, const std::vector<double> &xcoords,
144 const std::vector<double> &ycoords, size_t nContours = 10,
145 const ContourParams &ps = ContourParams(), const ROMol *mol = nullptr) {
146 std::vector<double> levels;
147 contourAndDrawGrid(drawer, grid, xcoords, ycoords, nContours, levels, ps,
148 mol);
149};
150
151//! Generates and draws contours for a set of gaussians
152/*
153 \param drawer: the MolDraw2D object to use
154 \param locs: locations of the gaussians
155 \param heights: the heights (or weights) of the gaussians
156 \param widths: the standard deviations of the gaussians
157 \param nContours: the number of contours to draw
158 \param levels: the contours to use
159 \param ps: additional parameters controlling the contouring.
160 \param mol: molecule to be used to adjust the scale of the drawing.
161
162 The values are calculated on a grid with spacing \c ps.gridResolution.
163 If \c ps.setScale is set, the grid size will be calculated based on the
164 locations of the gaussians and \c ps.extraGridPadding. Otherwise the current
165 size of the viewport will be used.
166
167 If the \c levels argument is empty, the contour levels will be determined
168 automatically from the max and min values on the grid and \c levels will
169 be updated to include the contour levels.
170
171 If \c ps.fillGrid is set, the data on the grid will also be drawn using
172 the color scheme in \c ps.colourMap
173
174 if the \c mol argument is given, it will be used to adjust the scale of
175 drawing. This is because a common use is to draw the molecule onto
176 the contour, and it makes sense if it fits.
177
178*/
180 MolDraw2D &drawer, const std::vector<Point2D> &locs,
181 const std::vector<double> &heights, const std::vector<double> &widths,
182 size_t nContours, std::vector<double> &levels,
183 const ContourParams &ps = ContourParams(), const ROMol *mol = nullptr);
184//! \overload
186 MolDraw2D &drawer, const std::vector<Point2D> &locs,
187 const std::vector<double> &heights, const std::vector<double> &widths,
188 size_t nContours = 10, const ContourParams &ps = ContourParams(),
189 const ROMol *mol = nullptr) {
190 std::vector<double> levels;
191 contourAndDrawGaussians(drawer, locs, heights, widths, nContours, levels, ps,
192 mol);
193};
194
195//! Draw a molecule to a MolDraw2D object according to ACS 1996 guidelines
196/*
197 The ACS1996 guidelines, as described at
198 https://en.wikipedia.org/wiki/Wikipedia:Manual_of_Style/Chemistry/Structure_drawing
199 A number of values in drawer.drawOptions() are changed.
200 This is designed to be used with a flexiCanvas, i.e. a MolDraw2D object
201 created with width and height -1, because it works to a fixed scale.
202 It will issue a warning if the dimensions are otherwise and the picture may
203 look sub-optimal.
204 */
206 MolDraw2D &drawer, const ROMol &mol, const std::string &legend,
207 const std::vector<int> *highlight_atoms,
208 const std::vector<int> *highlight_bonds,
209 const std::map<int, DrawColour> *highlight_atom_map = nullptr,
210 const std::map<int, DrawColour> *highlight_bond_map = nullptr,
211 const std::map<int, double> *highlight_radii = nullptr, int confId = -1);
212
213//! Set the draw options to produce something as close as possible to
214//! the ACS 1996 guidelines as described at
215//! https://en.wikipedia.org/wiki/Wikipedia:Manual_of_Style/Chemistry/Structure_drawing
216/*
217 \param MolDrawOptions opt - the options what will be changed
218 \param float meanBondLength - mean bond length of the molecule
219
220 Works best if the MolDraw2D object is created with width and height -1 (a
221 flexiCanvas).
222 The mean bond length may be calculated with MolDraw2DUtils::meanBondLength.
223 It is used to calculate the offset for the lines in multiple bonds.
224
225 Options changed are:
226 bondLineWidth = 0.6
227 scaleBondWidth = false
228 scalingFactor = 14.4 / meanBondLen
229 multipleBondOffset = 0.18
230 highlightBondWidthMultiplier = 32
231 setMonochromeMode - black and white
232 fixedFontSize = 10
233 additionalAtomLabelPadding = 0.066
234 fontFile - if it isn't set already, then if RDBASE is set and the file
235 exists, uses $RDBASE/Fonts/Data/FreeSans.ttf. Otherwise uses
236 BuiltinRobotoRegular.
237 */
239 double meanBondLen = 1.0);
240RDKIT_MOLDRAW2D_EXPORT double meanBondLength(const ROMol &mol, int confId = -1);
241} // namespace MolDraw2DUtils
242
243} // namespace RDKit
244#endif // MOLDRAW2DUTILS_H
Defines the editable molecule class RWMol.
MolDraw2D is the base class for doing 2D renderings of molecules.
Definition MolDraw2D.h:47
RWMol is a molecule class that is intended to be edited.
Definition RWMol.h:32
#define RDKIT_MOLDRAW2D_EXPORT
Definition export.h:401
RDKIT_MOLDRAW2D_EXPORT void updateMolDrawOptionsFromJSON(MolDrawOptions &opts, const char *json)
RDKIT_MOLDRAW2D_EXPORT void contourAndDrawGrid(MolDraw2D &drawer, const double *grid, const std::vector< double > &xcoords, const std::vector< double > &ycoords, size_t nContours, std::vector< double > &levels, const ContourParams &ps=ContourParams(), const ROMol *mol=nullptr)
Generates and draws contours for data on a grid.
RDKIT_MOLDRAW2D_EXPORT void prepareAndDrawMolecule(MolDraw2D &drawer, const ROMol &mol, const std::string &legend="", const std::vector< int > *highlight_atoms=nullptr, const std::vector< int > *highlight_bonds=nullptr, const std::map< int, DrawColour > *highlight_atom_map=nullptr, const std::map< int, DrawColour > *highlight_bond_map=nullptr, const std::map< int, double > *highlight_radii=nullptr, int confId=-1, bool kekulize=true, bool addChiralHs=true, bool wedgeBonds=true, bool forceCoords=false, bool wavyBonds=false)
prepare a molecule for drawing and draw it
RDKIT_MOLDRAW2D_EXPORT void prepareMolForDrawing(RWMol &mol, bool kekulize=true, bool addChiralHs=true, bool wedgeBonds=true, bool forceCoords=false, bool wavyBonds=false)
Does some cleanup operations on the molecule to prepare it to draw nicely.
RDKIT_MOLDRAW2D_EXPORT void setACS1996Options(MolDrawOptions &opts, double meanBondLen=1.0)
RDKIT_MOLDRAW2D_EXPORT void updateDrawerParamsFromJSON(MolDraw2D &drawer, const char *json)
RDKIT_MOLDRAW2D_EXPORT double meanBondLength(const ROMol &mol, int confId=-1)
RDKIT_MOLDRAW2D_EXPORT void drawMolACS1996(MolDraw2D &drawer, const ROMol &mol, const std::string &legend, const std::vector< int > *highlight_atoms, const std::vector< int > *highlight_bonds, const std::map< int, DrawColour > *highlight_atom_map=nullptr, const std::map< int, DrawColour > *highlight_bond_map=nullptr, const std::map< int, double > *highlight_radii=nullptr, int confId=-1)
Draw a molecule to a MolDraw2D object according to ACS 1996 guidelines.
RDKIT_MOLDRAW2D_EXPORT void contourAndDrawGaussians(MolDraw2D &drawer, const std::vector< Point2D > &locs, const std::vector< double > &heights, const std::vector< double > &widths, size_t nContours, std::vector< double > &levels, const ContourParams &ps=ContourParams(), const ROMol *mol=nullptr)
Generates and draws contours for a set of gaussians.
Std stuff.
std::vector< DrawColour > colourMap