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