mirror of
https://github.com/rdkit/rdkit.git
synced 2026-06-04 21:54:27 +08:00
* do not use new on loggers * del pointers in testDistGeom * Update Dict hasNonPOD status on bulk update * delete new Dicts in memtest1.cpp * fixes in MolSuppliers and testFMCS * PeriodicTable singleton as unique_ptr * fix EEM_arrays leak * fix leaks in testPBF * fix ParamCollection leak in test UFF * fix leaks in MMFF * clear prop dict before read in in pickler * fix leaks in testFreeSASA * fix leaks in test3D * modernize Dict.h & SmilesParse.cpp * fix leaks in testQuery * fix leaks in testCrystalFF * fix leaks in cxsmilesTest * fix leaks in Catalog & mol cat test * fix leaks in ShapeUtils & tests * fix leaks in testSubgraphs1 * fix leaks testFingerprintGenerators * fix leaks in Catalog/FilterCatalog * fix leaks in graphmolqueryTest * these changes reduce bison parse leaks * fixed leaks in testChirality.cpp * fix leaks + 2 tests in testMolWriter * fix 4m leaks in substructLibraryTest * small improvements to molTautomerTest; still leaks * fix leaks in testRGroupDecomp * fix leaks in test; parser still leaks * fix leaks in itertest * fix 4m leaks in testDepictor * fixes in smatest; still leaking due to parser * fixes in testSLNParse; still leaking due to parser * flex/bison: always add atoms with ownership; smarts error cleanup * fix leaks in testReaction * fix leaks in testSubstructMatch * fix leaks in resMolSupplierTest * fix leaks in testChemTransforms + bug in ChemTransforms * fix leaks in testPickler * fix leaks in testMolTransform * fix leaks in testFragCatalog * fix leak in testSLNParse. Still leaks due to Smiles * fixed most leaks in testMolSupplier * pre bison fix * fix some atom & bond parse problems; others still fail * bison smiles & smarts, atoms & bonds more or less fixed * fix leaks in molopstest.cpp * fix leaks in testFingerprints, MACCS.cpp & AtomPairs.cpp * fix leaks in moldraw2Dtest1 * fix leaks in testDescriptors * fix leaks in testInchi * fix leaks in testUFFForceFieldHelpers * fix leaks in hanoiTest & new_canon.h * fix leaks in testMMFFForceField * fix leaks in graphmolTest1 * fix leaks in testMMFFForceFieldHelpers * fix leaks in testDistGeomHelpers * fix leaks in testMolAlign * initialize occupancy & temp facto with default values * fix leak in TautomerTransform * updated suppressions * fix testStructChecker * fix logging & py tests * fix TautomerTransform class/struct issue * remove misplaced delete in testSLNParse * deinit in testAvalonLib1 * fix Avalon-triggered(?) bug in StructChecker/Pattern.cpp * fix random testMolWriter/Supplier fails - diversify output file names to avoid clashing. - unify Writers close/destruct behavior. - flushing/closing in tests. * use reset in FFs Params.cpp * comments on testMMFFForceField * unrequired 'if's added to mol suppliers * correct cast in FilterCatalog.h * use unique_ptr in MACCS Patterns * remove unrequred if in new_canon * update & move suppressions
145 lines
4.2 KiB
C++
145 lines
4.2 KiB
C++
// $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() {
|
|
// close the writer if it's still open:
|
|
if (dp_ostream != nullptr) close();
|
|
}
|
|
|
|
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++;
|
|
}
|
|
} // namespace RDKit
|