RDKit
Open-source cheminformatics and machine learning.
RGroupDecomp.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2017-2021, Novartis Institutes for BioMedical Research Inc.
3 // and other RDKit contributors
4 //
5 // @@ All Rights Reserved @@
6 // This file is part of the RDKit.
7 // The contents are covered by the terms of the BSD license
8 // which is included in the file license.txt, found at the root
9 // of the RDKit source tree.
10 //
11 #include <RDGeneral/export.h>
12 #ifndef RDKIT_RGROUPDECOMP_H
13 #define RDKIT_RGROUPDECOMP_H
14 
15 #include "../RDKitBase.h"
16 #include "RGroupDecompParams.h"
18 #include <chrono>
19 
20 namespace RDKit {
21 
23  const bool success;
24  const double score;
25  RGroupDecompositionProcessResult(const bool success, const double score)
26  : success(success), score(score) {}
27 };
28 
29 struct RGroupMatch;
30 
31 typedef std::map<std::string, ROMOL_SPTR> RGroupRow;
32 typedef std::vector<ROMOL_SPTR> RGroupColumn;
33 
34 typedef std::vector<RGroupRow> RGroupRows;
35 typedef std::map<std::string, RGroupColumn> RGroupColumns;
36 
37 class UsedLabelMap {
38  public:
39  UsedLabelMap(const std::map<int, int> &mapping) {
40  for (const auto &rl : mapping) {
41  d_map[rl.second] = std::make_pair(false, (rl.first > 0));
42  }
43  }
44  bool has(int label) const { return d_map.find(label) != d_map.end(); }
45  bool getIsUsed(int label) const { return d_map.at(label).first; }
46  void setIsUsed(int label) { d_map[label].first = true; }
47  bool isUserDefined(int label) const { return d_map.at(label).second; }
48 
49  private:
50  std::map<int, std::pair<bool, bool>> d_map;
51 };
52 
53 struct RGroupDecompData;
55  private:
56  RGroupDecompData *data; // implementation details
57  RGroupDecomposition(const RGroupDecomposition &); // no copy construct
58  RGroupDecomposition &operator=(
59  const RGroupDecomposition &); // Prevent assignment
60  RWMOL_SPTR outputCoreMolecule(const RGroupMatch &match,
61  const UsedLabelMap &usedRGroupMap) const;
62 
63  public:
65  const RGroupDecompositionParameters &params =
67  RGroupDecomposition(const std::vector<ROMOL_SPTR> &cores,
68  const RGroupDecompositionParameters &params =
70 
72 
73  //! Returns the index of the added molecule in the RGroupDecomposition
74  /// or a negative error code
75  /// :param mol: Molecule to add to the decomposition
76  /// :result: index of the molecle or
77  /// -1 if none of the core matches
78  /// -2 if the matched molecule has no sidechains, i.e. is the
79  /// same as the scaffold
80  int add(const ROMol &mol);
82  bool process();
83 
85  //! return the current group labels
86  std::vector<std::string> getRGroupLabels() const;
87 
88  //! return rgroups in row order group[row][attachment_point] = ROMol
90  //! return rgroups in column order group[attachment_point][row] = ROMol
92 };
93 
95  const std::vector<ROMOL_SPTR> &cores, const std::vector<ROMOL_SPTR> &mols,
96  RGroupRows &rows, std::vector<unsigned int> *unmatched = nullptr,
97  const RGroupDecompositionParameters &options =
99 
101  const std::vector<ROMOL_SPTR> &cores, const std::vector<ROMOL_SPTR> &mols,
102  RGroupColumns &columns, std::vector<unsigned int> *unmatched = nullptr,
103  const RGroupDecompositionParameters &options =
105 
106 inline bool checkForTimeout(const std::chrono::steady_clock::time_point &t0,
107  double timeout, bool throwOnTimeout = true) {
108  if (timeout <= 0) {
109  return false;
110  }
111  auto t1 = std::chrono::steady_clock::now();
112  std::chrono::duration<double> elapsed = t1 - t0;
113  if (elapsed.count() >= timeout) {
114  if (throwOnTimeout) {
115  throw std::runtime_error("operation timed out");
116  }
117  return true;
118  }
119  return false;
120 }
121 
122 } // namespace RDKit
123 
124 #endif
RGroupRows getRGroupsAsRows() const
return rgroups in row order group[row][attachment_point] = ROMol
RGroupDecomposition(const std::vector< ROMOL_SPTR > &cores, const RGroupDecompositionParameters &params=RGroupDecompositionParameters())
RGroupColumns getRGroupsAsColumns() const
return rgroups in column order group[attachment_point][row] = ROMol
const RGroupDecompositionParameters & params() const
RGroupDecomposition(const ROMol &core, const RGroupDecompositionParameters &params=RGroupDecompositionParameters())
int add(const ROMol &mol)
RGroupDecompositionProcessResult processAndScore()
std::vector< std::string > getRGroupLabels() const
return the current group labels
void setIsUsed(int label)
Definition: RGroupDecomp.h:46
bool getIsUsed(int label) const
Definition: RGroupDecomp.h:45
bool isUserDefined(int label) const
Definition: RGroupDecomp.h:47
UsedLabelMap(const std::map< int, int > &mapping)
Definition: RGroupDecomp.h:39
bool has(int label) const
Definition: RGroupDecomp.h:44
#define RDKIT_RGROUPDECOMPOSITION_EXPORT
Definition: export.h:401
Std stuff.
Definition: Abbreviations.h:19
std::map< std::string, ROMOL_SPTR > RGroupRow
Definition: RGroupDecomp.h:29
std::vector< ROMOL_SPTR > RGroupColumn
Definition: RGroupDecomp.h:32
std::map< std::string, RGroupColumn > RGroupColumns
Definition: RGroupDecomp.h:35
bool checkForTimeout(const std::chrono::steady_clock::time_point &t0, double timeout, bool throwOnTimeout=true)
Definition: RGroupDecomp.h:106
RDKIT_RGROUPDECOMPOSITION_EXPORT unsigned int RGroupDecompose(const std::vector< ROMOL_SPTR > &cores, const std::vector< ROMOL_SPTR > &mols, RGroupRows &rows, std::vector< unsigned int > *unmatched=nullptr, const RGroupDecompositionParameters &options=RGroupDecompositionParameters())
std::vector< RGroupRow > RGroupRows
Definition: RGroupDecomp.h:34
boost::shared_ptr< RWMol > RWMOL_SPTR
Definition: RWMol.h:216
RGroupDecompositionProcessResult(const bool success, const double score)
Definition: RGroupDecomp.h:25
RGroupMatch is the decomposition for a single molecule.
Definition: RGroupMatch.h:19