| 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 <algorithm> | 
|---|
| 40 |  | 
|---|
| 41 | #include "Atom/atom.hpp" | 
|---|
| 42 | #include "Bond/bond.hpp" | 
|---|
| 43 | #include "CodePatterns/Assert.hpp" | 
|---|
| 44 | #include "CodePatterns/Info.hpp" | 
|---|
| 45 | #include "CodePatterns/Log.hpp" | 
|---|
| 46 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 47 | #include "Element/element.hpp" | 
|---|
| 48 | #include "molecule.hpp" | 
|---|
| 49 |  | 
|---|
| 50 | CyclicStructureAnalysis::CyclicStructureAnalysis(const enum HydrogenTreatment _treatment) : | 
|---|
| 51 | treatment(_treatment) | 
|---|
| 52 | {} | 
|---|
| 53 |  | 
|---|
| 54 | CyclicStructureAnalysis::~CyclicStructureAnalysis() | 
|---|
| 55 | {} | 
|---|
| 56 |  | 
|---|
| 57 | /** Initialise vertex as white with no predecessor, no shortest path(-1), color white. | 
|---|
| 58 | * \param atom_id id of atom whose node we address | 
|---|
| 59 | */ | 
|---|
| 60 | void CyclicStructureAnalysis::InitNode(atomId_t atom_id) | 
|---|
| 61 | { | 
|---|
| 62 | ShortestPathList[atom_id] = -1; | 
|---|
| 63 | PredecessorList[atom_id] = 0; | 
|---|
| 64 | ColorList[atom_id] = GraphEdge::white; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | void CyclicStructureAnalysis::Reset() | 
|---|
| 68 | { | 
|---|
| 69 | // clear what's present | 
|---|
| 70 | ShortestPathList.clear(); | 
|---|
| 71 | PredecessorList.clear(); | 
|---|
| 72 | ColorList.clear(); | 
|---|
| 73 | BFSStack.clear(); | 
|---|
| 74 | TouchedStack.clear(); | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | /** Clean the accounting structure for all nodes touched so far. | 
|---|
| 78 | */ | 
|---|
| 79 | void CyclicStructureAnalysis::CleanAllTouched() | 
|---|
| 80 | { | 
|---|
| 81 | atom *Walker = NULL; | 
|---|
| 82 | while (!TouchedStack.empty()) { | 
|---|
| 83 | Walker = TouchedStack.front(); | 
|---|
| 84 | TouchedStack.pop_front(); | 
|---|
| 85 | PredecessorList[Walker->getNr()] = NULL; | 
|---|
| 86 | ShortestPathList[Walker->getNr()] = -1; | 
|---|
| 87 | ColorList[Walker->getNr()] = GraphEdge::white; | 
|---|
| 88 | } | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | /** Resets shortest path list and BFSStack. | 
|---|
| 92 | * \param *&Walker current node, pushed onto BFSStack and TouchedStack | 
|---|
| 93 | */ | 
|---|
| 94 | void CyclicStructureAnalysis::InitializeToRoot(atom *&Root) | 
|---|
| 95 | { | 
|---|
| 96 | ColorList.clear(); | 
|---|
| 97 | ShortestPathList.clear(); | 
|---|
| 98 | ShortestPathList[Root->getNr()] = 0; | 
|---|
| 99 | PredecessorList.clear(); | 
|---|
| 100 | BFSStack.clear(); // start with empty BFS stack | 
|---|
| 101 | BFSStack.push_front(Root); | 
|---|
| 102 | TouchedStack.push_front(Root); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | /** Performs a BFS from \a *Root, trying to find the same node and hence all cycles. | 
|---|
| 106 | * | 
|---|
| 107 | * We exclude the back edge, hence the direct way is prohibited. But as it is a back edge, | 
|---|
| 108 | * there must be at least one more way to \a *Root. This leads us to all cycles for this | 
|---|
| 109 | * back edge. | 
|---|
| 110 | * | 
|---|
| 111 | * \param OtherAtom pointing to Root on return indicating found cycle | 
|---|
| 112 | * \param *&BackEdge the edge from root that we don't want to move along | 
|---|
| 113 | * \param MinRingSize set to minimum over all cycles encountered | 
|---|
| 114 | */ | 
|---|
| 115 | void CyclicStructureAnalysis::findAllCyclesforBackEdge( | 
|---|
| 116 | atom *&OtherAtom, | 
|---|
| 117 | bond::ptr &BackEdge, | 
|---|
| 118 | int &MinRingSize) | 
|---|
| 119 | { | 
|---|
| 120 | size_t MaximumHorizon = (size_t)-1; // will overflow to largest number | 
|---|
| 121 | atom *Walker = NULL; | 
|---|
| 122 | do { // look for Root | 
|---|
| 123 | ASSERT(!BFSStack.empty(), "CyclicStructureAnalysis_CyclicBFSFromRootToRoot() - BFSStack is empty!"); | 
|---|
| 124 | Walker = BFSStack.back(); | 
|---|
| 125 | BFSStack.pop_back(); | 
|---|
| 126 | LOG(2, "INFO: Current Walker is " << *Walker << ", we look for SP to Root " << *Root << "."); | 
|---|
| 127 |  | 
|---|
| 128 | // check all edges/bonds from current Walker | 
|---|
| 129 | if (MaximumHorizon >=  (size_t)ShortestPathList[Walker->getNr()]) { | 
|---|
| 130 | const BondList& ListOfBonds = Walker->getListOfBonds(); | 
|---|
| 131 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
| 132 | Runner != ListOfBonds.end(); | 
|---|
| 133 | ++Runner) { | 
|---|
| 134 | if ((*Runner) != BackEdge) { // only walk along DFS spanning tree (otherwise we always find SP of one being backedge Binder) | 
|---|
| 135 | OtherAtom = (*Runner)->GetOtherAtom(Walker); | 
|---|
| 136 | if ((treatment == IncludeHydrogen) || (OtherAtom->getType()->getAtomicNumber() != 1)) { | 
|---|
| 137 | LOG(2, "INFO: Current OtherAtom is: " << OtherAtom->getName() << " for bond " << *(*Runner) << "."); | 
|---|
| 138 | if (ColorList[OtherAtom->getNr()] == GraphEdge::white) { | 
|---|
| 139 | // we discovered a new node/atom | 
|---|
| 140 | TouchedStack.push_front(OtherAtom); | 
|---|
| 141 | ColorList[OtherAtom->getNr()] = GraphEdge::lightgray; | 
|---|
| 142 | PredecessorList[OtherAtom->getNr()] = Walker; // Walker is the predecessor | 
|---|
| 143 | ShortestPathList[OtherAtom->getNr()] = ShortestPathList[Walker->getNr()] + 1; | 
|---|
| 144 | LOG(2, "INFO: Coloring OtherAtom " << OtherAtom->getName() << " lightgray, its predecessor is " << Walker->getName() << " and its Shortest Path is " << ShortestPathList[OtherAtom->getNr()] << " egde(s) long."); | 
|---|
| 145 | //if (ShortestPathList[OtherAtom->getNr()] < MinimumRingSize[Walker->GetTrueFather()->getNr()]) { // Check for maximum distance | 
|---|
| 146 | LOG(3, "ACCEPT: Putting OtherAtom " << OtherAtom->getName() << " into queue."); | 
|---|
| 147 | BFSStack.push_front(OtherAtom); | 
|---|
| 148 | //} | 
|---|
| 149 | } else { | 
|---|
| 150 | LOG(3, "REJECT: Not Adding, has already been visited."); | 
|---|
| 151 | } | 
|---|
| 152 | if (OtherAtom == Root) | 
|---|
| 153 | break; | 
|---|
| 154 | } else { | 
|---|
| 155 | LOG(2, "INFO: Skipping hydrogen atom " << *OtherAtom << "."); | 
|---|
| 156 | ColorList[OtherAtom->getNr()] = GraphEdge::black; | 
|---|
| 157 | } | 
|---|
| 158 | } else { | 
|---|
| 159 | LOG(2, "REJECT: Bond " << *(*Runner) << " not Visiting, is the back edge."); | 
|---|
| 160 | } | 
|---|
| 161 | } | 
|---|
| 162 | ColorList[Walker->getNr()] = GraphEdge::black; | 
|---|
| 163 | LOG(1, "INFO: Coloring Walker " << Walker->getName() << " " << GraphEdge::getColorName(ColorList[Walker->getNr()]) << "."); | 
|---|
| 164 | } else { | 
|---|
| 165 | LOG(1, "INFO: Skipping bonds for " << Walker->getName() << " as it resides on the horizon."); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | // have we closed the cycle, i.e. stumbled upon Root by another mean than | 
|---|
| 169 | // the back edge? | 
|---|
| 170 | if (OtherAtom == Root) { | 
|---|
| 171 | // check whether this cycle wasn't already found beforehand by stepping | 
|---|
| 172 | // through predecessor list | 
|---|
| 173 | int RingSize = RetrieveCycleMembers(OtherAtom, BackEdge, MinRingSize); | 
|---|
| 174 | MaximumHorizon = std::min( MaximumHorizon, (size_t)RingSize ); | 
|---|
| 175 | LOG(2, "INFO: Maximum horizon is set to " << MaximumHorizon); | 
|---|
| 176 |  | 
|---|
| 177 | // remove last step and prepare for a possible yet another path to Root | 
|---|
| 178 | do { | 
|---|
| 179 | ASSERT(!TouchedStack.empty(), "CyclicStructureAnalysis_CyclicBFSFromRootToRoot() - TouchedStack is empty!"); | 
|---|
| 180 | OtherAtom = TouchedStack.front(); | 
|---|
| 181 | TouchedStack.pop_front(); | 
|---|
| 182 | if (PredecessorList[OtherAtom->getNr()] == Walker) { | 
|---|
| 183 | LOG(4, "INFO: Removing " << *OtherAtom << " from lists and stacks."); | 
|---|
| 184 | PredecessorList[OtherAtom->getNr()] = NULL; | 
|---|
| 185 | ShortestPathList[OtherAtom->getNr()] = -1; | 
|---|
| 186 | ColorList[OtherAtom->getNr()] = GraphEdge::white; | 
|---|
| 187 | // rats ... deque has no find() | 
|---|
| 188 | std::deque<atom *>::iterator iter = find( | 
|---|
| 189 | BFSStack.begin(), | 
|---|
| 190 | BFSStack.end(), | 
|---|
| 191 | OtherAtom); | 
|---|
| 192 | ASSERT(iter != BFSStack.end(), | 
|---|
| 193 | "CyclicStructureAnalysis_CyclicBFSFromRootToRoot() - can't find "+toString(*OtherAtom)+" on stack!"); | 
|---|
| 194 | BFSStack.erase(iter); | 
|---|
| 195 | } | 
|---|
| 196 | } while ((!TouchedStack.empty()) && (PredecessorList[OtherAtom->getNr()] == NULL)); | 
|---|
| 197 | TouchedStack.push_front(OtherAtom); // last was wrongly popped | 
|---|
| 198 | OtherAtom = BackEdge->rightatom; // set to not Root | 
|---|
| 199 | } | 
|---|
| 200 | } while ((!BFSStack.empty()) && (OtherAtom != Root) && (OtherAtom != NULL)); // || (ShortestPathList[OtherAtom->getNr()] < MinimumRingSize[Walker->GetTrueFather()->getNr()]))); | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | /** Extracts cycle from BFSAccounting::PredecessorList with given \a BackEdge and current \a Root. | 
|---|
| 204 | * | 
|---|
| 205 | * \param BackEdge back edge of this cycle | 
|---|
| 206 | */ | 
|---|
| 207 | CyclicStructureAnalysis::cycle_t CyclicStructureAnalysis::extractCurrentCycle( | 
|---|
| 208 | bond::ptr &BackEdge) | 
|---|
| 209 | { | 
|---|
| 210 | CyclicStructureAnalysis::cycle_t currentcycle; | 
|---|
| 211 | atom *Walker = Root; | 
|---|
| 212 | currentcycle.insert(Walker->GetTrueFather()->getId()); | 
|---|
| 213 | std::stringstream output; | 
|---|
| 214 | while (Walker != BackEdge->rightatom) { // leftatom is root | 
|---|
| 215 | Walker = PredecessorList[Walker->getNr()]; | 
|---|
| 216 | Walker->GetTrueFather()->IsCyclic = true; | 
|---|
| 217 | output << Walker->getName() << " <-> "; | 
|---|
| 218 | #ifndef NDEBUG | 
|---|
| 219 | std::pair< cycle_t::iterator, bool > inserter = | 
|---|
| 220 | #endif | 
|---|
| 221 | currentcycle.insert(Walker->GetTrueFather()->getId()); | 
|---|
| 222 | ASSERT( inserter.second, | 
|---|
| 223 | "CyclicStructureAnalysis::RetrieveCycleMembers() - we already inserted " | 
|---|
| 224 | +toString(Walker->GetTrueFather()->getId())+" into currentcycle."); | 
|---|
| 225 | } | 
|---|
| 226 | LOG(3, "INFO: " << output.str() << Root->getName()); | 
|---|
| 227 | return currentcycle; | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | /** Climb back the BFSAccounting::PredecessorList and find cycle members. | 
|---|
| 231 | * \param *&OtherAtom | 
|---|
| 232 | * \param *&BackEdge denotes the edge we did not want to travel along when doing CyclicBFSFromRootToRoot() | 
|---|
| 233 | * \param &BFS accounting structure | 
|---|
| 234 | * \param &MinRingSize global minimum distance from one node without encountering oneself, set on return | 
|---|
| 235 | * \return size of found cycle, -1 - nothing found, something went wrong | 
|---|
| 236 | */ | 
|---|
| 237 | int CyclicStructureAnalysis::RetrieveCycleMembers( | 
|---|
| 238 | atom *&OtherAtom, | 
|---|
| 239 | bond::ptr &BackEdge, | 
|---|
| 240 | int &MinRingSize) | 
|---|
| 241 | { | 
|---|
| 242 | int RingSize = -1; | 
|---|
| 243 | bool newcycle = false; | 
|---|
| 244 |  | 
|---|
| 245 | if (OtherAtom == Root) { | 
|---|
| 246 | // now climb back the predecessor list and thus find the cycle members | 
|---|
| 247 | Root->GetTrueFather()->IsCyclic = true; | 
|---|
| 248 |  | 
|---|
| 249 | { | 
|---|
| 250 | // check whether cycle is present already | 
|---|
| 251 | atom *Walker = Root; | 
|---|
| 252 | LOG(4, "DEBUG: Checking whether all predecessors are already marked cyclic ..."); | 
|---|
| 253 | while (Walker != BackEdge->rightatom) {  // Note that leftatom is Root itself | 
|---|
| 254 | if (!Walker->GetTrueFather()->IsCyclic) { // if one bond in the loop is not marked as cyclic, we haven't found this cycle yet | 
|---|
| 255 | LOG(4, "\tDEBUG: Walker " << *Walker << " is not cyclic, breaking."); | 
|---|
| 256 | break; | 
|---|
| 257 | } else | 
|---|
| 258 | Walker = PredecessorList[Walker->getNr()]; | 
|---|
| 259 | } | 
|---|
| 260 | LOG(4, "DEBUG: Checking done."); | 
|---|
| 261 |  | 
|---|
| 262 | // if each atom in found cycle is cyclic, loop's been found before already | 
|---|
| 263 |  | 
|---|
| 264 | // exctract cycle | 
|---|
| 265 | { | 
|---|
| 266 | const cycle_t currentcycle = extractCurrentCycle(BackEdge); | 
|---|
| 267 | if (Walker != BackEdge->rightatom) { // loop got round completely | 
|---|
| 268 | allcycles.push_back(currentcycle); | 
|---|
| 269 | newcycle = true; | 
|---|
| 270 | } else { | 
|---|
| 271 | LOG(3, "INFO: All bonds are marked cyclic already, checking allcycles whether cycle is already present."); | 
|---|
| 272 | const cycles_t::const_iterator iter = | 
|---|
| 273 | std::find(allcycles.begin(), allcycles.end(), currentcycle); | 
|---|
| 274 | if (iter == allcycles.end()) { // not found | 
|---|
| 275 | allcycles.push_back(currentcycle); | 
|---|
| 276 | newcycle = true; | 
|---|
| 277 | } | 
|---|
| 278 | } | 
|---|
| 279 | RingSize = currentcycle.size(); | 
|---|
| 280 | if (newcycle) { | 
|---|
| 281 | LOG(0, "INFO: " << "Found ring contains: " << currentcycle << "  with a length of " << RingSize); | 
|---|
| 282 | } else { | 
|---|
| 283 | LOG(1, "INFO: cycle " << currentcycle << " is already present."); | 
|---|
| 284 | } | 
|---|
| 285 | } | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | if (newcycle) { | 
|---|
| 289 | // walk through all and set MinimumRingSize | 
|---|
| 290 | atom *Walker = Root; | 
|---|
| 291 | if ((MinimumRingSize.count(Walker->GetTrueFather()->getNr()) == 0) | 
|---|
| 292 | || (RingSize < MinimumRingSize[Walker->GetTrueFather()->getNr()])) { | 
|---|
| 293 | MinimumRingSize[Walker->GetTrueFather()->getNr()] = RingSize; | 
|---|
| 294 | } else { | 
|---|
| 295 | LOG(3, "INFO: Not setting MinimumRingSize of "<< *(Walker->GetTrueFather()) | 
|---|
| 296 | << " to " << RingSize << " which is already set to " | 
|---|
| 297 | << MinimumRingSize[Walker->GetTrueFather()->getNr()] << "."); | 
|---|
| 298 | } | 
|---|
| 299 | while (Walker != BackEdge->rightatom) { // note that Root is leftatom | 
|---|
| 300 | Walker = PredecessorList[Walker->getNr()]; | 
|---|
| 301 | if ((MinimumRingSize.count(Walker->GetTrueFather()->getNr()) == 0) | 
|---|
| 302 | || (RingSize < MinimumRingSize[Walker->GetTrueFather()->getNr()])) | 
|---|
| 303 | MinimumRingSize[Walker->GetTrueFather()->getNr()] = RingSize; | 
|---|
| 304 | } | 
|---|
| 305 | if ((RingSize < MinRingSize) || (MinRingSize == -1)) | 
|---|
| 306 | MinRingSize = RingSize; | 
|---|
| 307 | } | 
|---|
| 308 | } else { | 
|---|
| 309 | LOG(1, "INFO: No ring containing " << *Root << " with length equal to or smaller than " << MinimumRingSize[Root->GetTrueFather()->getNr()] << " found."); | 
|---|
| 310 | } | 
|---|
| 311 | return RingSize; | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | /** From a given node performs a BFS to touch the next cycle, for whose nodes \a MinimumRingSize is set and set it accordingly. | 
|---|
| 315 | * \param *&Walker node to look for closest cycle from, i.e. \a MinimumRingSize is set for this node | 
|---|
| 316 | * \param AtomCount number of nodes in graph | 
|---|
| 317 | */ | 
|---|
| 318 | void CyclicStructureAnalysis::BFSToNextCycle(atom *Walker) | 
|---|
| 319 | { | 
|---|
| 320 | atom *Root = Walker; | 
|---|
| 321 | atom *OtherAtom = Walker; | 
|---|
| 322 |  | 
|---|
| 323 | Reset(); | 
|---|
| 324 |  | 
|---|
| 325 | InitializeToRoot(Walker); | 
|---|
| 326 | while (OtherAtom != NULL) { // look for Root | 
|---|
| 327 | ASSERT(!BFSStack.empty(), "CyclicStructureAnalysis_BFSToNextCycle() - BFSStack is empty!"); | 
|---|
| 328 | Walker = BFSStack.front(); | 
|---|
| 329 | BFSStack.pop_front(); | 
|---|
| 330 | LOG(2, "INFO: Current Walker is " << *Walker << ", BFS-stepping away from Root " << *Root << "."); | 
|---|
| 331 | const BondList& ListOfBonds = Walker->getListOfBonds(); | 
|---|
| 332 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
| 333 | Runner != ListOfBonds.end(); | 
|---|
| 334 | ++Runner) { | 
|---|
| 335 | // "removed (*Runner) != BackEdge) || " from next if, is u | 
|---|
| 336 |  | 
|---|
| 337 | // only walk along DFS spanning tree (otherwise we always find SP of 1 | 
|---|
| 338 | // being backedge Binder), but terminal hydrogens may be connected via | 
|---|
| 339 | // backedge, hence extra check | 
|---|
| 340 | //      if ((ListOfBonds.size() != 1)) { | 
|---|
| 341 | OtherAtom = (*Runner)->GetOtherAtom(Walker); | 
|---|
| 342 | if ((treatment == IncludeHydrogen) || (OtherAtom->getType()->getAtomicNumber() != 1)) { | 
|---|
| 343 | LOG(2, "INFO: Current OtherAtom is: " << OtherAtom->getName() << " for bond " << *(*Runner) << "."); | 
|---|
| 344 | if (ColorList[OtherAtom->getNr()] == GraphEdge::white) { | 
|---|
| 345 | TouchedStack.push_front(OtherAtom); | 
|---|
| 346 | ColorList[OtherAtom->getNr()] = GraphEdge::lightgray; | 
|---|
| 347 | PredecessorList[OtherAtom->getNr()] = Walker; // Walker is the predecessor | 
|---|
| 348 | ShortestPathList[OtherAtom->getNr()] = ShortestPathList[Walker->getNr()] + 1; | 
|---|
| 349 | LOG(2, "ACCEPT: Coloring OtherAtom " | 
|---|
| 350 | << OtherAtom->getName() << " lightgray, its predecessor is " | 
|---|
| 351 | << Walker->getName() << " and its Shortest Path is " | 
|---|
| 352 | << ShortestPathList[OtherAtom->getNr()] << " egde(s) long."); | 
|---|
| 353 | // distance is a locally optimal criterion (we have eliminated all | 
|---|
| 354 | // cycles already). Hence, we may assume that all set MinimumRingSize | 
|---|
| 355 | // correspond to shortest distances to cycles. I.e., as soon as we reach | 
|---|
| 356 | // as set MinimumRingSize we may use it and the current shortest path | 
|---|
| 357 | // distance to it | 
|---|
| 358 | if (MinimumRingSize.count(OtherAtom->GetTrueFather()->getNr())) { | 
|---|
| 359 | LOG(2, "SUCCESS: Found set MinimumRingSize at " << *OtherAtom | 
|---|
| 360 | << ", walking back to Root " << *Root << "."); | 
|---|
| 361 | // set all predecessors | 
|---|
| 362 | const unsigned int shorttestpath = ShortestPathList[OtherAtom->getNr()]; | 
|---|
| 363 | atom *Backwalker = OtherAtom; | 
|---|
| 364 | while (Backwalker != Root) { | 
|---|
| 365 | Backwalker = PredecessorList[Backwalker->getNr()]; | 
|---|
| 366 | MinimumRingSize[Backwalker->GetTrueFather()->getNr()] = | 
|---|
| 367 | (shorttestpath - ShortestPathList[Backwalker->getNr()]) | 
|---|
| 368 | + MinimumRingSize[OtherAtom->GetTrueFather()->getNr()]; | 
|---|
| 369 | LOG(2, "Setting MinimumRingSize of " << *Backwalker << " to " | 
|---|
| 370 | << MinimumRingSize[Backwalker->GetTrueFather()->getNr()] << "."); | 
|---|
| 371 | } | 
|---|
| 372 | OtherAtom = NULL; //break; | 
|---|
| 373 | break; | 
|---|
| 374 | } else | 
|---|
| 375 | BFSStack.push_front(OtherAtom); | 
|---|
| 376 | } else { | 
|---|
| 377 | LOG(3, "REJECT: Not Adding, has already been visited."); | 
|---|
| 378 | } | 
|---|
| 379 | } else { | 
|---|
| 380 | LOG(3, "REJECT: Not Visiting, is a back edge to hydrogen."); | 
|---|
| 381 | } | 
|---|
| 382 | //      } | 
|---|
| 383 | } | 
|---|
| 384 | ColorList[Walker->getNr()] = GraphEdge::black; | 
|---|
| 385 | LOG(1, "INFO: Coloring Walker " << Walker->getName() << " " << GraphEdge::getColorName(ColorList[Walker->getNr()]) << "."); | 
|---|
| 386 | } | 
|---|
| 387 | } | 
|---|
| 388 |  | 
|---|
| 389 | /** All nodes that are not in cycles get assigned a \a *&MinimumRingSize by BFS to next cycle. | 
|---|
| 390 | * \param *&MinimumRingSize array with minimum distance without encountering oneself for each atom | 
|---|
| 391 | * \param MinRingSize global minium distance | 
|---|
| 392 | */ | 
|---|
| 393 | void CyclicStructureAnalysis::AssignRingSizetoNonCycleMembers(const int MinRingSize) | 
|---|
| 394 | { | 
|---|
| 395 | atom *Walker = NULL; | 
|---|
| 396 | if (MinRingSize != -1) { // if rings are present | 
|---|
| 397 | // go over all atoms | 
|---|
| 398 | World::AtomComposite allatoms = World::getInstance().getAllAtoms(); | 
|---|
| 399 | for (World::AtomComposite::const_iterator iter = allatoms.begin(); | 
|---|
| 400 | iter != allatoms.end(); | 
|---|
| 401 | ++iter) { | 
|---|
| 402 | Walker = *iter; | 
|---|
| 403 |  | 
|---|
| 404 | if (MinimumRingSize.find(Walker->GetTrueFather()->getNr()) == MinimumRingSize.end()) { // check whether MinimumRingSize is set, if not BFS to next where it is | 
|---|
| 405 | LOG(1, "---------------------------------------------------------------------------------------------------------"); | 
|---|
| 406 | BFSToNextCycle(Walker); | 
|---|
| 407 | } | 
|---|
| 408 | ASSERT(MinimumRingSize.find(Walker->GetTrueFather()->getNr()) != MinimumRingSize.end(), | 
|---|
| 409 | "CyclicStructureAnalysis::AssignRingSizetoNonCycleMembers() - BFSToNextCycle did not set MinimumRingSize of " | 
|---|
| 410 | +toString(*(Walker->GetTrueFather()))+"."); | 
|---|
| 411 | LOG(1, "INFO: Minimum ring size of " << *Walker << " is " << MinimumRingSize[Walker->GetTrueFather()->getNr()] << "."); | 
|---|
| 412 | } | 
|---|
| 413 | LOG(1, "INFO: Minimum ring size is " << MinRingSize << ", over " << allcycles.size() << " cycle(s) total."); | 
|---|
| 414 | } else | 
|---|
| 415 | LOG(1, "INFO: No rings were detected in the molecular structure."); | 
|---|
| 416 | } | 
|---|
| 417 |  | 
|---|
| 418 | /** Analyses the cycles found and returns minimum of all cycle lengths. | 
|---|
| 419 | * We begin with a list of Back edges found during DepthFirstSearchAnalysis(). We go through this list - one end is the Root, | 
|---|
| 420 | * the other our initial Walker - and do a Breadth First Search for the Root. We mark down each Predecessor and as soon as | 
|---|
| 421 | * we have found the Root via BFS, we may climb back the closed cycle via the Predecessors. Thereby we mark atoms and bonds | 
|---|
| 422 | * as cyclic and print out the cycles. | 
|---|
| 423 | * \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! | 
|---|
| 424 | * \todo BFS from the not-same-LP to find back to starting point of tributary cycle over more than one bond | 
|---|
| 425 | */ | 
|---|
| 426 | void CyclicStructureAnalysis::operator()(std::deque<bond::ptr > * BackEdgeStack) | 
|---|
| 427 | { | 
|---|
| 428 | Info FunctionInfo("CyclicStructureAnalysis"); | 
|---|
| 429 | atom *Walker = NULL; | 
|---|
| 430 | atom *OtherAtom = NULL; | 
|---|
| 431 | bond::ptr BackEdge; | 
|---|
| 432 | int MinRingSize = -1; | 
|---|
| 433 |  | 
|---|
| 434 | // clear cycle container | 
|---|
| 435 | allcycles.clear(); | 
|---|
| 436 |  | 
|---|
| 437 | { | 
|---|
| 438 | std::stringstream output; | 
|---|
| 439 | output << "Back edge list - "; | 
|---|
| 440 | for (std::deque<bond::ptr >::const_iterator iter = BackEdgeStack->begin(); | 
|---|
| 441 | iter != BackEdgeStack->end(); ++iter) | 
|---|
| 442 | output << **iter << " "; | 
|---|
| 443 | LOG(0, output.str()); | 
|---|
| 444 | } | 
|---|
| 445 |  | 
|---|
| 446 | LOG(1, "STATUS: Analysing cycles ... "); | 
|---|
| 447 | while (!BackEdgeStack->empty()) { | 
|---|
| 448 | BackEdge = BackEdgeStack->front(); | 
|---|
| 449 | BackEdgeStack->pop_front(); | 
|---|
| 450 | // this is the target | 
|---|
| 451 | Root = BackEdge->leftatom; | 
|---|
| 452 | // this is the source point | 
|---|
| 453 | Walker = BackEdge->rightatom; | 
|---|
| 454 |  | 
|---|
| 455 | InitializeToRoot(Walker); | 
|---|
| 456 |  | 
|---|
| 457 | LOG(1, "---------------------------------------------------------------------------------------------------------"); | 
|---|
| 458 | OtherAtom = NULL; | 
|---|
| 459 | // find all cycles for current BackEdge | 
|---|
| 460 | findAllCyclesforBackEdge(OtherAtom, BackEdge, MinRingSize); | 
|---|
| 461 |  | 
|---|
| 462 | CleanAllTouched(); | 
|---|
| 463 | } | 
|---|
| 464 | AssignRingSizetoNonCycleMembers(MinRingSize); | 
|---|
| 465 | } | 
|---|
| 466 |  | 
|---|
| 467 | /** Output a list of flags, stating whether the bond was visited or not. | 
|---|
| 468 | * \param *list list to print | 
|---|
| 469 | */ | 
|---|
| 470 | void CyclicStructureAnalysis::OutputAlreadyVisited(int *list) | 
|---|
| 471 | { | 
|---|
| 472 | std::stringstream output; | 
|---|
| 473 | output << "Already Visited Bonds:\t"; | 
|---|
| 474 | for (int i = 1; i <= list[0]; i++) | 
|---|
| 475 | output << list[i] << "  "; | 
|---|
| 476 | LOG(0, output.str()); | 
|---|
| 477 | } | 
|---|
| 478 |  | 
|---|
| 479 | const std::map<atomId_t, int >& CyclicStructureAnalysis::getMinimumRingSize() const | 
|---|
| 480 | { | 
|---|
| 481 | return MinimumRingSize; | 
|---|
| 482 | } | 
|---|