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