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 = true);
38 RDKIT_FILEPARSERS_EXPORT unsigned int toUnsigned(const std::string &input,
39  bool acceptSpaces = true);
40 RDKIT_FILEPARSERS_EXPORT double toDouble(const std::string &input,
41  bool acceptSpaces = true);
42 
43 // reads a line from an MDL v3K CTAB
44 RDKIT_FILEPARSERS_EXPORT std::string getV3000Line(std::istream *inStream,
45  unsigned int &line);
46 
47 // nAtoms and nBonds are ignored on input, set on output
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);
52 
53 // nAtoms and nBonds are used
55  std::istream *inStream, unsigned int &line, RWMol *mol, Conformer *&conf,
56  bool &chiralityPossible, unsigned int &nAtoms, unsigned int &nBonds,
57  bool strictParsing = true);
58 
59 //! finishes up the processing (sanitization, etc.) of a molecule read from CTAB
61  bool chiralityPossible,
62  bool sanitize, bool removeHs);
63 
64 //! Deprecated, please use QueryOps::replaceAtomWithQueryAtom instead
66 
67 //! applies a particular property to the atoms as an atom property list
68 template <typename T>
69 void applyMolListPropToAtoms(ROMol &mol, const std::string &pn,
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);
77  if (tokens.size() < mol.getNumAtoms()) {
79  << "Property list " << pn << " too short, only " << tokens.size()
80  << " elements found. Ignoring it." << std::endl;
81  return;
82  }
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);
88  first_token = 1;
89  }
90  if (mv.empty()) {
91  BOOST_LOG(rdWarningLog) << "Missing value marker for property " << pn
92  << " is empty." << std::endl;
93  }
94  for (size_t i = first_token; i < tokens.size(); ++i) {
95  if (tokens[i] != mv) {
96  unsigned int atomid = i - first_token;
97  try {
98  T apv = boost::lexical_cast<T>(tokens[i]);
99  mol.getAtomWithIdx(atomid)->setProp(atompn, apv);
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;
104  }
105  }
106  }
107 }
108 
109 //! applies all properties matching a particular prefix as an atom property list
110 template <typename T>
111 void applyMolListPropsToAtoms(ROMol &mol, const std::string &prefix,
112  const std::string missingValueMarker = "n/a") {
113  for (auto pn : mol.getPropList()) {
114  if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
115  applyMolListPropToAtoms<T>(mol, pn, prefix, missingValueMarker);
116  }
117  }
118 }
119 static const std::string atomPropPrefix = "atom.";
120 //! if the property name matches our rules for atom property lists, we'll apply
121 //! it to the atoms
123  ROMol &mol, const std::string pn,
124  const std::string &missingValueMarker = "n/a") {
125  if (pn.find(atomPropPrefix) == 0 && pn.length() > atomPropPrefix.length()) {
126  std::string prefix = atomPropPrefix + "prop.";
127  if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
128  applyMolListPropToAtoms<std::string>(mol, pn, prefix, missingValueMarker);
129  } else {
130  prefix = atomPropPrefix + "iprop.";
131  if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
132  applyMolListPropToAtoms<std::int64_t>(mol, pn, prefix,
133  missingValueMarker);
134  } else {
135  prefix = atomPropPrefix + "dprop.";
136  if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
137  applyMolListPropToAtoms<double>(mol, pn, prefix, missingValueMarker);
138  } else {
139  prefix = atomPropPrefix + "bprop.";
140  if (pn.find(prefix) == 0 && pn.length() > prefix.length()) {
141  applyMolListPropToAtoms<bool>(mol, pn, prefix, missingValueMarker);
142  }
143  }
144  }
145  }
146  }
147 }
148 //! loops over all properties and applies the ones that match the rules for atom
149 //! property lists to the atoms
151  ROMol &mol, const std::string &missingValueMarker = "n/a") {
152  for (auto pn : mol.getPropList()) {
153  processMolPropertyList(mol, pn, missingValueMarker);
154  }
155 }
156 template <typename T>
157 std::string getAtomPropertyList(ROMol &mol, const std::string &atomPropName,
158  std::string missingValueMarker = "",
159  unsigned int lineSize = 190) {
160  std::string res;
161  std::string propVal;
162  if (!missingValueMarker.empty()) {
163  propVal += boost::str(boost::format("[%s] ") % missingValueMarker);
164  } else {
165  missingValueMarker = "n/a";
166  }
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);
172  // seems like this should work, but it doesn't:
173  // atom->getProp(atomPropName,apVal);
174  }
175  if (propVal.length() + apVal.length() + 1 >= lineSize) {
176  // remove trailing space:
177  propVal.pop_back();
178  res += propVal + "\n";
179  propVal = "";
180  }
181  propVal += apVal + " ";
182  }
183  if (!propVal.empty()) {
184  // remove the trailing space:
185  propVal.pop_back();
186  res += propVal;
187  }
188  return res;
189 }
191  ROMol &mol, const std::string &atomPropName,
192  const std::string &missingValueMarker = "", unsigned int lineSize = 190) {
193  std::string molPropName = "atom.iprop." + atomPropName;
194  mol.setProp(molPropName,
195  getAtomPropertyList<boost::int64_t>(
196  mol, atomPropName, missingValueMarker, lineSize));
197 }
199  ROMol &mol, const std::string &atomPropName,
200  const std::string &missingValueMarker = "", unsigned int lineSize = 190) {
201  std::string molPropName = "atom.dprop." + atomPropName;
202  mol.setProp(molPropName,
203  getAtomPropertyList<double>(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.bprop." + atomPropName;
210  mol.setProp(molPropName,
211  getAtomPropertyList<bool>(mol, atomPropName, missingValueMarker,
212  lineSize));
213 }
215  ROMol &mol, const std::string &atomPropName,
216  const std::string &missingValueMarker = "", unsigned int lineSize = 190) {
217  std::string molPropName = "atom.prop." + atomPropName;
218  mol.setProp(molPropName,
219  getAtomPropertyList<std::string>(mol, atomPropName,
220  missingValueMarker, lineSize));
221 }
222 
223 } // namespace FileParserUtils
224 } // namespace RDKit
225 
226 #endif
#define BOOST_LOG(__arg__)
Definition: RDLog.h:90
RDKIT_RDGENERAL_EXPORT RDLogger rdWarningLog
The class for representing atoms.
Definition: Atom.h:68
The class for representing 2D or 3D conformation of a molecule.
Definition: Conformer.h:45
void getProp(const std::string &key, T &res) const
allows retrieval of a particular property value
Definition: RDProps.h:102
void setProp(const std::string &key, T val, bool computed=false) const
sets a property value
Definition: RDProps.h:72
STR_VECT getPropList(bool includePrivate=true, bool includeComputed=true) const
returns a list with the names of our properties
Definition: RDProps.h:40
unsigned int getNumAtoms() const
returns our number of atoms
Definition: ROMol.h:332
CXXAtomIterator< MolGraph, Atom * > atoms()
C++11 Range iterator.
Definition: ROMol.h:256
Atom * getAtomWithIdx(unsigned int idx)
returns a pointer to a particular Atom
RWMol is a molecule class that is intended to be edited.
Definition: RWMol.h:32
#define RDKIT_FILEPARSERS_EXPORT
Definition: export.h:153
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
Std stuff.
Definition: Abbreviations.h:18