Files
rdkit/Code/GraphMol/FileParsers/SDWriter.cpp
Ric a6b26253ff Fix (most of) mem problems (#2123)
* 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
2018-10-29 14:33:26 +00:00

161 lines
4.8 KiB
C++

// $Id$
//
// Copyright (C) 2003-2010 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 "MolWriters.h"
#include "FileParsers.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <boost/any.hpp>
namespace RDKit {
SDWriter::SDWriter(const std::string &fileName) {
if (fileName != "-") {
auto *tmpStream = new std::ofstream(fileName.c_str());
df_owner = true;
if (!tmpStream || !(*tmpStream) || (tmpStream->bad())) {
std::ostringstream errout;
errout << "Bad output file " << fileName;
throw BadFileException(errout.str());
}
dp_ostream = static_cast<std::ostream *>(tmpStream);
} else {
dp_ostream = static_cast<std::ostream *>(&std::cout);
df_owner = false;
}
d_molid = 0;
df_kekulize = true;
df_forceV3000 = false;
}
SDWriter::SDWriter(std::ostream *outStream, bool takeOwnership) {
PRECONDITION(outStream, "null stream");
if (outStream->bad()) {
throw FileParseException("Bad output stream");
}
dp_ostream = outStream;
df_owner = takeOwnership;
d_molid = 0;
df_kekulize = true;
df_forceV3000 = false;
}
SDWriter::~SDWriter() {
// close the writer if it's still open:
if (dp_ostream != nullptr) close();
}
void SDWriter::setProps(const STR_VECT &propNames) {
if (d_molid > 0) {
BOOST_LOG(rdWarningLog) << "WARNING: Setting property list after a few "
"molecules have been written\n";
}
d_props = propNames;
}
namespace {
void _writePropToStream(std::ostream *dp_ostream, const ROMol &mol,
const std::string &name, int d_molid) {
PRECONDITION(dp_ostream, "no output stream");
// write the property value
// FIX: we will assume for now that the desired property value is
// catable to a string
std::string pval;
try {
mol.getProp(name, pval);
} catch (boost::bad_any_cast &) {
return;
}
// write the property header line
(*dp_ostream) << "> <" << name << "> ";
if (d_molid >= 0) (*dp_ostream) << "(" << d_molid + 1 << ") ";
(*dp_ostream) << "\n";
(*dp_ostream) << pval << "\n";
// empty line after the property
(*dp_ostream) << "\n";
}
void _MolToSDStream(std::ostream *dp_ostream, const ROMol &mol, int confId,
bool df_kekulize, bool df_forceV3000, int d_molid,
STR_VECT *props) {
PRECONDITION(dp_ostream, "no output stream");
// write the molecule
(*dp_ostream) << MolToMolBlock(mol, true, confId, df_kekulize, df_forceV3000);
// now write the properties
STR_VECT_CI pi;
if (props && props->size() > 0) {
// check if we have any properties the user specified to write out
// in which loop over them and write them out
for (pi = props->begin(); pi != props->end(); pi++) {
if (mol.hasProp(*pi)) {
_writePropToStream(dp_ostream, mol, (*pi), d_molid);
}
}
} else {
// if use did not specify any properties, write all non computed properties
// out to the file
STR_VECT properties = mol.getPropList();
STR_VECT compLst;
mol.getPropIfPresent(RDKit::detail::computedPropName, compLst);
STR_VECT_CI pi;
for (pi = properties.begin(); pi != properties.end(); pi++) {
// ignore any of the following properties
if (((*pi) == RDKit::detail::computedPropName) ||
((*pi) == common_properties::_Name) || ((*pi) == "_MolFileInfo") ||
((*pi) == "_MolFileComments") ||
((*pi) == common_properties::_MolFileChiralFlag)) {
continue;
}
// check if this property is not computed
if (std::find(compLst.begin(), compLst.end(), (*pi)) == compLst.end()) {
_writePropToStream(dp_ostream, mol, (*pi), d_molid);
}
}
}
// add the $$$$ that marks the end of a molecule
(*dp_ostream) << "$$$$\n";
}
} // namespace
std::string SDWriter::getText(const ROMol &mol, int confId, bool kekulize,
bool forceV3000, int molid, STR_VECT *propNames) {
std::stringstream sstr;
_MolToSDStream(&sstr, mol, confId, kekulize, forceV3000, molid, propNames);
return sstr.str();
};
void SDWriter::write(const ROMol &mol, int confId) {
PRECONDITION(dp_ostream, "no output stream");
_MolToSDStream(dp_ostream, mol, confId, df_kekulize, df_forceV3000, d_molid,
&d_props);
++d_molid;
}
void SDWriter::writeProperty(const ROMol &mol, const std::string &name) {
PRECONDITION(dp_ostream, "no output stream");
_writePropToStream(dp_ostream, mol, name, d_molid);
}
} // namespace RDKit