1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
// $Id$
//
// Copyright (C) 2004-2008 Greg Landrum and Rational Discovery LLC
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include "FeatureParser.h"
#include "MolChemicalFeatureDef.h"
#include <RDGeneral/StreamOps.h>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <boost/shared_ptr.hpp>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/tokenizer.hpp>
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
#include <fstream>
#include <sstream>
#include <map>
namespace RDKit {
namespace Local {
typedef boost::tokenizer<boost::escaped_list_separator<char> > CommaTokenizer;
void getNextLine(std::istream &inStream, std::string &line,
unsigned int &lineNo) {
if (inStream.eof()) return;
line = "";
bool continuationLine = false;
while (!inStream.eof()) {
std::string tmpLine;
std::getline(inStream, tmpLine);
lineNo++;
// std::cerr << ">> " << lineNo << " " << tmpLine << std::endl;
if (tmpLine == "") continue;
if (tmpLine[0] != '#') {
// strip space at the end to check for a continuation line:
std::string stripLine =
boost::trim_right_copy_if(tmpLine, boost::is_any_of(" \t\r\n"));
if (stripLine == "") continue;
if (stripLine[stripLine.size() - 1] != '\\') {
if (continuationLine) {
// if it's a continuation line, strip any whitespace:
boost::trim_if(tmpLine, boost::is_any_of(" \t\r\n"));
}
line += tmpLine;
return;
} else {
continuationLine = true;
boost::trim_if(tmpLine, boost::is_any_of(" \t\r\n"));
line += tmpLine.substr(0, tmpLine.size() - 1);
}
}
}
}
// ------------------------------------------------------
bool expandAndTestSmarts(
std::string &smarts,
const std::map<std::string, std::string> &atomTypeDefs) {
for (const auto &atomTypeDef : atomTypeDefs) {
std::string atomName = atomTypeDef.first;
std::string atomSma = atomTypeDef.second;
boost::replace_all(smarts, atomName, atomSma);
}
RWMol *mol = nullptr;
try {
mol = SmartsToMol(smarts);
} catch (SmilesParseException &) {
return false;
}
if (mol) {
delete mol;
} else {
return false;
}
return true;
}
// ------------------------------------------------------
void parseAtomType(const std::string &inLine,
std::map<std::string, std::string> &atomTypeDefs,
const unsigned int &lineNo) {
boost::char_separator<char> sep(" \t");
boost::tokenizer<boost::char_separator<char> > tok(inLine, sep);
boost::tokenizer<boost::char_separator<char> >::iterator tokIt = tok.begin();
if (tokIt == tok.end()) {
throw FeatureFileParseException(lineNo, inLine,
"empty input line for AtomType");
}
std::string keyword = boost::to_upper_copy(*tokIt);
if (keyword != "ATOMTYPE") {
throw FeatureFileParseException(lineNo, inLine,
"bad input line for AtomType");
}
tokIt++;
if (tokIt == tok.end()) {
throw FeatureFileParseException(lineNo, inLine,
"bad AtomType line, missing label");
}
std::string atomType = *tokIt;
bool negater = false;
if (atomType[0] == '!') {
atomType.erase(0, 1);
negater = true;
}
atomType = "{" + atomType + "}";
tokIt++;
if (tokIt == tok.end()) {
throw FeatureFileParseException(lineNo, inLine,
"bad AtomType line, missing definition");
}
std::string sma;
if (atomTypeDefs.count(atomType)) {
std::string base = atomTypeDefs[atomType];
sma = "$(" + *tokIt + ")";
if (negater) {
std::string toAdd = "[!" + sma + ";";
boost::replace_first(base, "[", toAdd);
} else {
std::string toAdd = "," + sma + "]";
boost::replace_last(base, "]", toAdd);
}
sma = base;
} else {
sma = "$(" + *tokIt + ")";
}
// make it a valid smarts definition for an atom:
sma = "[" + sma + "]";
// make sure we get sensible SMARTS:
if (!expandAndTestSmarts(sma, atomTypeDefs)) {
std::string msg = "invalid SMARTS in AtomType (" + atomType + "): " + sma;
throw FeatureFileParseException(lineNo, inLine, msg);
}
// now cut the brackets back off:
sma = sma.substr(1, sma.size() - 2);
atomTypeDefs[atomType] = sma;
}
// ------------------------------------------------------
MolChemicalFeatureDef *parseFeatureDef(
std::istream &inStream, const std::string &inLine, unsigned int &lineNo,
const std::map<std::string, std::string> &atomTypeDefs) {
std::string nextLine = inLine;
MolChemicalFeatureDef *res = nullptr;
// handle a blank or comment first line:
boost::trim_if(nextLine, boost::is_any_of(" \t\r\n"));
while (nextLine == "" || nextLine[0] == '#') {
Local::getNextLine(inStream, nextLine, lineNo);
// need to check for EOS before we strip:
if (nextLine == "") {
// we hit EOS:
throw FeatureFileParseException(lineNo, inLine,
"EOF hit parsing feature definition");
}
boost::trim_if(nextLine, boost::is_any_of(" \t\r\n"));
}
boost::char_separator<char> sep(" \t");
boost::tokenizer<boost::char_separator<char> > tok(nextLine, sep);
if (tok.begin() == tok.end()) {
throw FeatureFileParseException(lineNo, inLine,
"bad DefineFeature line, no tokens found");
}
boost::tokenizer<boost::char_separator<char> >::iterator tokIt = tok.begin();
tokIt++;
if (tokIt == tok.end()) {
throw FeatureFileParseException(lineNo, inLine,
"bad DefineFeature line, missing subtype");
}
std::string subType = *tokIt;
tokIt++;
if (tokIt == tok.end()) {
throw FeatureFileParseException(lineNo, inLine,
"bad DefineFeature line, missing pattern");
}
std::string pattern = *tokIt;
//---------------
// make sure we get sensible SMARTS:
//
if (!expandAndTestSmarts(pattern, atomTypeDefs)) {
std::string msg =
"invalid SMARTS in DefineFeature for type " + subType + ": " + pattern;
throw FeatureFileParseException(lineNo, inLine, msg);
}
//---------------
// read out the rest of the definition
//
std::vector<double> weights;
std::string family = "";
bool foundEnd = false;
Local::getNextLine(inStream, nextLine, lineNo);
// std::getline(inStream,nextLine);
while (nextLine != "") {
boost::trim_if(nextLine, boost::is_any_of(" \t\r\n"));
if (nextLine != "" && nextLine[0] != '#') {
tok.assign(nextLine, sep);
tokIt = tok.begin();
std::string token = boost::to_upper_copy(*tokIt);
if (token == "ENDFEATURE") {
foundEnd = true;
break;
} else if (token == "FAMILY") {
tokIt++;
if (tokIt == tok.end()) {
std::string msg = "bad Type line for feature: " + subType;
throw FeatureFileParseException(lineNo, inLine, msg);
}
family = *tokIt;
} else if (token == "WEIGHTS") {
tokIt++;
if (tokIt == tok.end()) {
std::string msg = "bad Weights line for feature: " + subType;
throw FeatureFileParseException(lineNo, inLine, msg);
}
CommaTokenizer commaTok(*tokIt);
for (CommaTokenizer::const_iterator commaTokIt = commaTok.begin();
commaTokIt != commaTok.end(); commaTokIt++) {
std::string number = *commaTokIt;
try {
weights.push_back(boost::lexical_cast<double>(number));
} catch (boost::bad_lexical_cast &) {
std::string msg =
"bad weight value (" + number + ") for feature: " + subType;
throw FeatureFileParseException(lineNo, inLine, msg);
}
}
} else {
std::string msg = "bad input line for feature: " + subType;
throw FeatureFileParseException(lineNo, inLine, msg);
}
}
Local::getNextLine(inStream, nextLine, lineNo);
// std::getline(inStream,nextLine);
}
if (!foundEnd) {
std::string msg = "could not find EndFeature line for feature: " + subType;
throw FeatureFileParseException(lineNo, inLine, msg);
}
if (family == "") {
std::string msg = "did not find Family definition for feature: " + subType;
throw FeatureFileParseException(lineNo, inLine, msg);
}
//---------------
// Build the feature definition
//
res = new MolChemicalFeatureDef(pattern, family, subType);
if (weights.size()) {
res->setWeights(weights);
res->normalizeWeights();
}
return res;
}
} // end of namespace Local
// ------------------------------------------------------
int parseFeatureData(const std::string &defnText,
MolChemicalFeatureDef::CollectionType &featDefs) {
std::stringstream ss(defnText);
return parseFeatureData(ss, featDefs);
}
// ------------------------------------------------------
int parseFeatureData(std::istream &inStream,
MolChemicalFeatureDef::CollectionType &res) {
unsigned int lineNo = 0;
std::string inLine;
Local::getNextLine(inStream, inLine, lineNo);
std::map<std::string, std::string> atomTypeDefs;
while (!inStream.eof()) {
// clean any whitespace off the line:
boost::trim_if(inLine, boost::is_any_of(" \t\r\n"));
if (inLine != "" && inLine[0] != '#' && inLine[0] != '\n') {
boost::tokenizer<> tok(inLine);
boost::tokenizer<>::iterator tokIt = tok.begin();
std::string token = boost::to_upper_copy(*tokIt);
if (token == "ATOMTYPE") {
Local::parseAtomType(inLine, atomTypeDefs, lineNo);
} else if (token == "DEFINEFEATURE") {
MolChemicalFeatureDef *fDef =
Local::parseFeatureDef(inStream, inLine, lineNo, atomTypeDefs);
if (fDef) res.push_back(boost::shared_ptr<MolChemicalFeatureDef>(fDef));
} else {
throw FeatureFileParseException(lineNo, inLine,
"bad or missing keyword");
}
}
// std::getline(inStream,inLine);
Local::getNextLine(inStream, inLine, lineNo);
}
return 0;
}
// ------------------------------------------------------
int parseFeatureFile(const std::string &fileName,
MolChemicalFeatureDef::CollectionType &res) {
std::ifstream inStream(fileName.c_str());
if (!inStream || inStream.eof()) {
return -1;
}
return parseFeatureData(inStream, res);
}
}
|