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