Chemical Data Processing Library C++ API - Version 1.4.0
AutomorphismGroupSearch.hpp
Go to the documentation of this file.
1 /*
2  * AutomorphismGroupSearch.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_AUTOMORPHISMGROUPSEARCH_HPP
30 #define CDPL_CHEM_AUTOMORPHISMGROUPSEARCH_HPP
31 
32 #include <memory>
33 #include <functional>
34 
35 #include "CDPL/Chem/APIPrefix.hpp"
40 
41 
42 namespace CDPL
43 {
44 
45  namespace Chem
46  {
47 
58  {
59  public:
63  static constexpr unsigned int DEF_ATOM_PROPERTY_FLAGS =
68 
72  static constexpr unsigned int DEF_BOND_PROPERTY_FLAGS =
75 
77  typedef std::shared_ptr<AutomorphismGroupSearch> SharedPointer;
78 
83 
88 
90  typedef std::function<bool(const MolecularGraph&, const AtomBondMapping&)> MappingCallbackFunction;
91 
97  AutomorphismGroupSearch(unsigned int atom_flags = DEF_ATOM_PROPERTY_FLAGS,
98  unsigned int bond_flags = DEF_BOND_PROPERTY_FLAGS);
99 
101 
103 
108  void setAtomPropertyFlags(unsigned int flags);
109 
114  unsigned int getAtomPropertyFlags() const;
115 
120  void setBondPropertyFlags(unsigned int flags);
121 
126  unsigned int getBondPropertyFlags() const;
127 
132  void includeIdentityMapping(bool include);
133 
139 
152  bool findMappings(const MolecularGraph& molgraph);
153 
160  void stopSearch();
161 
166  std::size_t getNumMappings() const;
167 
174  AtomBondMapping& getMapping(std::size_t idx);
175 
182  const AtomBondMapping& getMapping(std::size_t idx) const;
183 
189 
195 
201 
207 
213 
219 
225 
231 
242  void setMaxNumMappings(std::size_t max_num_mappings);
243 
249  std::size_t getMaxNumMappings() const;
250 
263  void addAtomMappingConstraint(std::size_t atom1_idx, std::size_t atom2_idx);
264 
270 
283  void addBondMappingConstraint(std::size_t bond1_idx, std::size_t bond2_idx);
284 
290 
299 
305 
306  private:
308  getAtomMatchExpression(const Atom& atom) const;
309 
311  getBondMatchExpression(const Bond& bond) const;
312 
314  getMolGraphMatchExpression(const MolecularGraph& molgraph) const;
315 
316  class AtomMatchExpression : public MatchExpression<Atom, MolecularGraph>
317  {
318 
319  public:
320  AtomMatchExpression(AutomorphismGroupSearch* parent):
321  parent(parent) {}
322 
323  bool requiresAtomBondMapping() const;
324 
325  bool operator()(const Atom& query_atom, const MolecularGraph& query_molgraph,
326  const Atom& target_atom, const MolecularGraph& target_molgraph,
327  const Base::Any& aux_data) const;
328 
329  bool operator()(const Atom& query_atom, const MolecularGraph& query_molgraph,
330  const Atom& target_atom, const MolecularGraph& target_molgraph,
331  const AtomBondMapping& mapping, const Base::Any& aux_data) const;
332 
333  private:
334  AutomorphismGroupSearch* parent;
335  mutable unsigned int type;
336  mutable unsigned int hybState;
337  mutable std::size_t isotope;
338  mutable std::size_t hCount;
339  mutable long charge;
340  mutable bool aromatic;
341  mutable std::size_t expBondCount;
342  };
343 
344  class BondMatchExpression : public MatchExpression<Bond, MolecularGraph>
345  {
346 
347  public:
348  BondMatchExpression(AutomorphismGroupSearch* parent):
349  parent(parent) {}
350 
351  bool requiresAtomBondMapping() const;
352 
353  bool operator()(const Bond& query_bond, const MolecularGraph& query_molgraph,
354  const Bond& target_bond, const MolecularGraph& target_molgraph,
355  const Base::Any& aux_data) const;
356 
357  bool operator()(const Bond& query_bond, const MolecularGraph& query_molgraph,
358  const Bond& target_bond, const MolecularGraph& target_molgraph,
359  const AtomBondMapping& mapping, const Base::Any& aux_data) const;
360 
361  private:
362  AutomorphismGroupSearch* parent;
363  mutable std::size_t order;
364  mutable bool inRing;
365  mutable bool aromatic;
366  };
367 
368  class MolGraphMatchExpression : public MatchExpression<MolecularGraph>
369  {
370 
371  public:
372  MolGraphMatchExpression(const AutomorphismGroupSearch* parent):
373  parent(parent) {}
374 
375  bool requiresAtomBondMapping() const;
376 
377  bool operator()(const MolecularGraph& query_molgraph,
378  const MolecularGraph& target_molgraph,
379  const Base::Any& aux_data) const;
380 
381  bool operator()(const MolecularGraph& query_molgraph,
382  const MolecularGraph& target_molgraph,
383  const AtomBondMapping& mapping, const Base::Any& aux_data) const;
384 
385  private:
386  const AutomorphismGroupSearch* parent;
387  };
388 
391  typedef MatchExpression<MolecularGraph>::SharedPointer MolGraphMatchExprPtr;
392 
393  SubstructureSearch substructSearch;
394  bool incIdentityMapping;
395  unsigned int atomPropFlags;
396  unsigned int bondPropFlags;
397  AtomMatchExprPtr atomMatchExpr;
398  BondMatchExprPtr bondMatchExpr;
399  MolGraphMatchExprPtr molGraphMatchExpr;
400  MappingCallbackFunction mappingCallbackFunc;
401  const Atom* lastQueryAtom;
402  const Bond* lastQueryBond;
403  };
404  } // namespace Chem
405 } // namespace CDPL
406 
407 #endif // CDPL_CHEM_AUTOMORPHISMGROUPSEARCH_HPP
Definition of constants in namespace CDPL::Chem::BondPropertyFlag.
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 constants in namespace CDPL::Chem::AtomPropertyFlag.
Definition of class CDPL::Chem::MatchExpression.
Definition of class CDPL::Chem::SubstructureSearch.
Safe, type checked container for arbitrary data of variable type.
Definition: Any.hpp:60
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
Enumerates the atom/bond automorphisms of a molecular graph.
Definition: AutomorphismGroupSearch.hpp:58
AutomorphismGroupSearch(const AutomorphismGroupSearch &)=delete
AutomorphismGroupSearch(unsigned int atom_flags=DEF_ATOM_PROPERTY_FLAGS, unsigned int bond_flags=DEF_BOND_PROPERTY_FLAGS)
Constructs and initializes a AutomorphismGroupSearch instance.
MappingIterator getMappingsEnd()
Returns a mutable iterator pointing to the end of the stored atom/bond mapping objects.
bool identityMappingIncluded() const
Tells whether the identity mapping is included in the search results.
AutomorphismGroupSearch & operator=(const AutomorphismGroupSearch &)=delete
SubstructureSearch::ConstMappingIterator ConstMappingIterator
A constant random access iterator used to iterate over the stored atom/bond mapping objects.
Definition: AutomorphismGroupSearch.hpp:87
void clearAtomMappingConstraints()
Clears all previously defined atom mapping constraints.
bool findMappings(const MolecularGraph &molgraph)
Searches for the possible atom/bond mappings in the automorphism group of the given molecular graph.
MappingIterator end()
Returns a mutable iterator pointing to the end of the stored atom/bond mapping objects.
void addAtomMappingConstraint(std::size_t atom1_idx, std::size_t atom2_idx)
Adds a constraint on the allowed atom mappings.
void includeIdentityMapping(bool include)
Specifies whether the identity mapping shall be included in the search results.
void setMaxNumMappings(std::size_t max_num_mappings)
Allows to specify a limit on the number of stored atom/bond mappings.
ConstMappingIterator getMappingsBegin() const
Returns a constant iterator pointing to the beginning of the stored atom/bond mapping objects.
const AtomBondMapping & getMapping(std::size_t idx) const
Returns a const reference to the stored atom/bond mapping object at index idx.
void stopSearch()
Aborts a currently running findMappings() call.
ConstMappingIterator getMappingsEnd() const
Returns a constant iterator pointing to the end of the stored atom/bond mapping objects.
std::shared_ptr< AutomorphismGroupSearch > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated AutomorphismGroupSearch instances...
Definition: AutomorphismGroupSearch.hpp:77
void setBondPropertyFlags(unsigned int flags)
Specifies the bond properties considered for bond matching during automorphism search.
unsigned int getAtomPropertyFlags() const
Returns the atomic properties currently considered for atom matching.
const MappingCallbackFunction & getFoundMappingCallback() const
Returns the currently installed found-mapping callback.
ConstMappingIterator end() const
Returns a constant iterator pointing to the end of the stored atom/bond mapping objects.
void addBondMappingConstraint(std::size_t bond1_idx, std::size_t bond2_idx)
Adds a constraint on the allowed bond mappings.
std::size_t getMaxNumMappings() const
Returns the specified limit on the number of stored atom/bond mappings.
ConstMappingIterator begin() const
Returns a constant iterator pointing to the beginning of the stored atom/bond mapping objects.
void setFoundMappingCallback(const MappingCallbackFunction &func)
Sets a callback that is invoked for every atom/bond mapping found during findMappings().
std::function< bool(const MolecularGraph &, const AtomBondMapping &)> MappingCallbackFunction
Type of the callback invoked for every found mapping (return false to abort the search).
Definition: AutomorphismGroupSearch.hpp:90
std::size_t getNumMappings() const
Returns the number of atom/bond mappings that were recorded in the last call to findMappings().
MappingIterator begin()
Returns a mutable iterator pointing to the beginning of the stored atom/bond mapping objects.
unsigned int getBondPropertyFlags() const
Returns the bond properties currently considered for bond matching.
SubstructureSearch::MappingIterator MappingIterator
A mutable random access iterator used to iterate over the stored atom/bond mapping objects.
Definition: AutomorphismGroupSearch.hpp:82
void setAtomPropertyFlags(unsigned int flags)
Specifies the atomic properties considered for atom matching during automorphism search.
AtomBondMapping & getMapping(std::size_t idx)
Returns a non-const reference to the stored atom/bond mapping object at index idx.
void clearBondMappingConstraints()
Clears all previously defined bond mapping constraints.
MappingIterator getMappingsBegin()
Returns a mutable iterator pointing to the beginning of the stored atom/bond mapping objects.
Abstract base class representing a chemical bond between two Chem::Atom instances.
Definition: Bond.hpp:54
Generic boolean expression interface for the implementation of query/target object equivalence tests ...
Definition: MatchExpression.hpp:75
std::shared_ptr< MatchExpression > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated MatchExpression instances.
Definition: MatchExpression.hpp:81
Abstract base class for representations of a chemical structure as a graph of bonded atoms.
Definition: MolecularGraph.hpp:57
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: SubstructureSearch.hpp:96
boost::indirect_iterator< ABMappingList::iterator, AtomBondMapping > MappingIterator
A mutable random access iterator used to iterate over the stored atom/bond mapping objects.
Definition: SubstructureSearch.hpp:91
constexpr unsigned int FORMAL_CHARGE
Specifies the formal charge of an atom.
Definition: Chem/AtomPropertyFlag.hpp:73
constexpr unsigned int H_COUNT
Specifies the hydrogen count of an atom.
Definition: Chem/AtomPropertyFlag.hpp:78
constexpr unsigned int AROMATICITY
Specifies the membership of an atom in aromatic rings.
Definition: Chem/AtomPropertyFlag.hpp:93
constexpr unsigned int ISOTOPE
Specifies the isotopic mass of an atom.
Definition: Chem/AtomPropertyFlag.hpp:68
constexpr unsigned int CONFIGURATION
Specifies the configuration of a stereogenic atom.
Definition: Chem/AtomPropertyFlag.hpp:98
constexpr unsigned int HYBRIDIZATION_STATE
Specifies the hybridization state an atom.
Definition: Chem/AtomPropertyFlag.hpp:123
constexpr unsigned int TYPE
Specifies the generic type or element of an atom.
Definition: Chem/AtomPropertyFlag.hpp:63
constexpr unsigned int EXPLICIT_BOND_COUNT
Specifies the explicit bond count of an atom.
Definition: Chem/AtomPropertyFlag.hpp:118
constexpr unsigned int AROMATICITY
Specifies the membership of a bond in aromatic rings.
Definition: BondPropertyFlag.hpp:73
constexpr unsigned int ORDER
Specifies the order of a bond.
Definition: BondPropertyFlag.hpp:63
constexpr unsigned int CONFIGURATION
Specifies the steric configuration of a double bond.
Definition: BondPropertyFlag.hpp:78
constexpr unsigned int TOPOLOGY
Specifies the ring/chain topology of a bond.
Definition: BondPropertyFlag.hpp:68
constexpr unsigned int Any
Specifies any atom.
Definition: SybylAtomType.hpp:198
The namespace of the Chemical Data Processing Library.