RDKit
Open-source cheminformatics and machine learning.
MolWriters.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2002-2017 Greg Landrum, Rational Discovery LLC
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 _RD_MOLWRITERS_H_
13 #define _RD_MOLWRITERS_H_
14 
15 #include <RDGeneral/types.h>
16 
17 #include <string>
18 #include <iostream>
19 #include <GraphMol/ROMol.h>
20 
21 namespace RDKit {
22 
23 static int defaultConfId = -1;
25  public:
26  virtual ~MolWriter() {}
27  virtual void write(const ROMol &mol, int confId = defaultConfId) = 0;
28  virtual void flush() = 0;
29  virtual void close() = 0;
30  virtual void setProps(const STR_VECT &propNames) = 0;
31  virtual unsigned int numMols() const = 0;
32 };
33 
34 //! The SmilesWriter is for writing molecules and properties to
35 //! delimited text files.
37  /******************************************************************************
38  * A Smiles Table writer - this is how it is used
39  * - create a SmilesWriter with a output file name (or a ostream), a
40  *delimiter,
41  * and a list of properties that need to be written out
42  * - then a call is made to the write function for each molecule that needs
43  *to
44  * be written out
45  ******************************************************************************/
46  public:
47  /*!
48  \param fileName : filename to write to ("-" to write to stdout)
49  \param delimiter : delimiter to use in the text file
50  \param nameHeader : used to label the name column in the output. If this
51  is provided as the empty string, no names will be
52  written.
53  \param includeHeader : toggles inclusion of a header line in the output
54  \param isomericSmiles : toggles generation of isomeric SMILES
55  \param kekuleSmiles : toggles the generation of kekule SMILES
56 
57  */
58  SmilesWriter(const std::string &fileName, const std::string &delimiter = " ",
59  const std::string &nameHeader = "Name",
60  bool includeHeader = true, bool isomericSmiles = true,
61  bool kekuleSmiles = false);
62  //! \overload
63  SmilesWriter(std::ostream *outStream, std::string delimiter = " ",
64  std::string nameHeader = "Name", bool includeHeader = true,
65  bool takeOwnership = false, bool isomericSmiles = true,
66  bool kekuleSmiles = false);
67 
68  ~SmilesWriter() override;
69 
70  //! \brief set a vector of property names that are need to be
71  //! written out for each molecule
72  void setProps(const STR_VECT &propNames) override;
73 
74  //! \brief write a new molecule to the file
75  void write(const ROMol &mol, int confId = defaultConfId) override;
76 
77  //! \brief flush the ostream
78  void flush() override {
79  PRECONDITION(dp_ostream, "no output stream");
80  try {
81  dp_ostream->flush();
82  } catch (...) {
83  try {
84  if (dp_ostream->good()) dp_ostream->setstate(std::ios::badbit);
85  } catch (const std::runtime_error &) {
86  }
87  }
88  }
89 
90  //! \brief close our stream (the writer cannot be used again)
91  void close() override {
92  if (dp_ostream) {
93  flush();
94  }
95  if (df_owner) {
96  delete dp_ostream;
97  df_owner = false;
98  }
99  dp_ostream = nullptr;
100  }
101 
102  //! \brief get the number of molecules written so far
103  unsigned int numMols() const override { return d_molid; }
104 
105  private:
106  // local initialization
107  void init(const std::string &delimiter, const std::string &nameHeader,
108  bool includeHeader, bool isomericSmiles, bool kekuleSmiles);
109 
110  // dumps a header line to the output stream
111  void dumpHeader() const;
112 
113  std::ostream *dp_ostream;
114  bool df_owner;
115  bool df_includeHeader; // whether or not to include a title line
116  unsigned int d_molid; // the number of the molecules we wrote so far
117  std::string d_delim; // delimiter string between various records
118  std::string d_nameHeader; // header for the name column in the output file
119  STR_VECT d_props; // list of property name that need to be written out
120  bool df_isomericSmiles; // whether or not to do isomeric smiles
121  bool df_kekuleSmiles; // whether or not to do kekule smiles
122 };
123 
124 //! The SDWriter is for writing molecules and properties to
125 //! SD files
127  /**************************************************************************************
128  * A SD file ( or stream) writer - this is how it is used
129  * - create a SDMolWriter with a output file name (or a ostream),
130  * and a list of properties that need to be written out
131  * - then a call is made to the write function for each molecule that needs
132  *to be written out
133  **********************************************************************************************/
134  public:
135  /*!
136  \param fileName : filename to write to ("-" to write to stdout)
137  */
138  SDWriter(const std::string &fileName);
139  SDWriter(std::ostream *outStream, bool takeOwnership = false);
140 
141  ~SDWriter() override;
142 
143  //! \brief set a vector of property names that are need to be
144  //! written out for each molecule
145  void setProps(const STR_VECT &propNames) override;
146 
147  //! \brief return the text that would be written to the file
148  static std::string getText(const ROMol &mol, int confId = defaultConfId,
149  bool kekulize = true, bool force_V3000 = false,
150  int molid = -1, STR_VECT *propNames = nullptr);
151 
152  //! \brief write a new molecule to the file
153  void write(const ROMol &mol, int confId = defaultConfId) override;
154 
155  //! \brief flush the ostream
156  void flush() override {
157  PRECONDITION(dp_ostream, "no output stream");
158  try {
159  dp_ostream->flush();
160  } catch (...) {
161  try {
162  if (dp_ostream->good()) dp_ostream->setstate(std::ios::badbit);
163  } catch (const std::runtime_error &) {
164  }
165  }
166  }
167 
168  //! \brief close our stream (the writer cannot be used again)
169  void close() override {
170  if (dp_ostream) {
171  flush();
172  }
173  if (df_owner) {
174  delete dp_ostream;
175  df_owner = false;
176  }
177  dp_ostream = nullptr;
178  }
179 
180  //! \brief get the number of molecules written so far
181  unsigned int numMols() const override { return d_molid; }
182 
183  void setForceV3000(bool val) { df_forceV3000 = val; }
184  bool getForceV3000() const { return df_forceV3000; }
185 
186  void setKekulize(bool val) { df_kekulize = val; }
187  bool getKekulize() const { return df_kekulize; }
188 
189  private:
190  void writeProperty(const ROMol &mol, const std::string &name);
191 
192  std::ostream *dp_ostream;
193  bool df_owner;
194  unsigned int d_molid; // the number of the molecules we wrote so far
195  STR_VECT d_props; // list of property name that need to be written out
196  bool df_forceV3000; // force writing the mol blocks as V3000
197  bool df_kekulize; // toggle kekulization of molecules on writing
198 };
199 
200 //! The TDTWriter is for writing molecules and properties to
201 //! TDT files
203  /**************************************************************************************
204  * A TDT file ( or stream) writer - this is how it is used
205  * - create a TDTWriter with a output file name (or a ostream),
206  * and a list of properties that need to be written out
207  * - then a call is made to the write function for each molecule that needs
208  *to be written out
209  **********************************************************************************************/
210  public:
211  /*!
212  \param fileName : filename to write to ("-" to write to stdout)
213  */
214  TDTWriter(const std::string &fileName);
215  TDTWriter(std::ostream *outStream, bool takeOwnership = false);
216 
217  ~TDTWriter() override;
218 
219  //! \brief set a vector of property names that are need to be
220  //! written out for each molecule
221  void setProps(const STR_VECT &propNames) override;
222 
223  //! \brief write a new molecule to the file
224  void write(const ROMol &mol, int confId = defaultConfId) override;
225 
226  //! \brief flush the ostream
227  void flush() override {
228  PRECONDITION(dp_ostream, "no output stream");
229  try {
230  dp_ostream->flush();
231  } catch (...) {
232  try {
233  if (dp_ostream->good()) dp_ostream->setstate(std::ios::badbit);
234  } catch (const std::runtime_error &) {
235  }
236  }
237  }
238 
239  //! \brief close our stream (the writer cannot be used again)
240  void close() override {
241  if (dp_ostream) {
242  // if we've written any mols, finish with a "|" line
243  if (d_molid > 0) {
244  *dp_ostream << "|\n";
245  }
246  flush();
247  }
248  if (df_owner) {
249  delete dp_ostream;
250  df_owner = false;
251  }
252  dp_ostream = nullptr;
253  }
254 
255  //! \brief get the number of molecules written so far
256  unsigned int numMols() const override { return d_molid; }
257 
258  void setWrite2D(bool state = true) { df_write2D = state; }
259  bool getWrite2D() const { return df_write2D; }
260 
261  void setWriteNames(bool state = true) { df_writeNames = state; }
262  bool getWriteNames() const { return df_writeNames; }
263 
264  void setNumDigits(unsigned int numDigits) { d_numDigits = numDigits; }
265  unsigned int getNumDigits() const { return d_numDigits; }
266 
267  private:
268  void writeProperty(const ROMol &mol, const std::string &name);
269 
270  std::ostream *dp_ostream;
271  bool df_owner;
272  unsigned int d_molid; // the number of molecules we wrote so far
273  STR_VECT d_props; // list of property name that need to be written out
274  bool df_write2D; // write 2D coordinates instead of 3D
275  bool df_writeNames; // write a name record for each molecule
276  unsigned int
277  d_numDigits; // number of digits to use in our output of coordinates;
278 };
279 
280 //! The PDBWriter is for writing molecules to Brookhaven Protein
281 //! DataBank format files.
283  public:
284  PDBWriter(const std::string &fileName, unsigned int flavor = 0);
285  PDBWriter(std::ostream *outStream, bool takeOwnership = false,
286  unsigned int flavor = 0);
287  ~PDBWriter() override;
288 
289  //! \brief write a new molecule to the file
290  void write(const ROMol &mol, int confId = defaultConfId) override;
291 
292  void setProps(const STR_VECT &) override {}
293 
294  //! \brief flush the ostream
295  void flush() override {
296  PRECONDITION(dp_ostream, "no output stream");
297  try {
298  dp_ostream->flush();
299  } catch (...) {
300  try {
301  if (dp_ostream->good()) dp_ostream->setstate(std::ios::badbit);
302  } catch (const std::runtime_error &) {
303  }
304  }
305  }
306 
307  //! \brief close our stream (the writer cannot be used again)
308  void close() override {
309  if (dp_ostream) {
310  flush();
311  }
312  if (df_owner) {
313  delete dp_ostream;
314  df_owner = false;
315  }
316  dp_ostream = nullptr;
317  }
318 
319  //! \brief get the number of molecules written so far
320  unsigned int numMols() const override { return d_count; }
321 
322  private:
323  std::ostream *dp_ostream;
324  unsigned int d_flavor;
325  unsigned int d_count;
326  bool df_owner;
327 };
328 } // namespace RDKit
329 
330 #endif
#define PRECONDITION(expr, mess)
Definition: Invariant.h:109
Defines the primary molecule class ROMol as well as associated typedefs.
virtual void flush()=0
virtual void write(const ROMol &mol, int confId=defaultConfId)=0
virtual ~MolWriter()
Definition: MolWriters.h:26
virtual void close()=0
virtual void setProps(const STR_VECT &propNames)=0
virtual unsigned int numMols() const =0
PDBWriter(const std::string &fileName, unsigned int flavor=0)
void write(const ROMol &mol, int confId=defaultConfId) override
write a new molecule to the file
void flush() override
flush the ostream
Definition: MolWriters.h:295
PDBWriter(std::ostream *outStream, bool takeOwnership=false, unsigned int flavor=0)
void setProps(const STR_VECT &) override
Definition: MolWriters.h:292
~PDBWriter() override
unsigned int numMols() const override
get the number of molecules written so far
Definition: MolWriters.h:320
void close() override
close our stream (the writer cannot be used again)
Definition: MolWriters.h:308
~SDWriter() override
bool getForceV3000() const
Definition: MolWriters.h:184
unsigned int numMols() const override
get the number of molecules written so far
Definition: MolWriters.h:181
SDWriter(std::ostream *outStream, bool takeOwnership=false)
bool getKekulize() const
Definition: MolWriters.h:187
void flush() override
flush the ostream
Definition: MolWriters.h:156
void setProps(const STR_VECT &propNames) override
set a vector of property names that are need to be written out for each molecule
void write(const ROMol &mol, int confId=defaultConfId) override
write a new molecule to the file
static std::string getText(const ROMol &mol, int confId=defaultConfId, bool kekulize=true, bool force_V3000=false, int molid=-1, STR_VECT *propNames=nullptr)
return the text that would be written to the file
void close() override
close our stream (the writer cannot be used again)
Definition: MolWriters.h:169
void setForceV3000(bool val)
Definition: MolWriters.h:183
SDWriter(const std::string &fileName)
void setKekulize(bool val)
Definition: MolWriters.h:186
unsigned int numMols() const override
get the number of molecules written so far
Definition: MolWriters.h:103
SmilesWriter(const std::string &fileName, const std::string &delimiter=" ", const std::string &nameHeader="Name", bool includeHeader=true, bool isomericSmiles=true, bool kekuleSmiles=false)
void write(const ROMol &mol, int confId=defaultConfId) override
write a new molecule to the file
~SmilesWriter() override
SmilesWriter(std::ostream *outStream, std::string delimiter=" ", std::string nameHeader="Name", bool includeHeader=true, bool takeOwnership=false, bool isomericSmiles=true, bool kekuleSmiles=false)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void setProps(const STR_VECT &propNames) override
set a vector of property names that are need to be written out for each molecule
void close() override
close our stream (the writer cannot be used again)
Definition: MolWriters.h:91
void flush() override
flush the ostream
Definition: MolWriters.h:78
~TDTWriter() override
bool getWrite2D() const
Definition: MolWriters.h:259
void setNumDigits(unsigned int numDigits)
Definition: MolWriters.h:264
void setWrite2D(bool state=true)
Definition: MolWriters.h:258
void setProps(const STR_VECT &propNames) override
set a vector of property names that are need to be written out for each molecule
unsigned int numMols() const override
get the number of molecules written so far
Definition: MolWriters.h:256
unsigned int getNumDigits() const
Definition: MolWriters.h:265
TDTWriter(std::ostream *outStream, bool takeOwnership=false)
void close() override
close our stream (the writer cannot be used again)
Definition: MolWriters.h:240
TDTWriter(const std::string &fileName)
void write(const ROMol &mol, int confId=defaultConfId) override
write a new molecule to the file
void setWriteNames(bool state=true)
Definition: MolWriters.h:261
void flush() override
flush the ostream
Definition: MolWriters.h:227
bool getWriteNames() const
Definition: MolWriters.h:262
#define RDKIT_FILEPARSERS_EXPORT
Definition: export.h:153
Std stuff.
Definition: Abbreviations.h:18
std::vector< std::string > STR_VECT
Definition: Dict.h:29
static int defaultConfId
Definition: MolWriters.h:23