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