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