Chemical Data Processing Library C++ API - Version 1.4.0
ResonanceStructureGenerator.hpp
Go to the documentation of this file.
1 /*
2  * ResonanceStructureGenerator.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_RESONANCESTRUCTUREGENERATOR_HPP
30 #define CDPL_CHEM_RESONANCESTRUCTUREGENERATOR_HPP
31 
32 #include <vector>
33 #include <cstddef>
34 #include <unordered_set>
35 #include <memory>
36 
37 #include <boost/iterator/indirect_iterator.hpp>
38 
39 #include "CDPL/Chem/APIPrefix.hpp"
40 #include "CDPL/Util/Array.hpp"
41 #include "CDPL/Util/BitSet.hpp"
42 #include "CDPL/Util/ObjectPool.hpp"
43 
44 
45 namespace CDPL
46 {
47 
48  namespace Chem
49  {
50 
51  class MolecularGraph;
52  class Atom;
53 
63  {
64 
65  public:
66  class StructureData;
67 
68  private:
70  typedef StructureDataCache::SharedObjectPointer StructureDataPtr;
71  typedef std::vector<StructureDataPtr> StructureDataList;
72 
73  public:
75  typedef std::shared_ptr<ResonanceStructureGenerator> SharedPointer;
77  typedef boost::indirect_iterator<StructureDataList::const_iterator, const StructureData> ConstStructureDataIterator;
78 
83  {
84 
86 
87  public:
92  const Util::LArray& getAtomCharges() const;
93 
98  const Util::STArray& getBondOrders() const;
99 
100  private:
101  Util::LArray atomCharges;
102  Util::STArray bondOrders;
103  std::size_t numCharges;
104  };
105 
110 
116 
121 
128 
134 
140 
145  void minimizeOctetRuleViolations(bool minimize);
146 
152 
157  void minimizeSP1GeometryViolations(bool minimize);
158 
164 
169  void minimizeCarbonBond12Charges(bool minimize);
170 
176 
181  void setChargeCountWindow(std::size_t win_size);
182 
187  std::size_t getChargeCountWindow() const;
188 
193  void setMaxNumGeneratedStructures(std::size_t max_num);
194 
199  std::size_t getMaxNumGeneratedStructures() const;
200 
205  void generate(const MolecularGraph& molgraph);
206 
211  std::size_t getNumStructures() const;
212 
219  const StructureData& getStructureData(std::size_t idx) const;
220 
226 
232 
238 
244 
245  private:
246  struct BondData
247  {
248 
249  std::size_t atom1Index;
250  std::size_t atom2Index;
251  std::size_t bondIndex;
252  };
253 
254  class AtomData
255  {
256 
257  public:
258  long init(const Atom& atom, const MolecularGraph& molgraph, std::size_t idx);
259 
260  bool canShiftElectrons() const;
261 
262  std::size_t getNumBonds() const;
263 
264  std::size_t getBondIndex(std::size_t list_idx) const;
265 
266  std::size_t getAtomIndex(std::size_t list_idx) const;
267 
268  std::size_t getIndex() const;
269 
270  unsigned int getType() const;
271 
272  double getElectronegativity() const;
273 
274  bool isSP1Hybridized(const StructureData& res_struct) const;
275 
276  bool checkValenceState(const StructureData& res_struct, long val_diff, long charge_diff) const;
277 
278  bool octetRuleFulfilled(const StructureData& res_struct) const;
279 
280  std::size_t countRepChargePairs(const Util::LArray& charges) const;
281 
282  bool getVisitedFlag() const;
283 
284  void setVisitedFlag();
285 
286  bool getInSmallRingFlag() const;
287 
288  void setInSmallRingFlag();
289 
290  private:
291  typedef std::vector<std::size_t> IndexArray;
292 
293  bool canShiftElecs;
294  std::size_t index;
295  unsigned int type;
296  long valElecCount;
297  std::size_t unprdElecCount;
298  std::size_t implHCount;
299  double enegativity;
300  IndexArray bondIndices;
301  IndexArray atomIndices;
302  bool inSmallRing;
303  bool visited;
304  };
305 
306  struct StructureDataPtrHashFunc
307  {
308 
309  std::size_t operator()(const StructureDataPtr& rs_ptr) const;
310  };
311 
312  struct StructureDataPtrCmpFunc
313  {
314 
315  bool operator()(const StructureDataPtr& rs_ptr1, const StructureDataPtr& rs_ptr2) const
316  {
317  return (rs_ptr1->getBondOrders() == rs_ptr2->getBondOrders() &&
318  rs_ptr1->getAtomCharges() == rs_ptr2->getAtomCharges());
319  }
320  };
321 
322  typedef std::vector<AtomData> AtomDataArray;
323  typedef std::vector<const AtomData*> AtomDataPtrArray;
324  typedef std::vector<BondData> BondDataList;
325  typedef std::unordered_set<StructureDataPtr, StructureDataPtrHashFunc, StructureDataPtrCmpFunc> StructureDataSet;
326 
327  void init(const MolecularGraph& molgraph);
328 
329  void createInputResStructData();
330 
331  void extractResBonds();
332  void extractResBonds(AtomData& atom_data);
333 
334  void genStartResStructs();
335  void genStartResStructs(std::size_t depth, std::size_t num_rep_chg_pairs);
336 
337  std::size_t countRepChargePairs() const;
338 
339  void genOutputResStructs();
340  void genOutputResStructs(StructureData& res_struct, std::size_t depth, std::size_t con_idx,
341  std::size_t num_charges);
342 
343  void postprocOutputResStructs();
344 
345  void minimzeResStructProperty(std::size_t (ResonanceStructureGenerator::*prop_func)(const StructureData&) const);
346 
347  std::size_t countOctetRuleViolations(const StructureData& res_struct) const;
348  std::size_t countSP1GeometryViolations(const StructureData& res_struct) const;
349  std::size_t count12ChargedCBonds(const StructureData& res_struct) const;
350 
351  void modifyResStruct(StructureData& res_struct, std::size_t bond_idx, std::size_t atom1_idx, std::size_t atom2_idx,
352  long bond_order_diff, long atom1_chg_diff, long atom2_chg_diff) const;
353 
354  StructureDataPtr copyResStructPtr(const StructureDataPtr& res_struct_ptr);
355  StructureDataPtr copyResStruct(const StructureData& res_struct);
356 
357  StructureDataCache resStructDataCache;
358  bool minOctRuleViolations;
359  bool minSP1GeomViolations;
360  bool minCBond12Charges;
361  std::size_t chargeCountWin;
362  Util::BitSet octRuleCheckAtomTypes;
363  std::size_t maxNumGenStructs;
364  const MolecularGraph* molGraph;
365  AtomDataArray atomData;
366  AtomDataPtrArray resAtoms;
367  BondDataList resBonds;
368  StructureData inputResStruct;
369  StructureDataList startResStructs;
370  StructureDataSet workingResStructs;
371  StructureDataList outputResStructs;
372  StructureDataList tmpOutputResStructs;
373  Util::LArray chargeDiffPtn;
374  Util::BitSet visBondMask;
375  std::size_t minNumRepChargePairs;
376  std::size_t minNumCharges;
377  };
378  } // namespace Chem
379 } // namespace CDPL
380 
381 #endif // CDPL_CHEM_RESONANCESTRUCTUREGENERATOR_HPP
Definition of class CDPL::Util::Array.
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::Util::ObjectPool.
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
Holds the per-atom formal charges and per-bond bond-orders that define a single resonance structure.
Definition: ResonanceStructureGenerator.hpp:83
const Util::LArray & getAtomCharges() const
Returns the per-atom formal charges of this resonance structure.
const Util::STArray & getBondOrders() const
Returns the per-bond bond-orders of this resonance structure.
Enumerates the resonance structures of a molecular graph by redistributing π-electrons across the res...
Definition: ResonanceStructureGenerator.hpp:63
std::shared_ptr< ResonanceStructureGenerator > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated ResonanceStructureGenerator insta...
Definition: ResonanceStructureGenerator.hpp:75
ConstStructureDataIterator begin() const
Returns a constant iterator pointing to the first generated record (range-based for support).
ResonanceStructureGenerator(const ResonanceStructureGenerator &gen)
Constructs a copy of the ResonanceStructureGenerator instance gen.
void minimizeOctetRuleViolations(bool minimize)
Specifies whether resonance structures violating the octet rule shall be filtered out.
bool octetRuleViolationsMinimized() const
Tells whether octet-rule violations are minimized.
Util::BitSet & getOctetRuleCheckAtomTypes()
Returns the bit mask of atom types subjected to the octet-rule check during minimization.
void setChargeCountWindow(std::size_t win_size)
Sets the maximum allowed difference between the lowest and highest formal-charge count of accepted re...
std::size_t getMaxNumGeneratedStructures() const
Returns the upper limit on the number of resonance structures generated per molecule.
ConstStructureDataIterator getStructureDataEnd() const
Returns a constant iterator pointing one past the last generated resonance-structure record.
ConstStructureDataIterator end() const
Returns a constant iterator pointing one past the last generated record (range-based for support).
ResonanceStructureGenerator & operator=(const ResonanceStructureGenerator &gen)
Replaces the state of this generator by a copy of the state of gen.
const StructureData & getStructureData(std::size_t idx) const
Returns the resonance-structure record at index idx.
ResonanceStructureGenerator()
Constructs the ResonanceStructureGenerator instance.
boost::indirect_iterator< StructureDataList::const_iterator, const StructureData > ConstStructureDataIterator
A constant iterator over the generated resonance-structure records.
Definition: ResonanceStructureGenerator.hpp:77
bool sp1GeometryViolationsMinimized() const
Tells whether sp-hybridization-geometry violations are minimized.
ConstStructureDataIterator getStructureDataBegin() const
Returns a constant iterator pointing to the first generated resonance-structure record.
const Util::BitSet & getOctetRuleCheckAtomTypes() const
Returns the bit mask of atom types subjected to the octet-rule check during minimization.
void generate(const MolecularGraph &molgraph)
Generates all unique resonance structures of the molecular graph molgraph.
bool carbonBond12ChargesMinimized() const
Tells whether 1,2-charge separations on adjacent carbon atoms are minimized.
std::size_t getChargeCountWindow() const
Returns the configured charge-count window size.
void minimizeCarbonBond12Charges(bool minimize)
Specifies whether resonance structures with 1,2-charge separations on adjacent carbon atoms shall be ...
virtual ~ResonanceStructureGenerator()
Virtual destructor.
Definition: ResonanceStructureGenerator.hpp:120
void setMaxNumGeneratedStructures(std::size_t max_num)
Sets the upper limit on the number of resonance structures generated per molecule.
void minimizeSP1GeometryViolations(bool minimize)
Specifies whether resonance structures with sp-hybridization-geometry violations shall be filtered ou...
std::size_t getNumStructures() const
Returns the number of generated resonance structures.
std::shared_ptr< ObjectType > SharedObjectPointer
A smart pointer to a borrowed object that returns the object to the pool on destruction.
Definition: ObjectPool.hpp:71
CDPL_CHEM_API unsigned int getType(const Atom &atom)
Returns the Chem::AtomProperty::TYPE property of atom (see namespace Chem::AtomType).
Array< std::size_t > STArray
Array storing unsigned integers of type std::size_t.
Definition: Array.hpp:575
Array< long > LArray
Array storing integers of type long.
Definition: Array.hpp:580
boost::dynamic_bitset BitSet
Dynamic bitset class.
Definition: BitSet.hpp:46
The namespace of the Chemical Data Processing Library.