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:
89  void run(const ROMol &mol, bool reportAllFailures,
90  std::vector<ValidationErrorInfo> &errors) const override;
91  //! makes a copy of NoAtomValidation object and returns a MolVSValidations
92  //! pointer to it
93  virtual boost::shared_ptr<MolVSValidations> copy() const override {
94  return boost::make_shared<NoAtomValidation>(*this);
95  };
96 };
97 
98 //! The FragmentValidation class logs if certain fragments are present.
100  public:
101  void run(const ROMol &mol, bool reportAllFailures,
102  std::vector<ValidationErrorInfo> &errors) const override;
103  //! makes a copy of FragmentValidation object and returns a MolVSValidations
104  //! pointer to it
105  virtual boost::shared_ptr<MolVSValidations> copy() const override {
106  return boost::make_shared<FragmentValidation>(*this);
107  };
108 };
109 
110 //! The NeutralValidation class logs if not an overall neutral system.
112  public:
113  void run(const ROMol &mol, bool reportAllFailures,
114  std::vector<ValidationErrorInfo> &errors) const override;
115  //! makes a copy of NeutralValidation object and returns a MolVSValidations
116  //! pointer to it
117  virtual boost::shared_ptr<MolVSValidations> copy() const override {
118  return boost::make_shared<NeutralValidation>(*this);
119  };
120 };
121 
122 //! The IsotopeValidation class logs if molecule contains isotopes.
124  public:
125  void run(const ROMol &mol, bool reportAllFailures,
126  std::vector<ValidationErrorInfo> &errors) const override;
127  //! makes a copy of IsotopeValidation object and returns a MolVSValidations
128  //! pointer to it
129  virtual boost::shared_ptr<MolVSValidations> copy() const override {
130  return boost::make_shared<IsotopeValidation>(*this);
131  };
132 };
133 
134 ////////////////////////////////
135 
136 //! The MolVSValidation class can be used to perform all MolVSValidions.
138  public:
139  // constructor
140  MolVSValidation();
141  //! overloaded constructor to take in a user-defined list of MolVSValidations
143  const std::vector<boost::shared_ptr<MolVSValidations>> validations);
144  MolVSValidation(const MolVSValidation &other);
145  ~MolVSValidation();
146 
147  std::vector<ValidationErrorInfo> validate(
148  const ROMol &mol, bool reportAllFailures) const override;
149 
150  private:
151  std::vector<boost::shared_ptr<MolVSValidations>> d_validations;
152 };
153 
154 //! The AllowedAtomsValidation class lets the user input a list of atoms,
155 //! anything not on
156 // the list throws an error.
158  : public ValidationMethod {
159  public:
160  AllowedAtomsValidation(const std::vector<std::shared_ptr<Atom>> &atoms)
161  : d_allowedList(atoms){};
162  std::vector<ValidationErrorInfo> validate(
163  const ROMol &mol, bool reportAllFailures) const override;
164 
165  private:
166  std::vector<std::shared_ptr<Atom>> d_allowedList;
167 };
168 
169 //! The DisallowedAtomsValidation class lets the user input a list of atoms and
170 //! as long
171 // as there are no atoms from the list it is deemed acceptable.
173  : public ValidationMethod {
174  public:
175  DisallowedAtomsValidation(const std::vector<std::shared_ptr<Atom>> &atoms)
176  : d_disallowedList(atoms){};
177  std::vector<ValidationErrorInfo> validate(
178  const ROMol &mol, bool reportAllFailures) const override;
179 
180  private:
181  std::vector<std::shared_ptr<Atom>> d_disallowedList;
182 };
183 
184 //! A convenience function for quickly validating a single SMILES string.
185 RDKIT_MOLSTANDARDIZE_EXPORT std::vector<ValidationErrorInfo> validateSmiles(
186  const std::string &smiles);
187 
188 } // namespace MolStandardize
189 } // namespace RDKit
190 
191 #endif
#define BOOST_LOG(__arg__)
Definition: RDLog.h:88
ValidationErrorInfo(const std::string &msg)
Definition: Validate.h:39
Defines the primary molecule class ROMol as well as associated typedefs.
The MolVSValidation class can be used to perform all MolVSValidions.
Definition: Validate.h:137
The ValidationMethod class is the abstract base class upon which all the.
Definition: Validate.h:51
The IsotopeValidation class logs if molecule contains isotopes.
Definition: Validate.h:123
virtual boost::shared_ptr< MolVSValidations > copy() const override
Definition: Validate.h:93
pulls in the core RDKit functionality
virtual boost::shared_ptr< MolVSValidations > copy() const override
Definition: Validate.h:129
The NeutralValidation class logs if not an overall neutral system.
Definition: Validate.h:111
AllowedAtomsValidation(const std::vector< std::shared_ptr< Atom >> &atoms)
Definition: Validate.h:160
Std stuff.
Definition: Atom.h:30
RDKIT_MOLSTANDARDIZE_EXPORT std::vector< ValidationErrorInfo > validateSmiles(const std::string &smiles)
A convenience function for quickly validating a single SMILES string.
virtual boost::shared_ptr< MolVSValidations > copy() const override
Definition: Validate.h:117
The MolVSValidations class includes most of the same validations as.
Definition: Validate.h:78
The RDKitValidation class throws an error when there are no atoms in the.
Definition: Validate.h:65
The ValidationErrorInfo class is used to store the information returned by a.
Definition: Validate.h:37
The FragmentValidation class logs if certain fragments are present.
Definition: Validate.h:99
Defines the Atom class and associated typedefs.
The NoAtomValidation class throws an error if no atoms are present in the.
Definition: Validate.h:87
DisallowedAtomsValidation(const std::vector< std::shared_ptr< Atom >> &atoms)
Definition: Validate.h:175
#define RDKIT_MOLSTANDARDIZE_EXPORT
Definition: export.h:424
RDKIT_RDGENERAL_EXPORT boost::logging::rdLogger * rdInfoLog
virtual boost::shared_ptr< MolVSValidations > copy() const override
Definition: Validate.h:105