Chemical Data Processing Library C++ API - Version 1.0.0
CommonConnectedSubstructureSearch.hpp
Go to the documentation of this file.
1 /*
2  * CommonConnectedSubstructureSearch.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_COMMONCONNECTEDSUBSTRUCTURESEARCH_HPP
30 #define CDPL_CHEM_COMMONCONNECTEDSUBSTRUCTURESEARCH_HPP
31 
32 #include <vector>
33 #include <deque>
34 #include <set>
35 #include <cstddef>
36 
37 #include <boost/iterator/indirect_iterator.hpp>
38 
39 #include "CDPL/Chem/APIPrefix.hpp"
42 #include "CDPL/Util/BitSet.hpp"
44 
45 
46 namespace CDPL
47 {
48 
49  namespace Chem
50  {
51 
52  class MolecularGraph;
53  class Atom;
54  class Bond;
55 
61  {
62 
63  typedef std::vector<AtomBondMapping*> ABMappingList;
64 
65  public:
69  typedef boost::indirect_iterator<ABMappingList::iterator, AtomBondMapping> MappingIterator;
70 
74  typedef boost::indirect_iterator<ABMappingList::const_iterator, const AtomBondMapping> ConstMappingIterator;
75 
80 
86 
93 
98  void setQuery(const MolecularGraph& query);
99 
113  bool mappingExists(const MolecularGraph& target);
114 
129  bool findAllMappings(const MolecularGraph& target);
130 
145  bool findMaxMappings(const MolecularGraph& target);
146 
152  std::size_t getNumMappings() const;
153 
160  AtomBondMapping& getMapping(std::size_t idx);
161 
168  const AtomBondMapping& getMapping(std::size_t idx) const;
169 
175 
181 
187 
193 
199 
205 
211 
217 
229  void uniqueMappingsOnly(bool unique);
230 
236  bool uniqueMappingsOnly() const;
237 
248  void setMaxNumMappings(std::size_t max_num_mappings);
249 
255  std::size_t getMaxNumMappings() const;
256 
266  void setMinSubstructureSize(std::size_t min_size);
267 
273  std::size_t getMinSubstructureSize() const;
274 
275  private:
277 
279 
280  bool init(const MolecularGraph&);
281 
282  void initMatchExpressions();
283 
284  bool findEquivAtoms();
285  bool findEquivBonds();
286 
287  bool mapAtoms();
288  bool mapAtoms(std::size_t);
289  bool mapAtoms(std::size_t, std::size_t);
290 
291  bool nextTargetAtom(std::size_t, std::size_t&, std::size_t&) const;
292 
293  bool mappingFound();
294 
295  bool hasPostMappingMatchExprs() const;
296  bool foundMappingMatches(const AtomBondMapping*) const;
297 
298  bool foundMappingUnique();
299  bool mappingAlreadySeen(const AtomBondMapping*) const;
300 
301  void clearMappings();
302 
303  void freeAtomBondMappings();
304  void freeAtomBondMapping();
305 
306  AtomBondMapping* createAtomBondMapping();
307 
308  class ABMappingMask
309  {
310 
311  public:
312  void initQueryAtomMask(std::size_t);
313  void initTargetAtomMask(std::size_t);
314 
315  void initQueryBondMask(std::size_t);
316  void initTargetBondMask(std::size_t);
317 
318  void setQueryAtomBit(std::size_t);
319  void setTargetAtomBit(std::size_t);
320 
321  void resetQueryAtomBit(std::size_t);
322  void resetTargetAtomBit(std::size_t);
323 
324  bool testTargetAtomBit(std::size_t) const;
325 
326  void setQueryBondBit(std::size_t);
327  void setTargetBondBit(std::size_t);
328 
329  void resetBondMasks();
330 
331  bool operator<(const ABMappingMask&) const;
332  bool operator>(const ABMappingMask&) const;
333 
334  private:
335  Util::BitSet queryAtomMask;
336  Util::BitSet targetAtomMask;
337  Util::BitSet queryBondMask;
338  Util::BitSet targetBondMask;
339  };
340 
341  typedef MatchExpression<MolecularGraph>::SharedPointer MolGraphMatchExprPtr;
342 
343  typedef std::vector<Util::BitSet> BitMatrix;
344  typedef std::vector<const Atom*> AtomMappingTable;
345  typedef std::vector<std::size_t> AtomIndexList;
346  typedef std::vector<std::size_t> BondMappingStack;
347  typedef std::deque<std::size_t> AtomQueue;
348  typedef std::set<ABMappingMask> UniqueMappingList;
349  typedef std::vector<const Atom*> AtomList;
350  typedef std::vector<const Bond*> BondList;
351  typedef std::vector<MatchExpression<Atom, MolecularGraph>::SharedPointer> AtomMatchExprTable;
352  typedef std::vector<MatchExpression<Bond, MolecularGraph>::SharedPointer> BondMatchExprTable;
353  typedef Util::ObjectStack<AtomBondMapping> MappingCache;
354 
355  const MolecularGraph* query;
356  const MolecularGraph* target;
357  BitMatrix atomEquivMatrix;
358  BitMatrix bondEquivMatrix;
359  AtomQueue termQueryAtoms;
360  AtomIndexList termTargetAtoms;
361  BondMappingStack bondMappingStack;
362  AtomMappingTable queryAtomMapping;
363  ABMappingMask mappingMask;
364  Util::BitSet hiddenQueryAtomMask;
365  Util::BitSet termQueryAtomMask;
366  Util::BitSet termTargetAtomMask;
367  ABMappingList foundMappings;
368  UniqueMappingList uniqueMappings;
369  AtomMatchExprTable atomMatchExprTable;
370  BondMatchExprTable bondMatchExprTable;
371  MolGraphMatchExprPtr molGraphMatchExpr;
372  AtomList postMappingMatchAtoms;
373  BondList postMappingMatchBonds;
374  MappingCache mappingCache;
375  bool queryChanged;
376  bool initQueryData;
377  bool uniqueMatches;
378  bool saveMappings;
379  bool maxMappingsOnly;
380  std::size_t numQueryAtoms;
381  std::size_t numQueryBonds;
382  std::size_t numTargetAtoms;
383  std::size_t numTargetBonds;
384  std::size_t numMappedAtoms;
385  std::size_t currMaxSubstructureSize;
386  std::size_t maxBondStackSize;
387  std::size_t maxNumMappings;
388  std::size_t minSubstructureSize;
389  };
390  } // namespace Chem
391 } // namespace CDPL
392 
393 #endif // CDPL_CHEM_COMMONCONNECTEDSUBSTRUCTURESEARCH_HPP
CDPL::Chem::CommonConnectedSubstructureSearch::getMapping
const AtomBondMapping & getMapping(std::size_t idx) const
Returns a const reference to the stored atom/bond mapping object at index idx.
CDPL::Chem::CommonConnectedSubstructureSearch::getNumMappings
std::size_t getNumMappings() const
Returns the number of atom/bond mappings that were recorded in the last search for common substructur...
CDPL::Chem::CommonConnectedSubstructureSearch::begin
MappingIterator begin()
Returns a mutable iterator pointing to the beginning of the stored atom/bond mapping objects.
ObjectStack.hpp
Definition of the class CDPL::Util::ObjectStack.
CDPL::Chem::CommonConnectedSubstructureSearch::setMaxNumMappings
void setMaxNumMappings(std::size_t max_num_mappings)
Allows to specify a limit on the number of stored atom/bond mappings.
CDPL::Chem::CommonConnectedSubstructureSearch::uniqueMappingsOnly
bool uniqueMappingsOnly() const
Tells whether duplicate atom/bond mappings are discarded.
APIPrefix.hpp
Definition of the preprocessor macro CDPL_CHEM_API.
CDPL_CHEM_API
#define CDPL_CHEM_API
Tells the compiler/linker which classes, functions and variables are part of the library API.
CDPL::Chem::CommonConnectedSubstructureSearch::end
ConstMappingIterator end() const
Returns a constant iterator pointing to the end of the stored atom/bond mapping objects.
CDPL::Chem::CommonConnectedSubstructureSearch::getMappingsBegin
MappingIterator getMappingsBegin()
Returns a mutable iterator pointing to the beginning of the stored atom/bond mapping objects.
CDPL::Chem::CommonConnectedSubstructureSearch::ConstMappingIterator
boost::indirect_iterator< ABMappingList::const_iterator, const AtomBondMapping > ConstMappingIterator
A constant random access iterator used to iterate over the stored atom/bond mapping objects.
Definition: CommonConnectedSubstructureSearch.hpp:74
CDPL::Chem::CommonConnectedSubstructureSearch::findMaxMappings
bool findMaxMappings(const MolecularGraph &target)
Searches for all maximum-sized atom/bond mappings of connected query subgraphs to substructures of th...
CDPL::Chem::CommonConnectedSubstructureSearch::getMapping
AtomBondMapping & getMapping(std::size_t idx)
Returns a non-const reference to the stored atom/bond mapping object at index idx.
CDPL::Chem::CommonConnectedSubstructureSearch::mappingExists
bool mappingExists(const MolecularGraph &target)
Searches for a common connected substructure between the query and the specified target molecular gra...
CDPL::Util::BitSet
boost::dynamic_bitset BitSet
A dynamic bitset class.
Definition: BitSet.hpp:46
CDPL::Chem::CommonConnectedSubstructureSearch::~CommonConnectedSubstructureSearch
~CommonConnectedSubstructureSearch()
Destructor.
CDPL::Chem::CommonConnectedSubstructureSearch::getMappingsBegin
ConstMappingIterator getMappingsBegin() const
Returns a constant iterator pointing to the beginning of the stored atom/bond mapping objects.
CDPL::Chem::CommonConnectedSubstructureSearch::getMinSubstructureSize
std::size_t getMinSubstructureSize() const
Returns the minimum accepted common substructure size.
CDPL::Chem::CommonConnectedSubstructureSearch
CommonConnectedSubstructureSearch.
Definition: CommonConnectedSubstructureSearch.hpp:61
CDPL::Chem::MolecularGraph
MolecularGraph.
Definition: MolecularGraph.hpp:52
BitSet.hpp
Definition of the type CDPL::Util::BitSet.
CDPL::Util::operator<
bool operator<(const Array< ValueType > &array1, const Array< ValueType > &array2)
Less than comparison operator.
CDPL::Chem::CommonConnectedSubstructureSearch::setQuery
void setQuery(const MolecularGraph &query)
Allows to specify a new query structure.
CDPL::Chem::CommonConnectedSubstructureSearch::getMappingsEnd
MappingIterator getMappingsEnd()
Returns a mutable iterator pointing to the end of the stored atom/bond mapping objects.
AtomBondMapping.hpp
Definition of the class CDPL::Chem::AtomBondMapping.
CDPL::Chem::CommonConnectedSubstructureSearch::CommonConnectedSubstructureSearch
CommonConnectedSubstructureSearch()
Constructs and initializes a CommonConnectedSubstructureSearch instance.
CDPL::Chem::CommonConnectedSubstructureSearch::end
MappingIterator end()
Returns a mutable iterator pointing to the end of the stored atom/bond mapping objects.
CDPL::Chem::MatchExpression
A generic boolean expression interface for the implementation of query/target object equivalence test...
Definition: MatchExpression.hpp:75
CDPL::Chem::CommonConnectedSubstructureSearch::getMappingsEnd
ConstMappingIterator getMappingsEnd() const
Returns a constant iterator pointing to the end of the stored atom/bond mapping objects.
MatchExpression.hpp
Definition of the class CDPL::Chem::MatchExpression.
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Chem::CommonConnectedSubstructureSearch::setMinSubstructureSize
void setMinSubstructureSize(std::size_t min_size)
Allows to specify the minimum accepted common substructure size.
CDPL::Chem::CommonConnectedSubstructureSearch::getMaxNumMappings
std::size_t getMaxNumMappings() const
Returns the specified limit on the number of stored atom/bond mappings.
CDPL::Chem::AtomBondMapping
A data structure for the common storage of related atom to atom and bond to bond mappings.
Definition: AtomBondMapping.hpp:55
CDPL::Chem::CommonConnectedSubstructureSearch::uniqueMappingsOnly
void uniqueMappingsOnly(bool unique)
Allows to specify whether or not to store only unique atom/bond mappings.
CDPL::Chem::CommonConnectedSubstructureSearch::MappingIterator
boost::indirect_iterator< ABMappingList::iterator, AtomBondMapping > MappingIterator
A mutable random access iterator used to iterate over the stored atom/bond mapping objects.
Definition: CommonConnectedSubstructureSearch.hpp:69
CDPL::Chem::CommonConnectedSubstructureSearch::findAllMappings
bool findAllMappings(const MolecularGraph &target)
Searches for all possible atom/bond mappings of connected query subgraphs to substructures of the spe...
CDPL::Util::ObjectStack< AtomBondMapping >
CDPL::Util::operator>
bool operator>(const Array< ValueType > &array1, const Array< ValueType > &array2)
Greater than comparison operator.
CDPL::Chem::CommonConnectedSubstructureSearch::CommonConnectedSubstructureSearch
CommonConnectedSubstructureSearch(const MolecularGraph &query)
Constructs and initializes a CommonConnectedSubstructureSearch instance for the specified query struc...
CDPL::Chem::CommonConnectedSubstructureSearch::begin
ConstMappingIterator begin() const
Returns a constant iterator pointing to the beginning of the stored atom/bond mapping objects.