Chemical Data Processing Library C++ API - Version 1.4.0
MaxCommonAtomSubstructureSearch.hpp
Go to the documentation of this file.
1 /*
2  * MaxCommonAtomSubstructureSearch.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_MAXCOMMONATOMSUBSTRUCTURESEARCH_HPP
30 #define CDPL_CHEM_MAXCOMMONATOMSUBSTRUCTURESEARCH_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 
79  {
80 
81  typedef std::vector<AtomBondMapping*> ABMappingList;
82 
83  public:
88  typedef std::shared_ptr<MaxCommonAtomSubstructureSearch> SharedPointer;
89 
93  typedef boost::indirect_iterator<ABMappingList::iterator, AtomBondMapping> MappingIterator;
94 
98  typedef boost::indirect_iterator<ABMappingList::const_iterator, const AtomBondMapping> ConstMappingIterator;
99 
104 
110 
112 
119 
121 
126  void setQuery(const MolecularGraph& query);
127 
141  bool mappingExists(const MolecularGraph& target);
142 
158  bool findAllMappings(const MolecularGraph& target);
159 
175  bool findMaxBondMappings(const MolecularGraph& target);
176 
182  std::size_t getNumMappings() const;
183 
190  AtomBondMapping& getMapping(std::size_t idx);
191 
198  const AtomBondMapping& getMapping(std::size_t idx) const;
199 
205 
211 
217 
223 
229 
235 
241 
247 
259  void uniqueMappingsOnly(bool unique);
260 
266  bool uniqueMappingsOnly() const;
267 
278  void setMaxNumMappings(std::size_t max_num_mappings);
279 
285  std::size_t getMaxNumMappings() const;
286 
296  void setMinSubstructureSize(std::size_t min_size);
297 
303  std::size_t getMinSubstructureSize() const;
304 
305  private:
306  class AGNode;
307 
308  bool init(const MolecularGraph&);
309 
310  void initMatchExpressions();
311 
312  bool buildAssocGraph();
313 
314  bool findAssocGraphCliques(std::size_t);
315  bool isLegal(const AGNode*);
316 
317  bool mappingFound();
318 
319  bool hasPostMappingMatchExprs() const;
320  bool foundMappingMatches(const AtomBondMapping*) const;
321 
322  bool foundMappingUnique();
323 
324  void clearMappings();
325 
326  void freeAtomBondMapping();
327  void freeAtomBondMappings();
328  void freeAssocGraph();
329 
330  AtomBondMapping* createAtomBondMapping();
331 
332  class AGEdge;
333 
334  AGNode* allocAGNode(const Atom*, const Atom*);
335  AGEdge* allocAGEdge(const Bond*, const Bond*);
336 
337  typedef std::vector<const AGEdge*> AGraphEdgeList;
338 
339  class AGNode
340  {
341 
342  public:
343  void setQueryAtom(const Atom*);
344  const Atom* getQueryAtom() const;
345 
346  void setAssocAtom(const Atom*);
347  const Atom* getAssocAtom() const;
348 
349  void addEdge(const AGEdge*);
350 
351  bool isConnected(const AGNode*) const;
352  const AGEdge* findEdge(const AGNode*) const;
353 
354  void clear();
355 
356  void setIndex(std::size_t idx);
357 
358  private:
359  std::size_t index;
360  const Atom* queryAtom;
361  const Atom* assocAtom;
362  Util::BitSet connNodes;
363  AGraphEdgeList bondEdges;
364  };
365 
366  class AGEdge
367  {
368 
369  public:
370  void setQueryBond(const Bond*);
371  const Bond* getQueryBond() const;
372 
373  void setAssocBond(const Bond*);
374  const Bond* getAssocBond() const;
375 
376  void setNode1(const AGNode*);
377  void setNode2(const AGNode*);
378 
379  const AGNode* getNode1() const;
380  const AGNode* getNode2() const;
381 
382  const AGNode* getOther(const AGNode*) const;
383 
384  private:
385  const Bond* queryBond;
386  const Bond* assocBond;
387  const AGNode* node1;
388  const AGNode* node2;
389  };
390 
391  class ABMappingMask
392  {
393 
394  public:
395  void initQueryAtomMask(std::size_t);
396  void initTargetAtomMask(std::size_t);
397 
398  void initQueryBondMask(std::size_t);
399  void initTargetBondMask(std::size_t);
400 
401  void setQueryAtomBit(std::size_t);
402  void setTargetAtomBit(std::size_t);
403 
404  void setQueryBondBit(std::size_t);
405  void setTargetBondBit(std::size_t);
406 
407  void reset();
408 
409  bool operator<(const ABMappingMask&) const;
410  bool operator>(const ABMappingMask&) const;
411 
412  private:
413  Util::BitSet queryAtomMask;
414  Util::BitSet targetAtomMask;
415  Util::BitSet queryBondMask;
416  Util::BitSet targetBondMask;
417  };
418 
419  typedef MatchExpression<MolecularGraph>::SharedPointer MolGraphMatchExprPtr;
420 
421  typedef std::vector<AGNode*> AGraphNodeList;
422  typedef std::vector<AGraphNodeList> AGraphNodeMatrix;
423  typedef std::set<ABMappingMask> UniqueMappingList;
424  typedef std::vector<const Atom*> AtomList;
425  typedef std::vector<const Bond*> BondList;
426  typedef std::vector<MatchExpression<Atom, MolecularGraph>::SharedPointer> AtomMatchExprTable;
427  typedef std::vector<MatchExpression<Bond, MolecularGraph>::SharedPointer> BondMatchExprTable;
428  typedef Util::ObjectStack<AGNode> NodeCache;
429  typedef Util::ObjectStack<AGEdge> EdgeCache;
430  typedef Util::ObjectStack<AtomBondMapping> MappingCache;
431 
432  const MolecularGraph* query;
433  const MolecularGraph* target;
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  bool maxBondMappingsOnly;
453  std::size_t numQueryAtoms;
454  std::size_t numQueryBonds;
455  std::size_t numTargetAtoms;
456  std::size_t numTargetBonds;
457  std::size_t maxAtomSubstructureSize;
458  std::size_t maxBondSubstructureSize;
459  std::size_t currNumNullNodes;
460  std::size_t minNumNullNodes;
461  std::size_t maxNumMappings;
462  std::size_t minSubstructureSize;
463  std::size_t currNodeIdx;
464  };
465  } // namespace Chem
466 } // namespace CDPL
467 
468 #endif // CDPL_CHEM_MAXCOMMONATOMSUBSTRUCTURESEARCH_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 atom substructures between a query and a target molecular graph.
Definition: MaxCommonAtomSubstructureSearch.hpp:79
MaxCommonAtomSubstructureSearch(const MaxCommonAtomSubstructureSearch &)=delete
AtomBondMapping & getMapping(std::size_t idx)
Returns a non-const reference to the stored atom/bond mapping object at index idx.
MaxCommonAtomSubstructureSearch(const MolecularGraph &query)
Constructs and initializes the MaxCommonAtomSubstructureSearch instance for the query molecular graph...
bool mappingExists(const MolecularGraph &target)
Searches for a common substructure between the query and the target molecular graph target.
MappingIterator getMappingsEnd()
Returns a mutable iterator pointing to the end of the stored Chem::AtomBondMapping objects.
MappingIterator begin()
Returns a mutable iterator pointing to the beginning of the stored Chem::AtomBondMapping objects.
bool findAllMappings(const MolecularGraph &target)
Searches for all atom/bond mappings of query subgraphs to substructures of the target molecular graph...
MappingIterator end()
Returns a mutable iterator pointing to the end of the stored Chem::AtomBondMapping objects.
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::iterator, AtomBondMapping > MappingIterator
A mutable random access iterator used to iterate over the stored Chem::AtomBondMapping objects.
Definition: MaxCommonAtomSubstructureSearch.hpp:93
std::size_t getMaxNumMappings() const
Returns the specified limit on the number of stored atom/bond mappings.
MaxCommonAtomSubstructureSearch()
Constructs and initializes the MaxCommonAtomSubstructureSearch instance.
bool uniqueMappingsOnly() const
Tells whether duplicate atom/bond mappings are discarded.
void setMinSubstructureSize(std::size_t min_size)
Allows to specify the minimum accepted common substructure size.
ConstMappingIterator getMappingsEnd() const
Returns a constant iterator pointing to the end of the stored const Chem::AtomBondMapping objects.
ConstMappingIterator begin() const
Returns a constant iterator pointing to the beginning of the stored const Chem::AtomBondMapping objec...
std::size_t getNumMappings() const
Returns the number of atom/bond mappings that were recorded in the last search for common substructur...
bool findMaxBondMappings(const MolecularGraph &target)
Searches for all atom/bond mappings of query subgraphs to substructures of the target molecular graph...
void setQuery(const MolecularGraph &query)
Sets query as the new query molecular graph.
ConstMappingIterator getMappingsBegin() const
Returns a constant iterator pointing to the beginning of the stored const Chem::AtomBondMapping objec...
ConstMappingIterator end() const
Returns a constant iterator pointing to the end of the stored const Chem::AtomBondMapping objects.
MaxCommonAtomSubstructureSearch & operator=(const MaxCommonAtomSubstructureSearch &)=delete
MappingIterator getMappingsBegin()
Returns a mutable iterator pointing to the beginning of the stored Chem::AtomBondMapping objects.
std::size_t getMinSubstructureSize() const
Returns the minimum accepted common substructure size.
void uniqueMappingsOnly(bool unique)
Allows to specify whether or not to store only unique atom/bond mappings.
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: MaxCommonAtomSubstructureSearch.hpp:98
std::shared_ptr< MaxCommonAtomSubstructureSearch > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated MaxCommonAtomSubstructureSearch i...
Definition: MaxCommonAtomSubstructureSearch.hpp:88
void setMaxNumMappings(std::size_t max_num_mappings)
Allows to specify a limit on the number of stored atom/bond mappings.
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.