RDKit
Open-source cheminformatics and machine learning.
MolDraw2DUtils.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2016-2019 Greg Landrum
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 <boost/tuple/tuple.hpp>
17 
18 // ****************************************************************************
19 
20 namespace RDKit {
21 class MolDraw2D;
22 class MolDraw2DColour;
23 
24 namespace MolDraw2DUtils {
25 //! Does some cleanup operations on the molecule to prepare it to draw nicely
26 /*
27 The operations include: kekulization, addition of chiral Hs (so that we can draw
28 wedges to them), wedging of bonds at chiral centers, and generation of a 2D
29 conformation 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 
37 NOTE: the kekulization step can fail, throwing a MolSanitizeExecption. If this
38 happens the molecule will be in an inconsistent, partially kekulized, state.
39 This isn't normally a problem for molecules that have been sanitized, but can be
40 problematic if the molecules have been modified post santitization.
41 */
43  bool kekulize = true,
44  bool addChiralHs = true,
45  bool wedgeBonds = true,
46  bool forceCoords = 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  higlight 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 
75  const char *json);
77  const std::string &json);
78 
79 struct ContourParams {
80  bool setScale = true; // assumes the grid is drawn first
81  bool dashNegative = true; // use dashed lines for negative contours
82  bool fillGrid = false; // shade the grid
83  double gridResolution = 0.15; // spacing between elements of the grid
84  double contourWidth = 1.0; // linewidth for drawing contours
85  double extraGridPadding = 0.0; // extra padding (in molecule coordinates)
86  DrawColour contourColour = {0.5, 0.5, 0.5,
87  0.5}; // color for drawing contours
88  std::vector<DrawColour> colourMap = {
89  {0.557, 0.004, 0.322, 0.5},
90  {1, 1, 1, 0.5},
91  {0.153, 0.392, 0.098, 0.5}}; // similarity map color scheme
92 };
93 
94 //! Generates and draws contours for data on a grid
95 /*
96  \param drawer: the MolDraw2D object to use
97  \param grid: the data to be contoured
98  \param xcoords: x positions of the grid points
99  \param ycoords: y positions of the grid points
100  \param nContours: the number of contours to draw
101  \param levels: the contours to use
102  \param ps: additional parameters controlling the contouring.
103 
104  If the \c levels argument is empty, the contour levels will be determined
105  automatically from the max and min values on the grid and \c levels will
106  be updated to include the contour levels.
107 
108  If \c ps.fillGrid is set, the data on the grid will also be drawn using
109  the color scheme in \c ps.colourMap
110 
111 */
113  MolDraw2D &drawer, const double *grid, const std::vector<double> &xcoords,
114  const std::vector<double> &ycoords, size_t nContours,
115  std::vector<double> &levels, const ContourParams &ps = ContourParams());
116 //! \overload
118  MolDraw2D &drawer, const double *grid, const std::vector<double> &xcoords,
119  const std::vector<double> &ycoords, size_t nContours = 10,
120  const ContourParams &ps = ContourParams()) {
121  std::vector<double> levels;
122  contourAndDrawGrid(drawer, grid, xcoords, ycoords, nContours, levels, ps);
123 };
124 
125 //! Generates and draws contours for a set of gaussians
126 /*
127  \param drawer: the MolDraw2D object to use
128  \param locs: locations of the gaussians
129  \param heights: the heights (or weights) of the gaussians
130  \param widths: the standard deviations of the gaussians
131  \param nContours: the number of contours to draw
132  \param levels: the contours to use
133  \param ps: additional parameters controlling the contouring.
134 
135  The values are calculated on a grid with spacing \c ps.gridResolution.
136  If \c ps.setScale is set, the grid size will be calculated based on the
137  locations of the gaussians and \c ps.extraGridPadding. Otherwise the current
138  size of the viewport will be used.
139 
140  If the \c levels argument is empty, the contour levels will be determined
141  automatically from the max and min values on the grid and \c levels will
142  be updated to include the contour levels.
143 
144  If \c ps.fillGrid is set, the data on the grid will also be drawn using
145  the color scheme in \c ps.colourMap
146 
147 */
149  MolDraw2D &drawer, const std::vector<Point2D> &locs,
150  const std::vector<double> &heights, const std::vector<double> &widths,
151  size_t nContours, std::vector<double> &levels,
152  const ContourParams &ps = ContourParams());
153 //! \overload
155  MolDraw2D &drawer, const std::vector<Point2D> &locs,
156  const std::vector<double> &heights, const std::vector<double> &widths,
157  size_t nContours = 10, const ContourParams &ps = ContourParams()) {
158  std::vector<double> levels;
159  contourAndDrawGaussians(drawer, locs, heights, widths, nContours, levels, ps);
160 };
161 
162 } // namespace MolDraw2DUtils
163 } // namespace RDKit
164 #endif // MOLDRAW2DUTILS_H
RDKit::MolDraw2DUtils::ContourParams::fillGrid
bool fillGrid
Definition: MolDraw2DUtils.h:82
RDKIT_MOLDRAW2D_EXPORT
#define RDKIT_MOLDRAW2D_EXPORT
Definition: export.h:398
RDKit::MolDraw2DUtils::ContourParams::contourColour
DrawColour contourColour
Definition: MolDraw2DUtils.h:86
RDKit::MolDraw2DUtils::updateDrawerParamsFromJSON
RDKIT_MOLDRAW2D_EXPORT void updateDrawerParamsFromJSON(MolDraw2D &drawer, const char *json)
RDKit::RWMol
RWMol is a molecule class that is intended to be edited.
Definition: RWMol.h:31
RDKit::MolDraw2DUtils::ContourParams::colourMap
std::vector< DrawColour > colourMap
Definition: MolDraw2DUtils.h:88
RDKit::MolDraw2DUtils::contourAndDrawGrid
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())
Generates and draws contours for data on a grid.
RDKit::MolDraw2DUtils::ContourParams::extraGridPadding
double extraGridPadding
Definition: MolDraw2DUtils.h:85
RDKit::DrawColour
Definition: MolDraw2D.h:37
RDKit::ROMol
Definition: ROMol.h:171
RDKit::MolDraw2DUtils::ContourParams::dashNegative
bool dashNegative
Definition: MolDraw2DUtils.h:81
RDKit::MolDraw2DUtils::contourAndDrawGaussians
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())
Generates and draws contours for a set of gaussians.
RDKit
Std stuff.
Definition: Atom.h:30
RDKit::MolDraw2D
MolDraw2D is the base class for doing 2D renderings of molecules.
Definition: MolDraw2D.h:165
RDKit::MolDraw2DUtils::ContourParams
Definition: MolDraw2DUtils.h:79
RWMol.h
Defines the editable molecule class RWMol.
RDKit::MolDraw2DUtils::ContourParams::contourWidth
double contourWidth
Definition: MolDraw2DUtils.h:84
RDKit::MolDraw2DUtils::prepareMolForDrawing
RDKIT_MOLDRAW2D_EXPORT void prepareMolForDrawing(RWMol &mol, bool kekulize=true, bool addChiralHs=true, bool wedgeBonds=true, bool forceCoords=false)
Does some cleanup operations on the molecule to prepare it to draw nicely.
RDKit::MolDraw2DUtils::prepareAndDrawMolecule
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)
prepare a molecule for drawing and draw it
RDKit::MolDraw2DUtils::ContourParams::gridResolution
double gridResolution
Definition: MolDraw2DUtils.h:83
RDKit::MolDraw2DUtils::ContourParams::setScale
bool setScale
Definition: MolDraw2DUtils.h:80
export.h