11 #ifndef RD_FILEPARSERUTILS_H
12 #define RD_FILEPARSERUTILS_H
17 #include <boost/lexical_cast.hpp>
18 #include <boost/algorithm/string.hpp>
19 #include <boost/format.hpp>
26 namespace FileParserUtils {
29 std::string trimmed = boost::trim_copy(input);
30 if (acceptSpaces && trimmed ==
"") {
33 return boost::lexical_cast<T>(trimmed);
37 bool acceptSpaces =
true);
39 bool acceptSpaces =
true);
41 bool acceptSpaces =
true);
49 std::istream *inStream,
unsigned int &line,
RWMol *mol,
Conformer *&conf,
50 bool &chiralityPossible,
unsigned int &nAtoms,
unsigned int &nBonds,
51 bool strictParsing =
true,
bool expectMEND =
true);
55 std::istream *inStream,
unsigned int &line,
RWMol *mol,
Conformer *&conf,
56 bool &chiralityPossible,
unsigned int &nAtoms,
unsigned int &nBonds,
57 bool strictParsing =
true);
61 bool chiralityPossible,
70 const std::string &prefix,
71 const std::string &missingValueMarker =
"n/a") {
72 std::string atompn = pn.substr(prefix.size());
73 std::string strVect = mol.
getProp<std::string>(pn);
74 std::vector<std::string> tokens;
75 boost::split(tokens, strVect, boost::is_any_of(
" \t\n"),
76 boost::token_compress_on);
79 <<
"Property list " << pn <<
" too short, only " << tokens.size()
80 <<
" elements found. Ignoring it." << std::endl;
83 std::string mv = missingValueMarker;
84 size_t first_token = 0;
85 if (tokens.size() == mol.
getNumAtoms() + 1 && tokens[0].front() ==
'[' &&
86 tokens[0].back() ==
']') {
87 mv = std::string(tokens[0].begin() + 1, tokens[0].end() - 1);
92 <<
" is empty." << std::endl;
94 for (
size_t i = first_token; i < tokens.size(); ++i) {
95 if (tokens[i] != mv) {
96 unsigned int atomid = i - first_token;
98 T apv = boost::lexical_cast<T>(tokens[i]);
100 }
catch (
const boost::bad_lexical_cast &) {
102 <<
"Value " << tokens[i] <<
" for property " << pn <<
" of atom "
103 << atomid <<
" can not be parsed. Ignoring it." << std::endl;
110 template <
typename T>
112 const std::string missingValueMarker =
"n/a") {
114 if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
115 applyMolListPropToAtoms<T>(mol, pn, prefix, missingValueMarker);
123 ROMol &mol,
const std::string pn,
124 const std::string &missingValueMarker =
"n/a") {
127 if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
128 applyMolListPropToAtoms<std::string>(mol, pn, prefix, missingValueMarker);
131 if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
132 applyMolListPropToAtoms<std::int64_t>(mol, pn, prefix,
136 if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
137 applyMolListPropToAtoms<double>(mol, pn, prefix, missingValueMarker);
140 if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
141 applyMolListPropToAtoms<bool>(mol, pn, prefix, missingValueMarker);
151 ROMol &mol,
const std::string &missingValueMarker =
"n/a") {
156 template <
typename T>
158 std::string missingValueMarker =
"",
159 unsigned int lineSize = 190) {
162 if (!missingValueMarker.empty()) {
163 propVal += boost::str(boost::format(
"[%s] ") % missingValueMarker);
165 missingValueMarker =
"n/a";
167 for (
const auto &atom : mol.
atoms()) {
168 std::string apVal = missingValueMarker;
169 if (atom->hasProp(atomPropName)) {
170 T tVal = atom->getProp<T>(atomPropName);
171 apVal = boost::lexical_cast<std::string>(tVal);
175 if (propVal.length() + apVal.length() + 1 >= lineSize) {
178 res += propVal +
"\n";
181 propVal += apVal +
" ";
183 if (!propVal.empty()) {
191 ROMol &mol,
const std::string &atomPropName,
192 const std::string &missingValueMarker =
"",
unsigned int lineSize = 190) {
193 std::string molPropName =
"atom.iprop." + atomPropName;
195 getAtomPropertyList<boost::int64_t>(
196 mol, atomPropName, missingValueMarker, lineSize));
199 ROMol &mol,
const std::string &atomPropName,
200 const std::string &missingValueMarker =
"",
unsigned int lineSize = 190) {
201 std::string molPropName =
"atom.dprop." + atomPropName;
203 getAtomPropertyList<double>(mol, atomPropName, missingValueMarker,
207 ROMol &mol,
const std::string &atomPropName,
208 const std::string &missingValueMarker =
"",
unsigned int lineSize = 190) {
209 std::string molPropName =
"atom.bprop." + atomPropName;
211 getAtomPropertyList<bool>(mol, atomPropName, missingValueMarker,
215 ROMol &mol,
const std::string &atomPropName,
216 const std::string &missingValueMarker =
"",
unsigned int lineSize = 190) {
217 std::string molPropName =
"atom.prop." + atomPropName;
219 getAtomPropertyList<std::string>(mol, atomPropName,
220 missingValueMarker, lineSize));
#define BOOST_LOG(__arg__)
RDKIT_RDGENERAL_EXPORT RDLogger rdWarningLog
The class for representing atoms.
void getProp(const std::string &key, T &res) const
allows retrieval of a particular property value
void setProp(const std::string &key, T val, bool computed=false) const
sets a property value
STR_VECT getPropList(bool includePrivate=true, bool includeComputed=true) const
returns a list with the names of our properties
unsigned int getNumAtoms() const
returns our number of atoms
CXXAtomIterator< MolGraph, Atom * > atoms()
C++11 Range iterator.
Atom * getAtomWithIdx(unsigned int idx)
returns a pointer to a particular Atom
RWMol is a molecule class that is intended to be edited.
#define RDKIT_FILEPARSERS_EXPORT
void processMolPropertyList(ROMol &mol, const std::string pn, const std::string &missingValueMarker="n/a")
RDKIT_FILEPARSERS_EXPORT void finishMolProcessing(RWMol *res, bool chiralityPossible, bool sanitize, bool removeHs)
finishes up the processing (sanitization, etc.) of a molecule read from CTAB
void createAtomDoublePropertyList(ROMol &mol, const std::string &atomPropName, const std::string &missingValueMarker="", unsigned int lineSize=190)
RDKIT_FILEPARSERS_EXPORT double toDouble(const std::string &input, bool acceptSpaces=true)
void createAtomIntPropertyList(ROMol &mol, const std::string &atomPropName, const std::string &missingValueMarker="", unsigned int lineSize=190)
RDKIT_FILEPARSERS_EXPORT int toInt(const std::string &input, bool acceptSpaces=true)
RDKIT_FILEPARSERS_EXPORT Atom * replaceAtomWithQueryAtom(RWMol *mol, Atom *atom)
Deprecated, please use QueryOps::replaceAtomWithQueryAtom instead.
void createAtomStringPropertyList(ROMol &mol, const std::string &atomPropName, const std::string &missingValueMarker="", unsigned int lineSize=190)
void applyMolListPropToAtoms(ROMol &mol, const std::string &pn, const std::string &prefix, const std::string &missingValueMarker="n/a")
applies a particular property to the atoms as an atom property list
std::string getAtomPropertyList(ROMol &mol, const std::string &atomPropName, std::string missingValueMarker="", unsigned int lineSize=190)
void applyMolListPropsToAtoms(ROMol &mol, const std::string &prefix, const std::string missingValueMarker="n/a")
applies all properties matching a particular prefix as an atom property list
RDKIT_FILEPARSERS_EXPORT bool ParseV3000CTAB(std::istream *inStream, unsigned int &line, RWMol *mol, Conformer *&conf, bool &chiralityPossible, unsigned int &nAtoms, unsigned int &nBonds, bool strictParsing=true, bool expectMEND=true)
void processMolPropertyLists(ROMol &mol, const std::string &missingValueMarker="n/a")
RDKIT_FILEPARSERS_EXPORT bool ParseV2000CTAB(std::istream *inStream, unsigned int &line, RWMol *mol, Conformer *&conf, bool &chiralityPossible, unsigned int &nAtoms, unsigned int &nBonds, bool strictParsing=true)
static const std::string atomPropPrefix
RDKIT_FILEPARSERS_EXPORT std::string getV3000Line(std::istream *inStream, unsigned int &line)
T stripSpacesAndCast(const std::string &input, bool acceptSpaces=false)
RDKIT_FILEPARSERS_EXPORT unsigned int toUnsigned(const std::string &input, bool acceptSpaces=true)
void createAtomBoolPropertyList(ROMol &mol, const std::string &atomPropName, const std::string &missingValueMarker="", unsigned int lineSize=190)
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