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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
|
//
// Copyright (C) 2018 Susan H. Leung
//
// @@ 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 "MolStandardize.h"
#include "Metal.h"
#include "Normalize.h"
#include "Tautomer.h"
#include "Fragment.h"
#include <GraphMol/RDKitBase.h>
#include <iostream>
#include <GraphMol/ROMol.h>
#include <GraphMol/MolOps.h>
#include <GraphMol/MolStandardize/TransformCatalog/TransformCatalogParams.h>
#include "Charge.h"
#include <GraphMol/SmilesParse/SmilesWrite.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <RDGeneral/RDThreads.h>
#ifdef RDK_BUILD_THREADSAFE_SSS
#include <thread>
#endif
#include <RDGeneral/BoostStartInclude.h>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <RDGeneral/BoostEndInclude.h>
using namespace std;
namespace RDKit {
namespace MolStandardize {
const CleanupParameters defaultCleanupParameters;
#define PT_OPT_GET(opt) params.opt = pt.get(#opt, params.opt)
void updateCleanupParamsFromJSON(CleanupParameters ¶ms,
const std::string &json) {
if (json.empty()) {
return;
}
std::istringstream ss;
ss.str(json);
boost::property_tree::ptree pt;
boost::property_tree::read_json(ss, pt);
PT_OPT_GET(rdbase);
PT_OPT_GET(normalizations);
PT_OPT_GET(acidbaseFile);
PT_OPT_GET(fragmentFile);
PT_OPT_GET(tautomerTransforms);
PT_OPT_GET(maxRestarts);
PT_OPT_GET(preferOrganic);
PT_OPT_GET(doCanonical);
PT_OPT_GET(maxTautomers);
PT_OPT_GET(maxTransforms);
PT_OPT_GET(tautomerRemoveSp3Stereo);
PT_OPT_GET(tautomerRemoveBondStereo);
PT_OPT_GET(tautomerRemoveIsotopicHs);
PT_OPT_GET(tautomerReassignStereo);
{
const auto norm_tfs = pt.get_child_optional("normalizationData");
if (norm_tfs) {
for (const auto &entry : *norm_tfs) {
std::string nm = entry.second.get<std::string>("name", "");
std::string smarts = entry.second.get<std::string>("smarts", "");
if (nm.empty() || smarts.empty()) {
BOOST_LOG(rdWarningLog)
<< " empty transformation name or SMARTS" << std::endl;
continue;
}
params.normalizationData.push_back(std::make_pair(nm, smarts));
}
}
}
{
const auto frag_tfs = pt.get_child_optional("fragmentData");
if (frag_tfs) {
for (const auto &entry : *frag_tfs) {
std::string nm = entry.second.get<std::string>("name", "");
std::string smarts = entry.second.get<std::string>("smarts", "");
if (nm.empty() || smarts.empty()) {
BOOST_LOG(rdWarningLog)
<< " empty transformation name or SMARTS" << std::endl;
continue;
}
params.fragmentData.push_back(std::make_pair(nm, smarts));
}
}
}
{
const auto ab_data = pt.get_child_optional("acidbaseData");
if (ab_data) {
for (const auto &entry : *ab_data) {
std::string nm = entry.second.get<std::string>("name", "");
std::string acid = entry.second.get<std::string>("acid", "");
std::string base = entry.second.get<std::string>("base", "");
if (nm.empty() || acid.empty() || base.empty()) {
BOOST_LOG(rdWarningLog)
<< " empty component in acidbaseData" << std::endl;
continue;
}
params.acidbaseData.push_back(std::make_tuple(nm, acid, base));
}
}
}
{
const auto taut_data = pt.get_child_optional("tautomerTransformData");
if (taut_data) {
for (const auto &entry : *taut_data) {
std::string nm = entry.second.get<std::string>("name", "");
std::string smarts = entry.second.get<std::string>("smarts", "");
std::string bonds = entry.second.get<std::string>("bonds", "");
std::string charges = entry.second.get<std::string>("charges", "");
if (nm.empty() || smarts.empty()) {
BOOST_LOG(rdWarningLog)
<< " empty component in tautomerTransformData" << std::endl;
continue;
}
params.tautomerTransformData.push_back(
std::make_tuple(nm, smarts, bonds, charges));
}
}
}
}
namespace {
template <typename FuncType>
void standardizeMultipleMolsInPlace(FuncType sfunc, std::vector<RWMol *> &mols,
int numThreads,
const CleanupParameters ¶ms) {
unsigned int numThreadsToUse = std::min(
static_cast<unsigned int>(mols.size()), getNumThreadsToUse(numThreads));
if (numThreadsToUse == 1) {
for (auto molp : mols) {
sfunc(*molp, params);
}
}
#ifdef RDK_BUILD_THREADSAFE_SSS
else {
auto func = [&](unsigned int tidx) {
for (auto mi = tidx; mi < mols.size(); mi += numThreads) {
sfunc(*mols[mi], params);
}
};
std::vector<std::thread> threads;
for (auto tidx = 0u; tidx < numThreadsToUse; ++tidx) {
threads.emplace_back(func, tidx);
}
for (auto &t : threads) {
if (t.joinable()) {
t.join();
}
}
}
#endif
}
void throwIfMolPtrListContainsDuplicates(const std::vector<RWMol *> &mols) {
// we could do this with an unordered set, but that requires memory allocation
// and in the "normal" case where all elements are unique we *will* have to
// insert all of them.
// This way is O(N^2) instead of O(NlogN) - actually closer to O(N) with an
// unordered_set - but we're doing essentially no work inside the loop.
// And, when you get down to it, this code is going to be a vanishingly small
// part of the runtime of any real standardization function, even for large
// N
for (auto i = 1u; i < mols.size(); ++i) {
for (auto j = 0u; j < i; ++j) {
if (mols[i] == mols[j]) {
throw ValueErrorException("duplicate molecule in input list");
}
}
}
}
} // namespace
RWMol *cleanup(const RWMol *mol, const CleanupParameters ¶ms) {
auto nmol = new RWMol(*mol);
cleanupInPlace(*nmol, params);
return nmol;
}
void cleanupInPlace(RWMol &mol, const CleanupParameters ¶ms) {
MolOps::removeHs(mol);
MolStandardize::MetalDisconnector md;
md.disconnectInPlace(mol);
MolStandardize::normalizeInPlace(mol, params);
MolStandardize::reionizeInPlace(mol, params);
bool cleanIt = true;
bool force = true;
MolOps::assignStereochemistry(mol, cleanIt, force);
}
void cleanupInPlace(std::vector<RWMol *> &mols, int numThreads,
const CleanupParameters ¶ms) {
throwIfMolPtrListContainsDuplicates(mols);
standardizeMultipleMolsInPlace(
static_cast<void (*)(RWMol &, const CleanupParameters &)>(cleanupInPlace),
mols, numThreads, params);
}
void tautomerParentInPlace(RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
if (!skip_standardize) {
cleanupInPlace(mol, params);
}
canonicalTautomerInPlace(mol, params);
cleanupInPlace(mol, params);
}
void tautomerParentInPlace(std::vector<RWMol *> &mols, int numThreads,
const CleanupParameters ¶ms,
bool skip_standardize) {
throwIfMolPtrListContainsDuplicates(mols);
auto sfunc = [skip_standardize](RWMol &m, const CleanupParameters &ps) {
tautomerParentInPlace(m, ps, skip_standardize);
};
standardizeMultipleMolsInPlace(sfunc, mols, numThreads, params);
}
RWMol *tautomerParent(const RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
std::unique_ptr<RWMol> res{new RWMol(mol)};
tautomerParentInPlace(*res, params, skip_standardize);
return res.release();
}
void fragmentParentInPlace(std::vector<RWMol *> &mols, int numThreads,
const CleanupParameters ¶ms,
bool skip_standardize) {
throwIfMolPtrListContainsDuplicates(mols);
auto sfunc = [skip_standardize](RWMol &m, const CleanupParameters &ps) {
fragmentParentInPlace(m, ps, skip_standardize);
};
standardizeMultipleMolsInPlace(sfunc, mols, numThreads, params);
}
void fragmentParentInPlace(RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
if (!skip_standardize) {
cleanupInPlace(mol, params);
}
LargestFragmentChooser lfragchooser(params.preferOrganic);
lfragchooser.chooseInPlace(mol);
}
// Return the fragment parent of a given molecule.
// The fragment parent is the largest organic covalent unit in the molecule.
//
RWMol *fragmentParent(const RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
std::unique_ptr<RWMol> res{new RWMol(mol)};
fragmentParentInPlace(*res, params, skip_standardize);
return res.release();
}
void stereoParentInPlace(std::vector<RWMol *> &mols, int numThreads,
const CleanupParameters ¶ms,
bool skip_standardize) {
throwIfMolPtrListContainsDuplicates(mols);
auto sfunc = [skip_standardize](RWMol &m, const CleanupParameters &ps) {
stereoParentInPlace(m, ps, skip_standardize);
};
standardizeMultipleMolsInPlace(sfunc, mols, numThreads, params);
}
void stereoParentInPlace(RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
if (!skip_standardize) {
cleanupInPlace(mol, params);
}
MolOps::removeStereochemistry(mol);
}
RWMol *stereoParent(const RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
std::unique_ptr<RWMol> res{new RWMol(mol)};
stereoParentInPlace(*res, params, skip_standardize);
return res.release();
}
void isotopeParentInPlace(std::vector<RWMol *> &mols, int numThreads,
const CleanupParameters ¶ms,
bool skip_standardize) {
throwIfMolPtrListContainsDuplicates(mols);
auto sfunc = [skip_standardize](RWMol &m, const CleanupParameters &ps) {
isotopeParentInPlace(m, ps, skip_standardize);
};
standardizeMultipleMolsInPlace(sfunc, mols, numThreads, params);
}
void isotopeParentInPlace(RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
if (!skip_standardize) {
cleanupInPlace(mol, params);
}
for (auto atom : mol.atoms()) {
atom->setIsotope(0);
}
}
RWMol *isotopeParent(const RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
std::unique_ptr<RWMol> res{new RWMol(mol)};
isotopeParentInPlace(*res, params, skip_standardize);
return res.release();
}
void chargeParentInPlace(std::vector<RWMol *> &mols, int numThreads,
const CleanupParameters ¶ms,
bool skip_standardize) {
throwIfMolPtrListContainsDuplicates(mols);
auto sfunc = [skip_standardize](RWMol &m, const CleanupParameters &ps) {
chargeParentInPlace(m, ps, skip_standardize);
};
standardizeMultipleMolsInPlace(sfunc, mols, numThreads, params);
}
void chargeParentInPlace(RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
fragmentParentInPlace(mol, params, skip_standardize);
Uncharger uncharger(params.doCanonical);
uncharger.unchargeInPlace(mol);
cleanupInPlace(mol, params);
}
RWMol *chargeParent(const RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
// Return the charge parent of a given molecule.
// The charge parent is the uncharged version of the fragment parent.
std::unique_ptr<RWMol> res{new RWMol(mol)};
chargeParentInPlace(*res, params, skip_standardize);
return res.release();
}
void superParentInPlace(RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
if (!skip_standardize) {
cleanupInPlace(mol, params);
}
// we can skip fragmentParent since the chargeParent takes care of that
chargeParentInPlace(mol, params, true);
isotopeParentInPlace(mol, params, true);
stereoParentInPlace(mol, params, true);
tautomerParentInPlace(mol, params, true);
cleanupInPlace(mol, params);
}
void superParentInPlace(std::vector<RWMol *> &mols, int numThreads,
const CleanupParameters ¶ms,
bool skip_standardize) {
throwIfMolPtrListContainsDuplicates(mols);
auto sfunc = [skip_standardize](RWMol &m, const CleanupParameters &ps) {
superParentInPlace(m, ps, skip_standardize);
};
standardizeMultipleMolsInPlace(sfunc, mols, numThreads, params);
}
RWMol *superParent(const RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
std::unique_ptr<RWMol> res{new RWMol(mol)};
superParentInPlace(*res, params, skip_standardize);
return res.release();
}
RWMol *normalize(const RWMol *mol, const CleanupParameters ¶ms) {
PRECONDITION(mol, "bad molecule");
std::unique_ptr<Normalizer> normalizer{normalizerFromParams(params)};
return static_cast<RWMol *>(normalizer->normalize(*mol));
}
RWMol *reionize(const RWMol *mol, const CleanupParameters ¶ms) {
PRECONDITION(mol, "bad molecule");
std::unique_ptr<Reionizer> reionizer{reionizerFromParams(params)};
return static_cast<RWMol *>(reionizer->reionize(*mol));
}
void normalizeInPlace(RWMol &mol, const CleanupParameters ¶ms) {
std::unique_ptr<Normalizer> normalizer{normalizerFromParams(params)};
normalizer->normalizeInPlace(mol);
}
void normalizeInPlace(std::vector<RWMol *> &mols, int numThreads,
const CleanupParameters ¶ms) {
throwIfMolPtrListContainsDuplicates(mols);
std::unique_ptr<Normalizer> normalizer{normalizerFromParams(params)};
auto sfunc = [&normalizer](RWMol &m, const CleanupParameters &) {
normalizer->normalizeInPlace(m);
};
standardizeMultipleMolsInPlace(sfunc, mols, numThreads, params);
}
void reionizeInPlace(RWMol &mol, const CleanupParameters ¶ms) {
std::unique_ptr<Reionizer> reionizer{reionizerFromParams(params)};
reionizer->reionizeInPlace(mol);
}
void reionizeInPlace(std::vector<RWMol *> &mols, int numThreads,
const CleanupParameters ¶ms) {
throwIfMolPtrListContainsDuplicates(mols);
std::unique_ptr<Reionizer> reionizer{reionizerFromParams(params)};
auto sfunc = [&reionizer](RWMol &m, const CleanupParameters &) {
reionizer->reionizeInPlace(m);
};
standardizeMultipleMolsInPlace(sfunc, mols, numThreads, params);
}
RWMol *removeFragments(const RWMol *mol, const CleanupParameters ¶ms) {
PRECONDITION(mol, "bad molecule");
std::unique_ptr<FragmentRemover> remover{fragmentRemoverFromParams(params)};
return static_cast<RWMol *>(remover->remove(*mol));
}
void removeFragmentsInPlace(RWMol &mol, const CleanupParameters ¶ms) {
std::unique_ptr<FragmentRemover> remover{fragmentRemoverFromParams(params)};
remover->removeInPlace(mol);
}
void removeFragmentsInPlace(std::vector<RWMol *> &mols, int numThreads,
const CleanupParameters ¶ms) {
throwIfMolPtrListContainsDuplicates(mols);
std::unique_ptr<FragmentRemover> remover{fragmentRemoverFromParams(params)};
auto sfunc = [&remover](RWMol &m, const CleanupParameters &) {
remover->removeInPlace(m);
};
standardizeMultipleMolsInPlace(sfunc, mols, numThreads, params);
}
RWMol *canonicalTautomer(const RWMol *mol, const CleanupParameters ¶ms) {
PRECONDITION(mol, "bad molecule");
std::unique_ptr<TautomerEnumerator> te{tautomerEnumeratorFromParams(params)};
return static_cast<RWMol *>(te->canonicalize(*mol));
}
void canonicalTautomerInPlace(RWMol &mol, const CleanupParameters ¶ms) {
std::unique_ptr<TautomerEnumerator> te{tautomerEnumeratorFromParams(params)};
te->canonicalizeInPlace(mol);
}
std::string standardizeSmiles(const std::string &smiles) {
std::unique_ptr<RWMol> mol{SmilesToMol(smiles, 0, false)};
if (!mol) {
std::string message =
"SMILES Parse Error: syntax error for input: " + smiles;
throw ValueErrorException(message);
}
cleanupInPlace(*mol);
return MolToSmiles(*mol);
}
std::vector<std::string> enumerateTautomerSmiles(
const std::string &smiles, const CleanupParameters ¶ms) {
std::unique_ptr<RWMol> mol(SmilesToMol(smiles, 0, false));
cleanupInPlace(*mol, params);
MolOps::sanitizeMol(*mol);
TautomerEnumerator te(params);
auto res = te.enumerate(*mol);
return res.smiles();
}
void disconnectOrganometallics(
RWMol &mol, RDKit::MolStandardize::MetalDisconnectorOptions mdo) {
RDKit::MolStandardize::MetalDisconnector md(mdo);
md.disconnect(mol);
}
ROMol *disconnectOrganometallics(
const ROMol &mol, RDKit::MolStandardize::MetalDisconnectorOptions mdo) {
RDKit::MolStandardize::MetalDisconnector md(mdo);
return md.disconnect(mol);
}
} // namespace MolStandardize
} // namespace RDKit
|