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
|
// $Id$
//
// Copyright (C) 2003-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 <RDGeneral/BadFileException.h>
#include <RDGeneral/FileParseException.h>
#include <RDGeneral/RDLog.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>
#include "MolWriters.h"
#include "FileParsers.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
namespace RDKit {
SmilesWriter::SmilesWriter(const std::string &fileName,
const std::string &delimiter,
const std::string &nameHeader, bool includeHeader,
bool isomericSmiles, bool kekuleSmiles) {
if (fileName != "-") {
auto *tmpStream = new std::ofstream(fileName.c_str());
if (!tmpStream || !(*tmpStream) || (tmpStream->bad())) {
std::ostringstream errout;
errout << "Bad output file " << fileName;
throw BadFileException(errout.str());
}
dp_ostream = static_cast<std::ostream *>(tmpStream);
df_owner = true;
} else {
dp_ostream = static_cast<std::ostream *>(&std::cout);
df_owner = false;
}
this->init(delimiter, nameHeader, includeHeader, isomericSmiles,
kekuleSmiles);
}
SmilesWriter::SmilesWriter(std::ostream *outStream, std::string delimiter,
std::string nameHeader, bool includeHeader,
bool takeOwnership, bool isomericSmiles,
bool kekuleSmiles) {
PRECONDITION(outStream, "null stream");
if (outStream->bad()) {
throw FileParseException("Bad output stream.");
}
dp_ostream = outStream;
df_owner = takeOwnership;
this->init(delimiter, nameHeader, includeHeader, isomericSmiles,
kekuleSmiles);
}
void SmilesWriter::init(const std::string &delimiter,
const std::string &nameHeader, bool includeHeader,
bool isomericSmiles, bool kekuleSmiles) {
d_molid = 0;
d_delim = delimiter;
d_nameHeader = nameHeader;
df_includeHeader = includeHeader;
df_isomericSmiles = isomericSmiles;
df_kekuleSmiles = kekuleSmiles;
// these are set by the setProps function as required
d_props.clear();
}
void SmilesWriter::setProps(const STR_VECT &propNames) {
if (d_molid > 0) {
BOOST_LOG(rdErrorLog)
<< "ERROR: Atleast one molecule has already been written\n";
BOOST_LOG(rdErrorLog)
<< "ERROR: Cannot set properties now - ignoring setProps\n";
return;
}
d_props = propNames;
}
void SmilesWriter::dumpHeader() const {
CHECK_INVARIANT(dp_ostream, "no output stream");
if (df_includeHeader) {
(*dp_ostream) << "SMILES" << d_delim;
if (d_nameHeader != "") (*dp_ostream) << d_nameHeader << d_delim;
if (d_props.size() > 0) {
auto pi = d_props.begin();
(*dp_ostream) << (*pi);
pi++;
while (pi != d_props.end()) {
(*dp_ostream) << d_delim << (*pi);
pi++;
}
}
(*dp_ostream) << "\n";
}
}
SmilesWriter::~SmilesWriter() {
if (df_owner) {
// this has to be froma ofstream
// cast it back to fstream and clsoe it
// std::ofstream *tmpStream = static_cast<std::ofstream *>(dp_ostream);
// tmpStream->close();
delete dp_ostream;
}
}
void SmilesWriter::write(const ROMol &mol, int confId) {
RDUNUSED_PARAM(confId);
CHECK_INVARIANT(dp_ostream, "no output stream");
if (d_molid <= 0 && df_includeHeader) {
dumpHeader();
}
std::string name = "";
std::string smi = MolToSmiles(mol, df_isomericSmiles, df_kekuleSmiles);
(*dp_ostream) << smi;
if (d_nameHeader != "") {
if (!mol.getPropIfPresent(common_properties::_Name, name) ||
name.size() == 0) {
std::stringstream tstream;
tstream << d_molid;
name = tstream.str();
}
(*dp_ostream) << d_delim << name;
}
STR_VECT_CI pi;
for (pi = d_props.begin(); pi != d_props.end(); pi++) {
std::string pval;
// FIX: we will assume that any property that the user requests is castable
// to
// a std::string
if (mol.getPropIfPresent(*pi, pval)) {
(*dp_ostream) << d_delim << pval;
} else {
(*dp_ostream) << d_delim << "";
}
}
(*dp_ostream) << "\n";
d_molid++;
}
}
|