Chemical Data Processing Library C++ API - Version 1.3.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 #include <memory>
37 #include <functional>
38 
39 #include <boost/iterator/indirect_iterator.hpp>
40 
41 #include "CDPL/Chem/APIPrefix.hpp"
44 #include "CDPL/Util/BitSet.hpp"
46 
47 
48 namespace CDPL
49 {
50 
51  namespace Chem
52  {
53 
54  class MolecularGraph;
55  class Atom;
56  class Bond;
57 
63  {
64 
65  typedef std::vector<AtomBondMapping*> ABMappingList;
66 
67  typedef MatchExpression<MolecularGraph>::SharedPointer MolGraphMatchExprPtr;
70 
71  public:
72  typedef std::shared_ptr<CommonConnectedSubstructureSearch> SharedPointer;
73 
77  typedef boost::indirect_iterator<ABMappingList::iterator, AtomBondMapping> MappingIterator;
78 
82  typedef boost::indirect_iterator<ABMappingList::const_iterator, const AtomBondMapping> ConstMappingIterator;
83 
84  typedef std::function<const AtomMatchExprPtr&(const Atom&)> AtomMatchExpressionFunction;
85  typedef std::function<const BondMatchExprPtr&(const Bond&)> BondMatchExpressionFunction;
86  typedef std::function<const MolGraphMatchExprPtr&(const MolecularGraph&)> MolecularGraphMatchExpressionFunction;
87 
92 
98 
100 
107 
109 
111 
113 
115 
120  void setQuery(const MolecularGraph& query);
121 
135  bool mappingExists(const MolecularGraph& target);
136 
151  bool findAllMappings(const MolecularGraph& target);
152 
167  bool findMaxMappings(const MolecularGraph& target);
168 
174  std::size_t getNumMappings() const;
175 
182  AtomBondMapping& getMapping(std::size_t idx);
183 
190  const AtomBondMapping& getMapping(std::size_t idx) const;
191 
197 
203 
209 
215 
221 
227 
233 
239 
251  void uniqueMappingsOnly(bool unique);
252 
258  bool uniqueMappingsOnly() const;
259 
270  void setMaxNumMappings(std::size_t max_num_mappings);
271 
277  std::size_t getMaxNumMappings() const;
278 
288  void setMinSubstructureSize(std::size_t min_size);
289 
295  std::size_t getMinSubstructureSize() const;
296 
297  private:
298  bool init(const MolecularGraph&);
299 
300  void initMatchExpressions();
301 
302  bool findEquivAtoms();
303  bool findEquivBonds();
304 
305  bool mapAtoms();
306  bool mapAtoms(std::size_t);
307  bool mapAtoms(std::size_t, std::size_t);
308 
309  bool nextTargetAtom(std::size_t, std::size_t&, std::size_t&) const;
310 
311  bool mappingFound();
312 
313  bool hasPostMappingMatchExprs() const;
314  bool foundMappingMatches(const AtomBondMapping*) const;
315 
316  bool foundMappingUnique();
317  bool mappingAlreadySeen(const AtomBondMapping*) const;
318 
319  void clearMappings();
320 
321  void freeAtomBondMappings();
322  void freeAtomBondMapping();
323 
324  AtomBondMapping* createAtomBondMapping();
325 
326  class ABMappingMask
327  {
328 
329  public:
330  void initQueryAtomMask(std::size_t);
331  void initTargetAtomMask(std::size_t);
332 
333  void initQueryBondMask(std::size_t);
334  void initTargetBondMask(std::size_t);
335 
336  void setQueryAtomBit(std::size_t);
337  void setTargetAtomBit(std::size_t);
338 
339  void resetQueryAtomBit(std::size_t);
340  void resetTargetAtomBit(std::size_t);
341 
342  bool testTargetAtomBit(std::size_t) const;
343 
344  void setQueryBondBit(std::size_t);
345  void setTargetBondBit(std::size_t);
346 
347  void resetBondMasks();
348 
349  bool operator<(const ABMappingMask&) const;
350  bool operator>(const ABMappingMask&) const;
351 
352  private:
353  Util::BitSet queryAtomMask;
354  Util::BitSet targetAtomMask;
355  Util::BitSet queryBondMask;
356  Util::BitSet targetBondMask;
357  };
358 
359  typedef std::vector<Util::BitSet> BitMatrix;
360  typedef std::vector<const Atom*> AtomMappingTable;
361  typedef std::vector<std::size_t> AtomIndexList;
362  typedef std::vector<std::size_t> BondMappingStack;
363  typedef std::deque<std::size_t> AtomQueue;
364  typedef std::set<ABMappingMask> UniqueMappingList;
365  typedef std::vector<const Atom*> AtomList;
366  typedef std::vector<const Bond*> BondList;
367  typedef std::vector<MatchExpression<Atom, MolecularGraph>::SharedPointer> AtomMatchExprTable;
368  typedef std::vector<MatchExpression<Bond, MolecularGraph>::SharedPointer> BondMatchExprTable;
369  typedef Util::ObjectStack<AtomBondMapping> MappingCache;
370 
371  const MolecularGraph* query;
372  const MolecularGraph* target;
373  AtomMatchExpressionFunction atomMatchExprFunc;
374  BondMatchExpressionFunction bondMatchExprFunc;
375  MolecularGraphMatchExpressionFunction molGraphMatchExprFunc;
376  BitMatrix atomEquivMatrix;
377  BitMatrix bondEquivMatrix;
378  AtomQueue termQueryAtoms;
379  AtomIndexList termTargetAtoms;
380  BondMappingStack bondMappingStack;
381  AtomMappingTable queryAtomMapping;
382  ABMappingMask mappingMask;
383  Util::BitSet hiddenQueryAtomMask;
384  Util::BitSet termQueryAtomMask;
385  Util::BitSet termTargetAtomMask;
386  ABMappingList foundMappings;
387  UniqueMappingList uniqueMappings;
388  AtomMatchExprTable atomMatchExprTable;
389  BondMatchExprTable bondMatchExprTable;
390  MolGraphMatchExprPtr molGraphMatchExpr;
391  AtomList postMappingMatchAtoms;
392  BondList postMappingMatchBonds;
393  MappingCache mappingCache;
394  bool queryChanged;
395  bool initQueryData;
396  bool uniqueMatches;
397  bool saveMappings;
398  bool maxMappingsOnly;
399  std::size_t numQueryAtoms;
400  std::size_t numQueryBonds;
401  std::size_t numTargetAtoms;
402  std::size_t numTargetBonds;
403  std::size_t numMappedAtoms;
404  std::size_t currMaxSubstructureSize;
405  std::size_t maxBondStackSize;
406  std::size_t maxNumMappings;
407  std::size_t minSubstructureSize;
408  };
409  } // namespace Chem
410 } // namespace CDPL
411 
412 #endif // CDPL_CHEM_COMMONCONNECTEDSUBSTRUCTURESEARCH_HPP
Definition of the class CDPL::Chem::AtomBondMapping.
Definition of the 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 the class CDPL::Chem::MatchExpression.
Definition of the class CDPL::Util::ObjectStack.
A data structure for the common storage of related atom to atom and bond to bond mappings.
Definition: AtomBondMapping.hpp:55
Atom.
Definition: Atom.hpp:52
Bond.
Definition: Bond.hpp:50
CommonConnectedSubstructureSearch.
Definition: CommonConnectedSubstructureSearch.hpp:63
std::function< const MolGraphMatchExprPtr &(const MolecularGraph &)> MolecularGraphMatchExpressionFunction
Definition: CommonConnectedSubstructureSearch.hpp:86
std::size_t getNumMappings() const
Returns the number of atom/bond mappings that were recorded in the last search for common substructur...
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:77
std::function< const AtomMatchExprPtr &(const Atom &)> AtomMatchExpressionFunction
Definition: CommonConnectedSubstructureSearch.hpp:84
ConstMappingIterator end() const
Returns a constant iterator pointing to the end of the stored atom/bond mapping objects.
void setQuery(const MolecularGraph &query)
Allows to specify a new query structure.
void setMaxNumMappings(std::size_t max_num_mappings)
Allows to specify a limit on the number of stored atom/bond mappings.
MappingIterator begin()
Returns a mutable iterator pointing to the beginning of the stored atom/bond mapping objects.
bool mappingExists(const MolecularGraph &target)
Searches for a common connected substructure between the query and the specified target molecular gra...
void setAtomMatchExpressionFunction(const AtomMatchExpressionFunction &func)
const AtomBondMapping & getMapping(std::size_t idx) const
Returns a const reference to the stored atom/bond mapping object at index idx.
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:82
bool findMaxMappings(const MolecularGraph &target)
Searches for all maximum-sized atom/bond mappings of connected query subgraphs to substructures of th...
std::shared_ptr< CommonConnectedSubstructureSearch > SharedPointer
Definition: CommonConnectedSubstructureSearch.hpp:72
void setMolecularGraphMatchExpressionFunction(const MolecularGraphMatchExpressionFunction &func)
void setMinSubstructureSize(std::size_t min_size)
Allows to specify the minimum accepted common substructure size.
bool findAllMappings(const MolecularGraph &target)
Searches for all possible atom/bond mappings of connected query subgraphs to substructures of the spe...
bool uniqueMappingsOnly() const
Tells whether duplicate atom/bond mappings are discarded.
CommonConnectedSubstructureSearch(const MolecularGraph &query)
Constructs and initializes a CommonConnectedSubstructureSearch instance for the specified query struc...
MappingIterator getMappingsBegin()
Returns a mutable iterator pointing to the beginning of the stored atom/bond mapping objects.
AtomBondMapping & getMapping(std::size_t idx)
Returns a non-const reference to the stored atom/bond mapping object at index idx.
std::size_t getMinSubstructureSize() const
Returns the minimum accepted common substructure size.
ConstMappingIterator getMappingsEnd() const
Returns a constant iterator pointing to the end of the stored atom/bond mapping objects.
CommonConnectedSubstructureSearch(const CommonConnectedSubstructureSearch &)=delete
std::function< const BondMatchExprPtr &(const Bond &)> BondMatchExpressionFunction
Definition: CommonConnectedSubstructureSearch.hpp:85
CommonConnectedSubstructureSearch & operator=(const CommonConnectedSubstructureSearch &)=delete
void setBondMatchExpressionFunction(const BondMatchExpressionFunction &func)
ConstMappingIterator getMappingsBegin() const
Returns a constant iterator pointing to the beginning of the stored atom/bond mapping objects.
void uniqueMappingsOnly(bool unique)
Allows to specify whether or not to store only unique atom/bond mappings.
MappingIterator end()
Returns a mutable iterator pointing to the end of the stored atom/bond mapping objects.
ConstMappingIterator begin() const
Returns a constant iterator pointing to the beginning of the stored atom/bond mapping objects.
std::size_t getMaxNumMappings() const
Returns the specified limit on the number of stored atom/bond mappings.
CommonConnectedSubstructureSearch()
Constructs and initializes a CommonConnectedSubstructureSearch instance.
MappingIterator getMappingsEnd()
Returns a mutable iterator pointing to the end of the stored atom/bond mapping objects.
A generic boolean expression interface for the implementation of query/target object equivalence test...
Definition: MatchExpression.hpp:75
MolecularGraph.
Definition: MolecularGraph.hpp:52
bool operator<(const Array< ValueType > &array1, const Array< ValueType > &array2)
Less than comparison operator.
boost::dynamic_bitset BitSet
A dynamic bitset class.
Definition: BitSet.hpp:46
bool operator>(const Array< ValueType > &array1, const Array< ValueType > &array2)
Greater than comparison operator.
The namespace of the Chemical Data Processing Library.