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>
30 std::string_view orig, std::string stripChars =
" \t\r\n") {
31 std::string_view res = orig;
32 auto start = res.find_first_not_of(stripChars);
33 if (start != std::string_view::npos) {
34 auto end = res.find_last_not_of(stripChars) + 1;
35 res = res.substr(start, end - start);
44 auto trimmed =
strip(input,
" ");
45 if (acceptSpaces && trimmed.empty()) {
48 return boost::lexical_cast<T>(trimmed);
56 bool acceptSpaces =
true);
58 bool acceptSpaces =
true);
60 bool acceptSpaces =
true);
62 bool acceptSpaces =
true);
64 bool acceptSpaces =
true);
66 bool acceptSpaces =
true);
70 const ROMol &tmol,
const boost::dynamic_bitset<> &wasAromatic,
71 int confId = -1,
unsigned int precision = 6);
74 unsigned int precision = 6) {
75 boost::dynamic_bitset<> wasAromatic(tmol.
getNumBonds());
76 return getV3000CTAB(tmol, wasAromatic, confId, precision);
84 std::istream *inStream,
unsigned int &line,
RWMol *mol,
Conformer *&conf,
85 bool &chiralityPossible,
unsigned int &nAtoms,
unsigned int &nBonds,
86 bool strictParsing =
true,
bool expectMEND =
true,
87 bool expectMacroAtoms =
false);
91 std::istream *inStream,
unsigned int &line,
RWMol *mol,
Conformer *&conf,
92 bool &chiralityPossible,
unsigned int &nAtoms,
unsigned int &nBonds,
93 bool strictParsing =
true);
98 RWMol *res,
bool chiralityPossible,
102 bool sanitize,
bool removeHs) {
115 const std::string &prefix,
116 const std::string &missingValueMarker =
"n/a") {
117 std::string atompn = pn.substr(prefix.size());
118 std::string strVect = mol.
getProp<std::string>(pn);
119 std::vector<std::string> tokens;
120 boost::split(tokens, strVect, boost::is_any_of(
" \t\n"),
121 boost::token_compress_on);
124 <<
"Property list " << pn <<
" too short, only " << tokens.size()
125 <<
" elements found. Ignoring it." << std::endl;
128 std::string mv = missingValueMarker;
129 size_t first_token = 0;
130 if (tokens.size() == mol.
getNumAtoms() + 1 && tokens[0].front() ==
'[' &&
131 tokens[0].back() ==
']') {
132 mv = std::string(tokens[0].begin() + 1, tokens[0].end() - 1);
137 <<
" is empty." << std::endl;
139 for (
size_t i = first_token; i < tokens.size(); ++i) {
140 if (tokens[i] != mv) {
141 unsigned int atomid = i - first_token;
143 T apv = boost::lexical_cast<T>(tokens[i]);
145 }
catch (
const boost::bad_lexical_cast &) {
147 <<
"Value " << tokens[i] <<
" for property " << pn <<
" of atom "
148 << atomid <<
" can not be parsed. Ignoring it." << std::endl;
158 const std::string missingValueMarker =
"n/a") {
160 if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
169 ROMol &mol,
const std::string pn,
170 const std::string &missingValueMarker =
"n/a") {
173 if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
177 if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
182 if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
186 if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
197 ROMol &mol,
const std::string &missingValueMarker =
"n/a") {
204 std::string missingValueMarker =
"",
205 unsigned int lineSize = 190) {
208 if (!missingValueMarker.empty()) {
209 propVal += boost::str(boost::format(
"[%s] ") % missingValueMarker);
211 missingValueMarker =
"n/a";
213 for (
const auto &atom : mol.
atoms()) {
214 std::string apVal = missingValueMarker;
215 if (atom->hasProp(atomPropName)) {
216 T tVal = atom->getProp<T>(atomPropName);
217 apVal = boost::lexical_cast<std::string>(tVal);
221 if (propVal.length() + apVal.length() + 1 >= lineSize) {
224 res += propVal +
"\n";
227 propVal += apVal +
" ";
229 if (!propVal.empty()) {
237 ROMol &mol,
const std::string &atomPropName,
238 const std::string &missingValueMarker =
"",
unsigned int lineSize = 190) {
239 std::string molPropName =
"atom.iprop." + atomPropName;
242 mol, atomPropName, missingValueMarker, lineSize));
245 ROMol &mol,
const std::string &atomPropName,
246 const std::string &missingValueMarker =
"",
unsigned int lineSize = 190) {
247 std::string molPropName =
"atom.dprop." + atomPropName;
253 ROMol &mol,
const std::string &atomPropName,
254 const std::string &missingValueMarker =
"",
unsigned int lineSize = 190) {
255 std::string molPropName =
"atom.bprop." + atomPropName;
261 ROMol &mol,
const std::string &atomPropName,
262 const std::string &missingValueMarker =
"",
unsigned int lineSize = 190) {
263 std::string molPropName =
"atom.prop." + atomPropName;
266 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 getNumBonds(bool onlyHeavy=1) const
returns our number of Bonds
Atom * getAtomWithIdx(unsigned int idx)
returns a pointer to a particular Atom
unsigned int getNumAtoms() const
returns our number of atoms
CXXAtomIterator< MolGraph, Atom * > atoms()
C++11 Range iterator.
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 moveAdditionalPropertiesToSGroups(RWMol &mol)
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, bool expectMacroAtoms=false)
RDKIT_FILEPARSERS_EXPORT std::string getV3000CTAB(const ROMol &tmol, const boost::dynamic_bitset<> &wasAromatic, int confId=-1, unsigned int precision=6)
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.
T stripSpacesAndCast(std::string_view input, bool acceptSpaces=false)
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)
RDKIT_FILEPARSERS_EXPORT void finishMolProcessing(RWMol *res, bool chiralityPossible, const v2::FileParsers::MolFileParserParams &ps)
RDKIT_FILEPARSERS_EXPORT std::string_view strip(std::string_view orig, std::string stripChars=" \t\r\n")
void applyMolListPropsToAtoms(ROMol &mol, const std::string &prefix, const std::string missingValueMarker="n/a")
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)
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)