Chemical Data Processing Library C++ API - Version 1.4.0
SubstructureEditor.hpp
Go to the documentation of this file.
1 /*
2  * SubstructureEditor.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_SUBSTRUCTUREEDITOR_HPP
30 #define CDPL_CHEM_SUBSTRUCTUREEDITOR_HPP
31 
32 #include <cstddef>
33 #include <vector>
34 #include <memory>
35 #include <unordered_set>
36 
37 #include <boost/iterator/transform_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 Molecule;
53  class StereoDescriptor;
54 
67  {
68 
69  struct Pattern;
70 
71  typedef std::vector<Pattern> PatternList;
72  typedef const MolecularGraph::SharedPointer& (*GetMolGraphFunc)(const Pattern&);
73 
74  public:
78  typedef std::shared_ptr<SubstructureEditor> SharedPointer;
79 
83  typedef boost::transform_iterator<GetMolGraphFunc, PatternList::const_iterator> ConstPatternIterator;
84 
88  typedef boost::transform_iterator<GetMolGraphFunc, PatternList::iterator> PatternIterator;
89 
94 
100 
105 
111 
116  std::size_t getNumSearchPatterns() const;
117 
124  const MolecularGraph::SharedPointer& getSearchPattern(std::size_t idx) const;
125 
131  void removeSearchPattern(std::size_t idx);
132 
139 
144 
150 
156 
162 
168 
174 
179  std::size_t getNumExcludePatterns() const;
180 
187  const MolecularGraph::SharedPointer& getExcludePattern(std::size_t idx) const;
188 
194  void removeExcludePattern(std::size_t idx);
195 
202 
207 
213 
219 
225 
231 
237 
243 
247  void clear();
248 
255  std::size_t edit(Molecule& mol);
256 
263  std::size_t edit(const MolecularGraph& molgraph, Molecule& res_mol);
264 
271 
272  private:
273  struct Pattern
274  {
275 
276  Pattern(const MolecularGraph::SharedPointer& molgraph):
277  molGraph(molgraph), subSearch(new Chem::SubstructureSearch(*molgraph))
278  {}
279 
282  };
283 
284  bool edit(Molecule& mol, const AtomMapping& mapping);
285 
286  bool editAtomStereoDescriptors(Molecule& mol) const;
287  bool editBondStereoDescriptors(Molecule& mol) const;
288 
289  void getExcludeMatches(const MolecularGraph& molgraph);
290 
291  bool createMatchedAtomAndBondMask(const MolecularGraph& molgraph, const AtomBondMapping& mapping,
292  Util::BitSet& mask, bool lbld_only, bool init) const;
293 
294  Chem::Atom* getMappedAtomForID(const AtomMapping& mapping, std::size_t id) const;
295  const Chem::Atom* getNeighbor(const Atom* ctr_atom, const Atom* excl_nbr) const;
296 
297  template <typename T>
298  void copyProperty(const T& src_cntnr, T& tgt_cntnr, const Base::LookupKey& key) const;
299 
300  template <typename VT, typename T>
301  bool copyPropertyWithChangeCheck(const T& src_cntnr, T& tgt_cntnr, const Base::LookupKey& key) const;
302 
303  bool setStereoDescriptorWithChangeCheck(Atom& atom, const StereoDescriptor& descr) const;
304  bool setStereoDescriptorWithChangeCheck(Bond& bond, const StereoDescriptor& descr) const;
305 
306  static const MolecularGraph::SharedPointer& getMolGraph(const Pattern& ptn);
307 
308  typedef Util::ObjectStack<Util::BitSet> BitSetCache;
309  typedef std::vector<Util::BitSet*> BitSetList;
310  typedef std::vector<Atom*> AtomArray;
311  typedef std::unordered_set<Atom*> AtomSet;
312  typedef std::unordered_set<Bond*> BondSet;
313 
314  PatternList searchPatterns;
315  PatternList excludePatterns;
316  MolecularGraph::SharedPointer resultPattern;
317  BitSetList excludeMatches;
318  AtomArray resPtnAtomMapping;
319  AtomSet hybStateUpdateAtoms;
320  AtomSet configUpdateAtoms;
321  BondSet configUpdateBonds;
322  BitSetCache bitSetCache;
323  };
324  } // namespace Chem
325 } // namespace CDPL
326 
327 #endif // CDPL_CHEM_SUBSTRUCTUREEDITOR_HPP
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::MolecularGraph.
Definition of class CDPL::Util::ObjectStack.
Definition of class CDPL::Chem::SubstructureSearch.
Unique lookup key for control-parameter and property values.
Definition: LookupKey.hpp:54
Abstract base class representing a chemical atom and its bonded neighborhood.
Definition: Atom.hpp:57
Abstract base class for representations of a chemical structure as a graph of bonded atoms.
Definition: MolecularGraph.hpp:57
std::shared_ptr< MolecularGraph > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated MolecularGraph instances.
Definition: MolecularGraph.hpp:63
Abstract base class representing a mutable molecular graph that owns its atoms and bonds.
Definition: Molecule.hpp:53
Pattern-driven editor that rewrites matched substructures of a Chem::Molecule using a result template...
Definition: SubstructureEditor.hpp:67
PatternIterator getExcludePatternsEnd()
Returns a mutable iterator pointing to the end of the stored exclude patterns.
ConstPatternIterator getExcludePatternsBegin() const
Returns a constant iterator pointing to the beginning of the stored exclude patterns.
ConstPatternIterator getExcludePatternsEnd() const
Returns a constant iterator pointing to the end of the stored exclude patterns.
const MolecularGraph::SharedPointer & getSearchPattern(std::size_t idx) const
Returns the molecular graph of the search pattern at index idx.
const MolecularGraph::SharedPointer & getResultPattern() const
Returns the currently set result pattern.
void addExcludePattern(const MolecularGraph::SharedPointer &molgraph)
Appends a new substructure exclude pattern to the current set of patterns.
boost::transform_iterator< GetMolGraphFunc, PatternList::iterator > PatternIterator
A mutable iterator over the molecular graphs of the stored search/exclude patterns.
Definition: SubstructureEditor.hpp:88
void removeExcludePattern(const PatternIterator &it)
Removes the exclude pattern pointed to by it.
std::size_t edit(const MolecularGraph &molgraph, Molecule &res_mol)
Copies molgraph into res_mol and then edits res_mol via edit(Molecule&).
void removeSearchPattern(std::size_t idx)
Removes the search pattern at index idx.
void setResultPattern(const MolecularGraph::SharedPointer &pattern)
Sets the result pattern that defines how each matched search-pattern instance will be rewritten.
PatternIterator getSearchPatternsBegin()
Returns a mutable iterator pointing to the beginning of the stored search patterns.
PatternIterator getSearchPatternsEnd()
Returns a mutable iterator pointing to the end of the stored search patterns.
PatternIterator getExcludePatternsBegin()
Returns a mutable iterator pointing to the beginning of the stored exclude patterns.
SubstructureEditor()
Constructs the SubstructureEditor instance.
SubstructureEditor(const SubstructureEditor &editor)
Constructs a copy of the SubstructureEditor instance editor.
ConstPatternIterator getSearchPatternsEnd() const
Returns a constant iterator pointing to the end of the stored search patterns.
void clear()
Clears the current editing result pattern and the sets of substructure search and exclude patterns.
std::shared_ptr< SubstructureEditor > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated SubstructureEditor instances.
Definition: SubstructureEditor.hpp:78
ConstPatternIterator getSearchPatternsBegin() const
Returns a constant iterator pointing to the beginning of the stored search patterns.
void addSearchPattern(const MolecularGraph::SharedPointer &molgraph)
Appends a new substructure search pattern to the current set of patterns.
boost::transform_iterator< GetMolGraphFunc, PatternList::const_iterator > ConstPatternIterator
A constant iterator over the molecular graphs of the stored search/exclude patterns.
Definition: SubstructureEditor.hpp:83
SubstructureEditor & operator=(const SubstructureEditor &gen)
Copies the state of the SubstructureEditor instance gen.
std::size_t getNumSearchPatterns() const
Returns the number of stored substructure search patterns.
const MolecularGraph::SharedPointer & getExcludePattern(std::size_t idx) const
Returns the molecular graph of the exclude pattern at index idx.
void clearSearchPatterns()
Clears the current set of substructuresearch patterns.
void clearExcludePatterns()
Clears the current set of substructure exclude patterns.
std::size_t getNumExcludePatterns() const
Returns the number of stored substructure-exclude patterns.
void removeExcludePattern(std::size_t idx)
Removes the exclude pattern at index idx.
void removeSearchPattern(const PatternIterator &it)
Removes the search pattern pointed to by it.
std::size_t edit(Molecule &mol)
Edits mol in place by applying the result pattern at every search-pattern match that is not covered b...
Subgraph-isomorphism search of a query molecular graph against a target molecular graph,...
Definition: SubstructureSearch.hpp:74
std::shared_ptr< SubstructureSearch > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated SubstructureSearch instances.
Definition: SubstructureSearch.hpp:86
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
boost::dynamic_bitset BitSet
Dynamic bitset class.
Definition: BitSet.hpp:46
The namespace of the Chemical Data Processing Library.