RDKit
Open-source cheminformatics and machine learning.
ReactionParser.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2007-2014, Novartis Institutes for BioMedical Research Inc.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following
13 // disclaimer in the documentation and/or other materials provided
14 // with the distribution.
15 // * Neither the name of Novartis Institutes for BioMedical Research Inc.
16 // nor the names of its contributors may be used to endorse or promote
17 // products derived from this software without specific prior written
18 // permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 //
32 
33 #include <RDGeneral/export.h>
34 #ifndef RD_REACTIONPARSER_H_21Aug2006
35 #define RD_REACTIONPARSER_H_21Aug2006
36 
37 #include <string>
38 #include <iostream>
39 #include <fstream>
40 #include <sstream>
41 #include <boost/format.hpp>
44 
45 namespace RDKit {
46 class ChemicalReaction;
47 
48 //! used to indicate an error in parsing reaction data
50  : public std::exception {
51  public:
52  //! construct with an error message
53  explicit ChemicalReactionParserException(const char *msg) : _msg(msg){};
54  //! construct with an error message
55  explicit ChemicalReactionParserException(const std::string &msg)
56  : _msg(msg){};
57  //! get the error message
58  const char *what() const noexcept override { return _msg.c_str(); };
60 
61  private:
62  std::string _msg;
63 };
64 
65 //---------------------------------------------------------------------------
66 //! \name Reaction SMARTS/SMILES Support
67 //@{
68 
69 //! Parse a string containing "Reaction SMARTS" into a ChemicalReaction
70 /*!
71  Our definition of Reaction SMARTS is something that looks a lot like
72  reaction SMILES, except that SMARTS queries are allowed on the reactant
73  side and that atom-map numbers are required (at least for now)
74 
75  \param text the SMARTS to convert
76  \param replacements a string->string map of replacement strings.
77  \see SmilesToMol for more information about replacements
78  \param useSmiles if set, the SMILES parser will be used instead of the
79  SMARTS
80  parserfor the individual components
81  */
83  const std::string &text,
84  std::map<std::string, std::string> *replacements = nullptr,
85  bool useSmiles = false);
86 
87 //! returns the reaction SMARTS for a reaction
89  const ChemicalReaction &rxn);
90 
91 //! returns the reaction SMILES for a reaction
93  const ChemicalReaction &rxn, bool canonical = true);
94 //@}
95 
96 //---------------------------------------------------------------------------
97 //! \name Reaction Mol Support
98 //@{
99 
100 //! Parse a ROMol into a ChemicalReaction, RXN role must be set before
101 /*!
102  Alternative to build a reaction from a molecule (fragments) which have RXN
103  roles
104  set as atom properties: common_properties::molRxnRole (1=reactant, 2=product,
105  3=agent)
106 
107  \param mol ROMol with RXN roles set
108  */
110  const ROMol &mol);
111 
112 //! returns a ROMol with RXN roles used to describe the reaction
114  const ChemicalReaction &rxn);
115 //@}
116 
117 //---------------------------------------------------------------------------
118 //! \name MDL rxn Support
119 //@{
120 
121 //! Parse a text block in MDL rxn format into a ChemicalReaction
123  const std::string &rxnBlock, bool sanitize = false, bool removeHs = false,
124  bool strictParsing = true);
125 //! Parse a file in MDL rxn format into a ChemicalReaction
127  const std::string &fileName, bool sanitize = false, bool removeHs = false,
128  bool strictParsing = true);
129 //! Parse a text stream in MDL rxn format into a ChemicalReaction
131  std::istream &rxnStream, unsigned int &line, bool sanitize = false,
132  bool removeHs = false, bool strictParsing = true);
133 //! returns an rxn block for a reaction
134 /*!
135  \param rxn chemical reaction
136  \param separateAgents flag to decide if agents were put in a separate block,
137  otherwise they were included in the reactants block
138  (default)
139  */
141  const ChemicalReaction &rxn, bool separateAgents = false);
142 
143 //@}
144 
145 //---------------------------------------------------------------------------
146 //! \name PNG Support
147 //@{
148 
149 //! Tags used for PNG metadata
150 namespace PNGData {
151 RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnSmilesTag;
152 RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnSmartsTag;
153 RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnRxnTag;
154 RDKIT_CHEMREACTIONS_EXPORT extern const std::string rxnPklTag;
155 } // namespace PNGData
156 
157 //! \brief constructs a ChemicalReaction from the metadata in a PNG stream
158 /*!
159 
160 Looks through the metadata in the PNG to find the first tag that matches one of
161 the tags in \c RDKit::PNGData. A molecule is constructed from this chunk.
162 
163 Throws a \c FileParseException if no suitable tag is found.
164 
165 The caller is responsible for the returned pointer.
166 
167  */
169  std::istream &pngStream);
170 //! \brief constructs a ChemicalReaction from the metadata in a PNG string
171 //! See \c PNGStreamToChemicalReaction() for more details
172 inline ChemicalReaction *PNGStringToChemicalReaction(const std::string &data) {
173  std::stringstream inStream(data);
174  return PNGStreamToChemicalReaction(inStream);
175 };
176 //! \brief constructs a ChemicalReaction from the metadata in a PNG file
177 //! See \c PNGStreamToChemicalReaction() for more details
178 inline ChemicalReaction *PNGFileToChemicalReaction(const std::string &fname) {
179  std::ifstream inStream(fname.c_str(), std::ios::binary);
180  if (!inStream || (inStream.bad())) {
181  throw BadFileException((boost::format("Bad input file %s") % fname).str());
182  }
183  return PNGStreamToChemicalReaction(inStream);
184 };
185 
186 //! \brief adds metadata for a ChemicalReaction to the data from a PNG stream.
187 //! The modified PNG data is returned.
188 /*!
189 
190  \param rxn the reaction to add
191  \param iStream the stream to read from
192  \param includePkl include a reaction pickle
193  \param includeSmiles include reaction SMILES for the reaction
194  \param includeSmarts include reaction SMARTS for the reaction
195  \param includeRxn include an RXN block for the reaction
196 
197 */
199  const ChemicalReaction &rxn, std::istream &iStream, bool includePkl = true,
200  bool includeSmiles = true, bool includeSmarts = false,
201  bool includeRxn = false);
202 //! \brief adds metadata for a ChemicalReaction to the data from a PNG string.
203 //! See addChemicalReactionToPNGStream() for more details.
204 inline std::string addChemicalReactionToPNGString(const ChemicalReaction &rxn,
205  const std::string &pngString,
206  bool includePkl = true,
207  bool includeSmiles = true,
208  bool includeSmarts = false,
209  bool includeRxn = false) {
210  std::stringstream inStream(pngString);
212  rxn, inStream, includePkl, includeSmiles, includeSmarts, includeRxn);
213 }
214 //! \brief adds metadata for a ChemicalReaction to the data from a PNG string.
215 //! See addChemicalReactionToPNGStream() for more details.
216 inline std::string addChemicalReactionToPNGFile(const ChemicalReaction &rxn,
217  const std::string &fname,
218  bool includePkl = true,
219  bool includeSmiles = true,
220  bool includeSmarts = false,
221  bool includeRxn = false) {
222  std::ifstream inStream(fname.c_str(), std::ios::binary);
224  rxn, inStream, includePkl, includeSmiles, includeSmarts, includeRxn);
225 }
226 //@}
227 
228 }; // namespace RDKit
229 
230 #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(const char *msg)
construct with an error message
ChemicalReactionParserException(const std::string &msg)
construct with an error message
This is a class for storing and applying general chemical reactions.
Definition: Reaction.h:119
#define RDKIT_CHEMREACTIONS_EXPORT
Definition: export.h:86
RDKIT_GRAPHMOL_EXPORT ROMol * removeHs(const ROMol &mol, bool implicitOnly=false, bool updateExplicitCount=false, bool sanitize=true)
returns a copy of a molecule with hydrogens removed
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
Std stuff.
Definition: Abbreviations.h:17
RDKIT_CHEMREACTIONS_EXPORT std::string ChemicalReactionToRxnSmiles(const ChemicalReaction &rxn, bool canonical=true)
returns the reaction SMILES for a reaction
ChemicalReaction * PNGFileToChemicalReaction(const std::string &fname)
constructs a ChemicalReaction from the metadata in a PNG file See PNGStreamToChemicalReaction() for m...
RDKIT_CHEMREACTIONS_EXPORT ChemicalReaction * PNGStreamToChemicalReaction(std::istream &pngStream)
constructs a ChemicalReaction from the metadata in a PNG stream
RDKIT_CHEMREACTIONS_EXPORT 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.
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 ROMol * ChemicalReactionToRxnMol(const ChemicalReaction &rxn)
returns a ROMol with RXN roles used to describe the reaction
ChemicalReaction * PNGStringToChemicalReaction(const std::string &data)
constructs a ChemicalReaction from the metadata in a PNG string See PNGStreamToChemicalReaction() for...
RDKIT_CHEMREACTIONS_EXPORT 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.
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 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.
RDKIT_CHEMREACTIONS_EXPORT std::string ChemicalReactionToRxnBlock(const ChemicalReaction &rxn, bool separateAgents=false)
returns an 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 ChemicalReaction * RxnSmartsToChemicalReaction(const std::string &text, std::map< std::string, std::string > *replacements=nullptr, bool useSmiles=false)
Parse a string containing "Reaction SMARTS" into a ChemicalReaction.
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...