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