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