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