RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
ReactionParser.h
Go to the documentation of this file.
1//
2// Copyright (c) 2007-2024, Novartis Institutes for BioMedical Research Inc.
3// and other RDKit contributors
4//
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions are
9// met:
10//
11// * Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// * Redistributions in binary form must reproduce the above
14// copyright notice, this list of conditions and the following
15// disclaimer in the documentation and/or other materials provided
16// with the distribution.
17// * Neither the name of Novartis Institutes for BioMedical Research Inc.
18// nor the names of its contributors may be used to endorse or promote
19// products derived from this software without specific prior written
20// permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33//
34
35#include <RDGeneral/export.h>
36#ifndef RD_REACTIONPARSER_H_21Aug2006
37#define RD_REACTIONPARSER_H_21Aug2006
38
39#include <string>
40#include <iostream>
41#include <fstream>
42#include <map>
43#include <sstream>
44#include <utility>
45#include <boost/format.hpp>
50
51namespace RDKit {
52class ROMol;
53class ChemicalReaction;
54
55//! used to indicate an error in parsing reaction data
57 : public std::exception {
58 public:
59 //! construct with an error message
60 explicit ChemicalReactionParserException(const char *msg) : _msg(msg) {}
61 //! construct with an error message
62 explicit ChemicalReactionParserException(std::string msg)
63 : _msg(std::move(msg)) {}
64 //! get the error message
65 const char *what() const noexcept override { return _msg.c_str(); }
66 ~ChemicalReactionParserException() noexcept override = default;
67
68 private:
69 std::string _msg;
70};
71
72namespace v2 {
73namespace ReactionParser {
74struct RDKIT_CHEMREACTIONS_EXPORT ReactionSmartsParserParams {
75 bool sanitize = false; /**< sanitize the molecules after building them */
76 std::map<std::string, std::string>
77 replacements; /**< allows SMILES "macros" */
78 bool allowCXSMILES = true; /**< recognize and parse CXSMILES*/
79 bool strictCXSMILES =
80 true; /**< throw an exception if the CXSMILES parsing fails */
81};
82RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction> ReactionFromSmarts(
83 const std::string &smarts,
84 const ReactionSmartsParserParams &params = ReactionSmartsParserParams());
85
86RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction> ReactionFromSmiles(
87 const std::string &smarts,
88 const ReactionSmartsParserParams &params = ReactionSmartsParserParams());
89} // namespace ReactionParser
90} // namespace v2
91
92inline namespace v1 {
93//---------------------------------------------------------------------------
94//! \name Reaction SMARTS/SMILES Support
95//! @{
96
97//! Parse a string containing "Reaction SMARTS" into a ChemicalReaction
98/*!
99 Our definition of Reaction SMARTS is something that looks a lot like reaction
100 SMILES, except that SMARTS queries are allowed on the reactant side and that
101 atom-map numbers are required (at least for now)
102
103 \param text the SMARTS to convert
104
105 \param replacements a string->string map of replacement strings. \see
106 SmilesToMol for more information about replacements
107
108 \param useSmiles if set, the SMILES parser will be used instead of the
109 SMARTS parserfor the individual components
110
111 \param allowCXSMILES if set, any CXSMILES extensions present will be
112 parsed, otherwise it will be ignored
113 */
115 const std::string &text,
116 std::map<std::string, std::string> *replacements = nullptr,
117 bool useSmiles = false, bool allowCXSMILES = true) {
118 v2::ReactionParser::ReactionSmartsParserParams params;
119 if (replacements) {
120 params.replacements = *replacements;
121 }
122 params.allowCXSMILES = allowCXSMILES;
123 if (useSmiles) {
124 return v2::ReactionParser::ReactionFromSmiles(text, params).release();
125 } else {
126 return v2::ReactionParser::ReactionFromSmarts(text, params).release();
127 }
128}
129} // namespace v1
130//! returns the reaction SMARTS for a reaction
132 const ChemicalReaction &rxn, const SmilesWriteParams &params);
133//! \overload
134inline std::string ChemicalReactionToRxnSmarts(const ChemicalReaction &rxn) {
135 SmilesWriteParams params;
136 params.canonical = false;
137 return ChemicalReactionToRxnSmarts(rxn, params);
138}
139
140//! returns the reaction SMILES for a reaction
142 const ChemicalReaction &rxn,
143 const SmilesWriteParams &params = SmilesWriteParams());
144//! \overload
145inline std::string ChemicalReactionToRxnSmiles(const ChemicalReaction &rxn,
146 bool canonical) {
147 SmilesWriteParams params;
148 params.canonical = canonical;
149 return ChemicalReactionToRxnSmiles(rxn, params);
150}
151//! @}
152
153//---------------------------------------------------------------------------
154//! \name Reaction Mol Support
155//! @{
156
157//! Parse a ROMol into a ChemicalReaction, RXN role must be set before
158/*!
159 Alternative to build a reaction from a molecule (fragments) which have RXN
160 roles set as atom properties: common_properties::molRxnRole (1=reactant,
161 2=product, 3=agent)
162
163 \param mol ROMol with RXN roles set
164 */
166 const ROMol &mol);
167
168//! returns a ROMol with RXN roles used to describe the reaction
170 const ChemicalReaction &rxn);
171//! @}
172
173//---------------------------------------------------------------------------
174//! \name MDL rxn Support
175//! @{
176namespace v2 {
177namespace ReactionParser {
178
179RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction>
180ReactionFromRxnBlock(const std::string &rxnBlock,
183RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction>
184ReactionFromRxnFile(const std::string &fileName,
187RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction>
188ReactionFromRxnDataStream(std::istream &rxnStream, unsigned int &line,
191
192} // namespace ReactionParser
193} // namespace v2
194inline namespace v1 {
195//! Parse a text block in MDL rxn format into a ChemicalReaction
196inline ChemicalReaction *RxnBlockToChemicalReaction(const std::string &rxnBlock,
197 bool sanitize = false,
198 bool removeHs = false,
199 bool strictParsing = true) {
201 params.sanitize = sanitize;
202 params.removeHs = removeHs;
203 params.strictParsing = strictParsing;
204 return v2::ReactionParser::ReactionFromRxnBlock(rxnBlock, params).release();
205}
206//! Parse a file in MDL rxn format into a ChemicalReaction
207inline ChemicalReaction *RxnFileToChemicalReaction(const std::string &fileName,
208 bool sanitize = false,
209 bool removeHs = false,
210 bool strictParsing = true) {
212 params.sanitize = sanitize;
213 params.removeHs = removeHs;
214 params.strictParsing = strictParsing;
215 return v2::ReactionParser::ReactionFromRxnFile(fileName, params).release();
216}
217//! Parse a text stream in MDL rxn format into a ChemicalReaction
219 std::istream &rxnStream, unsigned int &line, bool sanitize = false,
220 bool removeHs = false, bool strictParsing = true) {
222 params.sanitize = sanitize;
223 params.removeHs = removeHs;
224 params.strictParsing = strictParsing;
225 return v2::ReactionParser::ReactionFromRxnDataStream(rxnStream, line, params)
226 .release();
227}
228} // namespace v1
229//! returns an rxn block for a reaction
230/*!
231 \param rxn chemical reaction
232
233 \param separateAgents flag to decide if agents are put in a separate block,
234 otherwise they are included in the reactants block
235 (default)
236
237 \param forceV3000 flag to cause the V3000 format to be used instead of
238 V2000
239 */
241 const ChemicalReaction &rxn, bool separateAgents = false,
242 bool forceV3000 = false);
243//! returns an V3000 rxn block for a reaction
244/*!
245 \param rxn chemical reaction
246
247 \param separateAgents flag to decide if agents are put in a separate block,
248 otherwise they are included in the reactants block
249 (default)
250*/
252 const ChemicalReaction &rxn, bool separateAgents = false);
253
254//! @}
255
256//---------------------------------------------------------------------------
257//! \name PNG Support
258//! @{
259
260//! Tags used for PNG metadata
261namespace PNGData {
262RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnSmilesTag;
263RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnSmartsTag;
264RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnRxnTag;
265RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnPklTag;
266} // namespace PNGData
267
268namespace v2 {
269namespace ReactionParser {
270
271//! \brief constructs a ChemicalReaction from the metadata in a PNG stream
272/*!
273
274Looks through the metadata in the PNG to find the first tag that matches one
275of the tags in \c RDKit::PNGData. A reaction is constructed from this chunk.
276
277Throws a \c FileParseException if no suitable tag is found.
278
279 */
280RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction>
281ReactionFromPNGStream(std::istream &pngStream);
282//! \brief constructs a ChemicalReaction from the metadata in a PNG string
283//! See \c PNGStreamToChemicalReaction() for more details
284inline std::unique_ptr<ChemicalReaction> ReactionFromPNGString(
285 const std::string &data) {
286 std::stringstream inStream(data);
287 return ReactionFromPNGStream(inStream);
288};
289//! \brief constructs a ChemicalReaction from the metadata in a PNG file
290//! See \c PNGStreamToChemicalReaction() for more details
291inline std::unique_ptr<ChemicalReaction> ReactionFromPNGFile(
292 const std::string &fname) {
293 std::ifstream inStream(fname.c_str(), std::ios::binary);
294 if (!inStream || (inStream.bad())) {
295 throw BadFileException((boost::format("Bad input file %s") % fname).str());
296 }
297 return ReactionFromPNGStream(inStream);
298};
299} // namespace ReactionParser
300} // namespace v2
301
302inline namespace v1 {
303//! \brief constructs a ChemicalReaction from the metadata in a PNG stream
304/*!
305
306Looks through the metadata in the PNG to find the first tag that matches one
307of the tags in \c RDKit::PNGData. A reaction is constructed from this chunk.
308
309Throws a \c FileParseException if no suitable tag is found.
310
311The caller is responsible for the returned pointer.
312
313 */
314inline ChemicalReaction *PNGStreamToChemicalReaction(std::istream &pngStream) {
315 return v2::ReactionParser::ReactionFromPNGStream(pngStream).release();
316}
317//! \brief constructs a ChemicalReaction from the metadata in a PNG string
318//! See \c PNGStreamToChemicalReaction() for more details
319inline ChemicalReaction *PNGStringToChemicalReaction(const std::string &data) {
320 return v2::ReactionParser::ReactionFromPNGString(data).release();
321}
322//! \brief constructs a ChemicalReaction from the metadata in a PNG file
323//! See \c PNGStreamToChemicalReaction() for more details
324inline ChemicalReaction *PNGFileToChemicalReaction(const std::string &fname) {
325 return v2::ReactionParser::ReactionFromPNGFile(fname).release();
326}
327} // namespace v1
328
329//! \brief adds metadata for a ChemicalReaction to the data from a PNG stream.
330//! The modified PNG data is returned.
331/*!
332
333 \param rxn the reaction to add
334 \param iStream the stream to read from
335 \param includePkl include a reaction pickle
336 \param includeSmiles include reaction SMILES for the reaction
337 \param includeSmarts include reaction SMARTS for the reaction
338 \param includeRxn include an RXN block for the reaction
339
340*/
342 const ChemicalReaction &rxn, std::istream &iStream, bool includePkl = true,
343 bool includeSmiles = true, bool includeSmarts = false,
344 bool includeRxn = false);
345//! \brief adds metadata for a ChemicalReaction to the data from a PNG string.
346//! See addChemicalReactionToPNGStream() for more details.
348 const std::string &pngString,
349 bool includePkl = true,
350 bool includeSmiles = true,
351 bool includeSmarts = false,
352 bool includeRxn = false) {
353 std::stringstream inStream(pngString);
355 rxn, inStream, includePkl, includeSmiles, includeSmarts, includeRxn);
356}
357//! \brief adds metadata for a ChemicalReaction to the data from a PNG string.
358//! See addChemicalReactionToPNGStream() for more details.
359inline std::string addChemicalReactionToPNGFile(const ChemicalReaction &rxn,
360 const std::string &fname,
361 bool includePkl = true,
362 bool includeSmiles = true,
363 bool includeSmarts = false,
364 bool includeRxn = false) {
365 std::ifstream inStream(fname.c_str(), std::ios::binary);
367 rxn, inStream, includePkl, includeSmiles, includeSmarts, includeRxn);
368}
369//! @}
370
371inline std::unique_ptr<ChemicalReaction> operator"" _rxnsmarts(const char *text,
372 size_t len) {
373 std::string sma(text, len);
374 std::unique_ptr<ChemicalReaction> ptr;
375 try {
376 ptr = v2::ReactionParser::ReactionFromSmarts(sma);
377 } catch (...) {
378 ptr = nullptr;
379 }
380 return ptr;
381}
382inline std::unique_ptr<ChemicalReaction> operator"" _rxnsmiles(const char *text,
383 size_t len) {
384 std::string sma(text, len);
385 std::unique_ptr<ChemicalReaction> ptr;
386 try {
387 ptr = v2::ReactionParser::ReactionFromSmiles(sma);
388 } catch (...) {
389 ptr = nullptr;
390 }
391 return ptr;
392}
393
394//---------------------------------------------------------------------------
395//! \name CDXML rxn Support
396///@{
397
398//! Parse text in CDXML rxn format into a vector of ChemicalReactions
399RDKIT_CHEMREACTIONS_EXPORT std::vector<std::unique_ptr<ChemicalReaction>>
400CDXMLToChemicalReactions(const std::string &rxnBlock, bool sanitize = false,
401 bool removeHs = false);
402//! Parse a file in CDXML rxn format into a vector of ChemicalReactions
403RDKIT_CHEMREACTIONS_EXPORT std::vector<std::unique_ptr<ChemicalReaction>>
404CDXMLFileToChemicalReactions(const std::string &fileName, bool sanitize = false,
405 bool removeHs = false);
406//! Parse a text stream in CDXML rxn format into a vector of ChemicalReactions
407RDKIT_CHEMREACTIONS_EXPORT std::vector<std::unique_ptr<ChemicalReaction>>
409 bool sanitize = false,
410 bool removeHs = false);
411
412} // namespace RDKit
413#endif
used by various file parsing classes to indicate a bad file
used to indicate an error in parsing reaction data
const char * what() const noexcept override
get the error message
ChemicalReactionParserException(std::string msg)
construct with an error message
~ChemicalReactionParserException() noexcept override=default
ChemicalReactionParserException(const char *msg)
construct with an error message
This is a class for storing and applying general chemical reactions.
Definition Reaction.h:121
#define RDKIT_CHEMREACTIONS_EXPORT
Definition export.h:49
RDKIT_CHEMREACTIONS_EXPORT const std::string rxnRxnTag
RDKIT_CHEMREACTIONS_EXPORT const std::string rxnSmilesTag
RDKIT_CHEMREACTIONS_EXPORT const std::string rxnSmartsTag
RDKIT_CHEMREACTIONS_EXPORT const std::string rxnPklTag
ChemicalReaction * RxnSmartsToChemicalReaction(const std::string &text, std::map< std::string, std::string > *replacements=nullptr, bool useSmiles=false, bool allowCXSMILES=true)
Parse a string containing "Reaction SMARTS" into a ChemicalReaction.
ChemicalReaction * RxnBlockToChemicalReaction(const std::string &rxnBlock, bool sanitize=false, bool removeHs=false, bool strictParsing=true)
Parse a text block in MDL rxn format into a ChemicalReaction.
ChemicalReaction * RxnDataStreamToChemicalReaction(std::istream &rxnStream, unsigned int &line, bool sanitize=false, bool removeHs=false, bool strictParsing=true)
Parse a text stream in MDL rxn format into a ChemicalReaction.
ChemicalReaction * PNGStreamToChemicalReaction(std::istream &pngStream)
constructs a ChemicalReaction from the metadata in a PNG stream
ChemicalReaction * RxnFileToChemicalReaction(const std::string &fileName, bool sanitize=false, bool removeHs=false, bool strictParsing=true)
Parse a file in MDL rxn format into a ChemicalReaction.
ChemicalReaction * PNGStringToChemicalReaction(const std::string &data)
constructs a ChemicalReaction from the metadata in a PNG string See PNGStreamToChemicalReaction() for...
ChemicalReaction * PNGFileToChemicalReaction(const std::string &fname)
constructs a ChemicalReaction from the metadata in a PNG file See PNGStreamToChemicalReaction() for m...
std::unique_ptr< ChemicalReaction > ReactionFromPNGString(const std::string &data)
constructs a ChemicalReaction from the metadata in a PNG string See PNGStreamToChemicalReaction() for...
RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr< ChemicalReaction > ReactionFromRxnBlock(const std::string &rxnBlock, const FileParsers::MolFileParserParams &params=FileParsers::MolFileParserParams())
RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr< ChemicalReaction > ReactionFromRxnDataStream(std::istream &rxnStream, unsigned int &line, const FileParsers::MolFileParserParams &params=FileParsers::MolFileParserParams())
std::unique_ptr< ChemicalReaction > ReactionFromPNGFile(const std::string &fname)
constructs a ChemicalReaction from the metadata in a PNG file See PNGStreamToChemicalReaction() for m...
RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr< ChemicalReaction > ReactionFromRxnFile(const std::string &fileName, const FileParsers::MolFileParserParams &params=FileParsers::MolFileParserParams())
RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr< ChemicalReaction > ReactionFromPNGStream(std::istream &pngStream)
constructs a ChemicalReaction from the metadata in a PNG stream
Std stuff.
RDKIT_CHEMREACTIONS_EXPORT std::string ChemicalReactionToRxnSmarts(const ChemicalReaction &rxn, const SmilesWriteParams &params)
returns the reaction SMARTS for a reaction
RDKIT_CHEMREACTIONS_EXPORT std::vector< std::unique_ptr< ChemicalReaction > > CDXMLFileToChemicalReactions(const std::string &fileName, bool sanitize=false, bool removeHs=false)
Parse a file in CDXML rxn format into a vector of ChemicalReactions.
RDKIT_CHEMREACTIONS_EXPORT std::string ChemicalReactionToRxnBlock(const ChemicalReaction &rxn, bool separateAgents=false, bool forceV3000=false)
returns an rxn block for a reaction
RDKIT_CHEMREACTIONS_EXPORT std::vector< std::unique_ptr< ChemicalReaction > > CDXMLDataStreamToChemicalReactions(std::istream &rxnStream, bool sanitize=false, bool removeHs=false)
Parse a text stream in CDXML rxn format into a vector of ChemicalReactions.
RDKIT_CHEMREACTIONS_EXPORT std::vector< std::unique_ptr< ChemicalReaction > > CDXMLToChemicalReactions(const std::string &rxnBlock, bool sanitize=false, bool removeHs=false)
Parse text in CDXML rxn format into a vector of ChemicalReactions.
RDKIT_CHEMREACTIONS_EXPORT std::string addChemicalReactionToPNGStream(const ChemicalReaction &rxn, std::istream &iStream, bool includePkl=true, bool includeSmiles=true, bool includeSmarts=false, bool includeRxn=false)
adds metadata for a ChemicalReaction to the data from a PNG stream. The modified PNG data is returned...
RDKIT_CHEMREACTIONS_EXPORT std::string ChemicalReactionToRxnSmiles(const ChemicalReaction &rxn, const SmilesWriteParams &params=SmilesWriteParams())
returns the reaction SMILES for a reaction
std::string addChemicalReactionToPNGFile(const ChemicalReaction &rxn, const std::string &fname, bool includePkl=true, bool includeSmiles=true, bool includeSmarts=false, bool includeRxn=false)
adds metadata for a ChemicalReaction to the data from a PNG string. See addChemicalReactionToPNGStrea...
RDKIT_CHEMREACTIONS_EXPORT std::string ChemicalReactionToV3KRxnBlock(const ChemicalReaction &rxn, bool separateAgents=false)
returns an V3000 rxn block for a reaction
RDKIT_CHEMREACTIONS_EXPORT ChemicalReaction * RxnMolToChemicalReaction(const ROMol &mol)
Parse a ROMol into a ChemicalReaction, RXN role must be set before.
RDKIT_CHEMREACTIONS_EXPORT ROMol * ChemicalReactionToRxnMol(const ChemicalReaction &rxn)
returns a ROMol with RXN roles used to describe the reaction
std::string addChemicalReactionToPNGString(const ChemicalReaction &rxn, const std::string &pngString, bool includePkl=true, bool includeSmiles=true, bool includeSmarts=false, bool includeRxn=false)
adds metadata for a ChemicalReaction to the data from a PNG string. See addChemicalReactionToPNGStrea...