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 
77  {
78 
79  typedef std::vector<AtomBondMapping*> ABMappingList;
80 
81  public:
86  typedef std::shared_ptr<MaxCommonBondSubstructureSearch> SharedPointer;
87 
91  typedef boost::indirect_iterator<ABMappingList::iterator, AtomBondMapping> MappingIterator;
92 
96  typedef boost::indirect_iterator<ABMappingList::const_iterator, const AtomBondMapping> ConstMappingIterator;
97 
102 
108 
110 
117 
119 
124  void setQuery(const MolecularGraph& query);
125 
139  bool mappingExists(const MolecularGraph& target);
140 
156  bool findMappings(const MolecularGraph& target);
157 
163  std::size_t getNumMappings() const;
164 
171  AtomBondMapping& getMapping(std::size_t idx);
172 
179  const AtomBondMapping& getMapping(std::size_t idx) const;
180 
186 
192 
198 
204 
210 
216 
222 
228 
240  void uniqueMappingsOnly(bool unique);
241 
247  bool uniqueMappingsOnly() const;
248 
259  void setMaxNumMappings(std::size_t max_num_mappings);
260 
266  std::size_t getMaxNumMappings() const;
267 
277  void setMinSubstructureSize(std::size_t min_size);
278 
284  std::size_t getMinSubstructureSize() const;
285 
286  private:
287  class AGNode;
288 
289  bool init(const MolecularGraph&);
290 
291  void initMatchExpressions();
292 
293  bool findEquivAtoms();
294 
295  bool atomsCompatible(const Bond&, const Bond&) const;
296 
297  const Atom* getCommonAtom(const Bond&, const Bond&) const;
298 
299  bool buildAssocGraph();
300 
301  bool findAssocGraphCliques(std::size_t);
302  bool isLegal(const AGNode*);
303 
304  void undoAtomMapping(std::size_t);
305 
306  bool mappingFound();
307 
308  bool hasPostMappingMatchExprs() const;
309  bool foundMappingMatches(const AtomBondMapping*) const;
310 
311  bool foundMappingUnique(bool);
312 
313  void clearMappings();
314 
315  AtomBondMapping* createAtomBondMapping(bool);
316  void freeAtomBondMapping();
317 
318  void freeAssocGraph();
319  void freeAtomBondMappings();
320 
321  class AGEdge;
322 
323  AGNode* allocAGNode(const Bond*, const Bond*);
324  AGEdge* allocAGEdge(const Atom*, const Atom*);
325 
326  typedef std::vector<const AGEdge*> AGraphEdgeList;
327 
328  class AGNode
329  {
330 
331  public:
332  void setQueryBond(const Bond*);
333  const Bond* getQueryBond() const;
334 
335  void setAssocBond(const Bond*);
336  const Bond* getAssocBond() const;
337 
338  void addEdge(const AGEdge*);
339 
340  bool isConnected(const AGNode*) const;
341  const AGEdge* findEdge(const AGNode*) const;
342 
343  void clear();
344 
345  void setIndex(std::size_t idx);
346 
347  private:
348  std::size_t index;
349  const Bond* queryBond;
350  const Bond* assocBond;
351  Util::BitSet connNodes;
352  AGraphEdgeList atomEdges;
353  };
354 
355  class AGEdge
356  {
357 
358  public:
359  void setQueryAtom(const Atom*);
360  const Atom* getQueryAtom() const;
361 
362  void setAssocAtom(const Atom*);
363  const Atom* getAssocAtom() const;
364 
365  void setNode1(const AGNode*);
366  void setNode2(const AGNode*);
367 
368  const AGNode* getNode1() const;
369  const AGNode* getNode2() const;
370 
371  const AGNode* getOther(const AGNode*) const;
372 
373  private:
374  const Atom* queryAtom;
375  const Atom* assocAtom;
376  const AGNode* node1;
377  const AGNode* node2;
378  };
379 
380  class ABMappingMask
381  {
382 
383  public:
384  void initQueryAtomMask(std::size_t);
385  void initTargetAtomMask(std::size_t);
386 
387  void initQueryBondMask(std::size_t);
388  void initTargetBondMask(std::size_t);
389 
390  void setQueryAtomBit(std::size_t);
391  void setTargetAtomBit(std::size_t);
392 
393  void resetQueryAtomBit(std::size_t);
394  void resetTargetAtomBit(std::size_t);
395 
396  bool testQueryAtomBit(std::size_t) const;
397  bool testTargetAtomBit(std::size_t) const;
398 
399  void setQueryBondBit(std::size_t);
400  void setTargetBondBit(std::size_t);
401 
402  void resetQueryAtomBits();
403  void resetTargetAtomBits();
404 
405  void resetBondBits();
406 
407  bool operator<(const ABMappingMask&) const;
408  bool operator>(const ABMappingMask&) const;
409 
410  private:
411  Util::BitSet queryAtomMask;
412  Util::BitSet targetAtomMask;
413  Util::BitSet queryBondMask;
414  Util::BitSet targetBondMask;
415  };
416 
417  typedef MatchExpression<MolecularGraph>::SharedPointer MolGraphMatchExprPtr;
418 
419  typedef std::vector<Util::BitSet> BitMatrix;
420  typedef std::vector<AGNode*> AGraphNodeList;
421  typedef std::vector<AGraphNodeList> AGraphNodeMatrix;
422  typedef std::set<ABMappingMask> UniqueMappingList;
423  typedef std::vector<const Atom*> AtomList;
424  typedef std::vector<const Bond*> BondList;
425  typedef std::vector<MatchExpression<Atom, MolecularGraph>::SharedPointer> AtomMatchExprTable;
426  typedef std::vector<MatchExpression<Bond, MolecularGraph>::SharedPointer> BondMatchExprTable;
427  typedef Util::ObjectStack<AGNode> NodeCache;
428  typedef Util::ObjectStack<AGEdge> EdgeCache;
429  typedef Util::ObjectStack<AtomBondMapping> MappingCache;
430 
431  const MolecularGraph* query;
432  const MolecularGraph* target;
433  BitMatrix atomEquivMatrix;
434  AGraphNodeMatrix nodeMatrix;
435  ABMappingList foundMappings;
436  UniqueMappingList uniqueMappings;
437  AGraphEdgeList cliqueEdges;
438  AGraphNodeList cliqueNodes;
439  ABMappingMask mappingMask;
440  AtomMatchExprTable atomMatchExprTable;
441  BondMatchExprTable bondMatchExprTable;
442  MolGraphMatchExprPtr molGraphMatchExpr;
443  AtomList postMappingMatchAtoms;
444  BondList postMappingMatchBonds;
445  NodeCache nodeCache;
446  EdgeCache edgeCache;
447  MappingCache mappingCache;
448  bool queryChanged;
449  bool initQueryData;
450  bool uniqueMatches;
451  bool saveMappings;
452  std::size_t numQueryAtoms;
453  std::size_t numQueryBonds;
454  std::size_t numTargetAtoms;
455  std::size_t numTargetBonds;
456  std::size_t maxBondSubstructureSize;
457  std::size_t currNumNullNodes;
458  std::size_t minNumNullNodes;
459  std::size_t maxNumMappings;
460  std::size_t minSubstructureSize;
461  std::size_t currNodeIdx;
462  };
463  } // namespace Chem
464 } // namespace CDPL
465 
466 #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:58
Abstract base class representing a chemical bond between two atoms (represented by Chem::Atom instanc...
Definition: Bond.hpp:54
std::shared_ptr< MatchExpression > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated MatchExpression instances.
Definition: MatchExpression.hpp:81
Searches for the maximum common bond substructures between a query and a target molecular graph.
Definition: MaxCommonBondSubstructureSearch.hpp:77
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 const Chem::AtomBondMapping objects.
MaxCommonBondSubstructureSearch()
Constructs and initializes the 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 the MaxCommonBondSubstructureSearch instance for the query molecular graph...
bool findMappings(const MolecularGraph &target)
Searches for all atom/bond mappings of query subgraphs to substructures of the target molecular graph...
MappingIterator getMappingsBegin()
Returns a mutable iterator pointing to the beginning of the stored Chem::AtomBondMapping objects.
MappingIterator begin()
Returns a mutable iterator pointing to the beginning of the stored Chem::AtomBondMapping 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:86
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 const Chem::AtomBondMapping objects.
ConstMappingIterator getMappingsBegin() const
Returns a constant iterator pointing to the beginning of the stored const Chem::AtomBondMapping objec...
MaxCommonBondSubstructureSearch(const MaxCommonBondSubstructureSearch &)=delete
void setQuery(const MolecularGraph &query)
Sets query as the new query molecular graph.
bool mappingExists(const MolecularGraph &target)
Searches for a common substructure between the query and the target molecular graph target.
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: MaxCommonBondSubstructureSearch.hpp:96
MappingIterator getMappingsEnd()
Returns a mutable iterator pointing to the end of the stored Chem::AtomBondMapping objects.
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...
boost::indirect_iterator< ABMappingList::iterator, AtomBondMapping > MappingIterator
A mutable random access iterator used to iterate over the stored Chem::AtomBondMapping objects.
Definition: MaxCommonBondSubstructureSearch.hpp:91
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.