Chemical Data Processing Library C++ API - Version 1.4.0
SubstructureSearch.hpp
Go to the documentation of this file.
1 /*
2  * SubstructureSearch.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_SUBSTRUCTURESEARCH_HPP
30 #define CDPL_CHEM_SUBSTRUCTURESEARCH_HPP
31 
32 #include <vector>
33 #include <deque>
34 #include <set>
35 #include <cstddef>
36 #include <unordered_map>
37 #include <memory>
38 #include <functional>
39 
40 #include <boost/iterator/indirect_iterator.hpp>
41 
42 #include "CDPL/Chem/APIPrefix.hpp"
45 #include "CDPL/Util/BitSet.hpp"
47 
48 
49 namespace CDPL
50 {
51 
52  namespace Chem
53  {
54 
55  class MolecularGraph;
56  class Atom;
57  class Bond;
58 
78  {
79 
80  typedef std::vector<AtomBondMapping*> ABMappingList;
81 
82  typedef MatchExpression<MolecularGraph>::SharedPointer MolGraphMatchExprPtr;
85 
86  public:
90  typedef std::shared_ptr<SubstructureSearch> SharedPointer;
91 
95  typedef boost::indirect_iterator<ABMappingList::iterator, AtomBondMapping> MappingIterator;
96 
100  typedef boost::indirect_iterator<ABMappingList::const_iterator, const AtomBondMapping> ConstMappingIterator;
101 
105  typedef std::function<const AtomMatchExprPtr&(const Atom&)> AtomMatchExpressionFunction;
106 
110  typedef std::function<const BondMatchExprPtr&(const Bond&)> BondMatchExpressionFunction;
111 
115  typedef std::function<const MolGraphMatchExprPtr&(const MolecularGraph&)> MolecularGraphMatchExpressionFunction;
116 
121 
127 
129 
136 
138 
144 
150 
156 
161  void setQuery(const MolecularGraph& query);
162 
175  bool mappingExists(const MolecularGraph& target);
176 
191  bool findMappings(const MolecularGraph& target);
192 
199  void stopSearch();
200 
205  std::size_t getNumMappings() const;
206 
213  AtomBondMapping& getMapping(std::size_t idx);
214 
221  const AtomBondMapping& getMapping(std::size_t idx) const;
222 
228 
234 
240 
246 
252 
258 
264 
270 
282  void uniqueMappingsOnly(bool unique);
283 
289  bool uniqueMappingsOnly() const;
290 
301  void setMaxNumMappings(std::size_t max_num_mappings);
302 
308  std::size_t getMaxNumMappings() const;
309 
323  void addAtomMappingConstraint(std::size_t query_atom_idx, std::size_t target_atom_idx);
324 
330 
344  void addBondMappingConstraint(std::size_t query_bond_idx, std::size_t target_bond_idx);
345 
351 
352  private:
353  bool init(const MolecularGraph&);
354 
355  void initMatchExpressions();
356 
357  bool findEquivAtoms();
358  bool findEquivBonds();
359 
360  bool mapAtoms();
361 
362  std::size_t nextQueryAtom() const;
363  bool nextTargetAtom(std::size_t, std::size_t&, std::size_t&) const;
364 
365  bool atomMappingAllowed(std::size_t, std::size_t) const;
366  bool checkAtomMappingConstraints(std::size_t, std::size_t) const;
367  bool checkBondMappingConstraints(std::size_t, std::size_t) const;
368 
369  bool mapBonds(std::size_t, std::size_t);
370  bool mapAtoms(std::size_t);
371  bool mapAtoms(std::size_t, std::size_t);
372 
373  bool mappingFound();
374 
375  bool hasPostMappingMatchExprs() const;
376  bool foundMappingMatches(const AtomBondMapping*) const;
377 
378  bool foundMappingUnique();
379 
380  void freeAtomBondMappings();
381  void freeAtomBondMapping();
382 
383  AtomBondMapping* createAtomBondMapping();
384 
385  class ABMappingMask
386  {
387 
388  public:
389  void initAtomMask(std::size_t);
390  void initBondMask(std::size_t);
391 
392  void setAtomBit(std::size_t);
393  void resetAtomBit(std::size_t);
394 
395  bool testAtomBit(std::size_t) const;
396 
397  void setBondBit(std::size_t);
398  void resetBondMask();
399 
400  bool operator<(const ABMappingMask&) const;
401  bool operator>(const ABMappingMask&) const;
402 
403  private:
404  Util::BitSet atomMask;
405  Util::BitSet bondMask;
406  };
407 
408  typedef std::vector<Util::BitSet> BitMatrix;
409  typedef std::vector<const Atom*> AtomMappingTable;
410  typedef std::vector<const Bond*> BondMappingTable;
411  typedef std::deque<std::size_t> AtomQueue;
412  typedef std::set<ABMappingMask> UniqueMappingList;
413  typedef std::vector<const Atom*> AtomList;
414  typedef std::vector<const Bond*> BondList;
415  typedef std::vector<AtomMatchExprPtr> AtomMatchExprTable;
416  typedef std::vector<BondMatchExprPtr> BondMatchExprTable;
417  typedef std::unordered_multimap<std::size_t, std::size_t> MappingConstraintMap;
418  typedef Util::ObjectStack<AtomBondMapping> MappingCache;
419 
420  const MolecularGraph* query;
421  const MolecularGraph* target;
422  AtomMatchExpressionFunction atomMatchExprFunc;
423  BondMatchExpressionFunction bondMatchExprFunc;
424  MolecularGraphMatchExpressionFunction molGraphMatchExprFunc;
425  BitMatrix atomEquivMatrix;
426  BitMatrix bondEquivMatrix;
427  MappingConstraintMap atomMappingConstrs;
428  MappingConstraintMap bondMappingConstrs;
429  AtomQueue termQueryAtoms;
430  AtomMappingTable queryAtomMapping;
431  BondMappingTable queryBondMapping;
432  Util::BitSet queryMappingMask;
433  ABMappingMask targetMappingMask;
434  ABMappingList foundMappings;
435  UniqueMappingList uniqueMappings;
436  AtomMatchExprTable atomMatchExprTable;
437  BondMatchExprTable bondMatchExprTable;
438  MolGraphMatchExprPtr molGraphMatchExpr;
439  AtomList postMappingMatchAtoms;
440  BondList postMappingMatchBonds;
441  MappingCache mappingCache;
442  bool queryChanged;
443  bool initQueryData;
444  bool uniqueMatches;
445  bool saveMappings;
446  bool exitSearch;
447  std::size_t numQueryAtoms;
448  std::size_t numQueryBonds;
449  std::size_t numTargetAtoms;
450  std::size_t numTargetBonds;
451  std::size_t numMappedAtoms;
452  std::size_t maxNumMappings;
453  };
454  } // namespace Chem
455 } // namespace CDPL
456 
457 #endif // CDPL_CHEM_SUBSTRUCTURESEARCH_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
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
Searches for substructures of a target molecular graph that match the topology of a given query molec...
Definition: SubstructureSearch.hpp:78
MappingIterator getMappingsBegin()
Returns a mutable iterator pointing to the beginning of the stored Chem::AtomBondMapping objects.
void clearAtomMappingConstraints()
Clears all previously defined query to target molecular graph atom mapping constraints.
void clearBondMappingConstraints()
Clears all previously defined query to target molecular graph bond mapping constraints.
void addBondMappingConstraint(std::size_t query_bond_idx, std::size_t target_bond_idx)
Adds a constraint on the allowed mappings between query and target molecular graph bonds.
void stopSearch()
Aborts the currently running substructure search process.
void setMolecularGraphMatchExpressionFunction(const MolecularGraphMatchExpressionFunction &func)
Installs a function that resolves the Chem::MatchExpression implementation instance for the query mol...
std::function< const BondMatchExprPtr &(const Bond &)> BondMatchExpressionFunction
Type of the functor used to retrieve the Chem::MatchExpression implementation instance for a query bo...
Definition: SubstructureSearch.hpp:110
bool uniqueMappingsOnly() const
Tells whether duplicate atom/bond mappings are discarded.
MappingIterator begin()
Returns a mutable iterator pointing to the beginning of the stored Chem::AtomBondMapping objects.
void uniqueMappingsOnly(bool unique)
Allows to specify whether or not to store only unique atom/bond mappings.
ConstMappingIterator getMappingsBegin() const
Returns a constant iterator pointing to the beginning of the stored const Chem::AtomBondMapping objec...
AtomBondMapping & getMapping(std::size_t idx)
Returns a non-const reference to the stored atom/bond mapping object at index idx.
MappingIterator end()
Returns a mutable iterator pointing to the end of the stored Chem::AtomBondMapping objects.
void addAtomMappingConstraint(std::size_t query_atom_idx, std::size_t target_atom_idx)
Adds a constraint on the allowed mappings between query and target molecular graph atoms.
std::size_t getNumMappings() const
Returns the number of atom/bond mappings that were recorded in the last call to findMappings().
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.
bool mappingExists(const MolecularGraph &target)
Tells whether the query molecular graph matches a substructure of the target molecular graph target.
void setBondMatchExpressionFunction(const BondMatchExpressionFunction &func)
Installs a function that resolves the Chem::MatchExpression implementation instance for a query bond.
std::shared_ptr< SubstructureSearch > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated SubstructureSearch instances.
Definition: SubstructureSearch.hpp:90
SubstructureSearch()
Constructs and initializes the SubstructureSearch instance.
bool findMappings(const MolecularGraph &target)
Searches for all possible atom/bond mappings of the query molecular graph to substructures of the tar...
SubstructureSearch(const MolecularGraph &query)
Constructs and initializes the SubstructureSearch instance for the query molecular graph query.
ConstMappingIterator begin() const
Returns a constant iterator pointing to the beginning of the stored const Chem::AtomBondMapping objec...
std::function< const MolGraphMatchExprPtr &(const MolecularGraph &)> MolecularGraphMatchExpressionFunction
Type of the functor used to retrieve the Chem::MatchExpression implementation instance for the query ...
Definition: SubstructureSearch.hpp:115
void setAtomMatchExpressionFunction(const AtomMatchExpressionFunction &func)
Installs a function that resolves the Chem::MatchExpression implementation instance for a query atom.
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: SubstructureSearch.hpp:100
std::function< const AtomMatchExprPtr &(const Atom &)> AtomMatchExpressionFunction
Type of the functor used to retrieve the Chem::MatchExpression implementation instance for a query at...
Definition: SubstructureSearch.hpp:105
boost::indirect_iterator< ABMappingList::iterator, AtomBondMapping > MappingIterator
A mutable random access iterator used to iterate over the stored Chem::AtomBondMapping objects.
Definition: SubstructureSearch.hpp:95
MappingIterator getMappingsEnd()
Returns a mutable iterator pointing to the end of the stored Chem::AtomBondMapping objects.
SubstructureSearch & operator=(const SubstructureSearch &)=delete
const AtomBondMapping & getMapping(std::size_t idx) const
Returns a const reference to the stored atom/bond mapping object at index idx.
std::size_t getMaxNumMappings() const
Returns the specified limit on the number of stored atom/bond mappings.
ConstMappingIterator end() const
Returns a constant iterator pointing to the end of the stored const Chem::AtomBondMapping objects.
SubstructureSearch(const SubstructureSearch &)=delete
ConstMappingIterator getMappingsEnd() const
Returns a constant iterator pointing to the end of the stored const Chem::AtomBondMapping objects.
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.