Chemical Data Processing Library C++ API - Version 1.4.0
SubstructureHistogramCalculator.hpp
Go to the documentation of this file.
1 /*
2  * SubstructureHistogramCalculator.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_CHEM_SUBSTRUCTUREHISTOGRAMCALCULATOR_HPP
30 #define CDPL_CHEM_SUBSTRUCTUREHISTOGRAMCALCULATOR_HPP
31 
32 #include <cstddef>
33 #include <vector>
34 #include <map>
35 #include <utility>
36 #include <memory>
37 #include <functional>
38 
39 #include "CDPL/Chem/APIPrefix.hpp"
42 #include "CDPL/Util/BitSet.hpp"
43 
44 
45 namespace CDPL
46 {
47 
48  namespace Chem
49  {
50 
62  {
63 
64  public:
65  class Pattern;
66 
67  private:
68  typedef std::vector<Pattern> PatternList;
69 
70  public:
74  typedef std::shared_ptr<SubstructureHistogramCalculator> SharedPointer;
75 
79  typedef PatternList::const_iterator ConstPatternIterator;
80 
84  typedef PatternList::iterator PatternIterator;
85 
90  {
91 
92  public:
101  Pattern(const MolecularGraph::SharedPointer& molgraph, std::size_t id, std::size_t priority = 0,
102  bool all_matches = true, bool unique_matches = true);
103 
109 
114  std::size_t getID() const;
115 
120  std::size_t getPriority() const;
121 
126  bool processAllMatches() const;
127 
133 
134  private:
136  std::size_t id;
137  std::size_t priority;
138  bool allMatches;
139  bool uniqueMatches;
140  };
141 
146 
152 
161  void addPattern(const MolecularGraph::SharedPointer& molgraph, std::size_t id, std::size_t priority = 0,
162  bool all_matches = true, bool unique_matches = true);
163 
168  void addPattern(const Pattern& pattern);
169 
176  const Pattern& getPattern(std::size_t idx) const;
177 
183  void removePattern(std::size_t idx);
184 
191 
195  void clear();
196 
201  std::size_t getNumPatterns() const;
202 
208 
214 
220 
226 
232 
238 
244 
250 
261  template <typename T>
262  void calculate(const MolecularGraph& molgraph, T& histo);
263 
270 
271  private:
272  typedef std::function<void(std::size_t)> HistoUpdateFunction;
273 
274  template <typename T>
275  class HistoUpdateFunctor
276  {
277 
278  public:
279  HistoUpdateFunctor(T& histo):
280  histo(histo) {}
281 
282  void operator()(std::size_t id)
283  {
284  histo[id] += 1;
285  }
286 
287  private:
288  T& histo;
289  };
290 
291  void doCalculate(const MolecularGraph& molgraph, const HistoUpdateFunction& func);
292 
293  void init(const MolecularGraph& molgraph);
294 
295  void processPattern(const Pattern& ptn, const HistoUpdateFunction& func);
296  bool processMatch(const AtomBondMapping& mapping, const Pattern& ptn, const HistoUpdateFunction& func);
297 
298  typedef std::pair<Util::BitSet, Util::BitSet> AtomBondMask;
299  typedef std::map<std::size_t, AtomBondMask> PriorityToAtomBondMaskMap;
300 
301  const MolecularGraph* molGraph;
302  PatternList patterns;
303  SubstructureSearch subSearch;
304  PriorityToAtomBondMaskMap matchedSubstructMasks;
305  AtomBondMask testingAtomBondMask;
306  Util::BitSet tmpMask;
307  };
308  } // namespace Chem
309 } // namespace CDPL
310 
311 
312 template <typename T>
314 {
315  doCalculate(molgraph, HistoUpdateFunctor<T>(histo));
316 }
317 
318 #endif // CDPL_CHEM_SUBSTRUCTUREHISTOGRAMCALCULATOR_HPP
Declaration of type CDPL::Util::BitSet.
Definition of the preprocessor macro CDPL_CHEM_API.
#define CDPL_CHEM_API
Tells the compiler/linker which classes, functions and variables are part of the library API.
Definition of class CDPL::Chem::MolecularGraph.
Definition of class CDPL::Chem::SubstructureSearch.
Abstract base class for representations of a chemical structure as a graph of bonded atoms.
Definition: MolecularGraph.hpp:57
std::shared_ptr< MolecularGraph > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated MolecularGraph instances.
Definition: MolecularGraph.hpp:63
Stores a single substructure query molecular graph, its histogram ID, its priority and match-handling...
Definition: SubstructureHistogramCalculator.hpp:90
Pattern(const MolecularGraph::SharedPointer &molgraph, std::size_t id, std::size_t priority=0, bool all_matches=true, bool unique_matches=true)
Constructs a Pattern instance for the specified values.
bool processUniqueMatchesOnly() const
Tells whether only one of multiple equivalent mappings is processed per match.
const MolecularGraph::SharedPointer & getStructure() const
Returns the query molecular graph of this pattern.
std::size_t getPriority() const
Returns the pattern priority.
bool processAllMatches() const
Tells whether all substructure matches are processed.
std::size_t getID() const
Returns the histogram bin ID of this pattern.
Counts occurrences of registered substructure patterns in a molecular graph, emitting the per-pattern...
Definition: SubstructureHistogramCalculator.hpp:62
PatternIterator end()
Returns a mutable iterator pointing one past the last registered pattern (range-based for support).
PatternIterator begin()
Returns a mutable iterator pointing to the first registered pattern (range-based for support).
ConstPatternIterator begin() const
Returns a constant iterator pointing to the first registered pattern (range-based for support).
void removePattern(const PatternIterator &it)
Removes the registered pattern referenced by it.
SubstructureHistogramCalculator & operator=(const SubstructureHistogramCalculator &gen)
Replaces the state of this calculator by a copy of the state of gen.
ConstPatternIterator end() const
Returns a constant iterator pointing one past the last registered pattern (range-based for support).
ConstPatternIterator getPatternsEnd() const
Returns a constant iterator pointing one past the last registered pattern.
PatternList::iterator PatternIterator
A mutable iterator over the registered patterns.
Definition: SubstructureHistogramCalculator.hpp:84
PatternIterator getPatternsBegin()
Returns a mutable iterator pointing to the first registered pattern.
PatternIterator getPatternsEnd()
Returns a mutable iterator pointing one past the last registered pattern.
void addPattern(const MolecularGraph::SharedPointer &molgraph, std::size_t id, std::size_t priority=0, bool all_matches=true, bool unique_matches=true)
Registers a new pattern by its query molecular graph and per-pattern settings.
std::size_t getNumPatterns() const
Returns the number of registered patterns.
void removePattern(std::size_t idx)
Removes the registered pattern at index idx.
void addPattern(const Pattern &pattern)
Appends a copy of the pre-built pattern pattern.
PatternList::const_iterator ConstPatternIterator
A constant iterator over the registered patterns.
Definition: SubstructureHistogramCalculator.hpp:79
SubstructureHistogramCalculator()
Constructs an empty SubstructureHistogramCalculator instance.
void clear()
Removes all registered patterns.
SubstructureHistogramCalculator(const SubstructureHistogramCalculator &gen)
Constructs a copy of the SubstructureHistogramCalculator instance gen.
void calculate(const MolecularGraph &molgraph, T &histo)
Counts substructure occurrences in molgraph and writes the per-pattern hit counts to histo.
Definition: SubstructureHistogramCalculator.hpp:313
const Pattern & getPattern(std::size_t idx) const
Returns the registered pattern at index idx.
ConstPatternIterator getPatternsBegin() const
Returns a constant iterator pointing to the first registered pattern.
std::shared_ptr< SubstructureHistogramCalculator > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated SubstructureHistogramCalculator i...
Definition: SubstructureHistogramCalculator.hpp:74
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
boost::dynamic_bitset BitSet
Dynamic bitset class.
Definition: BitSet.hpp:46
The namespace of the Chemical Data Processing Library.