Coverage Report

Created: 2026-09-28 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rdkit/Code/GraphMol/FileParsers/FileWriters.h
Line
Count
Source
1
//
2
//  Copyright (C) 2002-2024 Greg Landrum 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
#include <RDGeneral/export.h>
11
#ifndef RD_FILEWRITERS_H
12
#define RD_FILEWRITERS_H
13
14
#include <RDGeneral/types.h>
15
#include <GraphMol/RDKitBase.h>
16
#include <string>
17
18
namespace RDKit {
19
20
struct RDKIT_FILEPARSERS_EXPORT MolWriterParams {
21
  bool includeStereo = true;  /**< toggles inclusion of stereochemistry
22
                                   information*/
23
  bool kekulize = true;       /**< triggers kekulization of the molecule before
24
                                   it is written*/
25
  bool forceV3000 = false;    /**< force generation a V3000 mol block (happens
26
                                   automatically with more than 999 atoms or
27
                                   bond or if the magnitude of the coordinates
28
                                   are too large)*/
29
  unsigned int precision = 6; /**< precision of coordinates (only available in
30
                                   V3000)*/
31
};
32
33
// \brief generates an MDL mol block for a molecule
34
/*!
35
 *   \param mol             - the molecule in question
36
 *   \param MolWriterParams - parmeter struct with write options
37
 *   \param confId          - selects the conformer to be used
38
 *                            (default=-1 - find first in mol)
39
 */
40
RDKIT_FILEPARSERS_EXPORT std::string MolToMolBlock(
41
    const ROMol &mol, const MolWriterParams &params, int confId = -1);
42
43
// \brief generates an MDL mol block for a molecule
44
/*!
45
 *   \param mol           - the molecule in question
46
 *   \param includeStereo - toggles inclusion of stereochemistry information
47
 *                          (default=true)
48
 *   \param confId        - selects the conformer to be used
49
 *                          (default=-1 - find first in mol)
50
 *   \param kekulize      - triggers kekulization
51
 *                          of the molecule before it is written (default=true)
52
 *   \param forceV3000    - force generation a V3000 mol block (happens
53
 *                          automatically with more than 999 atoms or
54
 *                          bonds)(default=false)
55
 */
56
inline std::string MolToMolBlock(const ROMol &mol, bool includeStereo = true,
57
                                 int confId = -1, bool kekulize = true,
58
0
                                 bool forceV3000 = false) {
59
0
  MolWriterParams params{includeStereo, kekulize, forceV3000};
60
0
  return MolToMolBlock(mol, params, confId);
61
0
}
62
63
// \brief generates an MDL v3000 mol block for a molecule
64
/*!
65
 *   \param mol             - the molecule in question
66
 *   \param MolWriterParams - parameter struct with write options
67
 *   \param confId          - selects the conformer to be used
68
 *                            (default=-1 - find first in mol)
69
 */
70
inline std::string MolToV3KMolBlock(const ROMol &mol,
71
                                    const MolWriterParams &params,
72
0
                                    int confId = -1) {
73
0
  // have to set forceV300, prefer copy over mutable params argument
74
0
  MolWriterParams v3KParams{params};
75
0
  v3KParams.forceV3000 = true;
76
0
  return MolToMolBlock(mol, v3KParams, confId);
77
0
}
78
79
// \brief generates an MDL v3000 mol block for a molecule
80
/*!
81
 *   \param mol           - the molecule in question
82
 *   \param includeStereo - toggles inclusion of stereochemistry information
83
 *   \param confId        - selects the conformer to be used
84
 *                          (default=-1 - find first in mol)
85
 *   \param kekulize      - triggers kekulization of the molecule before it is
86
 *                        - written
87
 */
88
inline std::string MolToV3KMolBlock(const ROMol &mol, bool includeStereo = true,
89
0
                                    int confId = -1, bool kekulize = true) {
90
0
  MolWriterParams params{includeStereo, kekulize, true};
91
0
  return MolToMolBlock(mol, params, confId);
92
0
}
93
94
// \brief generates an MDL v2000 mol block for a molecule
95
/*!
96
 *   \param mol             - the molecule in question
97
 *   \param MolWriterParams - parameter struct with write options
98
 *   \param confId          - selects the conformer to be used
99
 *                            (default=-1 - find first in mol)
100
 *
101
 *  \note This function will throw a ValueError exception if the molecule has
102
 *   more than 999 atoms, bonds, or SGroups.
103
 */
104
RDKIT_FILEPARSERS_EXPORT std::string MolToV2KMolBlock(
105
    const ROMol &mol, const MolWriterParams &params = MolWriterParams(),
106
    int confId = -1);
107
108
// \brief Writes a molecule to an MDL mol file
109
/*!
110
 *   \param mol             - the molecule in question
111
 *   \param fName           - the name of the file to use
112
 *   \param MolWriterParams - parameter struct with write options
113
 *   \param confId          - selects the conformer to be used
114
 */
115
RDKIT_FILEPARSERS_EXPORT void MolToMolFile(const ROMol &mol,
116
                                           const std::string &fName,
117
                                           const MolWriterParams &params,
118
                                           int confId = -1);
119
120
// \brief Writes a molecule to an MDL mol file
121
/*!
122
 *   \param mol           - the molecule in question
123
 *   \param fName         - the name of the file to use
124
 *   \param includeStereo - toggles inclusion of stereochemistry information
125
 *   \param confId        - selects the conformer to be used
126
 *   \param kekulize      - triggers kekulization of the molecule before it is
127
 * written
128
 *   \param forceV3000    - force generation a V3000 mol block (happens
129
 * automatically with
130
 *                          more than 999 atoms or bonds)
131
 */
132
inline void MolToMolFile(const ROMol &mol, const std::string &fName,
133
                         bool includeStereo = true, int confId = -1,
134
0
                         bool kekulize = true, bool forceV3000 = false) {
135
0
  MolWriterParams params{includeStereo, kekulize, forceV3000};
136
0
  MolToMolFile(mol, fName, params, confId);
137
0
}
138
139
// \brief Writes a molecule to an MDL V3000 mol file
140
/*!
141
 *   \param mol             - the molecule in question
142
 *   \param fName           - the name of the file to use
143
 *   \param MolWriterParams - parameter struct with write options
144
 *   \param confId          - selects the conformer to be used
145
 */
146
inline void MolToV3KMolFile(const ROMol &mol, const std::string &fName,
147
0
                            const MolWriterParams &params, int confId = -1) {
148
0
  // have to set forceV300, prefer copy over mutable params argument
149
0
  MolWriterParams v3KParams{params};
150
0
  v3KParams.forceV3000 = true;
151
0
  MolToMolFile(mol, fName, v3KParams, confId);
152
0
}
153
154
// \brief Writes a molecule to an MDL V3000 mol file
155
/*!
156
 *   \param mol           - the molecule in question
157
 *   \param fName         - the name of the file to use
158
 *   \param includeStereo - toggles inclusion of stereochemistry information
159
 *   \param confId        - selects the conformer to be used
160
 *   \param kekulize      - triggers kekulization of the molecule before it is
161
 * written
162
 */
163
inline void MolToV3KMolFile(const ROMol &mol, const std::string &fName,
164
                            bool includeStereo = true, int confId = -1,
165
0
                            bool kekulize = true) {
166
0
  MolWriterParams params{includeStereo, kekulize, true};
167
0
  MolToMolFile(mol, fName, params, confId);
168
0
}
169
170
RDKIT_FILEPARSERS_EXPORT std::string MolToCMLBlock(const ROMol &mol,
171
                                                   int confId = -1,
172
                                                   bool kekulize = true);
173
174
RDKIT_FILEPARSERS_EXPORT void MolToCMLFile(const ROMol &mol,
175
                                           const std::string &fName,
176
                                           int confId = -1,
177
                                           bool kekulize = true);
178
179
// \brief Writes a molecule to an XYZ block
180
/*!
181
 *   \param mol       - the molecule in question
182
 *   \param confId    - selects which conformation to output
183
 *   \param precision - precision of the coordinates
184
 */
185
RDKIT_FILEPARSERS_EXPORT std::string MolToXYZBlock(const ROMol &mol,
186
                                                   int confId = -1,
187
                                                   unsigned int precision = 6);
188
189
// \brief Writes a molecule to an XYZ block
190
/*!
191
 *   \param mol       - the molecule in question
192
 *   \param fName     - the file to write to
193
 *   \param confId    - selects which conformation to output
194
 *   \param precision - precision of the coordinates
195
 */
196
RDKIT_FILEPARSERS_EXPORT void MolToXYZFile(const ROMol &mol,
197
                                           const std::string &fName,
198
                                           int confId = -1,
199
                                           unsigned int precision = 6);
200
201
RDKIT_FILEPARSERS_EXPORT std::string MolToTPLText(
202
    const ROMol &mol, const std::string &partialChargeProp = "_GasteigerCharge",
203
    bool writeFirstConfTwice = false);
204
RDKIT_FILEPARSERS_EXPORT void MolToTPLFile(
205
    const ROMol &mol, const std::string &fName,
206
    const std::string &partialChargeProp = "_GasteigerCharge",
207
    bool writeFirstConfTwice = false);
208
209
// \brief generates an PDB block for a molecule
210
/*!
211
 *   \param mol           - the molecule in question
212
 *   \param confId        - selects the conformer to be used
213
 *   \param flavor        - controls what gets written:
214
 *         flavor & 1 : Write MODEL/ENDMDL lines around each record
215
 *         flavor & 2 : Don't write single CONECT records
216
 *         flavor & 4 : Write CONECT records in both directions
217
 *         flavor & 8 : Don't use multiple CONECTs to encode bond order
218
 *         flavor & 16 : Write MASTER record
219
 *         flavor & 32 : Write TER record
220
 */
221
RDKIT_FILEPARSERS_EXPORT std::string MolToPDBBlock(const ROMol &mol,
222
                                                   int confId = -1,
223
                                                   unsigned int flavor = 0);
224
// \brief Writes a molecule to an MDL mol file
225
/*!
226
 *   \param mol           - the molecule in question
227
 *   \param fName         - the name of the file to use
228
 *   \param confId        - selects the conformer to be used
229
 *   \param flavor        - controls what gets written:
230
 *         flavor & 1 : Write MODEL/ENDMDL lines around each record
231
 *         flavor & 2 : Don't write single CONECT records
232
 *         flavor & 4 : Write CONECT records in both directions
233
 *         flavor & 8 : Don't use multiple CONECTs to encode bond order
234
 *         flavor & 16 : Write MASTER record
235
 *         flavor & 32 : Write TER record
236
 */
237
RDKIT_FILEPARSERS_EXPORT void MolToPDBFile(const ROMol &mol,
238
                                           const std::string &fname,
239
                                           int confId = -1,
240
                                           unsigned int flavor = 0);
241
242
}  // namespace RDKit
243
244
#endif