| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * | 
|---|
| 6 | * | 
|---|
| 7 | *   This file is part of MoleCuilder. | 
|---|
| 8 | * | 
|---|
| 9 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 10 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 11 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 12 | *    (at your option) any later version. | 
|---|
| 13 | * | 
|---|
| 14 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 15 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 17 | *    GNU General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | *    You should have received a copy of the GNU General Public License | 
|---|
| 20 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | /* | 
|---|
| 24 | * CyclicStructureAnalysis.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Feb 16, 2011 | 
|---|
| 27 | *      Author: heber | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | #include "CyclicStructureAnalysis.hpp" | 
|---|
| 38 |  | 
|---|
| 39 | #include "Atom/atom.hpp" | 
|---|
| 40 | #include "Bond/bond.hpp" | 
|---|
| 41 | #include "CodePatterns/Assert.hpp" | 
|---|
| 42 | #include "CodePatterns/Info.hpp" | 
|---|
| 43 | #include "CodePatterns/Log.hpp" | 
|---|
| 44 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 45 | #include "Element/element.hpp" | 
|---|
| 46 | #include "molecule.hpp" | 
|---|
| 47 |  | 
|---|
| 48 | CyclicStructureAnalysis::CyclicStructureAnalysis(const enum HydrogenTreatment _treatment) : | 
|---|
| 49 | treatment(_treatment) | 
|---|
| 50 | {} | 
|---|
| 51 |  | 
|---|
| 52 | CyclicStructureAnalysis::~CyclicStructureAnalysis() | 
|---|
| 53 | {} | 
|---|
| 54 |  | 
|---|
| 55 | /** Initialise vertex as white with no predecessor, no shortest path(-1), color white. | 
|---|
| 56 | * \param atom_id id of atom whose node we address | 
|---|
| 57 | */ | 
|---|
| 58 | void CyclicStructureAnalysis::InitNode(atomId_t atom_id) | 
|---|
| 59 | { | 
|---|
| 60 | ShortestPathList[atom_id] = -1; | 
|---|
| 61 | PredecessorList[atom_id] = 0; | 
|---|
| 62 | ColorList[atom_id] = GraphEdge::white; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | void CyclicStructureAnalysis::Reset() | 
|---|
| 66 | { | 
|---|
| 67 | // clear what's present | 
|---|
| 68 | ShortestPathList.clear(); | 
|---|
| 69 | PredecessorList.clear(); | 
|---|
| 70 | ColorList.clear(); | 
|---|
| 71 | BFSStack.clear(); | 
|---|
| 72 | TouchedStack.clear(); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | /** Clean the accounting structure for all nodes touched so far. | 
|---|
| 76 | */ | 
|---|
| 77 | void CyclicStructureAnalysis::CleanAllTouched() | 
|---|
| 78 | { | 
|---|
| 79 | atom *Walker = NULL; | 
|---|
| 80 | while (!TouchedStack.empty()) { | 
|---|
| 81 | Walker = TouchedStack.front(); | 
|---|
| 82 | TouchedStack.pop_front(); | 
|---|
| 83 | PredecessorList[Walker->getNr()] = NULL; | 
|---|
| 84 | ShortestPathList[Walker->getNr()] = -1; | 
|---|
| 85 | ColorList[Walker->getNr()] = GraphEdge::white; | 
|---|
| 86 | } | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | /** Resets shortest path list and BFSStack. | 
|---|
| 90 | * \param *&Walker current node, pushed onto BFSStack and TouchedStack | 
|---|
| 91 | */ | 
|---|
| 92 | void CyclicStructureAnalysis::InitializeToRoot(atom *&Root) | 
|---|
| 93 | { | 
|---|
| 94 | ShortestPathList[Root->getNr()] = 0; | 
|---|
| 95 | BFSStack.clear(); // start with empty BFS stack | 
|---|
| 96 | BFSStack.push_front(Root); | 
|---|
| 97 | TouchedStack.push_front(Root); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | /** Performs a BFS from \a *Root, trying to find the same node and hence a cycle. | 
|---|
| 101 | * \param OtherAtom pointing to Root on return indicating found cycle | 
|---|
| 102 | * \param *&BackEdge the edge from root that we don't want to move along | 
|---|
| 103 | */ | 
|---|
| 104 | void CyclicStructureAnalysis::CyclicBFSFromRootToRoot(atom *&OtherAtom, bond::ptr &BackEdge) | 
|---|
| 105 | { | 
|---|
| 106 | atom *Walker = NULL; | 
|---|
| 107 | do { // look for Root | 
|---|
| 108 | ASSERT(!BFSStack.empty(), "CyclicStructureAnalysis_CyclicBFSFromRootToRoot() - BFSStack is empty!"); | 
|---|
| 109 | Walker = BFSStack.front(); | 
|---|
| 110 | BFSStack.pop_front(); | 
|---|
| 111 | LOG(2, "INFO: Current Walker is " << *Walker << ", we look for SP to Root " << *Root << "."); | 
|---|
| 112 | const BondList& ListOfBonds = Walker->getListOfBonds(); | 
|---|
| 113 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
| 114 | Runner != ListOfBonds.end(); | 
|---|
| 115 | ++Runner) { | 
|---|
| 116 | if ((*Runner) != BackEdge) { // only walk along DFS spanning tree (otherwise we always find SP of one being backedge Binder) | 
|---|
| 117 | OtherAtom = (*Runner)->GetOtherAtom(Walker); | 
|---|
| 118 | if ((treatment == IncludeHydrogen) || (OtherAtom->getType()->getAtomicNumber() != 1)) { | 
|---|
| 119 | LOG(2, "INFO: Current OtherAtom is: " << OtherAtom->getName() << " for bond " << *(*Runner) << "."); | 
|---|
| 120 | if (ColorList[OtherAtom->getNr()] == GraphEdge::white) { | 
|---|
| 121 | TouchedStack.push_front(OtherAtom); | 
|---|
| 122 | ColorList[OtherAtom->getNr()] = GraphEdge::lightgray; | 
|---|
| 123 | PredecessorList[OtherAtom->getNr()] = Walker; // Walker is the predecessor | 
|---|
| 124 | ShortestPathList[OtherAtom->getNr()] = ShortestPathList[Walker->getNr()] + 1; | 
|---|
| 125 | LOG(2, "INFO: Coloring OtherAtom " << OtherAtom->getName() << " lightgray, its predecessor is " << Walker->getName() << " and its Shortest Path is " << ShortestPathList[OtherAtom->getNr()] << " egde(s) long."); | 
|---|
| 126 | //if (ShortestPathList[OtherAtom->getNr()] < MinimumRingSize[Walker->GetTrueFather()->getNr()]) { // Check for maximum distance | 
|---|
| 127 | LOG(3, "ACCEPT: Putting OtherAtom " << OtherAtom->getName() << " into queue."); | 
|---|
| 128 | BFSStack.push_front(OtherAtom); | 
|---|
| 129 | //} | 
|---|
| 130 | } else { | 
|---|
| 131 | LOG(3, "REJECT: Not Adding, has already been visited."); | 
|---|
| 132 | } | 
|---|
| 133 | if (OtherAtom == Root) | 
|---|
| 134 | break; | 
|---|
| 135 | } else { | 
|---|
| 136 | LOG(2, "INFO: Skipping hydrogen atom " << *OtherAtom << "."); | 
|---|
| 137 | ColorList[OtherAtom->getNr()] = GraphEdge::black; | 
|---|
| 138 | } | 
|---|
| 139 | } else { | 
|---|
| 140 | LOG(2, "REJECT: Bond " << *(*Runner) << " not Visiting, is the back edge."); | 
|---|
| 141 | } | 
|---|
| 142 | } | 
|---|
| 143 | ColorList[Walker->getNr()] = GraphEdge::black; | 
|---|
| 144 | LOG(1, "INFO: Coloring Walker " << Walker->getName() << " " << GraphEdge::getColorName(ColorList[Walker->getNr()]) << "."); | 
|---|
| 145 | if (OtherAtom == Root) { // if we have found the root, check whether this cycle wasn't already found beforehand | 
|---|
| 146 | // step through predecessor list | 
|---|
| 147 | LOG(4, "DEBUG: Checking whether all predecessors are already marked cyclic ..."); | 
|---|
| 148 | while (OtherAtom != BackEdge->rightatom) {  // Note that leftatom is Root itself | 
|---|
| 149 | if (!OtherAtom->GetTrueFather()->IsCyclic) { // if one bond in the loop is not marked as cyclic, we haven't found this cycle yet | 
|---|
| 150 | LOG(4, "\tDEBUG: OtherAtom " << *OtherAtom << " is not cyclic, breaking."); | 
|---|
| 151 | break; | 
|---|
| 152 | } else | 
|---|
| 153 | OtherAtom = PredecessorList[OtherAtom->getNr()]; | 
|---|
| 154 | } | 
|---|
| 155 | LOG(4, "DEBUG: Checking done."); | 
|---|
| 156 | // if each atom in found cycle is cyclic, loop's been found before already | 
|---|
| 157 | if (OtherAtom == BackEdge->rightatom) { // loop got round completely | 
|---|
| 158 | LOG(3, "INFO: This cycle was already found before, skipping and removing seeker from search."); | 
|---|
| 159 | do { | 
|---|
| 160 | ASSERT(!TouchedStack.empty(), "CyclicStructureAnalysis_CyclicBFSFromRootToRoot() - TouchedStack is empty!"); | 
|---|
| 161 | OtherAtom = TouchedStack.front(); | 
|---|
| 162 | TouchedStack.pop_front(); | 
|---|
| 163 | if (PredecessorList[OtherAtom->getNr()] == Walker) { | 
|---|
| 164 | LOG(4, "INFO: Removing " << *OtherAtom << " from lists and stacks."); | 
|---|
| 165 | PredecessorList[OtherAtom->getNr()] = NULL; | 
|---|
| 166 | ShortestPathList[OtherAtom->getNr()] = -1; | 
|---|
| 167 | ColorList[OtherAtom->getNr()] = GraphEdge::white; | 
|---|
| 168 | // rats ... deque has no find() | 
|---|
| 169 | std::deque<atom *>::iterator iter = find( | 
|---|
| 170 | BFSStack.begin(), | 
|---|
| 171 | BFSStack.end(), | 
|---|
| 172 | OtherAtom); | 
|---|
| 173 | ASSERT(iter != BFSStack.end(), | 
|---|
| 174 | "CyclicStructureAnalysis_CyclicBFSFromRootToRoot() - can't find "+toString(*OtherAtom)+" on stack!"); | 
|---|
| 175 | BFSStack.erase(iter); | 
|---|
| 176 | } | 
|---|
| 177 | } while ((!TouchedStack.empty()) && (PredecessorList[OtherAtom->getNr()] == NULL)); | 
|---|
| 178 | TouchedStack.push_front(OtherAtom); // last was wrongly popped | 
|---|
| 179 | OtherAtom = BackEdge->rightatom; // set to not Root | 
|---|
| 180 | } else { | 
|---|
| 181 | OtherAtom = Root; | 
|---|
| 182 | LOG(2, "INFO: We have reached Root " << *OtherAtom << " and may extract the cycle."); | 
|---|
| 183 | } | 
|---|
| 184 | } | 
|---|
| 185 | } while ((!BFSStack.empty()) && (OtherAtom != Root) && (OtherAtom != NULL)); // || (ShortestPathList[OtherAtom->getNr()] < MinimumRingSize[Walker->GetTrueFather()->getNr()]))); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | /** Climb back the BFSAccounting::PredecessorList and find cycle members. | 
|---|
| 189 | * \param *&OtherAtom | 
|---|
| 190 | * \param *&BackEdge denotes the edge we did not want to travel along when doing CyclicBFSFromRootToRoot() | 
|---|
| 191 | * \param &BFS accounting structure | 
|---|
| 192 | * \param &MinRingSize global minimum distance from one node without encountering oneself, set on return | 
|---|
| 193 | * \param &NumCyles number of cycles in graph | 
|---|
| 194 | */ | 
|---|
| 195 | void CyclicStructureAnalysis::RetrieveCycleMembers( | 
|---|
| 196 | atom *&OtherAtom, | 
|---|
| 197 | bond::ptr &BackEdge, | 
|---|
| 198 | int &MinRingSize, | 
|---|
| 199 | int &NumCycles) | 
|---|
| 200 | { | 
|---|
| 201 | atom *Walker = NULL; | 
|---|
| 202 | int RingSize = -1; | 
|---|
| 203 |  | 
|---|
| 204 | if (OtherAtom == Root) { | 
|---|
| 205 | // now climb back the predecessor list and thus find the cycle members | 
|---|
| 206 | NumCycles++; | 
|---|
| 207 | RingSize = 1; | 
|---|
| 208 | Root->GetTrueFather()->IsCyclic = true; | 
|---|
| 209 |  | 
|---|
| 210 | { | 
|---|
| 211 | CyclicStructureAnalysis::cycle_t currentcycle; | 
|---|
| 212 | std::stringstream output; | 
|---|
| 213 | output << "Found ring contains: "; | 
|---|
| 214 | Walker = Root; | 
|---|
| 215 | currentcycle.insert(Walker->GetTrueFather()->getId()); | 
|---|
| 216 | while (Walker != BackEdge->rightatom) { // leftatom is root | 
|---|
| 217 | output << Walker->getName() << " <-> "; | 
|---|
| 218 | Walker = PredecessorList[Walker->getNr()]; | 
|---|
| 219 | Walker->GetTrueFather()->IsCyclic = true; | 
|---|
| 220 | #ifndef NDEBUG | 
|---|
| 221 | std::pair< cycle_t::iterator, bool > inserter = | 
|---|
| 222 | #endif | 
|---|
| 223 | currentcycle.insert(Walker->GetTrueFather()->getId()); | 
|---|
| 224 | ASSERT( inserter.second, | 
|---|
| 225 | "CyclicStructureAnalysis::RetrieveCycleMembers() - we already inserted " | 
|---|
| 226 | +toString(Walker->GetTrueFather()->getId())+" into currentcycle."); | 
|---|
| 227 | RingSize++; | 
|---|
| 228 | } | 
|---|
| 229 | output << Walker->getName() << "  with a length of " << RingSize << "."; | 
|---|
| 230 | LOG(0, "INFO: " << output.str()); | 
|---|
| 231 | allcycles.push_back(currentcycle); | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | // walk through all and set MinimumRingSize | 
|---|
| 235 | Walker = Root; | 
|---|
| 236 | if ((MinimumRingSize.count(Walker->GetTrueFather()->getNr()) == 0) | 
|---|
| 237 | || (RingSize < MinimumRingSize[Walker->GetTrueFather()->getNr()])) { | 
|---|
| 238 | MinimumRingSize[Walker->GetTrueFather()->getNr()] = RingSize; | 
|---|
| 239 | } else { | 
|---|
| 240 | LOG(3, "INFO: Not setting MinimumRingSize of "<< *(Walker->GetTrueFather()) | 
|---|
| 241 | << " to " << RingSize << " which is already set to " | 
|---|
| 242 | << MinimumRingSize[Walker->GetTrueFather()->getNr()] << "."); | 
|---|
| 243 | } | 
|---|
| 244 | while (Walker != BackEdge->rightatom) { // note that Root is leftatom | 
|---|
| 245 | Walker = PredecessorList[Walker->getNr()]; | 
|---|
| 246 | if ((MinimumRingSize.count(Walker->GetTrueFather()->getNr()) == 0) | 
|---|
| 247 | || (RingSize < MinimumRingSize[Walker->GetTrueFather()->getNr()])) | 
|---|
| 248 | MinimumRingSize[Walker->GetTrueFather()->getNr()] = RingSize; | 
|---|
| 249 | } | 
|---|
| 250 | if ((RingSize < MinRingSize) || (MinRingSize == -1)) | 
|---|
| 251 | MinRingSize = RingSize; | 
|---|
| 252 | } else { | 
|---|
| 253 | LOG(1, "INFO: No ring containing " << *Root << " with length equal to or smaller than " << MinimumRingSize[Root->GetTrueFather()->getNr()] << " found."); | 
|---|
| 254 | } | 
|---|
| 255 | } | 
|---|
| 256 |  | 
|---|
| 257 | /** From a given node performs a BFS to touch the next cycle, for whose nodes \a MinimumRingSize is set and set it accordingly. | 
|---|
| 258 | * \param *&Walker node to look for closest cycle from, i.e. \a MinimumRingSize is set for this node | 
|---|
| 259 | * \param AtomCount number of nodes in graph | 
|---|
| 260 | */ | 
|---|
| 261 | void CyclicStructureAnalysis::BFSToNextCycle(atom *Walker) | 
|---|
| 262 | { | 
|---|
| 263 | atom *Root = Walker; | 
|---|
| 264 | atom *OtherAtom = Walker; | 
|---|
| 265 |  | 
|---|
| 266 | Reset(); | 
|---|
| 267 |  | 
|---|
| 268 | InitializeToRoot(Walker); | 
|---|
| 269 | while (OtherAtom != NULL) { // look for Root | 
|---|
| 270 | ASSERT(!BFSStack.empty(), "CyclicStructureAnalysis_BFSToNextCycle() - BFSStack is empty!"); | 
|---|
| 271 | Walker = BFSStack.front(); | 
|---|
| 272 | BFSStack.pop_front(); | 
|---|
| 273 | LOG(2, "INFO: Current Walker is " << *Walker << ", BFS-stepping away from Root " << *Root << "."); | 
|---|
| 274 | const BondList& ListOfBonds = Walker->getListOfBonds(); | 
|---|
| 275 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
| 276 | Runner != ListOfBonds.end(); | 
|---|
| 277 | ++Runner) { | 
|---|
| 278 | // "removed (*Runner) != BackEdge) || " from next if, is u | 
|---|
| 279 |  | 
|---|
| 280 | // only walk along DFS spanning tree (otherwise we always find SP of 1 | 
|---|
| 281 | // being backedge Binder), but terminal hydrogens may be connected via | 
|---|
| 282 | // backedge, hence extra check | 
|---|
| 283 | //      if ((ListOfBonds.size() != 1)) { | 
|---|
| 284 | OtherAtom = (*Runner)->GetOtherAtom(Walker); | 
|---|
| 285 | if ((treatment == IncludeHydrogen) || (OtherAtom->getType()->getAtomicNumber() != 1)) { | 
|---|
| 286 | LOG(2, "INFO: Current OtherAtom is: " << OtherAtom->getName() << " for bond " << *(*Runner) << "."); | 
|---|
| 287 | if (ColorList[OtherAtom->getNr()] == GraphEdge::white) { | 
|---|
| 288 | TouchedStack.push_front(OtherAtom); | 
|---|
| 289 | ColorList[OtherAtom->getNr()] = GraphEdge::lightgray; | 
|---|
| 290 | PredecessorList[OtherAtom->getNr()] = Walker; // Walker is the predecessor | 
|---|
| 291 | ShortestPathList[OtherAtom->getNr()] = ShortestPathList[Walker->getNr()] + 1; | 
|---|
| 292 | LOG(2, "ACCEPT: Coloring OtherAtom " | 
|---|
| 293 | << OtherAtom->getName() << " lightgray, its predecessor is " | 
|---|
| 294 | << Walker->getName() << " and its Shortest Path is " | 
|---|
| 295 | << ShortestPathList[OtherAtom->getNr()] << " egde(s) long."); | 
|---|
| 296 | // distance is a locally optimal criterion (we have eliminated all | 
|---|
| 297 | // cycles already). Hence, we may assume that all set MinimumRingSize | 
|---|
| 298 | // correspond to shortest distances to cycles. I.e., as soon as we reach | 
|---|
| 299 | // as set MinimumRingSize we may use it and the current shortest path | 
|---|
| 300 | // distance to it | 
|---|
| 301 | if (MinimumRingSize.count(OtherAtom->GetTrueFather()->getNr())) { | 
|---|
| 302 | LOG(2, "SUCCESS: Found set MinimumRingSize at " << *OtherAtom | 
|---|
| 303 | << ", walking back to Root " << *Root << "."); | 
|---|
| 304 | // set all predecessors | 
|---|
| 305 | const unsigned int shorttestpath = ShortestPathList[OtherAtom->getNr()]; | 
|---|
| 306 | atom *Backwalker = OtherAtom; | 
|---|
| 307 | while (Backwalker != Root) { | 
|---|
| 308 | Backwalker = PredecessorList[Backwalker->getNr()]; | 
|---|
| 309 | MinimumRingSize[Backwalker->GetTrueFather()->getNr()] = | 
|---|
| 310 | (shorttestpath - ShortestPathList[Backwalker->getNr()]) | 
|---|
| 311 | + MinimumRingSize[OtherAtom->GetTrueFather()->getNr()]; | 
|---|
| 312 | LOG(2, "Setting MinimumRingSize of " << *Backwalker << " to " | 
|---|
| 313 | << MinimumRingSize[Backwalker->GetTrueFather()->getNr()] << "."); | 
|---|
| 314 | } | 
|---|
| 315 | OtherAtom = NULL; //break; | 
|---|
| 316 | break; | 
|---|
| 317 | } else | 
|---|
| 318 | BFSStack.push_front(OtherAtom); | 
|---|
| 319 | } else { | 
|---|
| 320 | LOG(3, "REJECT: Not Adding, has already been visited."); | 
|---|
| 321 | } | 
|---|
| 322 | } else { | 
|---|
| 323 | LOG(3, "REJECT: Not Visiting, is a back edge to hydrogen."); | 
|---|
| 324 | } | 
|---|
| 325 | //      } | 
|---|
| 326 | } | 
|---|
| 327 | ColorList[Walker->getNr()] = GraphEdge::black; | 
|---|
| 328 | LOG(1, "INFO: Coloring Walker " << Walker->getName() << " " << GraphEdge::getColorName(ColorList[Walker->getNr()]) << "."); | 
|---|
| 329 | } | 
|---|
| 330 | } | 
|---|
| 331 |  | 
|---|
| 332 | /** All nodes that are not in cycles get assigned a \a *&MinimumRingSize by BFS to next cycle. | 
|---|
| 333 | * \param *&MinimumRingSize array with minimum distance without encountering oneself for each atom | 
|---|
| 334 | * \param MinRingSize global minium distance | 
|---|
| 335 | * \param NumCyles number of cycles in graph | 
|---|
| 336 | */ | 
|---|
| 337 | void CyclicStructureAnalysis::AssignRingSizetoNonCycleMembers(const int MinRingSize, const int NumCycles) | 
|---|
| 338 | { | 
|---|
| 339 | atom *Walker = NULL; | 
|---|
| 340 | if (MinRingSize != -1) { // if rings are present | 
|---|
| 341 | // go over all atoms | 
|---|
| 342 | World::AtomComposite allatoms = World::getInstance().getAllAtoms(); | 
|---|
| 343 | for (World::AtomComposite::const_iterator iter = allatoms.begin(); | 
|---|
| 344 | iter != allatoms.end(); | 
|---|
| 345 | ++iter) { | 
|---|
| 346 | Walker = *iter; | 
|---|
| 347 |  | 
|---|
| 348 | if (MinimumRingSize.find(Walker->GetTrueFather()->getNr()) == MinimumRingSize.end()) { // check whether MinimumRingSize is set, if not BFS to next where it is | 
|---|
| 349 | LOG(1, "---------------------------------------------------------------------------------------------------------"); | 
|---|
| 350 | BFSToNextCycle(Walker); | 
|---|
| 351 | } | 
|---|
| 352 | ASSERT(MinimumRingSize.find(Walker->GetTrueFather()->getNr()) != MinimumRingSize.end(), | 
|---|
| 353 | "CyclicStructureAnalysis::AssignRingSizetoNonCycleMembers() - BFSToNextCycle did not set MinimumRingSize of " | 
|---|
| 354 | +toString(*(Walker->GetTrueFather()))+"."); | 
|---|
| 355 | LOG(1, "INFO: Minimum ring size of " << *Walker << " is " << MinimumRingSize[Walker->GetTrueFather()->getNr()] << "."); | 
|---|
| 356 | } | 
|---|
| 357 | LOG(1, "INFO: Minimum ring size is " << MinRingSize << ", over " << NumCycles << " cycle(s) total."); | 
|---|
| 358 | } else | 
|---|
| 359 | LOG(1, "INFO: No rings were detected in the molecular structure."); | 
|---|
| 360 | } | 
|---|
| 361 |  | 
|---|
| 362 | /** Analyses the cycles found and returns minimum of all cycle lengths. | 
|---|
| 363 | * We begin with a list of Back edges found during DepthFirstSearchAnalysis(). We go through this list - one end is the Root, | 
|---|
| 364 | * the other our initial Walker - and do a Breadth First Search for the Root. We mark down each Predecessor and as soon as | 
|---|
| 365 | * we have found the Root via BFS, we may climb back the closed cycle via the Predecessors. Thereby we mark atoms and bonds | 
|---|
| 366 | * as cyclic and print out the cycles. | 
|---|
| 367 | * \param *BackEdgeStack stack with all back edges found during DFS scan. Beware: This stack contains the bonds from the total molecule, not from the subgraph! | 
|---|
| 368 | * \todo BFS from the not-same-LP to find back to starting point of tributary cycle over more than one bond | 
|---|
| 369 | */ | 
|---|
| 370 | void CyclicStructureAnalysis::operator()(std::deque<bond::ptr > * BackEdgeStack) | 
|---|
| 371 | { | 
|---|
| 372 | Info FunctionInfo("CyclicStructureAnalysis"); | 
|---|
| 373 | atom *Walker = NULL; | 
|---|
| 374 | atom *OtherAtom = NULL; | 
|---|
| 375 | bond::ptr BackEdge; | 
|---|
| 376 | int NumCycles = 0; | 
|---|
| 377 | int MinRingSize = -1; | 
|---|
| 378 |  | 
|---|
| 379 | // clear cycle container | 
|---|
| 380 | allcycles.clear(); | 
|---|
| 381 |  | 
|---|
| 382 | { | 
|---|
| 383 | std::stringstream output; | 
|---|
| 384 | output << "Back edge list - "; | 
|---|
| 385 | for (std::deque<bond::ptr >::const_iterator iter = BackEdgeStack->begin(); | 
|---|
| 386 | iter != BackEdgeStack->end(); ++iter) | 
|---|
| 387 | output << **iter << " "; | 
|---|
| 388 | LOG(0, output.str()); | 
|---|
| 389 | } | 
|---|
| 390 |  | 
|---|
| 391 | LOG(1, "STATUS: Analysing cycles ... "); | 
|---|
| 392 | NumCycles = 0; | 
|---|
| 393 | while (!BackEdgeStack->empty()) { | 
|---|
| 394 | BackEdge = BackEdgeStack->front(); | 
|---|
| 395 | BackEdgeStack->pop_front(); | 
|---|
| 396 | // this is the target | 
|---|
| 397 | Root = BackEdge->leftatom; | 
|---|
| 398 | // this is the source point | 
|---|
| 399 | Walker = BackEdge->rightatom; | 
|---|
| 400 |  | 
|---|
| 401 | InitializeToRoot(Walker); | 
|---|
| 402 |  | 
|---|
| 403 | LOG(1, "---------------------------------------------------------------------------------------------------------"); | 
|---|
| 404 | OtherAtom = NULL; | 
|---|
| 405 | // go to next cycle via BFS | 
|---|
| 406 | CyclicBFSFromRootToRoot(OtherAtom, BackEdge); | 
|---|
| 407 | // get all member nodes of this cycle | 
|---|
| 408 | RetrieveCycleMembers(OtherAtom, BackEdge, MinRingSize, NumCycles); | 
|---|
| 409 |  | 
|---|
| 410 | CleanAllTouched(); | 
|---|
| 411 | } | 
|---|
| 412 | AssignRingSizetoNonCycleMembers(MinRingSize, NumCycles); | 
|---|
| 413 | } | 
|---|
| 414 |  | 
|---|
| 415 | /** Output a list of flags, stating whether the bond was visited or not. | 
|---|
| 416 | * \param *list list to print | 
|---|
| 417 | */ | 
|---|
| 418 | void CyclicStructureAnalysis::OutputAlreadyVisited(int *list) | 
|---|
| 419 | { | 
|---|
| 420 | std::stringstream output; | 
|---|
| 421 | output << "Already Visited Bonds:\t"; | 
|---|
| 422 | for (int i = 1; i <= list[0]; i++) | 
|---|
| 423 | output << list[i] << "  "; | 
|---|
| 424 | LOG(0, output.str()); | 
|---|
| 425 | } | 
|---|
| 426 |  | 
|---|
| 427 | const std::map<atomId_t, int >& CyclicStructureAnalysis::getMinimumRingSize() const | 
|---|
| 428 | { | 
|---|
| 429 | return MinimumRingSize; | 
|---|
| 430 | } | 
|---|