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.
38using ValidationErrorInfo = std::string;
39
40//! The ValidationMethod class is the abstract base class upon which all the
41/// four different ValidationMethods inherit from.
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
55 : public ValidationMethod {
56 public:
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*/
81 public:
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
93};
94
95//////////////////////////////
96/// MolVS Validations
97//
98
99//! The NoAtomValidation class throws an error if no atoms are present in the
100/// molecule.
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.
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.
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*/
142 public:
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).
160 public:
161 // constructor
163 //! overloaded constructor to take in a user-defined list of ValidationMethod
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.
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.
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.
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.
225 public:
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 }
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.
255 public:
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.
270 public:
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
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.
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.
311RDKIT_MOLSTANDARDIZE_EXPORT std::vector<ValidationErrorInfo> validateSmiles(
312 const std::string &smiles);
313
314} // namespace MolStandardize
315} // namespace RDKit
316
317#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:177
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:182
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:64
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
CompositeValidation(const std::vector< std::shared_ptr< ValidationMethod > > &validations)
Definition Validate.h:57
DisallowedAtomsValidation(std::vector< std::shared_ptr< Atom > > atoms)
Definition Validate.h:195
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:200
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:217
FeaturesValidation(bool allowEnhancedStereo=false, bool allowAromaticBondType=false, bool allowDativeBondType=false, bool allowQueries=false, bool allowDummies=false, bool allowAtomAliases=false)
Definition Validate.h:226
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:239
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
The FragmentValidation class logs if certain fragments are present.
Definition Validate.h:112
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:117
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:259
Is2DValidation(double threshold=1.e-3)
Definition Validate.h:256
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:147
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:271
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:282
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:167
The NeutralValidation class logs if not an overall neutral system.
Definition Validate.h:123
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:128
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:106
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
RDKitValidation(bool allowEmptyMolecules=false)
Definition Validate.h:82
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:88
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
std::shared_ptr< ValidationMethod > copy() const override
Definition Validate.h:305
virtual std::shared_ptr< ValidationMethod > copy() const =0
virtual std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const =0
RWMol is a molecule class that is intended to be edited.
Definition RWMol.h:32
#define RDKIT_MOLSTANDARDIZE_EXPORT
Definition export.h:369
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:38
Std stuff.