RDKit
Open-source cheminformatics and machine learning.
FileParserUtils.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2010-2019 Greg Landrum and Rational Discovery LLC
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_FILEPARSERUTILS_H
12 #define RD_FILEPARSERUTILS_H
13 
14 #include <string>
15 #include <iostream>
17 #include <boost/lexical_cast.hpp>
18 #include <boost/algorithm/string.hpp>
19 #include <boost/format.hpp>
21 
22 namespace RDKit {
23 class RWMol;
24 class Conformer;
25 
26 namespace FileParserUtils {
27 template <typename T>
28 T stripSpacesAndCast(const std::string &input, bool acceptSpaces = false) {
29  std::string trimmed = boost::trim_copy(input);
30  if (acceptSpaces && trimmed == "") {
31  return 0;
32  } else {
33  return boost::lexical_cast<T>(trimmed);
34  }
35 }
36 RDKIT_FILEPARSERS_EXPORT int toInt(const std::string &input,
37  bool acceptSpaces = false);
38 RDKIT_FILEPARSERS_EXPORT double toDouble(const std::string &input,
39  bool acceptSpaces = true);
40 
41 // reads a line from an MDL v3K CTAB
42 RDKIT_FILEPARSERS_EXPORT std::string getV3000Line(std::istream *inStream,
43  unsigned int &line);
44 
45 // nAtoms and nBonds are ignored on input, set on output
47  std::istream *inStream, unsigned int &line, RWMol *mol, Conformer *&conf,
48  bool &chiralityPossible, unsigned int &nAtoms, unsigned int &nBonds,
49  bool strictParsing = true, bool expectMEND = true);
50 
51 // nAtoms and nBonds are used
53  std::istream *inStream, unsigned int &line, RWMol *mol, Conformer *&conf,
54  bool &chiralityPossible, unsigned int &nAtoms, unsigned int &nBonds,
55  bool strictParsing = true);
56 
58 
59 //! applies a particular property to the atoms as an atom property list
60 template <typename T>
61 void applyMolListPropToAtoms(ROMol &mol, const std::string &pn,
62  const std::string &prefix,
63  const std::string &missingValueMarker = "n/a") {
64  std::string atompn = pn.substr(prefix.size());
65  std::string strVect = mol.getProp<std::string>(pn);
66  std::vector<std::string> tokens;
67  boost::split(tokens, strVect, boost::is_any_of(" \t\n"),
68  boost::token_compress_on);
69  if (tokens.size() < mol.getNumAtoms()) {
71  << "Property list " << pn << " too short, only " << tokens.size()
72  << " elements found. Ignoring it." << std::endl;
73  return;
74  }
75  std::string mv = missingValueMarker;
76  size_t first_token = 0;
77  if (tokens.size() == mol.getNumAtoms() + 1 && tokens[0].front() == '[' &&
78  tokens[0].back() == ']') {
79  mv = std::string(tokens[0].begin() + 1, tokens[0].end() - 1);
80  first_token = 1;
81  }
82  if (mv.empty()) {
83  BOOST_LOG(rdWarningLog) << "Missing value marker for property " << pn
84  << " is empty." << std::endl;
85  }
86  for (size_t i = first_token; i < tokens.size(); ++i) {
87  if (tokens[i] != mv) {
88  unsigned int atomid = i - first_token;
89  try {
90  T apv = boost::lexical_cast<T>(tokens[i]);
91  mol.getAtomWithIdx(atomid)->setProp(atompn, apv);
92  } catch (const boost::bad_lexical_cast &) {
94  << "Value " << tokens[i] << " for property " << pn << " of atom "
95  << atomid << " can not be parsed. Ignoring it." << std::endl;
96  }
97  }
98  }
99 }
100 
101 //! applies all properties matching a particular prefix as an atom property list
102 template <typename T>
103 void applyMolListPropsToAtoms(ROMol &mol, const std::string &prefix,
104  const std::string missingValueMarker = "n/a") {
105  for (auto pn : mol.getPropList()) {
106  if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
107  applyMolListPropToAtoms<T>(mol, pn, prefix, missingValueMarker);
108  }
109  }
110 }
111 static const std::string atomPropPrefix = "atom.";
112 //! if the property name matches our rules for atom property lists, we'll apply
113 //! it to the atoms
115  ROMol &mol, const std::string pn,
116  const std::string &missingValueMarker = "n/a") {
117  if (pn.find(atomPropPrefix) == 0 && pn.length() > atomPropPrefix.length()) {
118  std::string prefix = atomPropPrefix + "prop.";
119  if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
120  applyMolListPropToAtoms<std::string>(mol, pn, prefix, missingValueMarker);
121  } else {
122  prefix = atomPropPrefix + "iprop.";
123  if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
124  applyMolListPropToAtoms<std::int64_t>(mol, pn, prefix,
125  missingValueMarker);
126  } else {
127  prefix = atomPropPrefix + "dprop.";
128  if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
129  applyMolListPropToAtoms<double>(mol, pn, prefix, missingValueMarker);
130  } else {
131  prefix = atomPropPrefix + "bprop.";
132  if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
133  applyMolListPropToAtoms<bool>(mol, pn, prefix, missingValueMarker);
134  }
135  }
136  }
137  }
138  }
139 }
140 //! loops over all properties and applies the ones that match the rules for atom
141 //! property lists to the atoms
143  ROMol &mol, const std::string &missingValueMarker = "n/a") {
144  for (auto pn : mol.getPropList()) {
145  processMolPropertyList(mol, pn, missingValueMarker);
146  }
147 }
148 template <typename T>
149 std::string getAtomPropertyList(ROMol &mol, const std::string &atomPropName,
150  std::string missingValueMarker = "",
151  unsigned int lineSize = 190) {
152  std::string res;
153  std::string propVal;
154  if (!missingValueMarker.empty()) {
155  propVal += boost::str(boost::format("[%s] ") % missingValueMarker);
156  } else {
157  missingValueMarker = "n/a";
158  }
159  for (const auto &atom : mol.atoms()) {
160  std::string apVal = missingValueMarker;
161  if (atom->hasProp(atomPropName)) {
162  T tVal = atom->getProp<T>(atomPropName);
163  apVal = boost::lexical_cast<std::string>(tVal);
164  // seems like this should work, but it doesn't:
165  // atom->getProp(atomPropName,apVal);
166  }
167  if (propVal.length() + apVal.length() + 1 >= lineSize) {
168  // remove trailing space:
169  propVal.pop_back();
170  res += propVal + "\n";
171  propVal = "";
172  }
173  propVal += apVal + " ";
174  }
175  if (!propVal.empty()) {
176  // remove the trailing space:
177  propVal.pop_back();
178  res += propVal;
179  }
180  return res;
181 }
183  ROMol &mol, const std::string &atomPropName,
184  const std::string &missingValueMarker = "", unsigned int lineSize = 190) {
185  std::string molPropName = "atom.iprop." + atomPropName;
186  mol.setProp(molPropName,
187  getAtomPropertyList<boost::int64_t>(
188  mol, atomPropName, missingValueMarker, lineSize));
189 }
191  ROMol &mol, const std::string &atomPropName,
192  const std::string &missingValueMarker = "", unsigned int lineSize = 190) {
193  std::string molPropName = "atom.dprop." + atomPropName;
194  mol.setProp(molPropName,
195  getAtomPropertyList<double>(mol, atomPropName, missingValueMarker,
196  lineSize));
197 }
199  ROMol &mol, const std::string &atomPropName,
200  const std::string &missingValueMarker = "", unsigned int lineSize = 190) {
201  std::string molPropName = "atom.bprop." + atomPropName;
202  mol.setProp(molPropName,
203  getAtomPropertyList<bool>(mol, atomPropName, missingValueMarker,
204  lineSize));
205 }
207  ROMol &mol, const std::string &atomPropName,
208  const std::string &missingValueMarker = "", unsigned int lineSize = 190) {
209  std::string molPropName = "atom.prop." + atomPropName;
210  mol.setProp(molPropName,
211  getAtomPropertyList<std::string>(mol, atomPropName,
212  missingValueMarker, lineSize));
213 }
214 
215 } // namespace FileParserUtils
216 } // namespace RDKit
217 
218 #endif
void processMolPropertyLists(ROMol &mol, const std::string &missingValueMarker="n/a")
RDKIT_FILEPARSERS_EXPORT int toInt(const std::string &input, bool acceptSpaces=false)
static const std::string atomPropPrefix
#define RDKIT_FILEPARSERS_EXPORT
Definition: export.h:216
RDKIT_FILEPARSERS_EXPORT std::string getV3000Line(std::istream *inStream, unsigned int &line)
#define BOOST_LOG(__arg__)
Definition: RDLog.h:88
void createAtomBoolPropertyList(ROMol &mol, const std::string &atomPropName, const std::string &missingValueMarker="", unsigned int lineSize=190)
RWMol is a molecule class that is intended to be edited.
Definition: RWMol.h:31
unsigned int getNumAtoms(bool onlyExplicit=1) const
returns our number of atoms
void createAtomDoublePropertyList(ROMol &mol, const std::string &atomPropName, const std::string &missingValueMarker="", unsigned int lineSize=190)
RDKIT_RDGENERAL_EXPORT std::shared_ptr< boost::logging::rdLogger > rdWarningLog
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)
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 double toDouble(const std::string &input, bool acceptSpaces=true)
Std stuff.
Definition: Atom.h:30
std::string getAtomPropertyList(ROMol &mol, const std::string &atomPropName, std::string missingValueMarker="", unsigned int lineSize=190)
void createAtomStringPropertyList(ROMol &mol, const std::string &atomPropName, const std::string &missingValueMarker="", unsigned int lineSize=190)
void processMolPropertyList(ROMol &mol, const std::string pn, const std::string &missingValueMarker="n/a")
T stripSpacesAndCast(const std::string &input, bool acceptSpaces=false)
bool hasProp(const std::string &key) const
Definition: RDProps.h:117
void getProp(const std::string &key, T &res) const
allows retrieval of a particular property value
Definition: RDProps.h:98
void setProp(const std::string &key, T val, bool computed=false) const
sets a property value
Definition: RDProps.h:67
The class for representing 2D or 3D conformation of a molecule.
Definition: Conformer.h:42
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)
STR_VECT getPropList(bool includePrivate=true, bool includeComputed=true) const
returns a list with the names of our properties
Definition: RDProps.h:35
Atom * getAtomWithIdx(unsigned int idx)
returns a pointer to a particular Atom
RDKIT_FILEPARSERS_EXPORT Atom * replaceAtomWithQueryAtom(RWMol *mol, Atom *atom)
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
CXXAtomIterator< MolGraph, Atom * > atoms()
C++11 Range iterator.
Definition: ROMol.h:249
The class for representing atoms.
Definition: Atom.h:69
void createAtomIntPropertyList(ROMol &mol, const std::string &atomPropName, const std::string &missingValueMarker="", unsigned int lineSize=190)