RDKit: Validate.h Source File

| RDKit Open-source cheminformatics and machine learning.

Loading...
Searching...
No Matches
Validate.h
Go to the documentation of this file.
1//
2// Copyright (C) 2018-2021 Susan H. Leung and other RDKit contributors
3//
4// @@ All Rights Reserved @@
5// This file is part of the RDKit.
6// The contents are covered by the terms of the BSD license
7// which is included in the file license.txt, found at the root
8// of the RDKit source tree.
9//
10/*! \file Validate.h
11
12 \brief Defines the ValidationErrorInfo class and four different
13 validation methods: RDKitValidation, MolVSValidation, AllowedAtomsValidation,
14 DisallowedAtomsValidation.
15
16*/
17#include <RDGeneral/export.h>
18#ifndef RD_VALIDATE_H
19#define RD_VALIDATE_H
20
21#include <GraphMol/RDKitBase.h>
22#include <GraphMol/ROMol.h>
23#include <GraphMol/Atom.h>
24#include <exception>
25#include <string>
26#include <utility>
27#include <vector>
28
29namespace RDKit {
30class RWMol;
31class ROMol;
32class Conformer;
33
34namespace MolStandardize {
35
36//! The ValidationErrorInfo class is used to store the information returned by a
37/// ValidationMethod validate.
38 using ValidationErrorInfo = std::string;
39
40//! The ValidationMethod class is the abstract base class upon which all the
41/// four different ValidationMethods inherit from.
42 class RDKIT_MOLSTANDARDIZE_EXPORT ValidationMethod {
43 public:
44 ValidationMethod() = default;
45 virtual ~ValidationMethod() = default;
46
47 virtual std::vector<ValidationErrorInfo> validate(
48 const ROMol &mol, bool reportAllFailures) const = 0;
49 virtual std::shared_ptr<ValidationMethod> copy() const = 0;
50};
51
52//! The CompositeValidation class provides a simple way to apply a collection of
53// ValidationMethod instances in sequence
54 class RDKIT_MOLSTANDARDIZE_EXPORT CompositeValidation
55 : public ValidationMethod {
56 public:
57 CompositeValidation(
58 const std::vector<std::shared_ptr<ValidationMethod>> &validations)
59 : validations(validations) {};
60
61 std::vector<ValidationErrorInfo> validate(
62 const ROMol &mol, bool reportAllFailures) const override;
63
64 std::shared_ptr<ValidationMethod> copy() const override {
65 return std::make_shared<CompositeValidation>(*this);
66 }
67
68 private:
69 std::vector<std::shared_ptr<ValidationMethod>> validations;
70};
71
72//! The RDKitValidation class throws an error when there are no atoms in the
73/// molecule or when there is incorrect atom valency.
74/*!
75
76 <b>Notes:</b>
77 - RDKit automatically throws up atom valency issues but this class was made
78 for completeness of the project.
79*/
80 class RDKIT_MOLSTANDARDIZE_EXPORT RDKitValidation : public ValidationMethod {
81 public:
82 RDKitValidation(bool allowEmptyMolecules = false)
83 : allowEmptyMolecules(allowEmptyMolecules) {};
84
85 std::vector<ValidationErrorInfo> validate(
86 const ROMol &mol, bool reportAllFailures) const override;
87
88 std::shared_ptr<ValidationMethod> copy() const override {
89 return std::make_shared<RDKitValidation>(*this);
90 }
91
92 bool allowEmptyMolecules;
93};
94
95//////////////////////////////
96/// MolVS Validations
97//
98
99//! The NoAtomValidation class throws an error if no atoms are present in the
100/// molecule.
101 class RDKIT_MOLSTANDARDIZE_EXPORT NoAtomValidation : public ValidationMethod {
102 public:
103 std::vector<ValidationErrorInfo> validate(
104 const ROMol &mol, bool reportAllFailures) const override;
105
106 std::shared_ptr<ValidationMethod> copy() const override {
107 return std::make_shared<NoAtomValidation>(*this);
108 }
109};
110
111//! The FragmentValidation class logs if certain fragments are present.
112 class RDKIT_MOLSTANDARDIZE_EXPORT FragmentValidation : public ValidationMethod {
113 public:
114 std::vector<ValidationErrorInfo> validate(
115 const ROMol &mol, bool reportAllFailures) const override;
116
117 std::shared_ptr<ValidationMethod> copy() const override {
118 return std::make_shared<FragmentValidation>(*this);
119 }
120};
121
122//! The NeutralValidation class logs if not an overall neutral system.
123 class RDKIT_MOLSTANDARDIZE_EXPORT NeutralValidation : public ValidationMethod {
124 public:
125 std::vector<ValidationErrorInfo> validate(
126 const ROMol &mol, bool reportAllFailures) const override;
127
128 std::shared_ptr<ValidationMethod> copy() const override {
129 return std::make_shared<NeutralValidation>(*this);
130 }
131};
132
133//! The IsotopeValidation class logs if molecule contains isotopes.
134/*!
135 <b>Notes:</b>
136 - By default, this class will return an error every time an isotopic
137 number is specified. When the `strict` constructor parameter is passed a
138 `true` argument, an error is returned only if the specified isotopic number
139 is not found in the RDKit periodic table.
140*/
141 class RDKIT_MOLSTANDARDIZE_EXPORT IsotopeValidation : public ValidationMethod {
142 public:
143 IsotopeValidation(bool strict = false) : strict(strict) {};
144 std::vector<ValidationErrorInfo> validate(
145 const ROMol &mol, bool reportAllFailures) const override;
146
147 std::shared_ptr<ValidationMethod> copy() const override {
148 return std::make_shared<IsotopeValidation>(*this);
149 }
150
151 bool strict;
152};
153
154////////////////////////////////
155//! The MolVSValidation class includes most of the same validations as
156/// molvs.validations, namely NoAtomValidation, FragmentValidation,
157/// NeutralValidation, IsotopeValidation. MolVS also has IsNoneValidation and
158/// DichloroethaneValidation but these were not included here (yet).
159 class RDKIT_MOLSTANDARDIZE_EXPORT MolVSValidation : public CompositeValidation {
160 public:
161 // constructor
162 MolVSValidation();
163 //! overloaded constructor to take in a user-defined list of ValidationMethod
164 MolVSValidation(
165 const std::vector<std::shared_ptr<ValidationMethod>> &validations);
166
167 std::shared_ptr<ValidationMethod> copy() const override {
168 return std::make_shared<MolVSValidation>(*this);
169 }
170};
171
172//! The AllowedAtomsValidation class lets the user input a list of atoms,
173//! anything not on the list throws an error.
174 class RDKIT_MOLSTANDARDIZE_EXPORT AllowedAtomsValidation
175 : public ValidationMethod {
176 public:
177 AllowedAtomsValidation(std::vector<std::shared_ptr<Atom>> atoms)
178 : d_allowedList(std::move(atoms)) {}
179 std::vector<ValidationErrorInfo> validate(
180 const ROMol &mol, bool reportAllFailures) const override;
181
182 std::shared_ptr<ValidationMethod> copy() const override {
183 return std::make_shared<AllowedAtomsValidation>(*this);
184 }
185
186 private:
187 std::vector<std::shared_ptr<Atom>> d_allowedList;
188};
189
190//! The DisallowedAtomsValidation class lets the user input a list of atoms and
191//! as long as there are no atoms from the list it is deemed acceptable.
192 class RDKIT_MOLSTANDARDIZE_EXPORT DisallowedAtomsValidation
193 : public ValidationMethod {
194 public:
195 DisallowedAtomsValidation(std::vector<std::shared_ptr<Atom>> atoms)
196 : d_disallowedList(std::move(atoms)) {}
197 std::vector<ValidationErrorInfo> validate(
198 const ROMol &mol, bool reportAllFailures) const override;
199
200 std::shared_ptr<ValidationMethod> copy() const override {
201 return std::make_shared<DisallowedAtomsValidation>(*this);
202 }
203
204 private:
205 std::vector<std::shared_ptr<Atom>> d_disallowedList;
206};
207
208//! The DisallowedRadicalValidation class reports an error if any
209/// unstable radical atoms are found.
210/// The allowed radicals are [N]=O and [O]-N.
211 class RDKIT_MOLSTANDARDIZE_EXPORT DisallowedRadicalValidation
212 : public ValidationMethod {
213 public:
214 std::vector<ValidationErrorInfo> validate(
215 const ROMol &mol, bool reportAllFailures) const override;
216
217 std::shared_ptr<ValidationMethod> copy() const override {
218 return std::make_shared<DisallowedRadicalValidation>(*this);
219 }
220};
221
222//! The FeaturesValidation class reports an error if the input
223/// molecule representation includes any undesired features.
224 class RDKIT_MOLSTANDARDIZE_EXPORT FeaturesValidation : public ValidationMethod {
225 public:
226 FeaturesValidation(bool allowEnhancedStereo = false,
227 bool allowAromaticBondType = false,
228 bool allowDativeBondType = false,
229 bool allowQueries = false, bool allowDummies = false,
230 bool allowAtomAliases = false)
231 : allowEnhancedStereo(allowEnhancedStereo),
232 allowAromaticBondType(allowAromaticBondType),
233 allowDativeBondType(allowDativeBondType),
234 allowQueries(allowQueries),
235 allowDummies(allowDummies),
236 allowAtomAliases(allowAtomAliases) {};
237 std::vector<ValidationErrorInfo> validate(
238 const ROMol &mol, bool reportAllFailures) const override;
239 std::shared_ptr<ValidationMethod> copy() const override {
240 return std::make_shared<FeaturesValidation>(*this);
241 }
242 bool allowEnhancedStereo;
243 bool allowAromaticBondType;
244 bool allowDativeBondType;
245 bool allowQueries;
246 bool allowDummies;
247 bool allowAtomAliases;
248};
249
250//! The Is2DValidation class reports an error if the input
251/// molecule representation is designated as 3D or if it includes
252/// non-null Z coordinates, and in case all atoms are assigned the
253/// same coordinates.
254 class RDKIT_MOLSTANDARDIZE_EXPORT Is2DValidation : public ValidationMethod {
255 public:
256 Is2DValidation(double threshold = 1.e-3) : threshold(threshold) {};
257 std::vector<ValidationErrorInfo> validate(
258 const ROMol &mol, bool reportAllFailures) const override;
259 std::shared_ptr<ValidationMethod> copy() const override {
260 return std::make_shared<Is2DValidation>(*this);
261 }
262
263 double threshold;
264};
265
266//! The Layout2DValidation class reports an error if any atoms are
267/// too close to any other atoms or bonds, and in case any bonds are
268/// too long.
269 class RDKIT_MOLSTANDARDIZE_EXPORT Layout2DValidation : public ValidationMethod {
270 public:
271 Layout2DValidation(double clashLimit = 0.15, double bondLengthLimit = 25.,
272 bool allowLongBondsInRings = true,
273 bool allowAtomBondClashExemption = true,
274 double minMedianBondLength = 1e-3)
275 : clashLimit(clashLimit),
276 bondLengthLimit(bondLengthLimit),
277 allowLongBondsInRings(allowLongBondsInRings),
278 allowAtomBondClashExemption(allowAtomBondClashExemption),
279 minMedianBondLength(minMedianBondLength) {};
280 std::vector<ValidationErrorInfo> validate(
281 const ROMol &mol, bool reportAllFailures) const override;
282 std::shared_ptr<ValidationMethod> copy() const override {
283 return std::make_shared<Layout2DValidation>(*this);
284 }
285
286 static double squaredMedianBondLength(const ROMol &mol,
287 const Conformer &conf);
288
289 double clashLimit;
290 double bondLengthLimit;
291 bool allowLongBondsInRings;
292 bool allowAtomBondClashExemption;
293 double minMedianBondLength;
294};
295
296//! The StereoValidation class checks various "syntactic" constraints
297/// related to the usage of stereo bonds on centers with 4 or 3 substituents,
298/// in an attempt to ensure that the associated stereochemical configuration
299/// can be interpreted unambiguously.
300/// These validation criteria were ported from the AvalonTools STRUCHK software.
301 class RDKIT_MOLSTANDARDIZE_EXPORT StereoValidation : public ValidationMethod {
302 public:
303 std::vector<ValidationErrorInfo> validate(
304 const ROMol &mol, bool reportAllFailures) const override;
305 std::shared_ptr<ValidationMethod> copy() const override {
306 return std::make_shared<StereoValidation>(*this);
307 }
308};
309
310//! A convenience function for quickly validating a single SMILES string.
311 RDKIT_MOLSTANDARDIZE_EXPORT std::vector<ValidationErrorInfo> validateSmiles(
312 const std::string &smiles);
313
314} // namespace MolStandardize
315} // namespace RDKit
316
317#endif
Atom.h
Defines the Atom class and associated typedefs.
RDKitBase.h
pulls in the core RDKit functionality
ROMol.h
Defines the primary molecule class ROMol as well as associated typedefs.
RDKit::Conformer
The class for representing 2D or 3D conformation of a molecule.
Definition Conformer.h:46
RDKit::MolStandardize::AllowedAtomsValidation::AllowedAtomsValidation
AllowedAtomsValidation(std::vector< std::shared_ptr< Atom > > atoms)
Definition Validate.h:177
RDKit::MolStandardize::AllowedAtomsValidation::copy
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:182
RDKit::MolStandardize::AllowedAtomsValidation::validate
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
RDKit::MolStandardize::CompositeValidation::copy
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:64
RDKit::MolStandardize::CompositeValidation::validate
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
RDKit::MolStandardize::CompositeValidation::CompositeValidation
CompositeValidation(const std::vector< std::shared_ptr< ValidationMethod > > &validations)
Definition Validate.h:57
RDKit::MolStandardize::DisallowedAtomsValidation::DisallowedAtomsValidation
DisallowedAtomsValidation(std::vector< std::shared_ptr< Atom > > atoms)
Definition Validate.h:195
RDKit::MolStandardize::DisallowedAtomsValidation::validate
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
RDKit::MolStandardize::DisallowedAtomsValidation::copy
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:200
RDKit::MolStandardize::DisallowedRadicalValidation
Definition Validate.h:212
RDKit::MolStandardize::DisallowedRadicalValidation::validate
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
RDKit::MolStandardize::DisallowedRadicalValidation::copy
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:217
RDKit::MolStandardize::FeaturesValidation::allowEnhancedStereo
bool allowEnhancedStereo
Definition Validate.h:242
RDKit::MolStandardize::FeaturesValidation::FeaturesValidation
FeaturesValidation(bool allowEnhancedStereo=false, bool allowAromaticBondType=false, bool allowDativeBondType=false, bool allowQueries=false, bool allowDummies=false, bool allowAtomAliases=false)
Definition Validate.h:226
RDKit::MolStandardize::FeaturesValidation::allowQueries
bool allowQueries
Definition Validate.h:245
RDKit::MolStandardize::FeaturesValidation::copy
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:239
RDKit::MolStandardize::FeaturesValidation::allowAromaticBondType
bool allowAromaticBondType
Definition Validate.h:243
RDKit::MolStandardize::FeaturesValidation::allowDativeBondType
bool allowDativeBondType
Definition Validate.h:244
RDKit::MolStandardize::FeaturesValidation::allowAtomAliases
bool allowAtomAliases
Definition Validate.h:247
RDKit::MolStandardize::FeaturesValidation::allowDummies
bool allowDummies
Definition Validate.h:246
RDKit::MolStandardize::FeaturesValidation::validate
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
RDKit::MolStandardize::FragmentValidation
The FragmentValidation class logs if certain fragments are present.
Definition Validate.h:112
RDKit::MolStandardize::FragmentValidation::copy
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:117
RDKit::MolStandardize::FragmentValidation::validate
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
RDKit::MolStandardize::Is2DValidation::validate
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
RDKit::MolStandardize::Is2DValidation::threshold
double threshold
Definition Validate.h:263
RDKit::MolStandardize::Is2DValidation::copy
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:259
RDKit::MolStandardize::Is2DValidation::Is2DValidation
Is2DValidation(double threshold=1.e-3)
Definition Validate.h:256
RDKit::MolStandardize::IsotopeValidation::copy
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:147
RDKit::MolStandardize::IsotopeValidation::strict
bool strict
Definition Validate.h:151
RDKit::MolStandardize::IsotopeValidation::validate
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
RDKit::MolStandardize::IsotopeValidation::IsotopeValidation
IsotopeValidation(bool strict=false)
Definition Validate.h:143
RDKit::MolStandardize::Layout2DValidation::clashLimit
double clashLimit
Definition Validate.h:289
RDKit::MolStandardize::Layout2DValidation::Layout2DValidation
Layout2DValidation(double clashLimit=0.15, double bondLengthLimit=25., bool allowLongBondsInRings=true, bool allowAtomBondClashExemption=true, double minMedianBondLength=1e-3)
Definition Validate.h:271
RDKit::MolStandardize::Layout2DValidation::allowAtomBondClashExemption
bool allowAtomBondClashExemption
Definition Validate.h:292
RDKit::MolStandardize::Layout2DValidation::minMedianBondLength
double minMedianBondLength
Definition Validate.h:293
RDKit::MolStandardize::Layout2DValidation::copy
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:282
RDKit::MolStandardize::Layout2DValidation::allowLongBondsInRings
bool allowLongBondsInRings
Definition Validate.h:291
RDKit::MolStandardize::Layout2DValidation::bondLengthLimit
double bondLengthLimit
Definition Validate.h:290
RDKit::MolStandardize::Layout2DValidation::validate
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
RDKit::MolStandardize::Layout2DValidation::squaredMedianBondLength
static double squaredMedianBondLength(const ROMol &mol, const Conformer &conf)
RDKit::MolStandardize::MolVSValidation::MolVSValidation
MolVSValidation(const std::vector< std::shared_ptr< ValidationMethod > > &validations)
overloaded constructor to take in a user-defined list of ValidationMethod
RDKit::MolStandardize::MolVSValidation::MolVSValidation
MolVSValidation()
RDKit::MolStandardize::MolVSValidation::copy
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:167
RDKit::MolStandardize::NeutralValidation
The NeutralValidation class logs if not an overall neutral system.
Definition Validate.h:123
RDKit::MolStandardize::NeutralValidation::validate
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
RDKit::MolStandardize::NeutralValidation::copy
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:128
RDKit::MolStandardize::NoAtomValidation
MolVS Validations.
Definition Validate.h:101
RDKit::MolStandardize::NoAtomValidation::copy
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:106
RDKit::MolStandardize::NoAtomValidation::validate
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
RDKit::MolStandardize::RDKitValidation::RDKitValidation
RDKitValidation(bool allowEmptyMolecules=false)
Definition Validate.h:82
RDKit::MolStandardize::RDKitValidation::validate
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
RDKit::MolStandardize::RDKitValidation::allowEmptyMolecules
bool allowEmptyMolecules
Definition Validate.h:92
RDKit::MolStandardize::RDKitValidation::copy
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:88
RDKit::MolStandardize::StereoValidation
Definition Validate.h:301
RDKit::MolStandardize::StereoValidation::validate
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
RDKit::MolStandardize::StereoValidation::copy
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:305
RDKit::MolStandardize::ValidationMethod::copy
virtual std::shared_ptr< ValidationMethod > copy() const =0
RDKit::MolStandardize::ValidationMethod::validate
virtual std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const =0
RDKit::MolStandardize::ValidationMethod::~ValidationMethod
virtual ~ValidationMethod()=default
RDKit::MolStandardize::ValidationMethod::ValidationMethod
ValidationMethod()=default
RDKit::ROMol
Definition ROMol.h:199
RDKit::RWMol
RWMol is a molecule class that is intended to be edited.
Definition RWMol.h:32
export.h
RDKIT_MOLSTANDARDIZE_EXPORT
#define RDKIT_MOLSTANDARDIZE_EXPORT
Definition export.h:369
RDKit::MolStandardize
Definition AcidBaseCatalogEntry.h:22
RDKit::MolStandardize::validateSmiles
RDKIT_MOLSTANDARDIZE_EXPORT std::vector< ValidationErrorInfo > validateSmiles(const std::string &smiles)
A convenience function for quickly validating a single SMILES string.
RDKit::MolStandardize::ValidationErrorInfo
std::string ValidationErrorInfo
Definition Validate.h:38
RDKit
Std stuff.
Definition Abbreviations.h:19