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
|
// $Id$
//
// Copyright (C) 2007-2010 Greg Landrum
//
// @@ 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/BoostStartInclude.h>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <RDGeneral/BoostEndInclude.h>
#include "FileParsers.h"
#include "FileParserUtils.h"
#include "MolFileStereochem.h"
#include <RDGeneral/StreamOps.h>
#include <fstream>
#include <RDGeneral/FileParseException.h>
#include <RDGeneral/BadFileException.h>
#include <typeinfo>
namespace RDKit {
void ParseTPLAtomLine(std::string text, unsigned int lineNum, RWMol *mol,
Conformer *conf) {
PRECONDITION(mol, "no molecule");
PRECONDITION(conf, "no conformer");
std::vector<std::string> splitLine;
boost::split(splitLine, text, boost::is_any_of(" \t"),
boost::token_compress_on);
if (splitLine.size() < 8) {
std::ostringstream errout;
errout << "Atom line " << lineNum << " only has " << splitLine.size()
<< " tokens. 8 are required." << std::endl;
throw FileParseException(errout.str());
}
auto *atom = new Atom(splitLine[1]);
unsigned int atomId;
atomId = mol->addAtom(atom, false, true);
atom->setFormalCharge(FileParserUtils::stripSpacesAndCast<int>(splitLine[2]));
double partialChg = FileParserUtils::stripSpacesAndCast<double>(splitLine[3]);
atom->setProp("TPLCharge", partialChg);
double xp = FileParserUtils::stripSpacesAndCast<double>(splitLine[4]);
double yp = FileParserUtils::stripSpacesAndCast<double>(splitLine[5]);
double zp = FileParserUtils::stripSpacesAndCast<double>(splitLine[6]);
// coords in TPL files are in picometers, adjust:
xp /= 100.;
yp /= 100.;
zp /= 100.;
conf->setAtomPos(atomId, RDGeom::Point3D(xp, yp, zp));
unsigned int nBonds =
FileParserUtils::stripSpacesAndCast<unsigned int>(splitLine[7]);
// the only remaining info we care about is stereochem, and then only if
// the number of bonds is 4:
if (nBonds == 4 && splitLine.size() > 8 + nBonds) {
std::string stereoChem = splitLine[8 + nBonds];
atom->setProp("TPLStereoFlag", stereoChem);
}
}
void ParseTPLBondLine(std::string text, unsigned int lineNum, RWMol *mol) {
PRECONDITION(mol, "no molecule");
std::vector<std::string> splitLine;
boost::split(splitLine, text, boost::is_any_of(" \t"),
boost::token_compress_on);
if (splitLine.size() < 5) {
std::ostringstream errout;
errout << "Bond line " << lineNum << " only has " << splitLine.size()
<< " tokens. 5 are required." << std::endl;
throw FileParseException(errout.str());
}
std::string tplOrder = boost::trim_copy(splitLine[1]).substr(0, 3);
Bond::BondType bondOrder;
if (tplOrder == "1.5") {
bondOrder = Bond::AROMATIC;
} else if (tplOrder == "1.0") {
bondOrder = Bond::SINGLE;
} else if (tplOrder == "2.0") {
bondOrder = Bond::DOUBLE;
} else if (tplOrder == "3.0") {
bondOrder = Bond::TRIPLE;
} else {
std::ostringstream errout;
errout << "Bond line " << lineNum << " has unknown order: " << tplOrder
<< std::endl;
throw FileParseException(errout.str());
}
unsigned int idx1, idx2;
idx1 = FileParserUtils::stripSpacesAndCast<unsigned int>(splitLine[2]) - 1;
idx2 = FileParserUtils::stripSpacesAndCast<unsigned int>(splitLine[3]) - 1;
unsigned int bondIdx = mol->addBond(idx1, idx2, bondOrder) - 1;
std::string stereoFlag1 = "";
std::string stereoFlag2 = "";
if (splitLine.size() > 4) {
stereoFlag1 = splitLine[4];
if (splitLine.size() > 5) {
stereoFlag2 = splitLine[5];
}
}
mol->getBondWithIdx(bondIdx)->setProp("TPLBondDir1", stereoFlag1);
mol->getBondWithIdx(bondIdx)->setProp("TPLBondDir2", stereoFlag2);
}
Conformer *ParseConfData(std::istream *inStream, unsigned int &line, RWMol *mol,
unsigned int confId) {
PRECONDITION(inStream, "no stream");
PRECONDITION(mol, "no mol");
std::string tempStr;
std::vector<std::string> splitLine;
line++;
tempStr = getLine(inStream);
boost::split(splitLine, tempStr, boost::is_any_of(" \t"),
boost::token_compress_on);
if (splitLine[0] != "NAME") {
std::ostringstream errout;
errout << "Did not find NAME tag on line " << line
<< " while reading conformer " << confId << std::endl;
throw FileParseException(errout.str());
}
std::ostringstream propName;
propName << "Conf_" << mol->getNumConformers() << common_properties::_Name;
mol->setProp(propName.str(),
boost::trim_copy(tempStr.substr(4, tempStr.size() - 4)));
auto *conf = new Conformer(mol->getNumAtoms());
for (unsigned int i = 0; i < mol->getNumAtoms(); ++i) {
line++;
tempStr = getLine(inStream);
if (inStream->eof()) {
std::ostringstream errout;
errout << "EOF hit while reading conformer " << confId << std::endl;
throw FileParseException(errout.str());
}
boost::trim(tempStr);
boost::split(splitLine, tempStr, boost::is_any_of(" \t"),
boost::token_compress_on);
if (splitLine.size() < 3) {
std::ostringstream errout;
errout << "Did not find enough fields on line " << line
<< " while reading conformer " << confId << std::endl;
throw FileParseException(errout.str());
}
double xp = FileParserUtils::stripSpacesAndCast<double>(splitLine[0]);
double yp = FileParserUtils::stripSpacesAndCast<double>(splitLine[1]);
double zp = FileParserUtils::stripSpacesAndCast<double>(splitLine[2]);
// coords in TPL files are in picometers, adjust:
xp /= 100.;
yp /= 100.;
zp /= 100.;
conf->setAtomPos(i, RDGeom::Point3D(xp, yp, zp));
}
return conf;
}
//*************************************
//
// Every effort has been made to adhere to the BioCad tpl definition
//
//*************************************
RWMol *TPLDataStreamToMol(std::istream *inStream, unsigned int &line,
bool sanitize, bool skipFirstConf) {
PRECONDITION(inStream, "no stream");
std::string tempStr;
std::vector<std::string> splitText;
// format line:
line++;
tempStr = getLine(inStream);
if (inStream->eof()) {
return nullptr;
}
// comment line:
line++;
tempStr = getLine(inStream);
if (inStream->eof()) {
return nullptr;
}
// optional name line:
line++;
tempStr = getLine(inStream);
if (inStream->eof()) {
return nullptr;
}
auto *res = new RWMol();
if (tempStr.size() >= 4 && tempStr.substr(0, 4) == "NAME") {
tempStr = boost::trim_copy(tempStr.substr(4, tempStr.size() - 4));
res->setProp(common_properties::_Name, tempStr);
line++;
tempStr = getLine(inStream);
if (inStream->eof()) {
return res;
}
}
if (tempStr.size() >= 4 && tempStr.substr(0, 4) == "PROP") {
line++;
tempStr = getLine(inStream);
if (inStream->eof()) {
return res;
}
}
// we're at the counts line:
boost::split(splitText, tempStr, boost::is_any_of(" \t"),
boost::token_compress_on);
unsigned int nAtoms, nBonds;
nAtoms = FileParserUtils::stripSpacesAndCast<unsigned int>(splitText[0]);
nBonds = FileParserUtils::stripSpacesAndCast<unsigned int>(splitText[1]);
auto *conf = new Conformer(nAtoms);
conf->setId(0);
for (unsigned int i = 0; i < nAtoms; ++i) {
line++;
tempStr = getLine(inStream);
if (inStream->eof()) {
throw FileParseException("EOF hit while reading atoms.");
}
ParseTPLAtomLine(tempStr, line, res, conf);
}
res->addConformer(conf, true);
for (unsigned int i = 0; i < nBonds; ++i) {
line++;
tempStr = getLine(inStream);
if (inStream->eof()) {
throw FileParseException("EOF hit while reading bonds.");
}
ParseTPLBondLine(tempStr, line, res);
}
line++;
tempStr = getLine(inStream);
if (inStream->eof()) {
return res;
}
unsigned int nConfs = 0;
if (tempStr.size() >= 5 && tempStr.substr(0, 5) == "CONFS") {
boost::split(splitText, tempStr, boost::is_any_of(" \t"),
boost::token_compress_on);
nConfs = FileParserUtils::stripSpacesAndCast<unsigned int>(splitText[1]);
}
for (unsigned int i = 0; i < nConfs; ++i) {
Conformer *conf = ParseConfData(inStream, line, res, i + 1);
if (i > 0 || !skipFirstConf) {
conf->setId(i + 1);
res->addConformer(conf, true);
} else {
delete conf;
}
// there should be a blank line:
line++;
tempStr = getLine(inStream);
boost::trim(tempStr);
if (!inStream->eof() && tempStr != "") {
throw FileParseException("Found a non-blank line between conformers.");
}
}
if (sanitize && res) {
MolOps::sanitizeMol(*res);
}
return res;
}
//------------------------------------------------
//
// Read a molecule from a file
//
//------------------------------------------------
RWMol *TPLFileToMol(const std::string &fName, bool sanitize,
bool skipFirstConf) {
std::ifstream inStream(fName.c_str());
if (!inStream || (inStream.bad())) {
std::ostringstream errout;
errout << "Bad input file " << fName;
throw BadFileException(errout.str());
}
RWMol *res = nullptr;
if (!inStream.eof()) {
unsigned int line = 0;
res = TPLDataStreamToMol(&inStream, line, sanitize, skipFirstConf);
}
return res;
}
#if 0
RWMol *MolDataStreamToMol(std::istream &inStream, unsigned int &line,
bool sanitize){
return MolDataStreamToMol(&inStream,line,sanitize);
};
//------------------------------------------------
//
// Read a molecule from a string
//
//------------------------------------------------
RWMol *MolBlockToMol(const std::string &molBlock, bool sanitize){
std::istringstream inStream(molBlock);
RWMol *res=NULL;
unsigned int line = 0;
return MolDataStreamToMol(inStream, line, sanitize);
}
#endif
}
|