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
20#include <boost/format.hpp>
21
22#include <string>
23#include <fstream>
24#include <sstream>
25
26namespace RDKit {
27
28//! Tags used for PNG metadata
29namespace PNGData {
30RDKIT_FILEPARSERS_EXPORT extern const std::string smilesTag;
31RDKIT_FILEPARSERS_EXPORT extern const std::string molTag;
32RDKIT_FILEPARSERS_EXPORT extern const std::string pklTag;
33} // namespace PNGData
34
35//! \name metadata to/from PNG
36//! @{
37
38//! \brief returns the metadata (tEXt and zTXt chunks) from PNG data
39RDKIT_FILEPARSERS_EXPORT std::vector<std::pair<std::string, std::string>>
41
42//! \brief returns the metadata (tEXt and zTXt chunks) from PNG data
43inline std::vector<std::pair<std::string, std::string>> PNGFileToMetadata(
44 const std::string &fname) {
45 std::ifstream inStream(fname.c_str(), std::ios::binary);
46 if (!inStream || (inStream.bad())) {
47 throw BadFileException((boost::format("Bad input file %s") % fname).str());
48 }
50}
51
52//! \brief returns the metadata (tEXt and zTXt chunks) from PNG data
53inline std::vector<std::pair<std::string, std::string>> PNGStringToMetadata(
54 const std::string &data) {
55 std::stringstream inStream(data);
57}
58
59//! \brief adds metadata to a PNG stream.
60//! The modified PNG data is returned.
61/*!
62
63The compressed flag is ignored if the RDKit is not built with
64boost::iostreams support
65
66*/
68 std::istream &iStream,
69 const std::vector<std::pair<std::string, std::string>> &metadata,
70 bool compressed = true);
71
72//! \brief adds metadata to a PNG string.
73//! The modified PNG data is returned.
74inline std::string addMetadataToPNGString(
75 const std::string &pngString,
76 const std::vector<std::pair<std::string, std::string>> &metadata,
77 bool compressed = true) {
78 std::stringstream inStream(pngString);
80}
81
82//! \brief adds metadata to a PNG file.
83//! The modified PNG data is returned.
84inline std::string addMetadataToPNGFile(
85 const std::string &fname,
86 const std::vector<std::pair<std::string, std::string>> &metadata,
87 bool compressed = true) {
88 std::ifstream inStream(fname.c_str(), std::ios::binary);
90}
91//! @}
92
93//! \name molecules to/from PNG
94//! @{
95
96//! \brief constructs an ROMol from the metadata in a PNG stream
97/*!
98
99Looks through the metadata in the PNG to find the first tag that matches one of
100the tags in \c RDKit::PNGData. A molecule is constructed from this chunk.
101
102Throws a \c FileParseException if no suitable tag is found.
103
104The caller is responsible for the returned pointer.
105
106 */
108 std::istream &inStream,
109 const SmilesParserParams &params = SmilesParserParams());
110//! \brief constructs an ROMol from the metadata in a PNG file.
111//! See \c PNGStreamToMol() for more details.
113 const std::string &fname,
114 const SmilesParserParams &params = SmilesParserParams()) {
115 std::ifstream inStream(fname.c_str(), std::ios::binary);
116 if (!inStream || (inStream.bad())) {
117 throw BadFileException((boost::format("Bad input file %s") % fname).str());
118 }
119 return PNGStreamToMol(inStream, params);
120}
121//! \brief constructs an ROMol from the metadata in a PNG string.
122//! See \c PNGStreamToMol() for more details.
124 const std::string &data,
125 const SmilesParserParams &params = SmilesParserParams()) {
126 std::stringstream inStream(data);
127 return PNGStreamToMol(inStream, params);
128}
129
130//! \brief constructs a vector of ROMol from the metadata in a PNG stream
131/*!
132
133Looks through the metadata in the PNG to find tags that start with tagToUse
134(must be one of the tags in \c RDKit::PNGData). The molecules constructed from
135these data are returned.
136
137 */
138RDKIT_FILEPARSERS_EXPORT std::vector<std::unique_ptr<ROMol>> PNGStreamToMols(
139 std::istream &inStream, const std::string &tagToUse = PNGData::pklTag,
140 const SmilesParserParams &params = SmilesParserParams());
141//! \brief constructs a vector of ROMol from the metadata in a PNG file.
142//! See \c PNGStreamToMols() for more details.
143inline std::vector<std::unique_ptr<ROMol>> PNGFileToMols(
144 const std::string &fname, const std::string &tagToUse = PNGData::pklTag,
145 const SmilesParserParams &params = SmilesParserParams()) {
146 std::ifstream inStream(fname.c_str(), std::ios::binary);
147 if (!inStream || (inStream.bad())) {
148 throw BadFileException((boost::format("Bad input file %s") % fname).str());
149 }
150 return PNGStreamToMols(inStream, tagToUse, params);
151}
152//! \brief constructs a vector of ROMol from the metadata in a PNG string.
153//! See \c PNGStreamToMols() for more details.
154inline std::vector<std::unique_ptr<ROMol>> PNGStringToMols(
155 const std::string &data, const std::string &tagToUse = PNGData::pklTag,
156 const SmilesParserParams &params = SmilesParserParams()) {
157 std::stringstream inStream(data);
158 return PNGStreamToMols(inStream, tagToUse, params);
159}
160
161//! \brief adds metadata for an ROMol to the data from a PNG stream.
162//! The modified PNG data is returned.
163/*!
164
165 \param mol the molecule to add
166 \param iStream the stream to read from
167 \param includePkl include a molecule pickle
168 \param includeSmiles include CXSMILES for the molecule
169 \param includeMol include a mol block for the molecule
170
171*/
173 const ROMol &mol, std::istream &iStream, bool includePkl = true,
174 bool includeSmiles = true, bool includeMol = false);
175
176//! \brief adds metadata for an ROMol to a PNG string.
177//! The modified PNG data is returned.
178//! See \c addMolToPNGStream() for more details.
179inline std::string addMolToPNGString(const ROMol &mol,
180 const std::string &pngString,
181 bool includePkl = true,
182 bool includeSmiles = true,
183 bool includeMol = false) {
184 std::stringstream inStream(pngString);
186 includeMol);
187}
188//! \brief adds metadata for an ROMol to the data from a PNG file.
189//! The modified PNG data is returned.
190//! See \c addMolToPNGStream() for more details.
191inline std::string addMolToPNGFile(const ROMol &mol, const std::string &fname,
192 bool includePkl = true,
193 bool includeSmiles = true,
194 bool includeMol = false) {
195 std::ifstream inStream(fname.c_str(), std::ios::binary);
197 includeMol);
198}
199//! @}
200
201} // namespace RDKit
202
203#endif
pulls in the core RDKit functionality
used by various file parsing classes to indicate a bad file
#define RDKIT_FILEPARSERS_EXPORT
Definition export.h:161
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:179
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:53
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:123
bool rdvalue_is(const RDValue_cast_t)
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:43
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:191
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:74
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:84
RDKIT_FILEPARSERS_EXPORT std::string addMolToPNGStream(const ROMol &mol, std::istream &iStream, bool includePkl=true, bool includeSmiles=true, bool includeMol=false)
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:143
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:154
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:112
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