Chemical Data Processing Library C++ API - Version 1.1.1
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 
55  {
56 
57  public:
58  class Pattern;
59 
60  private:
61  typedef std::vector<Pattern> PatternList;
62 
63  public:
64  typedef std::shared_ptr<SubstructureHistogramCalculator> SharedPointer;
65 
66  typedef PatternList::const_iterator ConstPatternIterator;
67  typedef PatternList::iterator PatternIterator;
68 
70  {
71 
72  public:
73  Pattern(const MolecularGraph::SharedPointer& structure, std::size_t id, std::size_t priority = 0,
74  bool all_matches = true, bool unique_matches = true);
75 
77 
78  std::size_t getID() const;
79 
80  std::size_t getPriority() const;
81 
82  bool processAllMatches() const;
83 
85 
86  private:
88  std::size_t id;
89  std::size_t priority;
90  bool allMatches;
91  bool uniqueMatches;
92  };
93 
95 
97 
98  void addPattern(const MolecularGraph::SharedPointer& structure, std::size_t id, std::size_t priority = 0,
99  bool all_matches = true, bool unique_matches = true);
100 
101  void addPattern(const Pattern& ptn);
102 
103  const Pattern& getPattern(std::size_t idx) const;
104 
105  void removePattern(std::size_t idx);
106 
107  void removePattern(const PatternIterator& ptn_it);
108 
109  void clear();
110 
111  std::size_t getNumPatterns() const;
112 
115 
118 
121 
124 
125  template <typename T>
126  void calculate(const MolecularGraph& molgraph, T& histo);
127 
129 
130  private:
131  typedef std::function<void(std::size_t)> HistoUpdateFunction;
132 
133  template <typename T>
134  class HistoUpdateFunctor
135  {
136 
137  public:
138  HistoUpdateFunctor(T& histo):
139  histo(histo) {}
140 
141  void operator()(std::size_t id)
142  {
143  histo[id] += 1;
144  }
145 
146  private:
147  T& histo;
148  };
149 
150  void doCalculate(const MolecularGraph& molgraph, const HistoUpdateFunction& func);
151 
152  void init(const MolecularGraph& molgraph);
153 
154  void processPattern(const Pattern& ptn, const HistoUpdateFunction& func);
155  bool processMatch(const AtomBondMapping& mapping, const Pattern& ptn, const HistoUpdateFunction& func);
156 
157  typedef std::pair<Util::BitSet, Util::BitSet> AtomBondMask;
158  typedef std::map<std::size_t, AtomBondMask> PriorityToAtomBondMaskMap;
159 
160  const MolecularGraph* molGraph;
161  PatternList patterns;
162  SubstructureSearch substructSearch;
163  PriorityToAtomBondMaskMap matchedSubstructMasks;
164  AtomBondMask testingAtomBondMask;
165  Util::BitSet tmpMask;
166  };
167  } // namespace Chem
168 } // namespace CDPL
169 
170 
171 template <typename T>
173 {
174  doCalculate(molgraph, HistoUpdateFunctor<T>(histo));
175 }
176 
177 #endif // CDPL_CHEM_SUBSTRUCTUREHISTOGRAMCALCULATOR_HPP
CDPL::Chem::MolecularGraph::SharedPointer
std::shared_ptr< MolecularGraph > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated MolecularGraph instances.
Definition: MolecularGraph.hpp:58
CDPL::Chem::SubstructureHistogramCalculator::SharedPointer
std::shared_ptr< SubstructureHistogramCalculator > SharedPointer
Definition: SubstructureHistogramCalculator.hpp:64
APIPrefix.hpp
Definition of the preprocessor macro CDPL_CHEM_API.
CDPL::Chem::SubstructureHistogramCalculator::calculate
void calculate(const MolecularGraph &molgraph, T &histo)
Definition: SubstructureHistogramCalculator.hpp:172
CDPL_CHEM_API
#define CDPL_CHEM_API
Tells the compiler/linker which classes, functions and variables are part of the library API.
CDPL::Chem::SubstructureHistogramCalculator::Pattern::getStructure
const MolecularGraph::SharedPointer & getStructure() const
CDPL::Util::BitSet
boost::dynamic_bitset BitSet
A dynamic bitset class.
Definition: BitSet.hpp:46
CDPL::Chem::SubstructureHistogramCalculator::Pattern::Pattern
Pattern(const MolecularGraph::SharedPointer &structure, std::size_t id, std::size_t priority=0, bool all_matches=true, bool unique_matches=true)
CDPL::Chem::SubstructureHistogramCalculator::clear
void clear()
CDPL::Chem::SubstructureHistogramCalculator::SubstructureHistogramCalculator
SubstructureHistogramCalculator(const SubstructureHistogramCalculator &gen)
CDPL::Chem::SubstructureHistogramCalculator::begin
PatternIterator begin()
CDPL::Chem::MolecularGraph
MolecularGraph.
Definition: MolecularGraph.hpp:52
BitSet.hpp
Definition of the type CDPL::Util::BitSet.
CDPL::Chem::SubstructureHistogramCalculator::Pattern::processUniqueMatchesOnly
bool processUniqueMatchesOnly() const
CDPL::Chem::SubstructureHistogramCalculator::removePattern
void removePattern(std::size_t idx)
CDPL::Chem::SubstructureHistogramCalculator::PatternIterator
PatternList::iterator PatternIterator
Definition: SubstructureHistogramCalculator.hpp:67
CDPL::Chem::SubstructureHistogramCalculator::end
ConstPatternIterator end() const
CDPL::Chem::SubstructureHistogramCalculator::getNumPatterns
std::size_t getNumPatterns() const
MolecularGraph.hpp
Definition of the class CDPL::Chem::MolecularGraph.
CDPL::Chem::AtomType::T
const unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
CDPL::Chem::SubstructureHistogramCalculator::begin
ConstPatternIterator begin() const
CDPL::Chem::SubstructureHistogramCalculator::getPattern
const Pattern & getPattern(std::size_t idx) const
CDPL::Chem::SubstructureHistogramCalculator
SubstructureHistogramCalculator.
Definition: SubstructureHistogramCalculator.hpp:55
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Chem::SubstructureHistogramCalculator::addPattern
void addPattern(const MolecularGraph::SharedPointer &structure, std::size_t id, std::size_t priority=0, bool all_matches=true, bool unique_matches=true)
CDPL::Chem::SubstructureHistogramCalculator::end
PatternIterator end()
CDPL::Chem::SubstructureHistogramCalculator::removePattern
void removePattern(const PatternIterator &ptn_it)
CDPL::Chem::SubstructureHistogramCalculator::Pattern
Definition: SubstructureHistogramCalculator.hpp:70
CDPL::Chem::SubstructureHistogramCalculator::getPatternsEnd
PatternIterator getPatternsEnd()
SubstructureSearch.hpp
Definition of the class CDPL::Chem::SubstructureSearch.
CDPL::Chem::SubstructureHistogramCalculator::Pattern::processAllMatches
bool processAllMatches() const
CDPL::Chem::SubstructureHistogramCalculator::ConstPatternIterator
PatternList::const_iterator ConstPatternIterator
Definition: SubstructureHistogramCalculator.hpp:66
CDPL::Chem::SubstructureHistogramCalculator::getPatternsBegin
PatternIterator getPatternsBegin()
CDPL::Chem::SubstructureHistogramCalculator::operator=
SubstructureHistogramCalculator & operator=(const SubstructureHistogramCalculator &gen)
CDPL::Chem::SubstructureHistogramCalculator::Pattern::getID
std::size_t getID() const
CDPL::Chem::SubstructureHistogramCalculator::SubstructureHistogramCalculator
SubstructureHistogramCalculator()
CDPL::Chem::SubstructureHistogramCalculator::getPatternsEnd
ConstPatternIterator getPatternsEnd() const
CDPL::Chem::SubstructureHistogramCalculator::getPatternsBegin
ConstPatternIterator getPatternsBegin() const
CDPL::Chem::SubstructureHistogramCalculator::addPattern
void addPattern(const Pattern &ptn)
CDPL::Chem::SubstructureHistogramCalculator::Pattern::getPriority
std::size_t getPriority() const