Chemical Data Processing Library C++ API - Version 1.4.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 
80  {
81 
82  typedef std::vector<AtomBondMapping*> ABMappingList;
83 
84  typedef MatchExpression<MolecularGraph>::SharedPointer MolGraphMatchExprPtr;
87 
88  public:
93  typedef std::shared_ptr<CommonConnectedSubstructureSearch> SharedPointer;
94 
98  typedef boost::indirect_iterator<ABMappingList::iterator, AtomBondMapping> MappingIterator;
99 
103  typedef boost::indirect_iterator<ABMappingList::const_iterator, const AtomBondMapping> ConstMappingIterator;
104 
108  typedef std::function<const AtomMatchExprPtr&(const Atom&)> AtomMatchExpressionFunction;
109 
113  typedef std::function<const BondMatchExprPtr&(const Bond&)> BondMatchExpressionFunction;
114 
118  typedef std::function<const MolGraphMatchExprPtr&(const MolecularGraph&)> MolecularGraphMatchExpressionFunction;
119 
124 
130 
132 
139 
141 
147 
153 
159 
164  void setQuery(const MolecularGraph& query);
165 
179  bool mappingExists(const MolecularGraph& target);
180 
195  bool findAllMappings(const MolecularGraph& target);
196 
211  bool findMaxMappings(const MolecularGraph& target);
212 
218  std::size_t getNumMappings() const;
219 
226  AtomBondMapping& getMapping(std::size_t idx);
227 
234  const AtomBondMapping& getMapping(std::size_t idx) const;
235 
241 
247 
253 
259 
265 
271 
277 
283 
295  void uniqueMappingsOnly(bool unique);
296 
302  bool uniqueMappingsOnly() const;
303 
314  void setMaxNumMappings(std::size_t max_num_mappings);
315 
321  std::size_t getMaxNumMappings() const;
322 
332  void setMinSubstructureSize(std::size_t min_size);
333 
339  std::size_t getMinSubstructureSize() const;
340 
341  private:
342  bool init(const MolecularGraph&);
343 
344  void initMatchExpressions();
345 
346  bool findEquivAtoms();
347  bool findEquivBonds();
348 
349  bool mapAtoms();
350  bool mapAtoms(std::size_t);
351  bool mapAtoms(std::size_t, std::size_t);
352 
353  bool nextTargetAtom(std::size_t, std::size_t&, std::size_t&) const;
354 
355  bool mappingFound();
356 
357  bool hasPostMappingMatchExprs() const;
358  bool foundMappingMatches(const AtomBondMapping*) const;
359 
360  bool foundMappingUnique();
361  bool mappingAlreadySeen(const AtomBondMapping*) const;
362 
363  void clearMappings();
364 
365  void freeAtomBondMappings();
366  void freeAtomBondMapping();
367 
368  AtomBondMapping* createAtomBondMapping();
369 
370  class ABMappingMask
371  {
372 
373  public:
374  void initQueryAtomMask(std::size_t);
375  void initTargetAtomMask(std::size_t);
376 
377  void initQueryBondMask(std::size_t);
378  void initTargetBondMask(std::size_t);
379 
380  void setQueryAtomBit(std::size_t);
381  void setTargetAtomBit(std::size_t);
382 
383  void resetQueryAtomBit(std::size_t);
384  void resetTargetAtomBit(std::size_t);
385 
386  bool testTargetAtomBit(std::size_t) const;
387 
388  void setQueryBondBit(std::size_t);
389  void setTargetBondBit(std::size_t);
390 
391  void resetBondMasks();
392 
393  bool operator<(const ABMappingMask&) const;
394  bool operator>(const ABMappingMask&) const;
395 
396  private:
397  Util::BitSet queryAtomMask;
398  Util::BitSet targetAtomMask;
399  Util::BitSet queryBondMask;
400  Util::BitSet targetBondMask;
401  };
402 
403  typedef std::vector<Util::BitSet> BitMatrix;
404  typedef std::vector<const Atom*> AtomMappingTable;
405  typedef std::vector<std::size_t> AtomIndexList;
406  typedef std::vector<std::size_t> BondMappingStack;
407  typedef std::deque<std::size_t> AtomQueue;
408  typedef std::set<ABMappingMask> UniqueMappingList;
409  typedef std::vector<const Atom*> AtomList;
410  typedef std::vector<const Bond*> BondList;
411  typedef std::vector<MatchExpression<Atom, MolecularGraph>::SharedPointer> AtomMatchExprTable;
412  typedef std::vector<MatchExpression<Bond, MolecularGraph>::SharedPointer> BondMatchExprTable;
413  typedef Util::ObjectStack<AtomBondMapping> MappingCache;
414 
415  const MolecularGraph* query;
416  const MolecularGraph* target;
417  AtomMatchExpressionFunction atomMatchExprFunc;
418  BondMatchExpressionFunction bondMatchExprFunc;
419  MolecularGraphMatchExpressionFunction molGraphMatchExprFunc;
420  BitMatrix atomEquivMatrix;
421  BitMatrix bondEquivMatrix;
422  AtomQueue termQueryAtoms;
423  AtomIndexList termTargetAtoms;
424  BondMappingStack bondMappingStack;
425  AtomMappingTable queryAtomMapping;
426  ABMappingMask mappingMask;
427  Util::BitSet hiddenQueryAtomMask;
428  Util::BitSet termQueryAtomMask;
429  Util::BitSet termTargetAtomMask;
430  ABMappingList foundMappings;
431  UniqueMappingList uniqueMappings;
432  AtomMatchExprTable atomMatchExprTable;
433  BondMatchExprTable bondMatchExprTable;
434  MolGraphMatchExprPtr molGraphMatchExpr;
435  AtomList postMappingMatchAtoms;
436  BondList postMappingMatchBonds;
437  MappingCache mappingCache;
438  bool queryChanged;
439  bool initQueryData;
440  bool uniqueMatches;
441  bool saveMappings;
442  bool maxMappingsOnly;
443  std::size_t numQueryAtoms;
444  std::size_t numQueryBonds;
445  std::size_t numTargetAtoms;
446  std::size_t numTargetBonds;
447  std::size_t numMappedAtoms;
448  std::size_t currMaxSubstructureSize;
449  std::size_t maxBondStackSize;
450  std::size_t maxNumMappings;
451  std::size_t minSubstructureSize;
452  };
453  } // namespace Chem
454 } // namespace CDPL
455 
456 #endif // CDPL_CHEM_COMMONCONNECTEDSUBSTRUCTURESEARCH_HPP
Definition of class CDPL::Chem::AtomBondMapping.
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::MatchExpression.
Definition of class CDPL::Util::ObjectStack.
Data structure for the common storage of related atom to atom and bond to bond mappings.
Definition: AtomBondMapping.hpp:55
Abstract base class representing a chemical atom and its bonded neighborhood.
Definition: Atom.hpp:58
Abstract base class representing a chemical bond between two atoms (represented by Chem::Atom instanc...
Definition: Bond.hpp:54
Enumerates all maximal common connected substructures shared between a query and a target molecular g...
Definition: CommonConnectedSubstructureSearch.hpp:80
std::function< const MolGraphMatchExprPtr &(const MolecularGraph &)> MolecularGraphMatchExpressionFunction
Type of the functor used to retrieve the Chem::MatchExpression implementation instance for the query ...
Definition: CommonConnectedSubstructureSearch.hpp:118
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 Chem::AtomBondMapping objects.
Definition: CommonConnectedSubstructureSearch.hpp:98
std::function< const AtomMatchExprPtr &(const Atom &)> AtomMatchExpressionFunction
Type of the functor used to retrieve the Chem::MatchExpression implementation instance for a query at...
Definition: CommonConnectedSubstructureSearch.hpp:108
ConstMappingIterator end() const
Returns a constant iterator pointing to the end of the stored const Chem::AtomBondMapping objects.
void setQuery(const MolecularGraph &query)
Sets query as the new query molecular graph.
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 Chem::AtomBondMapping objects.
bool mappingExists(const MolecularGraph &target)
Searches for a common connected substructure between the query and the target molecular graph target.
void setAtomMatchExpressionFunction(const AtomMatchExpressionFunction &func)
Installs a function that resolves the Chem::MatchExpression implementation instance for a query atom.
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 const Chem::AtomBondMapping objects...
Definition: CommonConnectedSubstructureSearch.hpp:103
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
A reference-counted smart pointer [SHPTR] for dynamically allocated CommonConnectedSubstructureSearch...
Definition: CommonConnectedSubstructureSearch.hpp:93
void setMolecularGraphMatchExpressionFunction(const MolecularGraphMatchExpressionFunction &func)
Installs a function that resolves the Chem::MatchExpression implementation instance for the query mol...
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 tar...
bool uniqueMappingsOnly() const
Tells whether duplicate atom/bond mappings are discarded.
CommonConnectedSubstructureSearch(const MolecularGraph &query)
Constructs and initializes the CommonConnectedSubstructureSearch instance for the query molecular gra...
MappingIterator getMappingsBegin()
Returns a mutable iterator pointing to the beginning of the stored Chem::AtomBondMapping 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 const Chem::AtomBondMapping objects.
CommonConnectedSubstructureSearch(const CommonConnectedSubstructureSearch &)=delete
std::function< const BondMatchExprPtr &(const Bond &)> BondMatchExpressionFunction
Type of the functor used to retrieve the Chem::MatchExpression implementation instance for a query bo...
Definition: CommonConnectedSubstructureSearch.hpp:113
CommonConnectedSubstructureSearch & operator=(const CommonConnectedSubstructureSearch &)=delete
void setBondMatchExpressionFunction(const BondMatchExpressionFunction &func)
Installs a function that resolves the Chem::MatchExpression implementation instance for a query bond.
ConstMappingIterator getMappingsBegin() const
Returns a constant iterator pointing to the beginning of the stored const Chem::AtomBondMapping objec...
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 Chem::AtomBondMapping objects.
ConstMappingIterator begin() const
Returns a constant iterator pointing to the beginning of the stored const Chem::AtomBondMapping objec...
std::size_t getMaxNumMappings() const
Returns the specified limit on the number of stored atom/bond mappings.
CommonConnectedSubstructureSearch()
Constructs and initializes the CommonConnectedSubstructureSearch instance.
MappingIterator getMappingsEnd()
Returns a mutable iterator pointing to the end of the stored Chem::AtomBondMapping objects.
Generic boolean expression interface for the implementation of query/target object equivalence tests ...
Definition: MatchExpression.hpp:75
Abstract base class for data structures that represent chemical structures as molecular graphs.
Definition: MolecularGraph.hpp:60
bool operator<(const Array< ValueType > &array1, const Array< ValueType > &array2)
Less than comparison operator.
boost::dynamic_bitset BitSet
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.