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 "Validate.h"
#include "Fragment.h"
#include <GraphMol/RDKitBase.h>
#include <GraphMol/ROMol.h>
#include <GraphMol/MolStandardize/FragmentCatalog/FragmentCatalogParams.h>
#include <GraphMol/Substruct/SubstructMatch.h>
#include <iostream>
#include <vector>
#include <string>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>
using namespace std;
using namespace RDKit;
namespace RDKit {
class RWMol;
class ROMol;
namespace MolStandardize {
std::vector<ValidationErrorInfo> RDKitValidation::validate(
const ROMol &mol, bool reportAllFailures) const {
ROMol molCopy = mol;
std::vector<ValidationErrorInfo> errors;
unsigned int na = mol.getNumAtoms();
if (!na) {
errors.push_back(
ValidationErrorInfo("ERROR: [NoAtomValidation] Molecule has no atoms"));
}
// loop over atoms
for (size_t i = 0; i < na; ++i) {
if (!reportAllFailures) {
if (errors.size() >= 1) {
break;
}
}
Atom *atom = molCopy.getAtomWithIdx(i);
try {
atom->calcExplicitValence();
} catch (const MolSanitizeException &e) {
errors.push_back(ValidationErrorInfo("INFO: [ValenceValidation] " +
std::string(e.message())));
}
}
return errors;
}
void NoAtomValidation::run(const ROMol &mol, bool reportAllFailures,
std::vector<ValidationErrorInfo> &errors) const {
RDUNUSED_PARAM(reportAllFailures);
unsigned int na = mol.getNumAtoms();
if (!na) {
errors.push_back(
ValidationErrorInfo("ERROR: [NoAtomValidation] Molecule has no atoms"));
}
}
void FragmentValidation::run(const ROMol &mol, bool reportAllFailures,
std::vector<ValidationErrorInfo> &errors) const {
// REVIEW: reportAllFailures is not being used here. is that correct?
RDUNUSED_PARAM(reportAllFailures);
std::string rdbase = getenv("RDBASE");
std::string fgrpFile = rdbase + "/Data/MolStandardize/fragmentPatterns.txt";
std::shared_ptr<FragmentCatalogParams> fparams(
new FragmentCatalogParams(fgrpFile));
FragmentCatalog fcat(fparams.get());
const std::vector<std::shared_ptr<ROMol>> &fgrps = fparams->getFuncGroups();
INT_VECT mapping;
VECT_INT_VECT atom_mapping;
std::vector<ROMOL_SPTR> frags =
MolOps::getMolFrags(mol, true, &mapping, &atom_mapping);
for (auto &fgrp : fgrps) {
std::string fname;
fgrp->getProp(common_properties::_Name, fname);
std::vector<RDKit::MatchVectType> res;
unsigned int matches = SubstructMatch(mol, *fgrp, res);
// std::cout << fname << " matches " << matches << std::endl;
if (matches != 0 && frags.size() != 0) {
VECT_INT_VECT substructmap; // store idxs of frag from substructmatch
for (const auto &match : res) {
std::vector<int> vec;
for (const auto &pair : match) {
vec.push_back(pair.second);
}
substructmap.push_back(vec);
}
// to stop the same fragment being reported many times if present
// multiple times in molecule
bool fpresent = false;
for (auto &molfragidx : atom_mapping) {
std::sort(molfragidx.begin(), molfragidx.end());
for (auto &substructidx : substructmap) {
std::sort(substructidx.begin(), substructidx.end());
// // help to debug...
// std::cout << "molfragidx: " <<
// std::endl; for (const auto
// &i : molfragidx)
// {
// std::cout << i; }; std::cout
// << std::endl; std::cout <<
//"substructidx: " << std::endl;
// for (const auto &i : substructidx) { std::cout << i; };
// std::cout <<
// std::endl;
// //
if ((molfragidx == substructidx) && !fpresent) {
std::string msg = fname + " is present";
errors.push_back(
ValidationErrorInfo("INFO: [FragmentValidation] " + msg));
fpresent = true;
}
}
}
}
}
}
void NeutralValidation::run(const ROMol &mol, bool reportAllFailures,
std::vector<ValidationErrorInfo> &errors) const {
RDUNUSED_PARAM(reportAllFailures);
int charge = RDKit::MolOps::getFormalCharge(mol);
if (charge != 0) {
std::string charge_str;
if (charge > 0) {
charge_str = "+" + std::to_string(charge);
} else {
charge_str = std::to_string(charge);
}
std::string msg = "Not an overall neutral system (" + charge_str + ')';
errors.push_back(ValidationErrorInfo("INFO: [NeutralValidation] " + msg));
}
}
void IsotopeValidation::run(const ROMol &mol, bool reportAllFailures,
std::vector<ValidationErrorInfo> &errors) const {
unsigned int na = mol.getNumAtoms();
std::set<string> isotopes;
// loop over atoms
for (size_t i = 0; i < na; ++i) {
if (!reportAllFailures) {
if (errors.size() >= 1) {
break;
}
}
const Atom *atom = mol.getAtomWithIdx(i);
unsigned int isotope = atom->getIsotope();
if (isotope != 0) {
std::string symbol = atom->getSymbol();
isotopes.insert(std::to_string(isotope) + symbol);
}
}
for (auto &isotope : isotopes) {
errors.push_back(ValidationErrorInfo(
"INFO: [IsotopeValidation] Molecule contains isotope " + isotope));
}
}
// constructor
MolVSValidation::MolVSValidation() {
std::vector<boost::shared_ptr<MolVSValidations>> validations = {
boost::make_shared<NoAtomValidation>(),
boost::make_shared<FragmentValidation>(),
boost::make_shared<NeutralValidation>(),
boost::make_shared<IsotopeValidation>()};
this->d_validations = validations;
}
// overloaded constructor
MolVSValidation::MolVSValidation(
const std::vector<boost::shared_ptr<MolVSValidations>> validations) {
this->d_validations = validations;
}
// copy constructor
MolVSValidation::MolVSValidation(const MolVSValidation &other) {
d_validations = other.d_validations;
}
MolVSValidation::~MolVSValidation(){};
std::vector<ValidationErrorInfo> MolVSValidation::validate(
const ROMol &mol, bool reportAllFailures) const {
std::vector<ValidationErrorInfo> errors;
for (const auto method : this->d_validations) {
method->run(mol, reportAllFailures, errors);
}
return errors;
}
std::vector<ValidationErrorInfo> AllowedAtomsValidation::validate(
const ROMol &mol, bool reportAllFailures) const {
std::vector<ValidationErrorInfo> errors;
unsigned int na = mol.getNumAtoms();
for (size_t i = 0; i < na; ++i) {
if (!reportAllFailures) {
if (errors.size() >= 1) {
break;
}
}
const Atom *qatom = mol.getAtomWithIdx(i);
bool match = false;
// checks to see qatom matches one of list of allowedAtoms
for (const auto &allowedAtom : this->d_allowedList) {
if (allowedAtom->Match(qatom)) {
match = true;
break;
}
}
// if no match, append to list of errors.
if (!match) {
std::string symbol = qatom->getSymbol();
errors.push_back(
ValidationErrorInfo("INFO: [AllowedAtomsValidation] Atom " + symbol +
" is not in allowedAtoms list"));
}
}
return errors;
}
std::vector<ValidationErrorInfo> DisallowedAtomsValidation::validate(
const ROMol &mol, bool reportAllFailures) const {
std::vector<ValidationErrorInfo> errors;
unsigned int na = mol.getNumAtoms();
for (size_t i = 0; i < na; ++i) {
if (!reportAllFailures) {
if (errors.size() >= 1) {
break;
}
}
const Atom *qatom = mol.getAtomWithIdx(i);
bool match = false;
// checks to see qatom matches one of list of allowedAtoms
for (const auto &disallowedAtom : this->d_disallowedList) {
if (disallowedAtom->Match(qatom)) {
match = true;
}
}
// if no match, append to list of errors.
if (match) {
std::string symbol = qatom->getSymbol();
errors.push_back(
ValidationErrorInfo("INFO: [DisallowedAtomsValidation] Atom " +
symbol + " is in disallowedAtoms list"));
}
}
return errors;
}
std::vector<ValidationErrorInfo> validateSmiles(const std::string &smiles) {
RWMOL_SPTR mol(SmilesToMol(smiles));
if (!mol) {
std::string message =
"SMILES Parse Error: syntax error for input: " + smiles;
throw ValueErrorException(message);
}
MolVSValidation vm;
std::vector<ValidationErrorInfo> errors = vm.validate(*mol, true);
return errors;
}
} // namespace MolStandardize
} // namespace RDKit
|