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//! returns the reaction SMARTS for a reaction with CX extension
154 const SmilesWriteParams &params, std::uint32_t flags = SmilesWrite::CXSmilesFields::CX_ALL);
155//! \overload
156inline std::string ChemicalReactionToCXRxnSmarts(const ChemicalReaction &rxn) {
157 SmilesWriteParams params;
158 params.canonical = false;
159 return ChemicalReactionToCXRxnSmarts(rxn, params);
160}
161
162//! returns the reaction SMILES for a reaction with CX extension
164 const SmilesWriteParams &params, std::uint32_t flags = SmilesWrite::CXSmilesFields::CX_ALL);
165//! \overload
166inline std::string ChemicalReactionToCXRxnSmiles(const ChemicalReaction &rxn, bool canonical = true) {
167 SmilesWriteParams params;
168 params.canonical = canonical;
169 return ChemicalReactionToCXRxnSmiles(rxn, params);
170}
171//! @}
172
173//---------------------------------------------------------------------------
174//! \name Reaction Mol Support
175//! @{
176
177//! Parse a ROMol into a ChemicalReaction, RXN role must be set before
178/*!
179 Alternative to build a reaction from a molecule (fragments) which have RXN
180 roles set as atom properties: common_properties::molRxnRole (1=reactant,
181 2=product, 3=agent)
182
183 \param mol ROMol with RXN roles set
184 */
186 const ROMol &mol);
187
188//! returns a ROMol with RXN roles used to describe the reaction
190 const ChemicalReaction &rxn);
191//! @}
192
193//---------------------------------------------------------------------------
194//! \name MDL rxn Support
195//! @{
196namespace v2 {
197namespace ReactionParser {
198
199RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction>
200ReactionFromRxnBlock(const std::string &rxnBlock,
203RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction>
204ReactionFromRxnFile(const std::string &fileName,
207RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction>
208ReactionFromRxnDataStream(std::istream &rxnStream, unsigned int &line,
211
212} // namespace ReactionParser
213} // namespace v2
214inline namespace v1 {
215//! Parse a text block in MDL rxn format into a ChemicalReaction
216inline ChemicalReaction *RxnBlockToChemicalReaction(const std::string &rxnBlock,
217 bool sanitize = false,
218 bool removeHs = false,
219 bool strictParsing = true) {
221 params.sanitize = sanitize;
222 params.removeHs = removeHs;
223 params.strictParsing = strictParsing;
224 return v2::ReactionParser::ReactionFromRxnBlock(rxnBlock, params).release();
225}
226//! Parse a file in MDL rxn format into a ChemicalReaction
227inline ChemicalReaction *RxnFileToChemicalReaction(const std::string &fileName,
228 bool sanitize = false,
229 bool removeHs = false,
230 bool strictParsing = true) {
232 params.sanitize = sanitize;
233 params.removeHs = removeHs;
234 params.strictParsing = strictParsing;
235 return v2::ReactionParser::ReactionFromRxnFile(fileName, params).release();
236}
237//! Parse a text stream in MDL rxn format into a ChemicalReaction
239 std::istream &rxnStream, unsigned int &line, bool sanitize = false,
240 bool removeHs = false, bool strictParsing = true) {
242 params.sanitize = sanitize;
243 params.removeHs = removeHs;
244 params.strictParsing = strictParsing;
245 return v2::ReactionParser::ReactionFromRxnDataStream(rxnStream, line, params)
246 .release();
247}
248} // namespace v1
249//! returns an rxn block for a reaction
250/*!
251 \param rxn chemical reaction
252
253 \param separateAgents flag to decide if agents are put in a separate block,
254 otherwise they are included in the reactants block
255 (default)
256
257 \param forceV3000 flag to cause the V3000 format to be used instead of
258 V2000
259 */
261 const ChemicalReaction &rxn, bool separateAgents = false,
262 bool forceV3000 = false);
263//! returns an V3000 rxn block for a reaction
264/*!
265 \param rxn chemical reaction
266
267 \param separateAgents flag to decide if agents are put in a separate block,
268 otherwise they are included in the reactants block
269 (default)
270*/
272 const ChemicalReaction &rxn, bool separateAgents = false);
273
274//! @}
275
276//---------------------------------------------------------------------------
277//! \name PNG Support
278//! @{
279
280//! Tags used for PNG metadata
281namespace PNGData {
282RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnSmilesTag;
283RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnSmartsTag;
284RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnRxnTag;
285RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnPklTag;
286} // namespace PNGData
287
288namespace v2 {
289namespace ReactionParser {
290
291//! \brief constructs a ChemicalReaction from the metadata in a PNG stream
292/*!
293
294Looks through the metadata in the PNG to find the first tag that matches one
295of the tags in \c RDKit::PNGData. A reaction is constructed from this chunk.
296
297Throws a \c FileParseException if no suitable tag is found.
298
299 */
300RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction>
301ReactionFromPNGStream(std::istream &pngStream);
302//! \brief constructs a ChemicalReaction from the metadata in a PNG string
303//! See \c PNGStreamToChemicalReaction() for more details
304inline std::unique_ptr<ChemicalReaction> ReactionFromPNGString(
305 const std::string &data) {
306 std::stringstream inStream(data);
307 return ReactionFromPNGStream(inStream);
308};
309//! \brief constructs a ChemicalReaction from the metadata in a PNG file
310//! See \c PNGStreamToChemicalReaction() for more details
311inline std::unique_ptr<ChemicalReaction> ReactionFromPNGFile(
312 const std::string &fname) {
313 std::ifstream inStream(fname.c_str(), std::ios::binary);
314 if (!inStream || (inStream.bad())) {
315 throw BadFileException((boost::format("Bad input file %s") % fname).str());
316 }
317 return ReactionFromPNGStream(inStream);
318};
319} // namespace ReactionParser
320} // namespace v2
321
322inline namespace v1 {
323//! \brief constructs a ChemicalReaction from the metadata in a PNG stream
324/*!
325
326Looks through the metadata in the PNG to find the first tag that matches one
327of the tags in \c RDKit::PNGData. A reaction is constructed from this chunk.
328
329Throws a \c FileParseException if no suitable tag is found.
330
331The caller is responsible for the returned pointer.
332
333 */
334inline ChemicalReaction *PNGStreamToChemicalReaction(std::istream &pngStream) {
335 return v2::ReactionParser::ReactionFromPNGStream(pngStream).release();
336}
337//! \brief constructs a ChemicalReaction from the metadata in a PNG string
338//! See \c PNGStreamToChemicalReaction() for more details
339inline ChemicalReaction *PNGStringToChemicalReaction(const std::string &data) {
340 return v2::ReactionParser::ReactionFromPNGString(data).release();
341}
342//! \brief constructs a ChemicalReaction from the metadata in a PNG file
343//! See \c PNGStreamToChemicalReaction() for more details
344inline ChemicalReaction *PNGFileToChemicalReaction(const std::string &fname) {
345 return v2::ReactionParser::ReactionFromPNGFile(fname).release();
346}
347} // namespace v1
348
349//! \brief adds metadata for a ChemicalReaction to the data from a PNG stream.
350//! The modified PNG data is returned.
351/*!
352
353 \param rxn the reaction to add
354 \param iStream the stream to read from
355 \param includePkl include a reaction pickle
356 \param includeSmiles include reaction SMILES for the reaction
357 \param includeSmarts include reaction SMARTS for the reaction
358 \param includeRxn include an RXN block for the reaction
359
360*/
362 const ChemicalReaction &rxn, std::istream &iStream, bool includePkl = true,
363 bool includeSmiles = true, bool includeSmarts = false,
364 bool includeRxn = false);
365//! \brief adds metadata for a ChemicalReaction to the data from a PNG string.
366//! See addChemicalReactionToPNGStream() for more details.
368 const std::string &pngString,
369 bool includePkl = true,
370 bool includeSmiles = true,
371 bool includeSmarts = false,
372 bool includeRxn = false) {
373 std::stringstream inStream(pngString);
375 rxn, inStream, includePkl, includeSmiles, includeSmarts, includeRxn);
376}
377//! \brief adds metadata for a ChemicalReaction to the data from a PNG string.
378//! See addChemicalReactionToPNGStream() for more details.
379inline std::string addChemicalReactionToPNGFile(const ChemicalReaction &rxn,
380 const std::string &fname,
381 bool includePkl = true,
382 bool includeSmiles = true,
383 bool includeSmarts = false,
384 bool includeRxn = false) {
385 std::ifstream inStream(fname.c_str(), std::ios::binary);
387 rxn, inStream, includePkl, includeSmiles, includeSmarts, includeRxn);
388}
389//! @}
390
391inline std::unique_ptr<ChemicalReaction> operator"" _rxnsmarts(const char *text,
392 size_t len) {
393 std::string sma(text, len);
394 std::unique_ptr<ChemicalReaction> ptr;
395 try {
396 ptr = v2::ReactionParser::ReactionFromSmarts(sma);
397 } catch (...) {
398 ptr = nullptr;
399 }
400 return ptr;
401}
402inline std::unique_ptr<ChemicalReaction> operator"" _rxnsmiles(const char *text,
403 size_t len) {
404 std::string sma(text, len);
405 std::unique_ptr<ChemicalReaction> ptr;
406 try {
407 ptr = v2::ReactionParser::ReactionFromSmiles(sma);
408 } catch (...) {
409 ptr = nullptr;
410 }
411 return ptr;
412}
413
414//---------------------------------------------------------------------------
415//! \name CDXML rxn Support
416///@{
417
418//! Parse text in CDXML rxn format into a vector of ChemicalReactions
419RDKIT_CHEMREACTIONS_EXPORT std::vector<std::unique_ptr<ChemicalReaction>>
420CDXMLToChemicalReactions(const std::string &rxnBlock, bool sanitize = false,
421 bool removeHs = false);
422//! Parse a file in CDXML rxn format into a vector of ChemicalReactions
423RDKIT_CHEMREACTIONS_EXPORT std::vector<std::unique_ptr<ChemicalReaction>>
424CDXMLFileToChemicalReactions(const std::string &fileName, bool sanitize = false,
425 bool removeHs = false);
426//! Parse a text stream in CDXML rxn format into a vector of ChemicalReactions
427RDKIT_CHEMREACTIONS_EXPORT std::vector<std::unique_ptr<ChemicalReaction>>
429 bool sanitize = false,
430 bool removeHs = false);
431
432} // namespace RDKit
433#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 ChemicalReactionToCXRxnSmarts(const ChemicalReaction &rxn, const SmilesWriteParams &params, std::uint32_t flags=SmilesWrite::CXSmilesFields::CX_ALL)
returns the reaction SMARTS for a reaction with CX extension
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 ChemicalReactionToCXRxnSmiles(const ChemicalReaction &rxn, const SmilesWriteParams &params, std::uint32_t flags=SmilesWrite::CXSmilesFields::CX_ALL)
returns the reaction SMILES for a reaction with CX extension
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...