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