RDKit
Open-source cheminformatics and machine learning.
MolDraw2DSVG.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2015 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 // derived from Dave Cosgrove's MolDraw2D
11 //
12 // This is a concrete class derived from MolDraw2D that uses RDKit to draw a
13 // molecule into an SVG file
14 
15 #include <RDGeneral/export.h>
16 #ifndef MOLDRAW2DSVG_H
17 #define MOLDRAW2DSVG_H
18 
19 #include <iostream>
20 #include <sstream>
21 #include "MolDraw2D.h"
22 
23 // ****************************************************************************
24 
25 namespace RDKit {
26 
28  public:
29  // initialize to use a particular ostream
30  MolDraw2DSVG(int width, int height, std::ostream &os, int panelWidth = -1,
31  int panelHeight = -1)
32  : MolDraw2D(width, height, panelWidth, panelHeight), d_os(os) {
33  initDrawing();
34  };
35  // initialize to use the internal stringstream
36  MolDraw2DSVG(int width, int height, int panelWidth = -1, int panelHeight = -1)
37  : MolDraw2D(width, height, panelWidth, panelHeight), d_os(d_ss) {
38  initDrawing();
39  };
40 
41  void setColour(const DrawColour &col) override;
42 
43  // not sure if this goes here or if we should do a dtor since initDrawing() is
44  // called in the ctor,
45  // but we'll start here
46  void finishDrawing();
47 
48  void drawLine(const Point2D &cds1, const Point2D &cds2) override;
49  void drawString(const std::string &str, const Point2D &cds) override;
50  void drawString(const std::string &str, const Point2D &cds,
51  AlignType align) override;
52  void alignString(const std::string &str,
53  const std::string &align_char, int align,
54  const Point2D &in_cds, Point2D &out_cds) const override;
55 
56  void drawPolygon(const std::vector<Point2D> &cds) override;
57  void drawEllipse(const Point2D &cds1, const Point2D &cds2) override;
58  void clearDrawing() override;
59 
60  void drawWavyLine(const Point2D &cds1, const Point2D &cds2,
61  const DrawColour &col1, const DrawColour &col2,
62  unsigned int nSegments = 16,
63  double vertOffset = 0.05) override;
64 
65  // using the current scale, work out the size of the label in molecule
66  // coordinates
67  void getStringSize(const std::string &label, double &label_width,
68  double &label_height) const override;
69 
70  // this only makes sense if the object was initialized without a stream
71  std::string getDrawingText() const { return d_ss.str(); };
72 
73  // adds additional tags to the atoms and bonds in the SVG. This should be
74  // invoked *after* the molecule has been drawn
75  using MolDraw2D::tagAtoms; // Avoid overload warning.
76  void tagAtoms(const ROMol &mol) override { tagAtoms(mol, 0.2); }
77  void tagAtoms(const ROMol &mol, double radius,
78  const std::map<std::string, std::string> &events = {});
79 
80  // adds metadata describing the molecule to the SVG. This allows
81  // molecules to be re-built from SVG with MolFromSVG
82  void addMoleculeMetadata(const ROMol &mol, int confId = -1) const;
83  void addMoleculeMetadata(const std::vector<ROMol *> &mols,
84  const std::vector<int> confIds = {}) const;
85 
86  private:
87  std::ostream &d_os;
88  std::stringstream d_ss;
89  std::string d_activeClass;
90 
91  void drawChar(char c, const Point2D &cds) override;
92  void initDrawing();
93 
94  protected:
95  void drawBond(
96  const ROMol &mol, const Bond *bond, int at1_idx, int at2_idx,
97  const std::vector<int> *highlight_atoms = nullptr,
98  const std::map<int, DrawColour> *highlight_atom_map = nullptr,
99  const std::vector<int> *highlight_bonds = nullptr,
100  const std::map<int, DrawColour> *highlight_bond_map = nullptr,
101  const std::vector<std::pair<DrawColour, DrawColour> > *bond_colours = nullptr) override;
102 };
103 } // namespace RDKit
104 #endif // MOLDRAW2DSVG_H
RDKIT_MOLDRAW2D_EXPORT
#define RDKIT_MOLDRAW2D_EXPORT
Definition: export.h:398
RDKit::MolDraw2DSVG::MolDraw2DSVG
MolDraw2DSVG(int width, int height, int panelWidth=-1, int panelHeight=-1)
Definition: MolDraw2DSVG.h:36
RDKit::MolDraw2DSVG::addMoleculeMetadata
void addMoleculeMetadata(const ROMol &mol, int confId=-1) const
RDKit::Bond
class for representing a bond
Definition: Bond.h:47
RDKit::MolDraw2DSVG::drawBond
void drawBond(const ROMol &mol, const Bond *bond, int at1_idx, int at2_idx, const std::vector< int > *highlight_atoms=nullptr, const std::map< int, DrawColour > *highlight_atom_map=nullptr, const std::vector< int > *highlight_bonds=nullptr, const std::map< int, DrawColour > *highlight_bond_map=nullptr, const std::vector< std::pair< DrawColour, DrawColour > > *bond_colours=nullptr) override
RDKit::MolDraw2D::tagAtoms
virtual void tagAtoms(const ROMol &mol)
Definition: MolDraw2D.h:543
RDKit::MolDraw2DSVG::getDrawingText
std::string getDrawingText() const
Definition: MolDraw2DSVG.h:71
MolDraw2D.h
RDKit::MolDraw2DSVG::finishDrawing
void finishDrawing()
RDKit::DrawColour
Definition: MolDraw2D.h:37
RDKit::ROMol
Definition: ROMol.h:171
RDKit::MolDraw2DSVG::drawPolygon
void drawPolygon(const std::vector< Point2D > &cds) override
RDKit::MolDraw2DSVG::tagAtoms
void tagAtoms(const ROMol &mol) override
Definition: MolDraw2DSVG.h:76
RDKit::MolDraw2DSVG::clearDrawing
void clearDrawing() override
clears the contents of the drawing
RDKit::MolDraw2DSVG::addMoleculeMetadata
void addMoleculeMetadata(const std::vector< ROMol * > &mols, const std::vector< int > confIds={}) const
RDKit::MolDraw2DSVG::MolDraw2DSVG
MolDraw2DSVG(int width, int height, std::ostream &os, int panelWidth=-1, int panelHeight=-1)
Definition: MolDraw2DSVG.h:30
RDKit
Std stuff.
Definition: Atom.h:30
RDKit::MolDraw2DSVG::drawEllipse
void drawEllipse(const Point2D &cds1, const Point2D &cds2) override
draw an ellipse
RDKit::MolDraw2D
MolDraw2D is the base class for doing 2D renderings of molecules.
Definition: MolDraw2D.h:214
RDKit::MolDraw2DSVG
Definition: MolDraw2DSVG.h:27
RDGeom::Point2D
Definition: point.h:258
RDKit::MolDraw2DSVG::drawString
void drawString(const std::string &str, const Point2D &cds, AlignType align) override
RDKit::MolDraw2DSVG::drawWavyLine
void drawWavyLine(const Point2D &cds1, const Point2D &cds2, const DrawColour &col1, const DrawColour &col2, unsigned int nSegments=16, double vertOffset=0.05) override
draw a wavy line like that used to indicate unknown stereochemistry
RDKit::MolDraw2DSVG::getStringSize
void getStringSize(const std::string &label, double &label_width, double &label_height) const override
RDKit::MolDraw2DSVG::setColour
void setColour(const DrawColour &col) override
sets the current draw color
RDKit::MolDraw2DSVG::alignString
void alignString(const std::string &str, const std::string &align_char, int align, const Point2D &in_cds, Point2D &out_cds) const override
RDKit::MolDraw2DSVG::tagAtoms
void tagAtoms(const ROMol &mol, double radius, const std::map< std::string, std::string > &events={})
RDKit::MolDraw2DSVG::drawLine
void drawLine(const Point2D &cds1, const Point2D &cds2) override
draws a line from cds1 to cds2 using the current drawing style
RDKit::MolDraw2DSVG::drawString
void drawString(const std::string &str, const Point2D &cds) override
drawString centres the string on cds.
RDKit::MolDraw2D::AlignType
AlignType
Definition: MolDraw2D.h:218
export.h