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
|
//
// 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/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));
}
}
}
}
RWMol *cleanup(const RWMol *mol, const CleanupParameters ¶ms) {
RWMol m(*mol);
MolOps::removeHs(m);
MolStandardize::MetalDisconnector md;
md.disconnect(m);
RWMOL_SPTR normalized(MolStandardize::normalize(&m, params));
RWMol *reionized = MolStandardize::reionize(normalized.get(), params);
bool cleanIt = true;
bool force = true;
MolOps::assignStereochemistry(*reionized, cleanIt, force);
// update properties of reionized using m.
reionized->updateProps(m);
return reionized;
}
RWMol *tautomerParent(const RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
const RWMol *cleaned = nullptr;
std::unique_ptr<RWMol> cleanedHolder;
if (!skip_standardize) {
cleanedHolder.reset(cleanup(mol, params));
cleaned = cleanedHolder.get();
} else {
cleaned = &mol;
}
std::unique_ptr<RWMol> ct{canonicalTautomer(cleaned, params)};
return cleanup(ct.get(), params);
}
// 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) {
const RWMol *cleaned = nullptr;
std::unique_ptr<RWMol> cleanedHolder;
if (!skip_standardize) {
cleanedHolder.reset(cleanup(mol, params));
cleaned = cleanedHolder.get();
} else {
cleaned = &mol;
}
LargestFragmentChooser lfragchooser(params.preferOrganic);
return static_cast<RWMol *>(lfragchooser.choose(*cleaned));
}
RWMol *stereoParent(const RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
RWMol *res;
if (!skip_standardize) {
res = cleanup(mol, params);
} else {
res = new RWMol(mol);
}
MolOps::removeStereochemistry(*res);
return res;
}
RWMol *isotopeParent(const RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
RWMol *res;
if (!skip_standardize) {
res = cleanup(mol, params);
} else {
res = new RWMol(mol);
}
for (auto atom : res->atoms()) {
atom->setIsotope(0);
}
return res;
}
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.
RWMOL_SPTR fragparent(fragmentParent(mol, params, skip_standardize));
// if fragment...
ROMol nm(*fragparent);
Uncharger uncharger(params.doCanonical);
ROMOL_SPTR uncharged(uncharger.uncharge(nm));
RWMol *omol = cleanup(static_cast<RWMol *>(uncharged.get()), params);
return omol;
}
RWMol *superParent(const RWMol &mol, const CleanupParameters ¶ms,
bool skip_standardize) {
std::unique_ptr<RWMol> res;
if (!skip_standardize) {
res.reset(cleanup(mol, params));
} else {
res.reset(new RWMol(mol));
}
// we can skip fragmentParent since the chargeParent takes care of that
res.reset(chargeParent(*res, params, true));
res.reset(isotopeParent(*res, params, true));
res.reset(stereoParent(*res, params, true));
res.reset(tautomerParent(*res, params, true));
return cleanup(*res, params);
}
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));
}
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));
}
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));
}
std::string standardizeSmiles(const std::string &smiles) {
RWMOL_SPTR mol(SmilesToMol(smiles, 0, false));
if (!mol) {
std::string message =
"SMILES Parse Error: syntax error for input: " + smiles;
throw ValueErrorException(message);
}
CleanupParameters params;
RWMOL_SPTR cleaned(cleanup(*mol, params));
return MolToSmiles(*cleaned);
}
std::vector<std::string> enumerateTautomerSmiles(
const std::string &smiles, const CleanupParameters ¶ms) {
std::unique_ptr<RWMol> mol(SmilesToMol(smiles, 0, false));
mol.reset(cleanup(mol.get(), params));
MolOps::sanitizeMol(*mol);
TautomerEnumerator te(params);
auto res = te.enumerate(*mol);
return res.smiles();
}
} // end of namespace MolStandardize
} // namespace RDKit
|