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 <fstream>
41#include <map>
42#include <sstream>
43#include <utility>
44#include <boost/format.hpp>
49
50namespace RDKit {
51class ROMol;
53
54//! used to indicate an error in parsing reaction data
56 : public std::exception {
57 public:
58 //! construct with an error message
59 explicit ChemicalReactionParserException(const char *msg) : _msg(msg) {}
60 //! construct with an error message
61 explicit ChemicalReactionParserException(std::string msg)
62 : _msg(std::move(msg)) {}
63 //! get the error message
64 const char *what() const noexcept override { return _msg.c_str(); }
65 ~ChemicalReactionParserException() noexcept override = default;
66
67 private:
68 std::string _msg;
69};
70
71namespace v2 {
72namespace ReactionParser {
74 bool sanitize = false; /**< sanitize the molecules after building them */
75 std::map<std::string, std::string>
76 replacements; /**< allows SMILES "macros" */
77 bool allowCXSMILES = true; /**< recognize and parse CXSMILES*/
79 true; /**< throw an exception if the CXSMILES parsing fails */
80};
81RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction> ReactionFromSmarts(
82 const std::string &smarts,
84
85RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction> ReactionFromSmiles(
86 const std::string &smarts,
88} // namespace ReactionParser
89} // namespace v2
90
91inline namespace v1 {
92//---------------------------------------------------------------------------
93//! \name Reaction SMARTS/SMILES Support
94//! @{
95
96//! Parse a string containing "Reaction SMARTS" into a ChemicalReaction
97/*!
98 Our definition of Reaction SMARTS is something that looks a lot like reaction
99 SMILES, except that SMARTS queries are allowed on the reactant side and that
100 atom-map numbers are required (at least for now)
101
102 \param text the SMARTS to convert
103
104 \param replacements a string->string map of replacement strings. \see
105 SmilesToMol for more information about replacements
106
107 \param useSmiles if set, the SMILES parser will be used instead of the
108 SMARTS parserfor the individual components
109
110 \param allowCXSMILES if set, any CXSMILES extensions present will be
111 parsed, otherwise it will be ignored
112 */
114 const std::string &text,
115 std::map<std::string, std::string> *replacements = nullptr,
116 bool useSmiles = false, bool allowCXSMILES = true) {
118 if (replacements) {
119 params.replacements = *replacements;
120 }
121 params.allowCXSMILES = allowCXSMILES;
122 if (useSmiles) {
123 return v2::ReactionParser::ReactionFromSmiles(text, params).release();
124 } else {
125 return v2::ReactionParser::ReactionFromSmarts(text, params).release();
126 }
127}
128} // namespace v1
129//! returns the reaction SMARTS for a reaction
131 const ChemicalReaction &rxn, const SmilesWriteParams &params);
132//! \overload
133inline std::string ChemicalReactionToRxnSmarts(const ChemicalReaction &rxn) {
134 SmilesWriteParams params;
135 params.canonical = false;
136 return ChemicalReactionToRxnSmarts(rxn, params);
137}
138
139//! returns the reaction SMILES for a reaction
141 const ChemicalReaction &rxn,
142 const SmilesWriteParams &params = SmilesWriteParams());
143//! \overload
144inline std::string ChemicalReactionToRxnSmiles(const ChemicalReaction &rxn,
145 bool canonical) {
146 SmilesWriteParams params;
147 params.canonical = canonical;
148 return ChemicalReactionToRxnSmiles(rxn, params);
149}
150
151//! returns the reaction SMARTS for a reaction with CX extension
153 const ChemicalReaction &rxn, const SmilesWriteParams &params,
154 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 ChemicalReaction &rxn, const SmilesWriteParams &params,
165 std::uint32_t flags = SmilesWrite::CXSmilesFields::CX_ALL);
166//! \overload
168 bool canonical = true) {
169 SmilesWriteParams params;
170 params.canonical = canonical;
171 return ChemicalReactionToCXRxnSmiles(rxn, params);
172}
173//! @}
174
175//---------------------------------------------------------------------------
176//! \name Reaction Mol Support
177//! @{
178
179//! Parse a ROMol into a ChemicalReaction, RXN role must be set before
180/*!
181 Alternative to build a reaction from a molecule (fragments) which have RXN
182 roles set as atom properties: common_properties::molRxnRole (1=reactant,
183 2=product, 3=agent)
184
185 \param mol ROMol with RXN roles set
186 */
188 const ROMol &mol);
189
190//! returns a ROMol with RXN roles used to describe the reaction
192 const ChemicalReaction &rxn);
193//! @}
194
195//---------------------------------------------------------------------------
196//! \name MDL rxn Support
197//! @{
198namespace v2 {
199namespace ReactionParser {
200
201RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction>
202ReactionFromRxnBlock(const std::string &rxnBlock,
205RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction>
206ReactionFromRxnFile(const std::string &fileName,
209RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction>
210ReactionFromRxnDataStream(std::istream &rxnStream, unsigned int &line,
213
214} // namespace ReactionParser
215} // namespace v2
216inline namespace v1 {
217//! Parse a text block in MDL rxn format into a ChemicalReaction
218inline ChemicalReaction *RxnBlockToChemicalReaction(const std::string &rxnBlock,
219 bool sanitize = false,
220 bool removeHs = false,
221 bool strictParsing = true) {
223 params.sanitize = sanitize;
224 params.removeHs = removeHs;
225 params.strictParsing = strictParsing;
226 return v2::ReactionParser::ReactionFromRxnBlock(rxnBlock, params).release();
227}
228//! Parse a file in MDL rxn format into a ChemicalReaction
229inline ChemicalReaction *RxnFileToChemicalReaction(const std::string &fileName,
230 bool sanitize = false,
231 bool removeHs = false,
232 bool strictParsing = true) {
234 params.sanitize = sanitize;
235 params.removeHs = removeHs;
236 params.strictParsing = strictParsing;
237 return v2::ReactionParser::ReactionFromRxnFile(fileName, params).release();
238}
239//! Parse a text stream in MDL rxn format into a ChemicalReaction
241 std::istream &rxnStream, unsigned int &line, bool sanitize = false,
242 bool removeHs = false, bool strictParsing = true) {
244 params.sanitize = sanitize;
245 params.removeHs = removeHs;
246 params.strictParsing = strictParsing;
247 return v2::ReactionParser::ReactionFromRxnDataStream(rxnStream, line, params)
248 .release();
249}
250} // namespace v1
251//! returns an rxn block for a reaction
252/*!
253 \param rxn chemical reaction
254
255 \param separateAgents flag to decide if agents are put in a separate block,
256 otherwise they are included in the reactants block
257 (default)
258
259 \param forceV3000 flag to cause the V3000 format to be used instead of
260 V2000
261 */
263 const ChemicalReaction &rxn, bool separateAgents = false,
264 bool forceV3000 = false);
265//! returns an V3000 rxn block for a reaction
266/*!
267 \param rxn chemical reaction
268
269 \param separateAgents flag to decide if agents are put in a separate block,
270 otherwise they are included in the reactants block
271 (default)
272*/
274 const ChemicalReaction &rxn, bool separateAgents = false);
275
276//! @}
277
278//---------------------------------------------------------------------------
279//! \name PNG Support
280//! @{
281
282//! Tags used for PNG metadata
283namespace PNGData {
284RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnSmilesTag;
285RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnSmartsTag;
286RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnRxnTag;
287RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnPklTag;
288} // namespace PNGData
289
290namespace v2 {
291namespace ReactionParser {
292
293//! \brief constructs a ChemicalReaction from the metadata in a PNG stream
294/*!
295
296Looks through the metadata in the PNG to find the first tag that matches one
297of the tags in \c RDKit::PNGData. A reaction is constructed from this chunk.
298
299Throws a \c FileParseException if no suitable tag is found.
300
301 */
302RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr<ChemicalReaction>
303ReactionFromPNGStream(std::istream &pngStream);
304//! \brief constructs a ChemicalReaction from the metadata in a PNG string
305//! See \c PNGStreamToChemicalReaction() for more details
306inline std::unique_ptr<ChemicalReaction> ReactionFromPNGString(
307 const std::string &data) {
308 std::stringstream inStream(data);
309 return ReactionFromPNGStream(inStream);
310};
311//! \brief constructs a ChemicalReaction from the metadata in a PNG file
312//! See \c PNGStreamToChemicalReaction() for more details
313inline std::unique_ptr<ChemicalReaction> ReactionFromPNGFile(
314 const std::string &fname) {
315 std::ifstream inStream(fname.c_str(), std::ios::binary);
316 if (!inStream || (inStream.bad())) {
317 throw BadFileException((boost::format("Bad input file %s") % fname).str());
318 }
319 return ReactionFromPNGStream(inStream);
320};
321} // namespace ReactionParser
322} // namespace v2
323
324inline namespace v1 {
325//! \brief constructs a ChemicalReaction from the metadata in a PNG stream
326/*!
327
328Looks through the metadata in the PNG to find the first tag that matches one
329of the tags in \c RDKit::PNGData. A reaction is constructed from this chunk.
330
331Throws a \c FileParseException if no suitable tag is found.
332
333The caller is responsible for the returned pointer.
334
335 */
336inline ChemicalReaction *PNGStreamToChemicalReaction(std::istream &pngStream) {
337 return v2::ReactionParser::ReactionFromPNGStream(pngStream).release();
338}
339//! \brief constructs a ChemicalReaction from the metadata in a PNG string
340//! See \c PNGStreamToChemicalReaction() for more details
341inline ChemicalReaction *PNGStringToChemicalReaction(const std::string &data) {
342 return v2::ReactionParser::ReactionFromPNGString(data).release();
343}
344//! \brief constructs a ChemicalReaction from the metadata in a PNG file
345//! See \c PNGStreamToChemicalReaction() for more details
346inline ChemicalReaction *PNGFileToChemicalReaction(const std::string &fname) {
347 return v2::ReactionParser::ReactionFromPNGFile(fname).release();
348}
349} // namespace v1
350
351//! \brief adds metadata for a ChemicalReaction to the data from a PNG stream.
352//! The modified PNG data is returned.
353/*!
354
355 \param rxn the reaction to add
356 \param iStream the stream to read from
357 \param includePkl include a reaction pickle
358 \param includeSmiles include reaction SMILES for the reaction
359 \param includeSmarts include reaction SMARTS for the reaction
360 \param includeRxn include an RXN block for the reaction
361
362*/
364 const ChemicalReaction &rxn, std::istream &iStream, bool includePkl = true,
365 bool includeSmiles = true, bool includeSmarts = false,
366 bool includeRxn = false);
367//! \brief adds metadata for a ChemicalReaction to the data from a PNG string.
368//! See addChemicalReactionToPNGStream() for more details.
370 const std::string &pngString,
371 bool includePkl = true,
372 bool includeSmiles = true,
373 bool includeSmarts = false,
374 bool includeRxn = false) {
375 std::stringstream inStream(pngString);
377 rxn, inStream, includePkl, includeSmiles, includeSmarts, includeRxn);
378}
379//! \brief adds metadata for a ChemicalReaction to the data from a PNG string.
380//! See addChemicalReactionToPNGStream() for more details.
381inline std::string addChemicalReactionToPNGFile(const ChemicalReaction &rxn,
382 const std::string &fname,
383 bool includePkl = true,
384 bool includeSmiles = true,
385 bool includeSmarts = false,
386 bool includeRxn = false) {
387 std::ifstream inStream(fname.c_str(), std::ios::binary);
389 rxn, inStream, includePkl, includeSmiles, includeSmarts, includeRxn);
390}
391//! @}
392
393inline std::unique_ptr<ChemicalReaction> operator""_rxnsmarts(const char *text,
394 size_t len) {
395 std::string sma(text, len);
396 std::unique_ptr<ChemicalReaction> ptr;
397 try {
399 } catch (...) {
400 ptr = nullptr;
401 }
402 return ptr;
403}
404inline std::unique_ptr<ChemicalReaction> operator""_rxnsmiles(const char *text,
405 size_t len) {
406 std::string sma(text, len);
407 std::unique_ptr<ChemicalReaction> ptr;
408 try {
410 } catch (...) {
411 ptr = nullptr;
412 }
413 return ptr;
414}
415
416//---------------------------------------------------------------------------
417//! \name CDXML rxn Support
418///@{
419
420//! Parse text in CDXML rxn format into a vector of ChemicalReactions
421RDKIT_CHEMREACTIONS_EXPORT std::vector<std::unique_ptr<ChemicalReaction>>
422CDXMLToChemicalReactions(const std::string &rxnBlock, bool sanitize = false,
423 bool removeHs = false);
424//! Parse a file in CDXML rxn format into a vector of ChemicalReactions
425RDKIT_CHEMREACTIONS_EXPORT std::vector<std::unique_ptr<ChemicalReaction>>
426CDXMLFileToChemicalReactions(const std::string &fileName, bool sanitize = false,
427 bool removeHs = false);
428//! Parse a text stream in CDXML rxn format into a vector of ChemicalReactions
429RDKIT_CHEMREACTIONS_EXPORT std::vector<std::unique_ptr<ChemicalReaction>>
431 bool sanitize = false,
432 bool removeHs = false);
433
434} // namespace RDKit
435#endif
used by various file parsing classes to indicate a bad file
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:57
Tags used for PNG metadata.
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 > ReactionFromSmarts(const std::string &smarts, const ReactionSmartsParserParams &params=ReactionSmartsParserParams())
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
RDKIT_CHEMREACTIONS_EXPORT std::unique_ptr< ChemicalReaction > ReactionFromSmiles(const std::string &smarts, const ReactionSmartsParserParams &params=ReactionSmartsParserParams())
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...
std::map< std::string, std::string > replacements