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