Chemical Data Processing Library C++ API - Version 1.4.0
ReactionSubstructureSearch.hpp
Go to the documentation of this file.
1 /*
2  * ReactionSubstructureSearch.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_REACTIONSUBSTRUCTURESEARCH_HPP
30 #define CDPL_CHEM_REACTIONSUBSTRUCTURESEARCH_HPP
31 
32 #include <vector>
33 #include <deque>
34 #include <set>
35 #include <utility>
36 #include <cstddef>
37 
38 #include <boost/iterator/indirect_iterator.hpp>
39 
40 #include "CDPL/Chem/APIPrefix.hpp"
43 #include "CDPL/Util/BitSet.hpp"
45 
46 
47 namespace CDPL
48 {
49 
50  namespace Chem
51  {
52 
53  class Reaction;
54  class MolecularGraph;
55  class Atom;
56  class Bond;
57 
71  {
72 
73  typedef std::vector<AtomBondMapping*> ABMappingList;
74 
75  public:
79  typedef boost::indirect_iterator<ABMappingList::iterator, AtomBondMapping> MappingIterator;
80 
84  typedef boost::indirect_iterator<ABMappingList::const_iterator, const AtomBondMapping> ConstMappingIterator;
85 
90 
96 
98 
105 
107 
112  void setQuery(const Reaction& query);
113 
126  bool mappingExists(const Reaction& target);
127 
142  bool findMappings(const Reaction& target);
143 
148  std::size_t getNumMappings() const;
149 
156  AtomBondMapping& getMapping(std::size_t idx);
157 
164  const AtomBondMapping& getMapping(std::size_t idx) const;
165 
171 
177 
183 
189 
195 
201 
207 
213 
225  void uniqueMappingsOnly(bool unique);
226 
232  bool uniqueMappingsOnly() const;
233 
244  void setMaxNumMappings(std::size_t max_num_mappings);
245 
251  std::size_t getMaxNumMappings() const;
252 
264  void setEnabledReactionRoles(unsigned int roles);
265 
272  unsigned int getEnabledReactionRoles() const;
273 
274  private:
275  typedef std::pair<std::size_t, std::size_t> IndexOffsetPair;
276 
277  bool init(const Reaction&);
278 
279  void initQueryData();
280  void initTargetData();
281 
282  bool findEquivAtoms();
283  bool findEquivBonds();
284 
285  bool mapAtoms();
286 
287  std::size_t nextQueryAtom() const;
288  bool nextTargetAtom(std::size_t, unsigned int, std::size_t&, std::size_t&) const;
289 
290  bool atomMappingAllowed(std::size_t, std::size_t) const;
291 
292  bool mapBonds(const Atom*, const Atom*, const IndexOffsetPair&);
293  bool mapAtoms(std::size_t);
294  bool mapAtoms(std::size_t, std::size_t);
295 
296  bool mappingFound();
297 
298  bool hasPostMappingMatchExprs() const;
299  bool foundMappingMatches(const AtomBondMapping*) const;
300 
301  bool foundMappingUnique();
302 
303  void freeAtomBondMappings();
304  void freeAtomBondMapping();
305 
306  AtomBondMapping* createAtomBondMapping();
307 
308  class ABMappingMask
309  {
310 
311  public:
312  void initAtomMask(std::size_t);
313  void initBondMask(std::size_t);
314 
315  void setAtomBit(std::size_t);
316  void resetAtomBit(std::size_t);
317 
318  bool testAtomBit(std::size_t) const;
319 
320  void setBondBit(std::size_t);
321  void resetBondMask();
322 
323  bool operator<(const ABMappingMask&) const;
324  bool operator>(const ABMappingMask&) const;
325 
326  private:
327  Util::BitSet atomMask;
328  Util::BitSet bondMask;
329  };
330 
331  typedef MatchExpression<Reaction>::SharedPointer ReactionMatchExprPtr;
332 
333  typedef std::vector<Util::BitSet> BitMatrix;
334  typedef std::vector<const Atom*> AtomMappingTable;
335  typedef std::vector<std::size_t> BondMappingTable;
336  typedef std::deque<std::size_t> AtomQueue;
337  typedef std::set<ABMappingMask> UniqueMappingList;
338  typedef std::vector<const Atom*> AtomList;
339  typedef std::vector<const Bond*> BondList;
340  typedef std::vector<std::size_t> IndexList;
341  typedef std::vector<MatchExpression<Atom, MolecularGraph>::SharedPointer> AtomMatchExprTable;
342  typedef std::vector<MatchExpression<Bond, MolecularGraph>::SharedPointer> BondMatchExprTable;
343  typedef std::vector<IndexOffsetPair> IndexOffsetTable;
344  typedef Util::ObjectStack<AtomBondMapping> MappingCache;
345 
346  const Reaction* query;
347  const Reaction* target;
348  AtomList queryAtoms;
349  BondList queryBonds;
350  AtomList targetAtoms;
351  BondList targetBonds;
352  IndexOffsetTable queryABIndexOffsets;
353  IndexOffsetTable targetABIndexOffsets;
354  BitMatrix atomEquivMatrix;
355  BitMatrix bondEquivMatrix;
356  AtomQueue termQueryAtoms;
357  AtomMappingTable queryAtomMapping;
358  BondMappingTable queryBondMapping;
359  Util::BitSet queryMappingMask;
360  ABMappingMask targetMappingMask;
361  ABMappingList foundMappings;
362  UniqueMappingList uniqueMappings;
363  AtomMatchExprTable atomMatchExprTable;
364  BondMatchExprTable bondMatchExprTable;
365  ReactionMatchExprPtr rxnMatchExpression;
366  IndexList postMappingMatchAtoms;
367  IndexList postMappingMatchBonds;
368  MappingCache mappingCache;
369  bool queryChanged;
370  bool initQueryMappingData;
371  bool uniqueMatches;
372  bool saveMappings;
373  unsigned int enabledRxnRoles;
374  std::size_t numQueryAtoms;
375  std::size_t numQueryBonds;
376  std::size_t numTargetAtoms;
377  std::size_t numTargetBonds;
378  std::size_t maxNumMappings;
379  };
380  } // namespace Chem
381 } // namespace CDPL
382 
383 #endif // CDPL_CHEM_REACTIONSUBSTRUCTURESEARCH_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
Generic boolean expression interface for the implementation of query/target object equivalence tests ...
Definition: MatchExpression.hpp:75
Searches for substructures of the components of a target reaction that match a given query reaction p...
Definition: ReactionSubstructureSearch.hpp:71
MappingIterator getMappingsBegin()
Returns a mutable iterator pointing to the beginning of the stored Chem::AtomBondMapping objects.
ReactionSubstructureSearch()
Constructs and initializes the ReactionSubstructureSearch instance.
ConstMappingIterator end() const
Returns a constant iterator pointing to the end of the stored const Chem::AtomBondMapping objects.
std::size_t getNumMappings() const
Returns the number of atom/bond mappings that were recorded in the last call to findMappings().
bool mappingExists(const Reaction &target)
Tells whether the query reaction pattern matches the target reaction target.
std::size_t getMaxNumMappings() const
Returns the specified limit on the number of stored atom/bond mappings.
void setQuery(const Reaction &query)
Sets query as the new query reaction pattern.
bool findMappings(const Reaction &target)
Searches for all possible atom/bond mappings between the query reaction pattern and the target reacti...
const AtomBondMapping & getMapping(std::size_t idx) const
Returns a const reference to the stored atom/bond mapping object at index idx.
void setMaxNumMappings(std::size_t max_num_mappings)
Allows to specify a limit on the number of stored atom/bond mappings.
MappingIterator getMappingsEnd()
Returns a mutable iterator pointing to the end of the stored Chem::AtomBondMapping objects.
void setEnabledReactionRoles(unsigned int roles)
Allows the reaction role specific exclusion of query and target components from the search for matchi...
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: ReactionSubstructureSearch.hpp:84
ConstMappingIterator begin() const
Returns a constant iterator pointing to the beginning of the stored const Chem::AtomBondMapping objec...
ReactionSubstructureSearch & operator=(const ReactionSubstructureSearch &)=delete
void uniqueMappingsOnly(bool unique)
Allows to specify whether or not to store only unique atom/bond mappings.
bool uniqueMappingsOnly() const
Tells whether duplicate atom/bond mappings are discarded.
AtomBondMapping & getMapping(std::size_t idx)
Returns a non-const reference to the stored atom/bond mapping object at index idx.
ReactionSubstructureSearch(const Reaction &query)
Constructs and initializes the ReactionSubstructureSearch instance for the specified query reaction p...
boost::indirect_iterator< ABMappingList::iterator, AtomBondMapping > MappingIterator
A mutable random access iterator used to iterate over the stored Chem::AtomBondMapping mapping object...
Definition: ReactionSubstructureSearch.hpp:79
MappingIterator begin()
Returns a mutable iterator pointing to the beginning of the stored Chem::AtomBondMapping objects.
unsigned int getEnabledReactionRoles() const
Tells which reaction component roles are considered in the search for matching reaction substructures...
ReactionSubstructureSearch(const ReactionSubstructureSearch &)=delete
MappingIterator end()
Returns a mutable iterator pointing to the end of the stored Chem::AtomBondMapping objects.
ConstMappingIterator getMappingsBegin() const
Returns a constant iterator pointing to the beginning of the stored const Chem::AtomBondMapping objec...
ConstMappingIterator getMappingsEnd() const
Returns a constant iterator pointing to the end of the stored const Chem::AtomBondMapping objects.
Abstract base class for chemical reactions composed of role-tagged reaction components of type Chem::...
Definition: Reaction.hpp:59
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.