RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
PNGParser.h
Go to the documentation of this file.
1//
2// Copyright (C) 2020 Greg Landrum
3//
4// @@ All Rights Reserved @@
5// This file is part of the RDKit.
6// The contents are covered by the terms of the BSD license
7// which is included in the file license.txt, found at the root
8// of the RDKit source tree.
9//
10#include <RDGeneral/export.h>
11#ifndef RD_PNGPARSER_H
12#define RD_PNGPARSER_H
13
14#include <RDGeneral/types.h>
16#include <GraphMol/RDKitBase.h>
19#include <GraphMol/MolPickler.h>
20
21#include <boost/format.hpp>
22
23#include <string>
24#include <fstream>
25#include <sstream>
26
27namespace RDKit {
28
29//! Tags used for PNG metadata
30namespace PNGData {
31RDKIT_FILEPARSERS_EXPORT extern const std::string smilesTag;
32RDKIT_FILEPARSERS_EXPORT extern const std::string molTag;
33RDKIT_FILEPARSERS_EXPORT extern const std::string pklTag;
34} // namespace PNGData
35
37 //! include molecule pickle
38 bool includePkl = true;
39 //! include CXSMILES for the molecule
40 bool includeSmiles = true;
41 //! include molblock for the molecule
42 bool includeMol = false;
43 //! choose properties to be included in the pickle
45 //! choose SmilesWriteParams for the CXSMILES string
47 //! choose CXSMILES fields to be included in the CXSMILES string
48 std::uint32_t cxSmilesFlags = SmilesWrite::CXSmilesFields::CX_ALL;
49 //! choose what to do with bond dirs in the CXSMILES string
50 RestoreBondDirOption restoreBondDirs =
51 RestoreBondDirOption::RestoreBondDirOptionClear;
52};
53
54//! \name metadata to/from PNG
55//! @{
56
57//! \brief returns the metadata (tEXt and zTXt chunks) from PNG data
58RDKIT_FILEPARSERS_EXPORT std::vector<std::pair<std::string, std::string>>
59PNGStreamToMetadata(std::istream &inStream);
60
61//! \brief returns the metadata (tEXt and zTXt chunks) from PNG data
62inline std::vector<std::pair<std::string, std::string>> PNGFileToMetadata(
63 const std::string &fname) {
64 std::ifstream inStream(fname.c_str(), std::ios::binary);
65 if (!inStream || (inStream.bad())) {
66 throw BadFileException((boost::format("Bad input file %s") % fname).str());
67 }
68 return PNGStreamToMetadata(inStream);
69}
70
71//! \brief returns the metadata (tEXt and zTXt chunks) from PNG data
72inline std::vector<std::pair<std::string, std::string>> PNGStringToMetadata(
73 const std::string &data) {
74 std::stringstream inStream(data);
75 return PNGStreamToMetadata(inStream);
76}
77
78//! \brief adds metadata to a PNG stream.
79//! The modified PNG data is returned.
80/*!
81
82The compressed flag is ignored if the RDKit is not built with
83boost::iostreams support
84
85*/
87 std::istream &iStream,
88 const std::vector<std::pair<std::string, std::string>> &metadata,
89 bool compressed = true);
90
91//! \brief adds metadata to a PNG string.
92//! The modified PNG data is returned.
93inline std::string addMetadataToPNGString(
94 const std::string &pngString,
95 const std::vector<std::pair<std::string, std::string>> &metadata,
96 bool compressed = true) {
97 std::stringstream inStream(pngString);
98 return addMetadataToPNGStream(inStream, metadata, compressed);
99}
100
101//! \brief adds metadata to a PNG file.
102//! The modified PNG data is returned.
103inline std::string addMetadataToPNGFile(
104 const std::string &fname,
105 const std::vector<std::pair<std::string, std::string>> &metadata,
106 bool compressed = true) {
107 std::ifstream inStream(fname.c_str(), std::ios::binary);
108 return addMetadataToPNGStream(inStream, metadata, compressed);
109}
110//! @}
111
112//! \name molecules to/from PNG
113//! @{
114
115//! \brief constructs an ROMol from the metadata in a PNG stream
116/*!
117
118Looks through the metadata in the PNG to find the first tag that matches one of
119the tags in \c RDKit::PNGData. A molecule is constructed from this chunk.
120
121Throws a \c FileParseException if no suitable tag is found.
122
123The caller is responsible for the returned pointer.
124
125 */
127 std::istream &inStream,
128 const SmilesParserParams &params = SmilesParserParams());
129//! \brief constructs an ROMol from the metadata in a PNG file.
130//! See \c PNGStreamToMol() for more details.
132 const std::string &fname,
133 const SmilesParserParams &params = SmilesParserParams()) {
134 std::ifstream inStream(fname.c_str(), std::ios::binary);
135 if (!inStream || (inStream.bad())) {
136 throw BadFileException((boost::format("Bad input file %s") % fname).str());
137 }
138 return PNGStreamToMol(inStream, params);
139}
140//! \brief constructs an ROMol from the metadata in a PNG string.
141//! See \c PNGStreamToMol() for more details.
143 const std::string &data,
144 const SmilesParserParams &params = SmilesParserParams()) {
145 std::stringstream inStream(data);
146 return PNGStreamToMol(inStream, params);
147}
148
149//! \brief constructs a vector of ROMol from the metadata in a PNG stream
150/*!
151
152Looks through the metadata in the PNG to find tags that start with tagToUse
153(must be one of the tags in \c RDKit::PNGData). The molecules constructed from
154these data are returned.
155
156 */
157RDKIT_FILEPARSERS_EXPORT std::vector<std::unique_ptr<ROMol>> PNGStreamToMols(
158 std::istream &inStream, const std::string &tagToUse = PNGData::pklTag,
159 const SmilesParserParams &params = SmilesParserParams());
160//! \brief constructs a vector of ROMol from the metadata in a PNG file.
161//! See \c PNGStreamToMols() for more details.
162inline std::vector<std::unique_ptr<ROMol>> PNGFileToMols(
163 const std::string &fname, const std::string &tagToUse = PNGData::pklTag,
164 const SmilesParserParams &params = SmilesParserParams()) {
165 std::ifstream inStream(fname.c_str(), std::ios::binary);
166 if (!inStream || (inStream.bad())) {
167 throw BadFileException((boost::format("Bad input file %s") % fname).str());
168 }
169 return PNGStreamToMols(inStream, tagToUse, params);
170}
171//! \brief constructs a vector of ROMol from the metadata in a PNG string.
172//! See \c PNGStreamToMols() for more details.
173inline std::vector<std::unique_ptr<ROMol>> PNGStringToMols(
174 const std::string &data, const std::string &tagToUse = PNGData::pklTag,
175 const SmilesParserParams &params = SmilesParserParams()) {
176 std::stringstream inStream(data);
177 return PNGStreamToMols(inStream, tagToUse, params);
178}
179
180//! \brief adds metadata for an ROMol to the data from a PNG stream.
181//! The modified PNG data is returned.
182/*!
183
184 \param mol the molecule to add
185 \param iStream the stream to read from
186 \param params instance of PNGMetadataParams
187
188*/
190 const ROMol &mol, std::istream &iStream, const PNGMetadataParams &params);
191
192//! \brief adds metadata for an ROMol to the data from a PNG stream.
193//! The modified PNG data is returned.
194/*!
195
196 \param mol the molecule to add
197 \param iStream the stream to read from
198 \param includePkl include a molecule pickle
199 \param includeSmiles include CXSMILES for the molecule
200 \param includeMol include a mol block for the molecule
201
202*/
203inline std::string addMolToPNGStream(const ROMol &mol, std::istream &iStream,
204 bool includePkl = true,
205 bool includeSmiles = true,
206 bool includeMol = false) {
207 PNGMetadataParams params;
208 params.includePkl = includePkl;
209 params.includeSmiles = includeSmiles;
210 params.includeMol = includeMol;
211 return addMolToPNGStream(mol, iStream, params);
212}
213
214//! \brief adds metadata for an ROMol to a PNG string.
215//! The modified PNG data is returned.
216//! See \c addMolToPNGStream() for more details.
217inline std::string addMolToPNGString(const ROMol &mol,
218 const std::string &pngString,
219 bool includePkl = true,
220 bool includeSmiles = true,
221 bool includeMol = false) {
222 std::stringstream inStream(pngString);
223 return addMolToPNGStream(mol, inStream, includePkl, includeSmiles,
224 includeMol);
225}
226
227//! \brief adds metadata for an ROMol to a PNG string.
228//! The modified PNG data is returned.
229//! See \c addMolToPNGStream() for more details.
230inline std::string addMolToPNGString(const ROMol &mol,
231 const std::string &pngString,
232 const PNGMetadataParams &params) {
233 std::stringstream inStream(pngString);
234 return addMolToPNGStream(mol, inStream, params);
235}
236
237//! \brief adds metadata for an ROMol to the data from a PNG file.
238//! The modified PNG data is returned.
239//! See \c addMolToPNGStream() for more details.
240inline std::string addMolToPNGFile(const ROMol &mol, const std::string &fname,
241 bool includePkl = true,
242 bool includeSmiles = true,
243 bool includeMol = false) {
244 std::ifstream inStream(fname.c_str(), std::ios::binary);
245 return addMolToPNGStream(mol, inStream, includePkl, includeSmiles,
246 includeMol);
247}
248
249//! \brief adds metadata for an ROMol to the data from a PNG file.
250//! The modified PNG data is returned.
251//! See \c addMolToPNGStream() for more details.
252inline std::string addMolToPNGFile(const ROMol &mol, const std::string &fname,
253 const PNGMetadataParams &params) {
254 std::ifstream inStream(fname.c_str(), std::ios::binary);
255 return addMolToPNGStream(mol, inStream, params);
256}
257//! @}
258
259} // namespace RDKit
260
261#endif
pulls in the core RDKit functionality
used by various file parsing classes to indicate a bad file
static unsigned int getDefaultPickleProperties()
#define RDKIT_FILEPARSERS_EXPORT
Definition export.h:177
Tags used for PNG metadata.
RDKIT_FILEPARSERS_EXPORT const std::string molTag
RDKIT_FILEPARSERS_EXPORT const std::string smilesTag
RDKIT_FILEPARSERS_EXPORT const std::string pklTag
Std stuff.
RDKIT_FILEPARSERS_EXPORT std::string addMetadataToPNGStream(std::istream &iStream, const std::vector< std::pair< std::string, std::string > > &metadata, bool compressed=true)
adds metadata to a PNG stream. The modified PNG data is returned.
std::string addMolToPNGString(const ROMol &mol, const std::string &pngString, bool includePkl=true, bool includeSmiles=true, bool includeMol=false)
adds metadata for an ROMol to a PNG string. The modified PNG data is returned. See addMolToPNGStream(...
Definition PNGParser.h:217
std::vector< std::pair< std::string, std::string > > PNGStringToMetadata(const std::string &data)
returns the metadata (tEXt and zTXt chunks) from PNG data
Definition PNGParser.h:72
RDKIT_FILEPARSERS_EXPORT std::vector< std::unique_ptr< ROMol > > PNGStreamToMols(std::istream &inStream, const std::string &tagToUse=PNGData::pklTag, const SmilesParserParams &params=SmilesParserParams())
constructs a vector of ROMol from the metadata in a PNG stream
ROMol * PNGStringToMol(const std::string &data, const SmilesParserParams &params=SmilesParserParams())
constructs an ROMol from the metadata in a PNG string. See PNGStreamToMol() for more details.
Definition PNGParser.h:142
RDKIT_FILEPARSERS_EXPORT ROMol * PNGStreamToMol(std::istream &inStream, const SmilesParserParams &params=SmilesParserParams())
constructs an ROMol from the metadata in a PNG stream
std::vector< std::pair< std::string, std::string > > PNGFileToMetadata(const std::string &fname)
returns the metadata (tEXt and zTXt chunks) from PNG data
Definition PNGParser.h:62
std::string addMolToPNGFile(const ROMol &mol, const std::string &fname, bool includePkl=true, bool includeSmiles=true, bool includeMol=false)
adds metadata for an ROMol to the data from a PNG file. The modified PNG data is returned....
Definition PNGParser.h:240
std::string addMetadataToPNGString(const std::string &pngString, const std::vector< std::pair< std::string, std::string > > &metadata, bool compressed=true)
adds metadata to a PNG string. The modified PNG data is returned.
Definition PNGParser.h:93
std::string addMetadataToPNGFile(const std::string &fname, const std::vector< std::pair< std::string, std::string > > &metadata, bool compressed=true)
adds metadata to a PNG file. The modified PNG data is returned.
Definition PNGParser.h:103
RDKIT_FILEPARSERS_EXPORT std::string addMolToPNGStream(const ROMol &mol, std::istream &iStream, const PNGMetadataParams &params)
adds metadata for an ROMol to the data from a PNG stream. The modified PNG data is returned.
std::vector< std::unique_ptr< ROMol > > PNGFileToMols(const std::string &fname, const std::string &tagToUse=PNGData::pklTag, const SmilesParserParams &params=SmilesParserParams())
constructs a vector of ROMol from the metadata in a PNG file. See PNGStreamToMols() for more details.
Definition PNGParser.h:162
std::vector< std::unique_ptr< ROMol > > PNGStringToMols(const std::string &data, const std::string &tagToUse=PNGData::pklTag, const SmilesParserParams &params=SmilesParserParams())
constructs a vector of ROMol from the metadata in a PNG string. See PNGStreamToMols() for more detail...
Definition PNGParser.h:173
ROMol * PNGFileToMol(const std::string &fname, const SmilesParserParams &params=SmilesParserParams())
constructs an ROMol from the metadata in a PNG file. See PNGStreamToMol() for more details.
Definition PNGParser.h:131
RDKIT_FILEPARSERS_EXPORT std::vector< std::pair< std::string, std::string > > PNGStreamToMetadata(std::istream &inStream)
returns the metadata (tEXt and zTXt chunks) from PNG data
bool includeMol
include molblock for the molecule
Definition PNGParser.h:42
unsigned int propertyFlags
choose properties to be included in the pickle
Definition PNGParser.h:44
bool includePkl
include molecule pickle
Definition PNGParser.h:38
RestoreBondDirOption restoreBondDirs
choose what to do with bond dirs in the CXSMILES string
Definition PNGParser.h:50
bool includeSmiles
include CXSMILES for the molecule
Definition PNGParser.h:40
std::uint32_t cxSmilesFlags
choose CXSMILES fields to be included in the CXSMILES string
Definition PNGParser.h:48
SmilesWriteParams smilesWriteParams
choose SmilesWriteParams for the CXSMILES string
Definition PNGParser.h:46