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
|
// $Id$
//
// Copyright (C) 2002-2012 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 <RDGeneral/FileParseException.h>
#include <RDGeneral/BadFileException.h>
#include <RDGeneral/StreamOps.h>
#include <RDGeneral/RDLog.h>
#include <GraphMol/SanitException.h>
#include <boost/algorithm/string.hpp>
#include "MolSupplier.h"
#include "FileParsers.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
namespace RDKit {
SDMolSupplier::SDMolSupplier(const std::string &fileName, bool sanitize,
bool removeHs, bool strictParsing) {
init();
// FIX: this binary mode of opening file is here because of a bug in VC++ 6.0
// the function "tellg" does not work correctly if we do not open it this way
// Jan 2009: Confirmed that this is still the case in visual studio 2008
std::istream *tmpStream = nullptr;
tmpStream = static_cast<std::istream *>(
new std::ifstream(fileName.c_str(), std::ios_base::binary));
if (!tmpStream || (!(*tmpStream)) || (tmpStream->bad())) {
std::ostringstream errout;
errout << "Bad input file " << fileName;
throw BadFileException(errout.str());
}
// dp_inStream = static_cast<std::istream *>(tmpStream);
dp_inStream = tmpStream;
df_owner = true;
d_molpos.push_back(dp_inStream->tellg());
df_sanitize = sanitize;
df_removeHs = removeHs;
df_strictParsing = strictParsing;
this->checkForEnd();
if (df_end) {
// checkForEnd() sets d_len if we're at EOF. undo that (was GitHub issue
// 19):
d_len = 0;
}
POSTCONDITION(dp_inStream, "bad instream");
}
SDMolSupplier::SDMolSupplier(std::istream *inStream, bool takeOwnership,
bool sanitize, bool removeHs, bool strictParsing) {
PRECONDITION(inStream, "bad stream");
init();
dp_inStream = inStream;
df_owner = takeOwnership;
d_molpos.push_back(dp_inStream->tellg());
df_sanitize = sanitize;
df_removeHs = removeHs;
df_strictParsing = strictParsing;
this->checkForEnd();
if (df_end) {
// checkForEnd() sets d_len if we're at EOF. undo that (was GitHub issue
// 19):
d_len = 0;
}
POSTCONDITION(dp_inStream, "bad instream");
}
void SDMolSupplier::init() {
ForwardSDMolSupplier::init();
d_len = -1;
d_last = 0;
}
void SDMolSupplier::setDataCommon(const std::string &text, bool sanitize,
bool removeHs) {
if (dp_inStream && df_owner) delete dp_inStream;
init();
std::istream *tmpStream = nullptr;
tmpStream = static_cast<std::istream *>(
new std::istringstream(text, std::ios_base::binary));
dp_inStream = tmpStream;
df_owner = true;
d_molpos.push_back(dp_inStream->tellg());
df_sanitize = sanitize;
df_removeHs = removeHs;
this->checkForEnd();
if (df_end) {
// checkForEnd() sets d_len if we're at EOF. undo that (was GitHub issue
// 19):
d_len = 0;
}
POSTCONDITION(dp_inStream, "bad instream");
}
void SDMolSupplier::setData(const std::string &text, bool sanitize,
bool removeHs) {
df_strictParsing = true;
setDataCommon(text, sanitize, removeHs);
}
void SDMolSupplier::setData(const std::string &text, bool sanitize,
bool removeHs, bool strictParsing) {
df_strictParsing = strictParsing;
setDataCommon(text, sanitize, removeHs);
}
void SDMolSupplier::checkForEnd() {
PRECONDITION(dp_inStream, "no stream");
// we will call it end of file if we have more than 4 contiguous empty lines
// or we reach end of file in the meantime
if (dp_inStream->eof()) {
df_end = true;
d_len = rdcast<int>(d_molpos.size());
return;
}
// we are not at the end of file, check for blank lines
unsigned int nempty = 0;
std::string tempStr, stmp;
for (unsigned int i = 0; i < 4; i++) {
tempStr = getLine(dp_inStream);
if (dp_inStream->eof()) {
df_end = true;
d_len = rdcast<int>(d_molpos.size());
return;
}
if (tempStr.find_first_not_of(" \t\r\n") == std::string::npos) {
++nempty;
}
}
if (nempty == 4) {
df_end = true;
d_len = rdcast<int>(d_molpos.size());
}
}
void SDMolSupplier::reset() {
PRECONDITION(dp_inStream, "no stream");
dp_inStream->clear();
dp_inStream->seekg(0, std::ios::beg);
df_end = false;
d_last = 0;
d_line = 0;
}
ROMol *SDMolSupplier::next() {
PRECONDITION(dp_inStream, "no stream");
if (df_end && d_last >= d_len) {
throw FileParseException("EOF hit.");
}
// set the stream to the current position
dp_inStream->seekg(d_molpos[d_last]);
std::string tempStr;
ROMol *res = nullptr;
// finally if we reached the end of the file set end to be true
if (dp_inStream->eof()) {
// FIX: we should probably be throwing an exception here
df_end = true;
d_len = rdcast<int>(d_molpos.size());
return res;
}
res = _next();
++d_last;
std::streampos posHold = dp_inStream->tellg();
this->checkForEnd();
if (!this->df_end && d_last >= static_cast<int>(d_molpos.size())) {
d_molpos.push_back(posHold);
}
return res;
}
std::string SDMolSupplier::getItemText(unsigned int idx) {
PRECONDITION(dp_inStream, "no stream");
unsigned int holder = d_last;
moveTo(idx);
std::streampos begP = d_molpos[idx];
std::streampos endP;
try {
moveTo(idx + 1);
endP = d_molpos[idx + 1];
} catch (FileParseException &) {
dp_inStream->clear();
dp_inStream->seekg(0, std::ios_base::end);
endP = dp_inStream->tellg();
}
d_last = holder;
auto *buff = new char[endP - begP];
dp_inStream->seekg(begP);
dp_inStream->read(buff, endP - begP);
std::string res(buff, endP - begP);
delete[] buff;
return res;
}
void SDMolSupplier::moveTo(unsigned int idx) {
PRECONDITION(dp_inStream, "no stream");
// dp_inStream->seekg() is called for all idx values
// and earlier calls to next() may have put the stream into a bad state
dp_inStream->clear();
// move until we hit the desired idx
if (idx < d_molpos.size()) {
dp_inStream->seekg(d_molpos[idx]);
d_last = idx;
} else {
std::string tempStr;
dp_inStream->seekg(d_molpos.back());
d_last = rdcast<int>(d_molpos.size()) - 1;
while ((d_last < static_cast<int>(idx)) && (!dp_inStream->eof())) {
d_line++;
tempStr = getLine(dp_inStream);
if (tempStr[0] == '$' && tempStr.substr(0, 4) == "$$$$") {
std::streampos posHold = dp_inStream->tellg();
this->checkForEnd();
if (!this->df_end) {
d_molpos.push_back(posHold);
d_last++;
}
}
}
// if we reached end of file without reaching "idx" we have an index error
if (dp_inStream->eof()) {
d_len = rdcast<int>(d_molpos.size());
std::ostringstream errout;
errout << "ERROR: Index error (idx = " << idx << ") : "
<< " we do no have enough mol blocks";
throw FileParseException(errout.str());
}
}
}
ROMol *SDMolSupplier::operator[](unsigned int idx) {
PRECONDITION(dp_inStream, "no stream");
// get the molecule with index idx
moveTo(idx);
return next();
}
unsigned int SDMolSupplier::length() {
PRECONDITION(dp_inStream, "no stream");
// return the number of mol blocks in the sdfile
if (d_len > 0 || (df_end && d_len == 0)) {
return d_len;
} else {
std::string tempStr;
d_len = rdcast<int>(d_molpos.size());
dp_inStream->seekg(d_molpos.back());
while (!dp_inStream->eof()) {
std::getline(*dp_inStream, tempStr);
if (tempStr.length() >= 4 && tempStr[0] == '$' && tempStr[1] == '$' &&
tempStr[2] == '$' && tempStr[3] == '$') {
std::streampos posHold = dp_inStream->tellg();
// don't worry about the last molecule:
this->checkForEnd();
if (!this->df_end) {
d_molpos.push_back(posHold);
++d_len;
}
}
}
// now remember to set the stream to the last postion we want to read
dp_inStream->clear();
dp_inStream->seekg(d_molpos[d_last]);
return d_len;
}
}
bool SDMolSupplier::atEnd() {
PRECONDITION(dp_inStream, "no stream");
return df_end;
}
void SDMolSupplier::setStreamIndices(const std::vector<std::streampos> &locs) {
d_molpos.clear();
d_molpos.resize(locs.size());
std::copy(locs.begin(), locs.end(), d_molpos.begin());
this->reset();
d_len = rdcast<int>(d_molpos.size());
}
}
|