Chemical Data Processing Library C++ API - Version 1.4.0
CanonicalNumberingCalculator.hpp
Go to the documentation of this file.
1 /*
2  * CanonicalNumberingCalculator.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_CANONICALNUMBERINGCALCULATOR_HPP
30 #define CDPL_CHEM_CANONICALNUMBERINGCALCULATOR_HPP
31 
32 #include <cstddef>
33 #include <cstdint>
34 #include <vector>
35 #include <utility>
36 #include <functional>
37 
38 #include "CDPL/Chem/APIPrefix.hpp"
42 #include "CDPL/Util/Array.hpp"
43 #include "CDPL/Util/BitSet.hpp"
45 
46 
47 namespace CDPL
48 {
49 
50  namespace Chem
51  {
52 
53  class MolecularGraph;
54  class Fragment;
55  class Atom;
56  class Bond;
57 
68  {
69 
70  public:
75  static constexpr unsigned int DEF_ATOM_PROPERTY_FLAGS =
78 
83  static constexpr unsigned int DEF_BOND_PROPERTY_FLAGS =
85 
90  typedef std::function<std::size_t(const Atom&, const MolecularGraph&)> HydrogenCountFunction;
91 
96 
108 
110 
112 
129  void setAtomPropertyFlags(unsigned int flags);
130 
137  unsigned int getAtomPropertyFlags() const;
138 
152  void setBondPropertyFlags(unsigned int flags);
153 
160  unsigned int getBondPropertyFlags() const;
161 
167 
173 
183  void calculate(const MolecularGraph& molgraph, Util::STArray& numbering);
184 
185  private:
186  class AtomNode;
187  class Edge;
188 
190  typedef std::vector<Edge*> EdgeList;
191 
192  typedef std::vector<std::uint64_t> ConnectionTable;
193 
194  void init(const MolecularGraph&, Util::STArray&);
195  void setup(const MolecularGraph&);
196 
198  void canonicalize(std::size_t);
199 
200  void processNewSolution();
201 
202  int testNewSolution();
203 
204  void buildConnectionTable(ConnectionTable& ctab) const;
205  void appendAtomConfigs(ConnectionTable& ctab);
206  void appendBondConfigs(ConnectionTable& ctab);
207 
208  void establishCanonNumbering(Util::STArray&);
209 
210  void saveState();
211  void restoreState();
212 
213  AtomNode* allocNode(Calculator* calculator, const Atom* atom, std::uint64_t label, std::size_t id);
214 
215  Edge* allocEdge(const Calculator* calculator, const Bond* bond, std::uint64_t label,
216  AtomNode* nbr_node, std::size_t id);
217 
218  class AtomNode
219  {
220 
221  public:
222  typedef EdgeList::const_iterator EdgeIterator;
223 
224  AtomNode();
225 
226  void init(Calculator* calculator, const Atom* atom, std::uint64_t label, std::size_t id);
227 
228  const Atom* getAtom() const;
229 
230  void addEdge(Edge* edge);
231 
232  std::uint64_t getLabel() const;
233 
234  void setLabel(std::uint64_t label);
235  void setNewLabel(std::size_t label);
236 
237  void updateLabel();
238 
239  std::size_t getID() const;
240 
241  void sortEdges();
242 
243  std::size_t getNumEdges() const;
244 
245  EdgeIterator getEdgesBegin() const;
246  EdgeIterator getEdgesEnd() const;
247 
248  void appendConnectivityData(ConnectionTable& ctab) const;
249  void appendBondConfigData(ConnectionTable& ctab) const;
250  void appendAtomConfigData(ConnectionTable& ctab);
251 
252  bool involvedInStereocenter();
253 
254  bool isEquivalent(const AtomNode* node) const;
255  bool isNonEquivalent(const AtomNode* node) const;
256 
257  void addToEquivalenceSet(const AtomNode* node);
258  void addToNonEquivalenceSet(const AtomNode* node);
259 
260  static bool terminalAndOnCommonNonStereoNode(const AtomNode* node1, const AtomNode* node2);
261 
262  struct LessCmpFunc
263  {
264 
265  bool operator()(const AtomNode*, const AtomNode*) const;
266  };
267 
268  private:
269  bool initConfigurationData();
270 
271  Calculator* calculator;
272  const Atom* atom;
273  std::uint64_t initialLabel;
274  std::uint64_t label;
275  std::size_t newLabel;
276  std::size_t id;
277  Util::BitSet equivNodeMask;
278  EdgeList edges;
279  StereoDescriptor stereoDescr;
280  bool hasConfiguration;
281  bool configDataValid;
282  bool partOfStereocenter;
283  bool partOfStereocenterValid;
284  };
285 
286  class Edge
287  {
288 
289  public:
290  Edge();
291 
292  void init(const Calculator* calculator, const Bond* bond, std::uint64_t label,
293  AtomNode* nbr_node, std::size_t id);
294 
295  void appendBondData(ConnectionTable&) const;
296  void appendConfigurationData(const AtomNode* node, ConnectionTable& ctab);
297 
298  AtomNode* getNeighborNode() const;
299 
300  bool representsStereoBond(const AtomNode* node);
301 
302  std::size_t getID() const;
303 
304  struct LessCmpFunc
305  {
306 
307  bool operator()(const Edge*, const Edge*) const;
308  };
309 
310  private:
311  bool initConfigurationData(const AtomNode* node);
312 
313  const Calculator* calculator;
314  const Bond* bond;
315  AtomNode* nbrNode;
316  std::uint64_t label;
317  std::size_t id;
318  StereoDescriptor stereoDescr;
319  bool hasConfiguration;
320  bool configDataValid;
321  };
322 
323  typedef std::pair<const Fragment*, const ConnectionTable*> CanonComponentInfo;
324 
325  struct ComponentCmpFunc
326  {
327 
328  bool operator()(const CanonComponentInfo&, const CanonComponentInfo&) const;
329  };
330 
331  typedef std::pair<AtomNode*, std::uint64_t> NodeLabelingState;
332  typedef std::vector<NodeLabelingState> NodeLabelingStack;
333  typedef std::vector<AtomNode*> NodeList;
334  typedef std::vector<ConnectionTable> ConnectionTableList;
335  typedef std::vector<CanonComponentInfo> CanonComponentList;
336  typedef Util::ObjectStack<AtomNode> NodeCache;
337  typedef Util::ObjectStack<Edge> EdgeCache;
338 
339  NodeCache nodeCache;
340  EdgeCache edgeCache;
341  unsigned int atomPropertyFlags;
342  unsigned int bondPropertyFlags;
343  HydrogenCountFunction hCountFunc;
344  bool foundStereogenicAtoms;
345  bool foundStereogenicBonds;
346  const MolecularGraph* molGraph;
347  NodeList allocNodes;
348  EdgeList allocEdges;
349  NodeList nodeList;
350  NodeList equivNodeStack;
351  NodeLabelingStack nodeLabelingStack;
352  ConnectionTableList compConnectionTables;
353  ConnectionTableList levelConnectionTables;
354  ConnectionTable testConnectionTable;
355  NodeList minNodeList;
356  CanonComponentList canonComponentList;
357  Util::BitSet visitedEdgeMask;
358  };
359  } // namespace Chem
360 } // namespace CDPL
361 
362 #endif // CDPL_CHEM_CANONICALNUMBERINGCALCULATOR_HPP
Definition of class CDPL::Util::Array.
Declaration of type CDPL::Util::BitSet.
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::Util::ObjectStack.
Definition of the type CDPL::Chem::StereoDescriptor.
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
Computes canonical atom numberings for molecular graphs using McKay's algorithm.
Definition: CanonicalNumberingCalculator.hpp:68
void setHydrogenCountFunction(const HydrogenCountFunction &func)
Specifies a function for the retrieval of the hydrogen count of an atom.
void setAtomPropertyFlags(unsigned int flags)
Allows to specify the set of atomic properties that has to be considered by the canonical numering al...
const HydrogenCountFunction & getHydrogenCountFunction()
Returns the function used for the retrieval of the hydrogen count of an atom.
unsigned int getBondPropertyFlags() const
Returns the set of bond properties that gets considered by the canonical numbering algorithm.
unsigned int getAtomPropertyFlags() const
Returns the set of atomic properties that gets considered by the canonical numbering algorithm.
std::function< std::size_t(const Atom &, const MolecularGraph &)> HydrogenCountFunction
Type of the generic functor class used to store user-defined functions or function objects for the re...
Definition: CanonicalNumberingCalculator.hpp:90
CanonicalNumberingCalculator & operator=(const CanonicalNumberingCalculator &)=delete
CanonicalNumberingCalculator()
Constructs the CanonicalNumberingCalculator instance.
CanonicalNumberingCalculator(const CanonicalNumberingCalculator &)=delete
CanonicalNumberingCalculator(const MolecularGraph &molgraph, Util::STArray &numbering)
Constructs the CanonicalNumberingCalculator instance and performs a canonical numbering of the atoms ...
void calculate(const MolecularGraph &molgraph, Util::STArray &numbering)
Performs a canonical numbering of the atoms in the molecular graph molgraph.
void setBondPropertyFlags(unsigned int flags)
Allows to specify the set of bond properties that has to be considered by the canonical numering algo...
Abstract base class for data structures that represent chemical structures as molecular graphs.
Definition: MolecularGraph.hpp:60
Data structure for the storage and retrieval of stereochemical information about atoms and bonds.
Definition: StereoDescriptor.hpp:102
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 TYPE
Specifies the generic type or element of an atom.
Definition: Chem/AtomPropertyFlag.hpp:63
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
CDPL_CHEM_API void canonicalize(MolecularGraph &molgraph, const AtomCompareFunction &func, bool atoms=true, bool atom_nbrs=true, bool bonds=true, bool bond_atoms=false)
Reorders the atoms (and optionally their neighbors and bonds) of the molecular graph molgraph accordi...
Array< std::size_t > STArray
Array storing unsigned integers of type std::size_t.
Definition: Array.hpp:578
boost::dynamic_bitset BitSet
Dynamic bitset class.
Definition: BitSet.hpp:46
The namespace of the Chemical Data Processing Library.
Definition: CanonicalNumberingCalculator.hpp:263
bool operator()(const AtomNode *, const AtomNode *) const
Definition: CanonicalNumberingCalculator.hpp:305
bool operator()(const Edge *, const Edge *) const