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);
52 std::istream *inStream,
unsigned int &line,
RWMol *mol,
Conformer *&conf,
53 bool &chiralityPossible,
unsigned int &nAtoms,
unsigned int &nBonds,
54 bool strictParsing =
true,
bool expectMEND =
true);
58 std::istream *inStream,
unsigned int &line,
RWMol *mol,
Conformer *&conf,
59 bool &chiralityPossible,
unsigned int &nAtoms,
unsigned int &nBonds,
60 bool strictParsing =
true);
65 bool chiralityPossible,
74 const std::string &prefix,
75 const std::string &missingValueMarker =
"n/a") {
76 std::string atompn = pn.substr(prefix.size());
77 std::string strVect = mol.
getProp<std::string>(pn);
78 std::vector<std::string> tokens;
79 boost::split(tokens, strVect, boost::is_any_of(
" \t\n"),
80 boost::token_compress_on);
83 <<
"Property list " << pn <<
" too short, only " << tokens.size()
84 <<
" elements found. Ignoring it." << std::endl;
87 std::string mv = missingValueMarker;
88 size_t first_token = 0;
89 if (tokens.size() == mol.
getNumAtoms() + 1 && tokens[0].front() ==
'[' &&
90 tokens[0].back() ==
']') {
91 mv = std::string(tokens[0].begin() + 1, tokens[0].end() - 1);
96 <<
" is empty." << std::endl;
98 for (
size_t i = first_token; i < tokens.size(); ++i) {
99 if (tokens[i] != mv) {
100 unsigned int atomid = i - first_token;
102 T apv = boost::lexical_cast<T>(tokens[i]);
104 }
catch (
const boost::bad_lexical_cast &) {
106 <<
"Value " << tokens[i] <<
" for property " << pn <<
" of atom "
107 << atomid <<
" can not be parsed. Ignoring it." << std::endl;
115 template <
typename T>
117 const std::string missingValueMarker =
"n/a") {
119 if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
120 applyMolListPropToAtoms<T>(mol, pn, prefix, missingValueMarker);
128 ROMol &mol,
const std::string pn,
129 const std::string &missingValueMarker =
"n/a") {
132 if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
133 applyMolListPropToAtoms<std::string>(mol, pn, prefix, missingValueMarker);
136 if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
137 applyMolListPropToAtoms<std::int64_t>(mol, pn, prefix,
141 if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
142 applyMolListPropToAtoms<double>(mol, pn, prefix, missingValueMarker);
145 if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
146 applyMolListPropToAtoms<bool>(mol, pn, prefix, missingValueMarker);
156 ROMol &mol,
const std::string &missingValueMarker =
"n/a") {
161 template <
typename T>
163 std::string missingValueMarker =
"",
164 unsigned int lineSize = 190) {
167 if (!missingValueMarker.empty()) {
168 propVal += boost::str(boost::format(
"[%s] ") % missingValueMarker);
170 missingValueMarker =
"n/a";
172 for (
const auto &atom : mol.
atoms()) {
173 std::string apVal = missingValueMarker;
174 if (atom->hasProp(atomPropName)) {
175 T tVal = atom->getProp<T>(atomPropName);
176 apVal = boost::lexical_cast<std::string>(tVal);
180 if (propVal.length() + apVal.length() + 1 >= lineSize) {
183 res += propVal +
"\n";
186 propVal += apVal +
" ";
188 if (!propVal.empty()) {
196 ROMol &mol,
const std::string &atomPropName,
197 const std::string &missingValueMarker =
"",
unsigned int lineSize = 190) {
198 std::string molPropName =
"atom.iprop." + atomPropName;
200 getAtomPropertyList<boost::int64_t>(
201 mol, atomPropName, missingValueMarker, lineSize));
204 ROMol &mol,
const std::string &atomPropName,
205 const std::string &missingValueMarker =
"",
unsigned int lineSize = 190) {
206 std::string molPropName =
"atom.dprop." + atomPropName;
208 getAtomPropertyList<double>(mol, atomPropName, missingValueMarker,
212 ROMol &mol,
const std::string &atomPropName,
213 const std::string &missingValueMarker =
"",
unsigned int lineSize = 190) {
214 std::string molPropName =
"atom.bprop." + atomPropName;
216 getAtomPropertyList<bool>(mol, atomPropName, missingValueMarker,
220 ROMol &mol,
const std::string &atomPropName,
221 const std::string &missingValueMarker =
"",
unsigned int lineSize = 190) {
222 std::string molPropName =
"atom.prop." + atomPropName;
224 getAtomPropertyList<std::string>(mol, atomPropName,
225 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 std::string getV3000CTAB(const ROMol &tmol, int confId=-1)
RDKIT_FILEPARSERS_EXPORT void finishMolProcessing(RWMol *res, bool chiralityPossible, bool sanitize, bool removeHs)
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")
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