RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
MolDraw2DHelpers.h
Go to the documentation of this file.
1//
2// Copyright (C) 2014-2021 David Cosgrove 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// Original author: David Cosgrove (CozChemIx Limited)
11//
12// A load of helper classes used by MolDraw2D.
13
14#ifndef RDKIT_MOLDRAW2DHELPERS_H
15#define RDKIT_MOLDRAW2DHELPERS_H
16
17#include <Geometry/point.h>
18
19using RDGeom::Point2D;
20
21namespace RDKit {
22
23namespace MolDraw2D_detail {
24// for aligning the drawing of text to the passed in coords.
25enum class OrientType : unsigned char {
26 C = 0,
27 N,
28 E,
29 S,
30 W
31};
32enum class TextAlignType : unsigned char {
33 MIDDLE = 0,
34 START,
35 END
36};
37} // namespace MolDraw2D_detail
38
39struct DrawColour {
40 double r = 0.0, g = 0.0, b = 0.0, a = 1.0;
41 DrawColour() = default;
42 DrawColour(double r, double g, double b, double a = 1.0)
43 : r(r), g(g), b(b), a(a) {}
44 bool operator==(const DrawColour &other) const {
45 return r == other.r && g == other.g && b == other.b && a == other.a;
46 }
47 bool operator!=(const DrawColour &other) const { return !(*this == other); }
48 bool feq(const DrawColour &other, double tol = 0.001,
49 bool ignoreAlpha = true) const {
50 return fabs(r - other.r) <= tol && fabs(g - other.g) <= tol &&
51 fabs(b - other.b) <= tol &&
52 (ignoreAlpha || fabs(a - other.a) <= tol);
53 }
55 return {r + other.r, g + other.g, b + other.b, a + other.a};
56 }
58 return {r - other.r, g - other.g, b - other.b, a - other.a};
59 }
60 DrawColour operator/(double v) const {
61 PRECONDITION(v != 0.0, "divide by zero");
62 return {r / v, g / v, b / v, a / v};
63 }
64 DrawColour operator*(double v) const { return {r * v, g * v, b * v, a * v}; }
65};
66
67typedef std::map<int, DrawColour> ColourPalette;
68typedef std::vector<double> DashPattern;
69
70// This is used to convert the line width into something that SVG and
71// Cairo use. It was picked by eye, and was formerly hidden in
72// MolDraw2D::getDrawLineWidth().
73static const double lineWidthScaleFactor = 0.02;
74
75//! use the RDKit's default palette r
76// 201 is for hydrogens when atom symbols are not being drawn.
78 palette.clear();
79 palette[-1] = DrawColour(0, 0, 0);
80 palette[0] = DrawColour(0.1, 0.1, 0.1);
81 palette[1] = palette[6] = DrawColour(0.0, 0.0, 0.0);
82 palette[7] = DrawColour(0.0, 0.0, 1.0);
83 palette[8] = DrawColour(1.0, 0.0, 0.0);
84 palette[9] = DrawColour(0.2, 0.8, 0.8);
85 palette[15] = DrawColour(1.0, 0.5, 0.0);
86 palette[16] = DrawColour(0.8, 0.8, 0.0);
87 palette[17] = DrawColour(0.0, 0.802, 0.0);
88 palette[35] = DrawColour(0.5, 0.3, 0.1);
89 palette[53] = DrawColour(0.63, 0.12, 0.94);
90 palette[201] = DrawColour(0.68, 0.85, 0.90);
91};
92
93//! use the color palette from the Avalon renderer
94// 201 is for hydrogens when atom symbols are not being drawn.
96 palette.clear();
97 palette[-1] = DrawColour(0, 0, 0);
98 palette[0] = DrawColour(0.1, 0.1, 0.1);
99 palette[1] = palette[6] = DrawColour(0.0, 0.0, 0.0);
100 palette[7] = DrawColour(0.0, 0.0, 1.0);
101 palette[8] = DrawColour(1.0, 0.0, 0.0);
102 palette[9] = DrawColour(0.0, 0.498, 0.0);
103 palette[15] = DrawColour(0.498, 0.0, 0.498);
104 palette[16] = DrawColour(0.498, 0.247, 0.0);
105 palette[17] = DrawColour(0.0, 0.498, 0.0);
106 palette[35] = DrawColour(0.0, 0.498, 0.0);
107 palette[53] = DrawColour(0.247, 0.0, 0.498);
108 palette[201] = DrawColour(0.68, 0.85, 0.90);
109};
110
111//! use (part of) the CDK color palette
112/*!
113 data source:
114 https://github.com/cdk/cdk/blob/master/display/render/src/main/java/org/openscience/cdk/renderer/color/CDK2DAtomColors.java
115*/
116// 201 is for hydrogens when atom symbols are not being drawn.
118 palette.clear();
119 palette[-1] = DrawColour(0, 0, 0);
120 palette[0] = DrawColour(0.1, 0.1, 0.1);
121 palette[1] = palette[6] = DrawColour(0.0, 0.0, 0.0);
122 palette[7] = DrawColour(0.188, 0.314, 0.972);
123 palette[8] = DrawColour(1.0, 0.051, 0.051);
124 palette[9] = DrawColour(0.565, 0.878, 0.314);
125 palette[15] = DrawColour(1.0, 0.5, 0.0);
126 palette[16] = DrawColour(0.776, 0.776, 0.173);
127 palette[17] = DrawColour(0.122, 0.498, 0.122);
128 palette[35] = DrawColour(0.651, 0.161, 0.161);
129 palette[53] = DrawColour(0.580, 0.0, 0.580);
130 palette[5] = DrawColour(1.000, 0.710, 0.710);
131 palette[201] = DrawColour(0.68, 0.85, 0.90);
132};
133
134// 201 is for hydrogens when atom symbols are not being drawn.
136 palette.clear();
137 palette[-1] = DrawColour(0.8, 0.8, 0.8);
138 palette[0] = DrawColour(0.9, 0.9, 0.9);
139 palette[1] = palette[6] = DrawColour(0.9, 0.9, 0.9);
140 palette[7] = DrawColour(0.33, 0.41, 0.92);
141 palette[8] = DrawColour(1.0, 0.2, 0.2);
142 palette[9] = DrawColour(0.2, 0.8, 0.8);
143 palette[15] = DrawColour(1.0, 0.5, 0.0);
144 palette[16] = DrawColour(0.8, 0.8, 0.0);
145 palette[17] = DrawColour(0.0, 0.802, 0.0);
146 palette[35] = DrawColour(0.71, 0.4, 0.07);
147 palette[53] = DrawColour(0.89, 0.004, 1);
148 palette[201] = DrawColour(0.68, 0.85, 0.90);
149};
150
152 palette.clear();
153 palette[-1] = DrawColour(0, 0, 0);
154};
155
160
162 bool atomLabelDeuteriumTritium =
163 false; // toggles replacing 2H with D and 3H with T
164 bool dummiesAreAttachments = false; // draws "breaks" at dummy atoms
165 bool circleAtoms = true; // draws circles under highlighted atoms
166 bool splitBonds = false; // split bonds into per atom segments
167 // most useful for dynamic manipulation of drawing
168 // especially for svg
169 DrawColour highlightColour{1.0, 0.5, 0.5, 1.0}; // default highlight color
170 bool continuousHighlight = true; // highlight by drawing an outline
171 // *underneath* the molecule
172 bool fillHighlights = true; // fill the areas used to highlight atoms and
173 // atom regions
174 double highlightRadius = 0.3; // default if nothing given for a particular
175 // atom. units are "Angstrom"
176 int flagCloseContactsDist = 3; // if positive, this will be used as a cutoff
177 // (in pixels) for highlighting close contacts
178 bool includeAtomTags =
179 false; // toggles inclusion of atom tags in the output. does
180 // not make sense for all renderers.
181 bool clearBackground = true; // toggles clearing the background before
182 // drawing a molecule
183 DrawColour backgroundColour{
184 1.0, 1.0, 1.0, 1.0}; // color to be used while clearing the background
185 DrawColour queryColour{0.5, 0.5, 0.5,
186 1.0}; // color to be used for query bonds
187 int legendFontSize = 16; // font size (in pixels) to be used for the legend
188 // (if present)
189 double legendFraction =
190 0.1; // fraction of the draw panel to be used for the legend if present
191 int maxFontSize = 40; // maximum size in pixels for font in drawn molecule.
192 // -1 means no max.
193 int minFontSize = 6; // likewise for -1.
194 int fixedFontSize =
195 -1; // font size to use, in pixels. Default -1 means not fixed. If set,
196 // always used irrespective of scale, minFontSize and maxFontSize.
197 double annotationFontScale = 0.5; // scales font relative to atom labels for
198 // atom and bond annotation.
199 std::string fontFile = ""; // name of font file for freetype rendering. If
200 // given, over-rides default
201 // (BuiltinTelexRegular). Can also be
202 // BuiltinRobotoRegular.
203 DrawColour legendColour{0, 0,
204 0}; // color to be used for the legend (if present)
205 double multipleBondOffset = 0.15; // offset for the extra lines
206 // in a multiple bond as a fraction of
207 // mean bond length
208 double padding =
209 0.05; // fraction of empty space to leave around the molecule
210 double additionalAtomLabelPadding = 0.0; // additional padding to leave
211 // around atom labels. Expressed as
212 // a fraction of the font size.
213 std::map<int, std::string> atomLabels; // replacement labels for atoms
214 bool noAtomLabels =
215 false; // disables inclusion of atom labels in the rendering
216 std::vector<std::vector<int>> atomRegions; // regions
217 DrawColour symbolColour{
218 0.0, 0.0, 0.0,
219 1.0}; // color to be used for the symbols and arrows in reactions
220 DrawColour annotationColour{0.0, 0.0, 0.0,
221 1.0}; // color to be used for annotations
222 double bondLineWidth = 2.0; // default line width when drawing bonds
223 bool scaleBondWidth = false; // whether to apply scale() to the bond width
224 bool scaleHighlightBondWidth = true; // likewise with bond highlights.
225 int highlightBondWidthMultiplier = 8; // what to multiply standard bond width
226 // by for highlighting.
227 bool prepareMolsBeforeDrawing = true; // call prepareMolForDrawing() on each
228 // molecule passed to drawMolecules()
229 std::vector<DrawColour> highlightColourPalette; // defining 10 default colors
230 // for highlighting atoms and bonds
231 // or reactants in a reactions
232 ColourPalette atomColourPalette; // the palette used to assign
233 // colors to atoms based on
234 // atomic number.
235 double fixedScale =
236 -1.0; // fixes scale to this fraction of draw window width, so
237 // an average bond is this fraction of the width. If
238 // scale comes out smaller than this, reduces scale, but
239 // won't make it larger. The default of -1.0 means no fix.
240 double fixedBondLength =
241 -1.0; // fixes the bond length (and hence the scale) to
242 // always be this number of pixels. Assuming a bond
243 // length in coordinates is 1, as is normal. If
244 // scale comes out smaller than this, reduces scale,
245 // but won't make it larger. The default -1.0 means no
246 // fix. If both fixedScale and fixedBondLength are >
247 // 0.0, fixedScale wins.
248 double rotate = 0.0; // angle in degrees to rotate coords by about centre
249 // before drawing.
250 bool addAtomIndices = false; // adds atom indices to drawings.
251 bool addBondIndices = false; // adds bond indices to drawings.
252 bool isotopeLabels = true; // adds isotope to non-dummy atoms.
253 bool dummyIsotopeLabels = true; // adds isotope labels to dummy atoms.
254
255 bool addStereoAnnotation = false; // adds E/Z and R/S to drawings.
256 bool atomHighlightsAreCircles = false; // forces atom highlights always to be
257 // circles. Default (false) is to put
258 // ellipses round longer labels.
259 MultiColourHighlightStyle multiColourHighlightStyle =
260 MultiColourHighlightStyle::CIRCLEANDLINE;
261 bool centreMoleculesBeforeDrawing = false; // moves the centre of the drawn
262 // molecule to (0,0)
263 bool explicitMethyl = false; // draw terminal methyl and related as CH3
264 bool includeRadicals =
265 true; // include radicals in the drawing (it can be useful to turn this
266 // off for reactions and queries)
267 bool includeMetadata =
268 true; // when possible include metadata about molecules and reactions in
269 // the output to allow them to be reconstructed
270 bool comicMode = false; // simulate hand-drawn lines for bonds. When combined
271 // with a font like Comic-Sans or Comic-Neue, this
272 // gives xkcd-like drawings.
273 int variableBondWidthMultiplier = 16; // what to multiply standard bond width
274 // by for variable attachment points.
275 double variableAtomRadius = 0.4; // radius value to use for atoms involved in
276 // variable attachment points.
277 DrawColour variableAttachmentColour = {
278 0.8, 0.8, 0.8, 1.0}; // colour to use for variable attachment points
279 bool includeChiralFlagLabel =
280 false; // add a molecule annotation with "ABS" if the chiral flag is set
281 bool simplifiedStereoGroupLabel =
282 false; // if all specified stereocenters are in a single StereoGroup,
283 // show a molecule-level annotation instead of the individual
284 // labels
285 bool unspecifiedStereoIsUnknown = false; // if true, double bonds with
286 // unspecified stereo are drawn
287 // crossed, potential stereocenters
288 // with unspecified stereo are drawn
289 // with a wavy bond.
290 bool singleColourWedgeBonds =
291 false; // if true wedged and dashed bonds are drawn
292 // using symbolColour rather than inheriting
293 // their colour from the atoms
294 bool useMolBlockWedging = false; // If the molecule came from a MolBlock,
295 // prefer the wedging information that
296 // provides. If false, use RDKit rules.
297 double scalingFactor = 20.0; // scaling factor used for pixels->angstrom
298 // when auto scaling is being used
299 double baseFontSize =
300 -1.0; // when > 0 this is used to set the baseFontSize used for text
301 // drawing. As a reference point: the default value for
302 // DrawText::baseFontSize is 0.6
303 bool drawMolsSameScale = true; // when drawing multiple molecules with
304 // DrawMolecules, forces them to use the same
305 // scale. Default is true.
306 bool useComplexQueryAtomSymbols =
307 true; // replace any atom, any hetero, any halo queries
308 // with complex query symbols A, Q, X, M, optionally followed
309 // by H if hydrogen is included (except for AH, which stays *).
310 // Default is true.
311 bool bracketsAroundAtomLists = true; // If true, puts brackets round atom
312 // lists in query atoms.
313
315 highlightColourPalette.emplace_back(
316 DrawColour(1., 1., .67)); // popcorn yellow
317 highlightColourPalette.emplace_back(DrawColour(1., .8, .6)); // sand
318 highlightColourPalette.emplace_back(
319 DrawColour(1., .71, .76)); // light pink
320 highlightColourPalette.emplace_back(
321 DrawColour(.8, 1., .8)); // offwhitegreen
322 highlightColourPalette.emplace_back(DrawColour(.87, .63, .87)); // plum
323 highlightColourPalette.emplace_back(
324 DrawColour(.76, .94, .96)); // pastel blue
325 highlightColourPalette.emplace_back(
326 DrawColour(.67, .67, 1.)); // periwinkle
327 highlightColourPalette.emplace_back(DrawColour(.64, .76, .34)); // avocado
328 highlightColourPalette.emplace_back(
329 DrawColour(.56, .93, .56)); // light green
330 highlightColourPalette.emplace_back(DrawColour(.20, .63, .79)); // peacock
331 assignDefaultPalette(atomColourPalette);
332 }
333};
334
335} // namespace RDKit
336
337#endif // RDKIT_MOLDRAW2DHELPERS_H
#define PRECONDITION(expr, mess)
Definition Invariant.h:109
#define RDKIT_MOLDRAW2D_EXPORT
Definition export.h:297
Std stuff.
std::vector< double > DashPattern
bool rdvalue_is(const RDValue_cast_t)
static const double lineWidthScaleFactor
MultiColourHighlightStyle
void assignDefaultPalette(ColourPalette &palette)
use the RDKit's default palette r
void assignAvalonPalette(ColourPalette &palette)
use the color palette from the Avalon renderer
void assignDarkModePalette(ColourPalette &palette)
std::map< int, DrawColour > ColourPalette
void assignBWPalette(ColourPalette &palette)
void assignCDKPalette(ColourPalette &palette)
use (part of) the CDK color palette
bool feq(const DrawColour &other, double tol=0.001, bool ignoreAlpha=true) const
DrawColour()=default
DrawColour operator+(const DrawColour &other) const
DrawColour operator/(double v) const
DrawColour operator*(double v) const
DrawColour(double r, double g, double b, double a=1.0)
bool operator!=(const DrawColour &other) const
DrawColour operator-(const DrawColour &other) const
bool operator==(const DrawColour &other) const
std::vector< std::vector< int > > atomRegions
std::map< int, std::string > atomLabels
ColourPalette atomColourPalette
std::vector< DrawColour > highlightColourPalette