Chemical Data Processing Library C++ API - Version 1.4.0
MaxCommonBondSubstructureSearch.hpp
Go to the documentation of this file.
1 /*
2  * MaxCommonBondSubstructureSearch.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_MAXCOMMONBONDSUBSTRUCTURESEARCH_HPP
30 #define CDPL_CHEM_MAXCOMMONBONDSUBSTRUCTURESEARCH_HPP
31 
32 #include <vector>
33 #include <set>
34 #include <cstddef>
35 #include <memory>
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 
71  {
72 
73  typedef std::vector<AtomBondMapping*> ABMappingList;
74 
75  public:
80  typedef std::shared_ptr<MaxCommonBondSubstructureSearch> SharedPointer;
81 
85  typedef boost::indirect_iterator<ABMappingList::iterator, AtomBondMapping> MappingIterator;
86 
90  typedef boost::indirect_iterator<ABMappingList::const_iterator, const AtomBondMapping> ConstMappingIterator;
91 
96 
102 
104 
111 
113 
118  void setQuery(const MolecularGraph& query);
119 
133  bool mappingExists(const MolecularGraph& target);
134 
150  bool findMappings(const MolecularGraph& target);
151 
157  std::size_t getNumMappings() const;
158 
165  AtomBondMapping& getMapping(std::size_t idx);
166 
173  const AtomBondMapping& getMapping(std::size_t idx) const;
174 
180 
186 
192 
198 
204 
210 
216 
222 
234  void uniqueMappingsOnly(bool unique);
235 
241  bool uniqueMappingsOnly() const;
242 
253  void setMaxNumMappings(std::size_t max_num_mappings);
254 
260  std::size_t getMaxNumMappings() const;
261 
271  void setMinSubstructureSize(std::size_t min_size);
272 
278  std::size_t getMinSubstructureSize() const;
279 
280  private:
281  class AGNode;
282 
283  bool init(const MolecularGraph&);
284 
285  void initMatchExpressions();
286 
287  bool findEquivAtoms();
288 
289  bool atomsCompatible(const Bond&, const Bond&) const;
290 
291  const Atom* getCommonAtom(const Bond&, const Bond&) const;
292 
293  bool buildAssocGraph();
294 
295  bool findAssocGraphCliques(std::size_t);
296  bool isLegal(const AGNode*);
297 
298  void undoAtomMapping(std::size_t);
299 
300  bool mappingFound();
301 
302  bool hasPostMappingMatchExprs() const;
303  bool foundMappingMatches(const AtomBondMapping*) const;
304 
305  bool foundMappingUnique(bool);
306 
307  void clearMappings();
308 
309  AtomBondMapping* createAtomBondMapping(bool);
310  void freeAtomBondMapping();
311 
312  void freeAssocGraph();
313  void freeAtomBondMappings();
314 
315  class AGEdge;
316 
317  AGNode* allocAGNode(const Bond*, const Bond*);
318  AGEdge* allocAGEdge(const Atom*, const Atom*);
319 
320  typedef std::vector<const AGEdge*> AGraphEdgeList;
321 
322  class AGNode
323  {
324 
325  public:
326  void setQueryBond(const Bond*);
327  const Bond* getQueryBond() const;
328 
329  void setAssocBond(const Bond*);
330  const Bond* getAssocBond() const;
331 
332  void addEdge(const AGEdge*);
333 
334  bool isConnected(const AGNode*) const;
335  const AGEdge* findEdge(const AGNode*) const;
336 
337  void clear();
338 
339  void setIndex(std::size_t idx);
340 
341  private:
342  std::size_t index;
343  const Bond* queryBond;
344  const Bond* assocBond;
345  Util::BitSet connNodes;
346  AGraphEdgeList atomEdges;
347  };
348 
349  class AGEdge
350  {
351 
352  public:
353  void setQueryAtom(const Atom*);
354  const Atom* getQueryAtom() const;
355 
356  void setAssocAtom(const Atom*);
357  const Atom* getAssocAtom() const;
358 
359  void setNode1(const AGNode*);
360  void setNode2(const AGNode*);
361 
362  const AGNode* getNode1() const;
363  const AGNode* getNode2() const;
364 
365  const AGNode* getOther(const AGNode*) const;
366 
367  private:
368  const Atom* queryAtom;
369  const Atom* assocAtom;
370  const AGNode* node1;
371  const AGNode* node2;
372  };
373 
374  class ABMappingMask
375  {
376 
377  public:
378  void initQueryAtomMask(std::size_t);
379  void initTargetAtomMask(std::size_t);
380 
381  void initQueryBondMask(std::size_t);
382  void initTargetBondMask(std::size_t);
383 
384  void setQueryAtomBit(std::size_t);
385  void setTargetAtomBit(std::size_t);
386 
387  void resetQueryAtomBit(std::size_t);
388  void resetTargetAtomBit(std::size_t);
389 
390  bool testQueryAtomBit(std::size_t) const;
391  bool testTargetAtomBit(std::size_t) const;
392 
393  void setQueryBondBit(std::size_t);
394  void setTargetBondBit(std::size_t);
395 
396  void resetQueryAtomBits();
397  void resetTargetAtomBits();
398 
399  void resetBondBits();
400 
401  bool operator<(const ABMappingMask&) const;
402  bool operator>(const ABMappingMask&) const;
403 
404  private:
405  Util::BitSet queryAtomMask;
406  Util::BitSet targetAtomMask;
407  Util::BitSet queryBondMask;
408  Util::BitSet targetBondMask;
409  };
410 
411  typedef MatchExpression<MolecularGraph>::SharedPointer MolGraphMatchExprPtr;
412 
413  typedef std::vector<Util::BitSet> BitMatrix;
414  typedef std::vector<AGNode*> AGraphNodeList;
415  typedef std::vector<AGraphNodeList> AGraphNodeMatrix;
416  typedef std::set<ABMappingMask> UniqueMappingList;
417  typedef std::vector<const Atom*> AtomList;
418  typedef std::vector<const Bond*> BondList;
419  typedef std::vector<MatchExpression<Atom, MolecularGraph>::SharedPointer> AtomMatchExprTable;
420  typedef std::vector<MatchExpression<Bond, MolecularGraph>::SharedPointer> BondMatchExprTable;
421  typedef Util::ObjectStack<AGNode> NodeCache;
422  typedef Util::ObjectStack<AGEdge> EdgeCache;
423  typedef Util::ObjectStack<AtomBondMapping> MappingCache;
424 
425  const MolecularGraph* query;
426  const MolecularGraph* target;
427  BitMatrix atomEquivMatrix;
428  AGraphNodeMatrix nodeMatrix;
429  ABMappingList foundMappings;
430  UniqueMappingList uniqueMappings;
431  AGraphEdgeList cliqueEdges;
432  AGraphNodeList cliqueNodes;
433  ABMappingMask mappingMask;
434  AtomMatchExprTable atomMatchExprTable;
435  BondMatchExprTable bondMatchExprTable;
436  MolGraphMatchExprPtr molGraphMatchExpr;
437  AtomList postMappingMatchAtoms;
438  BondList postMappingMatchBonds;
439  NodeCache nodeCache;
440  EdgeCache edgeCache;
441  MappingCache mappingCache;
442  bool queryChanged;
443  bool initQueryData;
444  bool uniqueMatches;
445  bool saveMappings;
446  std::size_t numQueryAtoms;
447  std::size_t numQueryBonds;
448  std::size_t numTargetAtoms;
449  std::size_t numTargetBonds;
450  std::size_t maxBondSubstructureSize;
451  std::size_t currNumNullNodes;
452  std::size_t minNumNullNodes;
453  std::size_t maxNumMappings;
454  std::size_t minSubstructureSize;
455  std::size_t currNodeIdx;
456  };
457  } // namespace Chem
458 } // namespace CDPL
459 
460 #endif // CDPL_CHEM_MAXCOMMONBONDSUBSTRUCTURESEARCH_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:57
Abstract base class representing a chemical bond between two Chem::Atom instances.
Definition: Bond.hpp:54
std::shared_ptr< MatchExpression > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated MatchExpression instances.
Definition: MatchExpression.hpp:81
Computes the maximum common bond substructure between a query and a target molecular graph by reducin...
Definition: MaxCommonBondSubstructureSearch.hpp:71
std::size_t getMinSubstructureSize() const
Returns the minimum accepted common substructure size.
AtomBondMapping & getMapping(std::size_t idx)
Returns a non-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.
ConstMappingIterator getMappingsEnd() const
Returns a constant iterator pointing to the end of the stored atom/bond mapping objects.
MaxCommonBondSubstructureSearch()
Constructs and initializes a MaxCommonBondSubstructureSearch instance.
bool uniqueMappingsOnly() const
Tells whether duplicate atom/bond mappings are discarded.
std::size_t getMaxNumMappings() const
Returns the specified limit on the number of stored atom/bond mappings.
MaxCommonBondSubstructureSearch(const MolecularGraph &query)
Constructs and initializes a MaxCommonBondSubstructureSearch instance for the specified query structu...
bool findMappings(const MolecularGraph &target)
Searches for all atom/bond mappings of query subgraphs to substructures of the specified target molec...
MappingIterator getMappingsBegin()
Returns a mutable iterator pointing to the beginning of the stored atom/bond mapping objects.
MappingIterator begin()
Returns a mutable iterator pointing to the beginning of the stored atom/bond mapping objects.
std::size_t getNumMappings() const
Returns the number of atom/bond mappings that were recorded in the last search for common substructur...
void setMinSubstructureSize(std::size_t min_size)
Allows to specify the minimum accepted common substructure size.
void uniqueMappingsOnly(bool unique)
Allows to specify whether or not to store only unique atom/bond mappings.
std::shared_ptr< MaxCommonBondSubstructureSearch > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated MaxCommonBondSubstructureSearch i...
Definition: MaxCommonBondSubstructureSearch.hpp:80
MaxCommonBondSubstructureSearch & operator=(const MaxCommonBondSubstructureSearch &)=delete
const AtomBondMapping & getMapping(std::size_t idx) const
Returns a const reference to the stored atom/bond mapping object at index idx.
ConstMappingIterator end() const
Returns a constant iterator pointing to the end of the stored atom/bond mapping objects.
ConstMappingIterator getMappingsBegin() const
Returns a constant iterator pointing to the beginning of the stored atom/bond mapping objects.
MaxCommonBondSubstructureSearch(const MaxCommonBondSubstructureSearch &)=delete
void setQuery(const MolecularGraph &query)
Allows to specify a new query structure.
bool mappingExists(const MolecularGraph &target)
Searches for a common substructure between the query and the specified target molecular graph.
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: MaxCommonBondSubstructureSearch.hpp:90
MappingIterator getMappingsEnd()
Returns a mutable iterator pointing to the end of the stored atom/bond mapping objects.
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.
boost::indirect_iterator< ABMappingList::iterator, AtomBondMapping > MappingIterator
A mutable random access iterator used to iterate over the stored atom/bond mapping objects.
Definition: MaxCommonBondSubstructureSearch.hpp:85
Abstract base class for representations of a chemical structure as a graph of bonded atoms.
Definition: MolecularGraph.hpp:57
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.