RDKit
Open-source cheminformatics and machine learning.
MolDraw2D.h
Go to the documentation of this file.
1 //
2 // @@ All Rights Reserved @@
3 // This file is part of the RDKit.
4 // The contents are covered by the terms of the BSD license
5 // which is included in the file license.txt, found at the root
6 // of the RDKit source tree.
7 //
8 // Original author: David Cosgrove (AstraZeneca)
9 // 27th May 2014
10 //
11 // This class makes a 2D drawing of an RDKit molecule.
12 // It draws heavily on $RDBASE/GraphMol/MolDrawing/MolDrawing.h.
13 // One purpose of this is to make it easier to overlay annotations on top of
14 // the molecule drawing, which is difficult to do from the output of
15 // MolDrawing.h
16 // The class design philosophy echoes a standard one:
17 // a virtual base class defines the interface and does all
18 // the heavy lifting and concrete derived classes implement
19 // library-specific drawing code such as drawing lines, writing strings
20 // etc.
21 
22 #ifndef RDKITMOLDRAW2D_H
23 #define RDKITMOLDRAW2D_H
24 
25 #include <vector>
26 
27 #include <Geometry/point.h>
28 #include <GraphMol/RDKitBase.h>
29 
30 #include <boost/tuple/tuple.hpp>
31 
32 // ****************************************************************************
33 using RDGeom::Point2D;
34 
35 namespace RDKit {
36 
37  typedef boost::tuple<float,float,float> DrawColour;
38  typedef std::vector<unsigned int> DashPattern;
39 
40  struct MolDrawOptions {
41  bool dummiesAreAttachments; // draws "breaks" at dummy atoms
42  bool circleAtoms; // draws circles under highlighted atoms
43  DrawColour highlightColour; // default highlight color
44  bool continuousHighlight; // highlight by drawing an outline *underneath* the molecule
45  int flagCloseContactsDist; // if positive, this will be used as a cutoff (in pixels) for highlighting close contacts
46  bool includeAtomTags; // toggles inclusion of atom tags in the output. does not make sense for all renderers.
47  std::map<int,std::string> atomLabels; // replacement labels for atoms
48  std::vector<std::vector<int> > atomRegions; // regions
49 
51  dummiesAreAttachments(false),
52  circleAtoms(true),
53  highlightColour(1,.5,.5),
54  continuousHighlight(true),
55  flagCloseContactsDist(3),
56  includeAtomTags(false)
57  {};
58  };
59 
60  class MolDraw2D {
61  public :
62 
63  typedef enum { C = 0 , N , E , S , W } OrientType;
64 
65  MolDraw2D( int width , int height );
66  virtual ~MolDraw2D() {}
67 
68  virtual void drawMolecule( const ROMol &mol ,
69  const std::vector<int> *highlight_atoms = NULL,
70  const std::map<int,DrawColour> *highlight_map = NULL,
71  const std::map<int,double> *highlight_radii = NULL,
72  int confId=-1);
73 
74  virtual void drawMolecule( const ROMol &mol ,
75  const std::vector<int> *highlight_atoms,
76  const std::vector<int> *highlight_bonds,
77  const std::map<int,DrawColour> *highlight_atom_map = NULL,
78  const std::map<int,DrawColour> *highlight_bond_map = NULL,
79  const std::map<int,double> *highlight_radii = NULL,
80  int confId=-1 );
81 
82  // transform a set of coords in the molecule's coordinate system
83  // to drawing system coordinates and vice versa. Note that the coordinates have
84  // the origin in the top left corner, which is how Qt and Cairo have it, no
85  // doubt a holdover from X Windows. This means that a higher y value will be
86  // nearer the bottom of the screen. This doesn't really matter except when
87  // doing text superscripts and subscripts.
88  virtual Point2D getDrawCoords( const Point2D &mol_cds ) const;
89  virtual Point2D getDrawCoords( int at_num ) const;
90  virtual Point2D getAtomCoords( const std::pair<int,int> &screen_cds ) const;
91  virtual Point2D getAtomCoords( int at_num ) const;
92 
93 
94  virtual int width() const { return width_; }
95  virtual int height() const { return height_; }
96 
97  virtual double scale() const { return scale_; }
98  virtual void calculateScale();
99 
100  virtual double fontSize() const { return font_size_; }
101  // set font size in molecule coordinate units. That's probably Angstrom for
102  // RDKit.
103  virtual void setFontSize( double new_size );
104 
105  virtual void setColour( const DrawColour &col ) { curr_colour_ = col; }
106  virtual DrawColour colour() const { return curr_colour_; }
107 
108  virtual void setDash( const DashPattern &patt ) { curr_dash_ = patt; }
109  virtual const DashPattern &dash() const { return curr_dash_; }
110 
111  virtual void setLineWidth( int width ) { curr_width_ = width; }
112  virtual const int lineWidth() const { return curr_width_; }
113 
114  // establishes whether to put string draw mode into super- or sub-script
115  // mode based on contents of instring from i onwards. Increments i appropriately
116  // and returns true or false depending on whether it did something or not
117  bool setStringDrawMode( const std::string &instring , int &draw_mode ,
118  int &i ) const;
119 
120  virtual void clearDrawing() = 0;
121 
122  virtual void drawLine( const Point2D &cds1 ,
123  const Point2D &cds2 ) = 0;
124 
125  // using the current scale, work out the size of the label in molecule coordinates.
126  // Bear in mind when implementing this, that, for example, NH2 will appear as
127  // NH<sub>2</sub> to convey that the 2 is a subscript, and this needs to accounted
128  // for in the width and height.
129  virtual void getStringSize( const std::string &label , double &label_width ,
130  double &label_height ) const = 0;
131  // drawString centres the string on cds.
132  virtual void drawString( const std::string &str ,
133  const Point2D &cds );
134 
135  // draw polygons:
136  virtual void drawPolygon( const std::vector<Point2D > &cds ) = 0;
137  virtual void drawTriangle( const Point2D &cds1 ,
138  const Point2D &cds2 ,
139  const Point2D &cds3 );
140  virtual void drawEllipse( const Point2D &cds1 ,
141  const Point2D &cds2 );
142  virtual void drawRect( const Point2D &cds1 ,
143  const Point2D &cds2 );
144  virtual void drawAttachmentLine( const Point2D &cds1 ,
145  const Point2D &cds2,
146  const DrawColour &col,
147  double len=1.0,
148  unsigned int nSegments=8 );
149 
150 
151  virtual void tagAtoms( const ROMol &mol ) {};
152 
153  virtual bool fillPolys() const { return fill_polys_; }
154  virtual void setFillPolys(bool val) { fill_polys_ = val; }
155 
156  MolDrawOptions &drawOptions() { return options_; }
157  const MolDrawOptions &drawOptions() const { return options_; }
158 
159  const std::vector<Point2D > & atomCoords() const { return at_cds_; };
160  const std::vector<std::pair<std::string,OrientType> > & atomSyms() const { return atom_syms_; };
161  private :
162  int width_ , height_;
163  double scale_;
164  double x_min_ , y_min_ , x_range_ , y_range_;
165  double x_trans_ , y_trans_;
166  // font_size_ in molecule coordinate units. Default 0.5 (a bit bigger
167  // than the default width of a double bond)
168  double font_size_;
169  int curr_width_;
170  bool fill_polys_;
171  DrawColour curr_colour_;
172  DashPattern curr_dash_;
173  MolDrawOptions options_;
174 
175  std::vector<Point2D > at_cds_; // from mol
176  std::vector<int> atomic_nums_;
177  std::vector<std::pair<std::string,OrientType> > atom_syms_;
178 
179  // draw the char, with the bottom left hand corner at cds
180  virtual void drawChar( char c , const Point2D &cds ) = 0;
181 
182  // return a DrawColour based on the contents of highlight_atoms or
183  // highlight_map, falling back to atomic number by default
184  DrawColour getColour( int atom_idx ,
185  const std::vector<int> *highlight_atoms=NULL ,
186  const std::map<int,DrawColour> *highlight_map=NULL );
187  DrawColour getColourByAtomicNum( int atomic_num );
188 
189  void extractAtomCoords( const ROMol &mol,int confId );
190  void extractAtomSymbols( const ROMol &mol );
191 
192  virtual void drawLine( const Point2D &cds1 ,
193  const Point2D &cds2 ,
194  const DrawColour &col1 ,
195  const DrawColour &col2 );
196  void drawBond( const ROMol &mol , const BOND_SPTR &bond ,
197  int at1_idx , int at2_idx ,
198  const std::vector<int> *highlight_atoms=NULL ,
199  const std::map<int,DrawColour> *highlight_atom_map=NULL,
200  const std::vector<int> *highlight_bonds=NULL ,
201  const std::map<int,DrawColour> *highlight_bond_map=NULL );
202  void drawWedgedBond( const Point2D &cds1 ,
203  const Point2D &cds2 ,
204  bool draw_dashed , const DrawColour &col1 ,
205  const DrawColour &col2 );
206  void drawAtomLabel( int atom_num ,
207  const std::vector<int> *highlight_atoms=NULL ,
208  const std::map<int,DrawColour> *highlight_map=NULL );
209 
210  // calculate normalised perpendicular to vector between two coords
211  Point2D calcPerpendicular( const Point2D &cds1 ,
212  const Point2D &cds2 );
213  // cds1 and cds2 are 2 atoms in a ring. Returns the perpendicular pointing into
214  // the ring.
215  Point2D bondInsideRing( const ROMol &mol , const BOND_SPTR &bond ,
216  const Point2D &cds1 ,
217  const Point2D &cds2 );
218  // cds1 and cds2 are 2 atoms in a chain double bond. Returns the perpendicular
219  // pointing into the inside of the bond
220  Point2D bondInsideDoubleBond( const ROMol &mol , const BOND_SPTR &bond );
221  // calculate normalised perpendicular to vector between two coords, such that
222  // it's inside the angle made between (1 and 2) and (2 and 3).
223  Point2D calcInnerPerpendicular( const Point2D &cds1 ,
224  const Point2D &cds2 ,
225  const Point2D &cds3 );
226 
227  // take the coords for atnum, with neighbour nbr_cds, and move cds out to accommodate
228  // the label associated with it.
229  void adjustBondEndForLabel( int atnum , const Point2D &nbr_cds ,
230  Point2D &cds ) const;
231 
232  // adds LaTeX-like annotation for super- and sub-script.
233  std::pair<std::string,OrientType> getAtomSymbolAndOrientation( const Atom &atom ,
234  const Point2D &nbr_sum );
235 
236  virtual void doContinuousHighlighting( const ROMol &mol ,
237  const std::vector<int> *highlight_atoms,
238  const std::vector<int> *highlight_bonds,
239  const std::map<int,DrawColour> *highlight_atom_map,
240  const std::map<int,DrawColour> *highlight_bond_map,
241  const std::map<int,double> *highlight_radii );
242 
243  virtual void highlightCloseContacts();
244 
245  };
246 
247 }
248 
249 #endif // RDKITMOLDRAW2D_H
virtual void setColour(const DrawColour &col)
Definition: MolDraw2D.h:105
virtual void setLineWidth(int width)
Definition: MolDraw2D.h:111
boost::shared_ptr< Bond > BOND_SPTR
Definition: ROMol.h:38
virtual int height() const
Definition: MolDraw2D.h:95
virtual DrawColour colour() const
Definition: MolDraw2D.h:106
std::vector< unsigned int > DashPattern
Definition: MolDraw2D.h:38
virtual void setDash(const DashPattern &patt)
Definition: MolDraw2D.h:108
virtual void setFillPolys(bool val)
Definition: MolDraw2D.h:154
boost::tuple< float, float, float > DrawColour
Definition: MolDraw2D.h:37
const std::vector< std::pair< std::string, OrientType > > & atomSyms() const
Definition: MolDraw2D.h:160
virtual const DashPattern & dash() const
Definition: MolDraw2D.h:109
pulls in the core RDKit functionality
ROMol is a molecule class that is intended to have a fixed topology.
Definition: ROMol.h:105
const std::vector< Point2D > & atomCoords() const
Definition: MolDraw2D.h:159
MolDrawOptions & drawOptions()
Definition: MolDraw2D.h:156
std::map< int, std::string > atomLabels
Definition: MolDraw2D.h:47
virtual bool fillPolys() const
Definition: MolDraw2D.h:153
const MolDrawOptions & drawOptions() const
Definition: MolDraw2D.h:157
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:28
virtual ~MolDraw2D()
Definition: MolDraw2D.h:66
std::pair< std::string, OrientType > getAtomSymbolAndOrientation(const Atom &atom, RDGeom::Point2D nbrSum)
Definition: MolDrawing.h:73
virtual int width() const
Definition: MolDraw2D.h:94
virtual double scale() const
Definition: MolDraw2D.h:97
virtual const int lineWidth() const
Definition: MolDraw2D.h:112
std::vector< std::vector< int > > atomRegions
Definition: MolDraw2D.h:48
DrawColour highlightColour
Definition: MolDraw2D.h:43
virtual double fontSize() const
Definition: MolDraw2D.h:100
The class for representing atoms.
Definition: Atom.h:67
void drawLine(std::vector< ElementType > &res, int atnum1, int atnum2, int lineWidth, int dashed, double x1, double y1, double x2, double y2)
Definition: MolDrawing.h:57
virtual void tagAtoms(const ROMol &mol)
Definition: MolDraw2D.h:151