RDKit
Open-source cheminformatics and machine learning.
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 
30 namespace RDKit {
31 class RWMol;
32 class ROMol;
33 
34 namespace MolStandardize {
35 
36 //! The ValidationErrorInfo class is used to store the information returned by a
37 /// ValidationMethod validate.
38 class RDKIT_MOLSTANDARDIZE_EXPORT ValidationErrorInfo : public std::exception {
39  public:
40  ValidationErrorInfo(std::string msg) : d_msg(std::move(msg)) {
41  BOOST_LOG(rdInfoLog) << d_msg << std::endl;
42  }
43  const char *what() const noexcept override { return d_msg.c_str(); }
44  ~ValidationErrorInfo() noexcept override = default;
45 
46  private:
47  std::string d_msg;
48 }; // class ValidationErrorInfo
49 
50 //! The ValidationMethod class is the abstract base class upon which all the
51 /// four different ValidationMethods inherit from.
53  public:
54  ValidationMethod() = default;
55  virtual ~ValidationMethod() = default;
56 
57  virtual std::vector<ValidationErrorInfo> validate(
58  const ROMol &mol, bool reportAllFailures) const = 0;
59 };
60 
61 //! The RDKitValidation class throws an error when there are no atoms in the
62 /// molecule or when there is incorrect atom valency.
63 /*!
64 
65  <b>Notes:</b>
66  - RDKit automatically throws up atom valency issues but this class was made
67  for completeness of the project.
68 */
70  public:
71  std::vector<ValidationErrorInfo> validate(
72  const ROMol &mol, bool reportAllFailures) const override;
73 };
74 
75 //////////////////////////////
76 /// MolVS Validations
77 //
78 //! The MolVSValidations class includes most of the same validations as
79 /// molvs.validations, namely NoAtomValidation, FragmentValidation,
80 /// NeutralValidation, IsotopeValidation. MolVS also has IsNoneValidation and
81 /// DichloroethaneValidation but these were not included here (yet).
83  public:
84  virtual void run(const ROMol &mol, bool reportAllFailures,
85  std::vector<ValidationErrorInfo> &errors) const = 0;
86  virtual boost::shared_ptr<MolVSValidations> copy() const = 0;
87 };
88 
89 //! The NoAtomValidation class throws an error if no atoms are present in the
90 /// molecule.
92  : public MolVSValidations {
93  public:
94  void run(const ROMol &mol, bool reportAllFailures,
95  std::vector<ValidationErrorInfo> &errors) const override;
96  //! makes a copy of NoAtomValidation object and returns a MolVSValidations
97  //! pointer to it
98  boost::shared_ptr<MolVSValidations> copy() const override {
99  return boost::make_shared<NoAtomValidation>(*this);
100  }
101 };
102 
103 //! The FragmentValidation class logs if certain fragments are present.
105  : public MolVSValidations {
106  public:
107  void run(const ROMol &mol, bool reportAllFailures,
108  std::vector<ValidationErrorInfo> &errors) const override;
109  //! makes a copy of FragmentValidation object and returns a MolVSValidations
110  //! pointer to it
111  boost::shared_ptr<MolVSValidations> copy() const override {
112  return boost::make_shared<FragmentValidation>(*this);
113  }
114 };
115 
116 //! The NeutralValidation class logs if not an overall neutral system.
118  : public MolVSValidations {
119  public:
120  void run(const ROMol &mol, bool reportAllFailures,
121  std::vector<ValidationErrorInfo> &errors) const override;
122  //! makes a copy of NeutralValidation object and returns a MolVSValidations
123  //! pointer to it
124  boost::shared_ptr<MolVSValidations> copy() const override {
125  return boost::make_shared<NeutralValidation>(*this);
126  }
127 };
128 
129 //! The IsotopeValidation class logs if molecule contains isotopes.
131  : public MolVSValidations {
132  public:
133  void run(const ROMol &mol, bool reportAllFailures,
134  std::vector<ValidationErrorInfo> &errors) const override;
135  //! makes a copy of IsotopeValidation object and returns a MolVSValidations
136  //! pointer to it
137  boost::shared_ptr<MolVSValidations> copy() const override {
138  return boost::make_shared<IsotopeValidation>(*this);
139  }
140 };
141 
142 ////////////////////////////////
143 
144 //! The MolVSValidation class can be used to perform all MolVSValidions.
146  public:
147  // constructor
149  //! overloaded constructor to take in a user-defined list of MolVSValidations
151  const std::vector<boost::shared_ptr<MolVSValidations>> validations);
153  ~MolVSValidation() override;
154 
155  std::vector<ValidationErrorInfo> validate(
156  const ROMol &mol, bool reportAllFailures) const override;
157 
158  private:
159  std::vector<boost::shared_ptr<MolVSValidations>> d_validations;
160 };
161 
162 //! The AllowedAtomsValidation class lets the user input a list of atoms,
163 //! anything not on
164 /// the list throws an error.
166  : public ValidationMethod {
167  public:
168  AllowedAtomsValidation(std::vector<std::shared_ptr<Atom>> atoms)
169  : d_allowedList(std::move(atoms)) {}
170  std::vector<ValidationErrorInfo> validate(
171  const ROMol &mol, bool reportAllFailures) const override;
172 
173  private:
174  std::vector<std::shared_ptr<Atom>> d_allowedList;
175 };
176 
177 //! The DisallowedAtomsValidation class lets the user input a list of atoms and
178 //! as long
179 /// as there are no atoms from the list it is deemed acceptable.
181  : public ValidationMethod {
182  public:
183  DisallowedAtomsValidation(std::vector<std::shared_ptr<Atom>> atoms)
184  : d_disallowedList(std::move(atoms)) {}
185  std::vector<ValidationErrorInfo> validate(
186  const ROMol &mol, bool reportAllFailures) const override;
187 
188  private:
189  std::vector<std::shared_ptr<Atom>> d_disallowedList;
190 };
191 
192 //! A convenience function for quickly validating a single SMILES string.
193 RDKIT_MOLSTANDARDIZE_EXPORT std::vector<ValidationErrorInfo> validateSmiles(
194  const std::string &smiles);
195 
196 } // namespace MolStandardize
197 } // namespace RDKit
198 
199 #endif
Defines the Atom class and associated typedefs.
pulls in the core RDKit functionality
#define BOOST_LOG(__arg__)
Definition: RDLog.h:92
RDKIT_RDGENERAL_EXPORT RDLogger rdInfoLog
Defines the primary molecule class ROMol as well as associated typedefs.
AllowedAtomsValidation(std::vector< std::shared_ptr< Atom >> atoms)
Definition: Validate.h:168
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
DisallowedAtomsValidation(std::vector< std::shared_ptr< Atom >> atoms)
Definition: Validate.h:183
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
The FragmentValidation class logs if certain fragments are present.
Definition: Validate.h:105
void run(const ROMol &mol, bool reportAllFailures, std::vector< ValidationErrorInfo > &errors) const override
boost::shared_ptr< MolVSValidations > copy() const override
Definition: Validate.h:111
The IsotopeValidation class logs if molecule contains isotopes.
Definition: Validate.h:131
void run(const ROMol &mol, bool reportAllFailures, std::vector< ValidationErrorInfo > &errors) const override
boost::shared_ptr< MolVSValidations > copy() const override
Definition: Validate.h:137
The MolVSValidation class can be used to perform all MolVSValidions.
Definition: Validate.h:145
MolVSValidation(const std::vector< boost::shared_ptr< MolVSValidations >> validations)
overloaded constructor to take in a user-defined list of MolVSValidations
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
MolVSValidation(const MolVSValidation &other)
virtual void run(const ROMol &mol, bool reportAllFailures, std::vector< ValidationErrorInfo > &errors) const =0
virtual boost::shared_ptr< MolVSValidations > copy() const =0
The NeutralValidation class logs if not an overall neutral system.
Definition: Validate.h:118
void run(const ROMol &mol, bool reportAllFailures, std::vector< ValidationErrorInfo > &errors) const override
boost::shared_ptr< MolVSValidations > copy() const override
Definition: Validate.h:124
boost::shared_ptr< MolVSValidations > copy() const override
Definition: Validate.h:98
void run(const ROMol &mol, bool reportAllFailures, std::vector< ValidationErrorInfo > &errors) const override
std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const override
~ValidationErrorInfo() noexcept override=default
const char * what() const noexcept override
Definition: Validate.h:43
virtual std::vector< ValidationErrorInfo > validate(const ROMol &mol, bool reportAllFailures) const =0
#define RDKIT_MOLSTANDARDIZE_EXPORT
Definition: export.h:321
RDKIT_MOLSTANDARDIZE_EXPORT std::vector< ValidationErrorInfo > validateSmiles(const std::string &smiles)
A convenience function for quickly validating a single SMILES string.
Std stuff.
Definition: Abbreviations.h:19