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