| [cee0b57] | 1 | /* | 
|---|
|  | 2 | * molecule_graph.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Oct 5, 2009 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [f66195] | 8 | #include "atom.hpp" | 
|---|
|  | 9 | #include "bond.hpp" | 
|---|
| [b70721] | 10 | #include "bondgraph.hpp" | 
|---|
| [cee0b57] | 11 | #include "config.hpp" | 
|---|
| [f66195] | 12 | #include "element.hpp" | 
|---|
|  | 13 | #include "helpers.hpp" | 
|---|
| [b8b75d] | 14 | #include "linkedcell.hpp" | 
|---|
| [f66195] | 15 | #include "lists.hpp" | 
|---|
| [e138de] | 16 | #include "log.hpp" | 
|---|
| [cee0b57] | 17 | #include "memoryallocator.hpp" | 
|---|
|  | 18 | #include "molecule.hpp" | 
|---|
|  | 19 |  | 
|---|
| [9eefda] | 20 | struct BFSAccounting | 
|---|
|  | 21 | { | 
|---|
|  | 22 | atom **PredecessorList; | 
|---|
|  | 23 | int *ShortestPathList; | 
|---|
|  | 24 | enum Shading *ColorList; | 
|---|
|  | 25 | class StackClass<atom *> *BFSStack; | 
|---|
|  | 26 | class StackClass<atom *> *TouchedStack; | 
|---|
|  | 27 | int AtomCount; | 
|---|
|  | 28 | int BondOrder; | 
|---|
|  | 29 | atom *Root; | 
|---|
|  | 30 | bool BackStepping; | 
|---|
|  | 31 | int CurrentGraphNr; | 
|---|
|  | 32 | int ComponentNr; | 
|---|
|  | 33 | }; | 
|---|
| [cee0b57] | 34 |  | 
|---|
| [9eefda] | 35 | /** Accounting data for Depth First Search. | 
|---|
|  | 36 | */ | 
|---|
|  | 37 | struct DFSAccounting | 
|---|
|  | 38 | { | 
|---|
|  | 39 | class StackClass<atom *> *AtomStack; | 
|---|
|  | 40 | class StackClass<bond *> *BackEdgeStack; | 
|---|
|  | 41 | int CurrentGraphNr; | 
|---|
|  | 42 | int ComponentNumber; | 
|---|
|  | 43 | atom *Root; | 
|---|
|  | 44 | bool BackStepping; | 
|---|
|  | 45 | }; | 
|---|
|  | 46 |  | 
|---|
|  | 47 | /************************************* Functions for class molecule *********************************/ | 
|---|
| [cee0b57] | 48 |  | 
|---|
|  | 49 | /** Creates an adjacency list of the molecule. | 
|---|
|  | 50 | * We obtain an outside file with the indices of atoms which are bondmembers. | 
|---|
|  | 51 | */ | 
|---|
| [e138de] | 52 | void molecule::CreateAdjacencyListFromDbondFile(ifstream *input) | 
|---|
| [cee0b57] | 53 | { | 
|---|
|  | 54 |  | 
|---|
|  | 55 | // 1 We will parse bonds out of the dbond file created by tremolo. | 
|---|
| [44a59b] | 56 | int atom1, atom2; | 
|---|
|  | 57 | atom *Walker, *OtherWalker; | 
|---|
|  | 58 |  | 
|---|
| [9eefda] | 59 | if (!input) { | 
|---|
| [e138de] | 60 | Log() << Verbose(1) << "Opening silica failed \n"; | 
|---|
| [44a59b] | 61 | }; | 
|---|
|  | 62 |  | 
|---|
|  | 63 | *input >> ws >> atom1; | 
|---|
|  | 64 | *input >> ws >> atom2; | 
|---|
| [e138de] | 65 | Log() << Verbose(1) << "Scanning file\n"; | 
|---|
| [44a59b] | 66 | while (!input->eof()) // Check whether we read everything already | 
|---|
|  | 67 | { | 
|---|
|  | 68 | *input >> ws >> atom1; | 
|---|
|  | 69 | *input >> ws >> atom2; | 
|---|
|  | 70 |  | 
|---|
| [9eefda] | 71 | if (atom2 < atom1) //Sort indices of atoms in order | 
|---|
| [44a59b] | 72 | flip(atom1, atom2); | 
|---|
| [9eefda] | 73 | Walker = FindAtom(atom1); | 
|---|
|  | 74 | OtherWalker = FindAtom(atom2); | 
|---|
| [44a59b] | 75 | AddBond(Walker, OtherWalker); //Add the bond between the two atoms with respective indices. | 
|---|
|  | 76 | } | 
|---|
| [9eefda] | 77 | } | 
|---|
|  | 78 | ; | 
|---|
| [cee0b57] | 79 |  | 
|---|
|  | 80 | /** Creates an adjacency list of the molecule. | 
|---|
|  | 81 | * Generally, we use the CSD approach to bond recognition, that is the the distance | 
|---|
|  | 82 | * between two atoms A and B must be within [Rcov(A)+Rcov(B)-t,Rcov(A)+Rcov(B)+t] with | 
|---|
|  | 83 | * a threshold t = 0.4 Angstroem. | 
|---|
|  | 84 | * To make it O(N log N) the function uses the linked-cell technique as follows: | 
|---|
|  | 85 | * The procedure is step-wise: | 
|---|
|  | 86 | *  -# Remove every bond in list | 
|---|
|  | 87 | *  -# Count the atoms in the molecule with CountAtoms() | 
|---|
|  | 88 | *  -# partition cell into smaller linked cells of size \a bonddistance | 
|---|
|  | 89 | *  -# put each atom into its corresponding cell | 
|---|
|  | 90 | *  -# go through every cell, check the atoms therein against all possible bond partners in the 27 adjacent cells, add bond if true | 
|---|
|  | 91 | *  -# correct the bond degree iteratively (single->double->triple bond) | 
|---|
|  | 92 | *  -# finally print the bond list to \a *out if desired | 
|---|
|  | 93 | * \param *out out stream for printing the matrix, NULL if no output | 
|---|
|  | 94 | * \param bonddistance length of linked cells (i.e. maximum minimal length checked) | 
|---|
|  | 95 | * \param IsAngstroem whether coordinate system is gauged to Angstroem or Bohr radii | 
|---|
| [b70721] | 96 | * \param *minmaxdistance function to give upper and lower bound on whether particle is bonded to some other | 
|---|
|  | 97 | * \param *BG BondGraph with the member function above or NULL, if just standard covalent should be used. | 
|---|
| [cee0b57] | 98 | */ | 
|---|
| [e138de] | 99 | void molecule::CreateAdjacencyList(double bonddistance, bool IsAngstroem, void (BondGraph::*minmaxdistance)(BondedParticle * const , BondedParticle * const , double &, double &, bool), BondGraph *BG) | 
|---|
| [cee0b57] | 100 | { | 
|---|
| [b8b75d] | 101 | atom *Walker = NULL; | 
|---|
|  | 102 | atom *OtherWalker = NULL; | 
|---|
|  | 103 | atom **AtomMap = NULL; | 
|---|
|  | 104 | int n[NDIM]; | 
|---|
| [b70721] | 105 | double MinDistance, MaxDistance; | 
|---|
| [b8b75d] | 106 | LinkedCell *LC = NULL; | 
|---|
| [b70721] | 107 | bool free_BG = false; | 
|---|
|  | 108 |  | 
|---|
|  | 109 | if (BG == NULL) { | 
|---|
|  | 110 | BG = new BondGraph(IsAngstroem); | 
|---|
|  | 111 | free_BG = true; | 
|---|
|  | 112 | } | 
|---|
| [cee0b57] | 113 |  | 
|---|
|  | 114 | BondDistance = bonddistance; // * ((IsAngstroem) ? 1. : 1./AtomicLengthToAngstroem); | 
|---|
| [e138de] | 115 | Log() << Verbose(0) << "Begin of CreateAdjacencyList." << endl; | 
|---|
| [cee0b57] | 116 | // remove every bond from the list | 
|---|
| [ae38fb] | 117 | bond *Binder = NULL; | 
|---|
|  | 118 | while (last->previous != first) { | 
|---|
|  | 119 | Binder = last->previous; | 
|---|
|  | 120 | Binder->leftatom->UnregisterBond(Binder); | 
|---|
|  | 121 | Binder->rightatom->UnregisterBond(Binder); | 
|---|
|  | 122 | removewithoutcheck(Binder); | 
|---|
| [cee0b57] | 123 | } | 
|---|
| [3c349b] | 124 | BondCount = 0; | 
|---|
| [cee0b57] | 125 |  | 
|---|
|  | 126 | // count atoms in molecule = dimension of matrix (also give each unique name and continuous numbering) | 
|---|
| [e138de] | 127 | CountAtoms(); | 
|---|
|  | 128 | Log() << Verbose(1) << "AtomCount " << AtomCount << " and bonddistance is " << bonddistance << "." << endl; | 
|---|
| [cee0b57] | 129 |  | 
|---|
| [34e0013] | 130 | if ((AtomCount > 1) && (bonddistance > 1.)) { | 
|---|
| [e138de] | 131 | Log() << Verbose(2) << "Creating Linked Cell structure ... " << endl; | 
|---|
| [b8b75d] | 132 | LC = new LinkedCell(this, bonddistance); | 
|---|
| [cee0b57] | 133 |  | 
|---|
| [b8b75d] | 134 | // create a list to map Tesselpoint::nr to atom * | 
|---|
| [e138de] | 135 | Log() << Verbose(2) << "Creating TesselPoint to atom map ... " << endl; | 
|---|
| [7218f8] | 136 | AtomMap = Calloc<atom *> (AtomCount, "molecule::CreateAdjacencyList - **AtomCount"); | 
|---|
| [cee0b57] | 137 | Walker = start; | 
|---|
| [b8b75d] | 138 | while (Walker->next != end) { | 
|---|
| [cee0b57] | 139 | Walker = Walker->next; | 
|---|
| [b8b75d] | 140 | AtomMap[Walker->nr] = Walker; | 
|---|
| [cee0b57] | 141 | } | 
|---|
|  | 142 |  | 
|---|
|  | 143 | // 3a. go through every cell | 
|---|
| [e138de] | 144 | Log() << Verbose(2) << "Celling ... " << endl; | 
|---|
| [b8b75d] | 145 | for (LC->n[0] = 0; LC->n[0] < LC->N[0]; LC->n[0]++) | 
|---|
|  | 146 | for (LC->n[1] = 0; LC->n[1] < LC->N[1]; LC->n[1]++) | 
|---|
|  | 147 | for (LC->n[2] = 0; LC->n[2] < LC->N[2]; LC->n[2]++) { | 
|---|
| [776b64] | 148 | const LinkedNodes *List = LC->GetCurrentCell(); | 
|---|
| [e138de] | 149 | //Log() << Verbose(2) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << " containing " << List->size() << " points." << endl; | 
|---|
| [b8b75d] | 150 | if (List != NULL) { | 
|---|
| [776b64] | 151 | for (LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) { | 
|---|
| [b8b75d] | 152 | Walker = AtomMap[(*Runner)->nr]; | 
|---|
| [e138de] | 153 | //Log() << Verbose(0) << "Current Atom is " << *Walker << "." << endl; | 
|---|
| [cee0b57] | 154 | // 3c. check for possible bond between each atom in this and every one in the 27 cells | 
|---|
| [9eefda] | 155 | for (n[0] = -1; n[0] <= 1; n[0]++) | 
|---|
|  | 156 | for (n[1] = -1; n[1] <= 1; n[1]++) | 
|---|
|  | 157 | for (n[2] = -1; n[2] <= 1; n[2]++) { | 
|---|
| [776b64] | 158 | const LinkedNodes *OtherList = LC->GetRelativeToCurrentCell(n); | 
|---|
| [e138de] | 159 | //Log() << Verbose(2) << "Current relative cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << " containing " << List->size() << " points." << endl; | 
|---|
| [b8b75d] | 160 | if (OtherList != NULL) { | 
|---|
| [776b64] | 161 | for (LinkedNodes::const_iterator OtherRunner = OtherList->begin(); OtherRunner != OtherList->end(); OtherRunner++) { | 
|---|
| [b8b75d] | 162 | if ((*OtherRunner)->nr > Walker->nr) { | 
|---|
|  | 163 | OtherWalker = AtomMap[(*OtherRunner)->nr]; | 
|---|
| [e138de] | 164 | //Log() << Verbose(1) << "Checking distance " << OtherWalker->x.PeriodicDistanceSquared(&(Walker->x), cell_size) << " against typical bond length of " << bonddistance*bonddistance << "." << endl; | 
|---|
| [b70721] | 165 | (BG->*minmaxdistance)(Walker, OtherWalker, MinDistance, MaxDistance, IsAngstroem); | 
|---|
|  | 166 | const double distance = OtherWalker->x.PeriodicDistanceSquared(&(Walker->x), cell_size); | 
|---|
|  | 167 | const bool status = (distance <= MaxDistance * MaxDistance) && (distance >= MinDistance * MinDistance); | 
|---|
|  | 168 | if ((OtherWalker->father->nr > Walker->father->nr) && (status)) { // create bond if distance is smaller | 
|---|
| [e138de] | 169 | //Log() << Verbose(1) << "Adding Bond between " << *Walker << " and " << *OtherWalker << " in distance " << sqrt(distance) << "." << endl; | 
|---|
| [9eefda] | 170 | AddBond(Walker->father, OtherWalker->father, 1); // also increases molecule::BondCount | 
|---|
| [b8b75d] | 171 | } else { | 
|---|
| [e138de] | 172 | //Log() << Verbose(1) << "Not Adding: Wrong label order or distance too great." << endl; | 
|---|
| [b8b75d] | 173 | } | 
|---|
| [cee0b57] | 174 | } | 
|---|
|  | 175 | } | 
|---|
|  | 176 | } | 
|---|
|  | 177 | } | 
|---|
|  | 178 | } | 
|---|
|  | 179 | } | 
|---|
|  | 180 | } | 
|---|
| [b8b75d] | 181 | Free(&AtomMap); | 
|---|
| [9eefda] | 182 | delete (LC); | 
|---|
| [e138de] | 183 | Log() << Verbose(1) << "I detected " << BondCount << " bonds in the molecule with distance " << BondDistance << "." << endl; | 
|---|
| [cee0b57] | 184 |  | 
|---|
| [b8b75d] | 185 | // correct bond degree by comparing valence and bond degree | 
|---|
| [e138de] | 186 | Log() << Verbose(2) << "Correcting bond degree ... " << endl; | 
|---|
|  | 187 | CorrectBondDegree(); | 
|---|
| [cee0b57] | 188 |  | 
|---|
| [b8b75d] | 189 | // output bonds for debugging (if bond chain list was correctly installed) | 
|---|
| [e138de] | 190 | ActOnAllAtoms( &atom::OutputBondOfAtom ); | 
|---|
| [b8b75d] | 191 | } else | 
|---|
| [e138de] | 192 | Log() << Verbose(1) << "AtomCount is " << AtomCount << ", thus no bonds, no connections!." << endl; | 
|---|
|  | 193 | Log() << Verbose(0) << "End of CreateAdjacencyList." << endl; | 
|---|
| [b70721] | 194 | if (free_BG) | 
|---|
|  | 195 | delete(BG); | 
|---|
| [9eefda] | 196 | } | 
|---|
|  | 197 | ; | 
|---|
| [cee0b57] | 198 |  | 
|---|
| [b8b75d] | 199 | /** Prints a list of all bonds to \a *out. | 
|---|
|  | 200 | * \param output stream | 
|---|
|  | 201 | */ | 
|---|
| [e138de] | 202 | void molecule::OutputBondsList() const | 
|---|
| [b8b75d] | 203 | { | 
|---|
| [e138de] | 204 | Log() << Verbose(1) << endl << "From contents of bond chain list:"; | 
|---|
| [b8b75d] | 205 | bond *Binder = first; | 
|---|
| [9eefda] | 206 | while (Binder->next != last) { | 
|---|
| [b8b75d] | 207 | Binder = Binder->next; | 
|---|
| [e138de] | 208 | Log() << Verbose(0) << *Binder << "\t" << endl; | 
|---|
| [b8b75d] | 209 | } | 
|---|
| [e138de] | 210 | Log() << Verbose(0) << endl; | 
|---|
| [9eefda] | 211 | } | 
|---|
|  | 212 | ; | 
|---|
| [cee0b57] | 213 |  | 
|---|
| [b8b75d] | 214 | /** correct bond degree by comparing valence and bond degree. | 
|---|
|  | 215 | * correct Bond degree of each bond by checking both bond partners for a mismatch between valence and current sum of bond degrees, | 
|---|
|  | 216 | * iteratively increase the one first where the other bond partner has the fewest number of bonds (i.e. in general bonds oxygene | 
|---|
|  | 217 | * preferred over carbon bonds). Beforehand, we had picked the first mismatching partner, which lead to oxygenes with single instead of | 
|---|
|  | 218 | * double bonds as was expected. | 
|---|
|  | 219 | * \param *out output stream for debugging | 
|---|
|  | 220 | * \return number of bonds that could not be corrected | 
|---|
|  | 221 | */ | 
|---|
| [e138de] | 222 | int molecule::CorrectBondDegree() const | 
|---|
| [b8b75d] | 223 | { | 
|---|
| [99593f] | 224 | int No = 0, OldNo = -1; | 
|---|
| [b8b75d] | 225 |  | 
|---|
|  | 226 | if (BondCount != 0) { | 
|---|
| [e138de] | 227 | Log() << Verbose(1) << "Correcting Bond degree of each bond ... " << endl; | 
|---|
| [b8b75d] | 228 | do { | 
|---|
| [99593f] | 229 | OldNo = No; | 
|---|
| [e138de] | 230 | No = SumPerAtom( &atom::CorrectBondDegree ); | 
|---|
| [99593f] | 231 | } while (OldNo != No); | 
|---|
| [e138de] | 232 | Log() << Verbose(0) << " done." << endl; | 
|---|
| [b8b75d] | 233 | } else { | 
|---|
| [e138de] | 234 | Log() << Verbose(1) << "BondCount is " << BondCount << ", no bonds between any of the " << AtomCount << " atoms." << endl; | 
|---|
| [b8b75d] | 235 | } | 
|---|
| [e138de] | 236 | Log() << Verbose(0) << No << " bonds could not be corrected." << endl; | 
|---|
| [cee0b57] | 237 |  | 
|---|
| [266237] | 238 | return (No); | 
|---|
| [9eefda] | 239 | } | 
|---|
|  | 240 | ; | 
|---|
| [cee0b57] | 241 |  | 
|---|
|  | 242 | /** Counts all cyclic bonds and returns their number. | 
|---|
|  | 243 | * \note Hydrogen bonds can never by cyclic, thus no check for that | 
|---|
|  | 244 | * \param *out output stream for debugging | 
|---|
|  | 245 | * \return number opf cyclic bonds | 
|---|
|  | 246 | */ | 
|---|
| [e138de] | 247 | int molecule::CountCyclicBonds() | 
|---|
| [cee0b57] | 248 | { | 
|---|
| [266237] | 249 | NoCyclicBonds = 0; | 
|---|
| [cee0b57] | 250 | int *MinimumRingSize = NULL; | 
|---|
|  | 251 | MoleculeLeafClass *Subgraphs = NULL; | 
|---|
|  | 252 | class StackClass<bond *> *BackEdgeStack = NULL; | 
|---|
|  | 253 | bond *Binder = first; | 
|---|
|  | 254 | if ((Binder->next != last) && (Binder->next->Type == Undetermined)) { | 
|---|
| [e138de] | 255 | Log() << Verbose(0) << "No Depth-First-Search analysis performed so far, calling ..." << endl; | 
|---|
|  | 256 | Subgraphs = DepthFirstSearchAnalysis(BackEdgeStack); | 
|---|
| [cee0b57] | 257 | while (Subgraphs->next != NULL) { | 
|---|
|  | 258 | Subgraphs = Subgraphs->next; | 
|---|
| [9eefda] | 259 | delete (Subgraphs->previous); | 
|---|
| [cee0b57] | 260 | } | 
|---|
| [9eefda] | 261 | delete (Subgraphs); | 
|---|
|  | 262 | delete[] (MinimumRingSize); | 
|---|
| [cee0b57] | 263 | } | 
|---|
| [9eefda] | 264 | while (Binder->next != last) { | 
|---|
| [cee0b57] | 265 | Binder = Binder->next; | 
|---|
|  | 266 | if (Binder->Cyclic) | 
|---|
| [266237] | 267 | NoCyclicBonds++; | 
|---|
| [cee0b57] | 268 | } | 
|---|
| [9eefda] | 269 | delete (BackEdgeStack); | 
|---|
| [266237] | 270 | return NoCyclicBonds; | 
|---|
| [9eefda] | 271 | } | 
|---|
|  | 272 | ; | 
|---|
| [b8b75d] | 273 |  | 
|---|
| [cee0b57] | 274 | /** Returns Shading as a char string. | 
|---|
|  | 275 | * \param color the Shading | 
|---|
|  | 276 | * \return string of the flag | 
|---|
|  | 277 | */ | 
|---|
| [fa649a] | 278 | string molecule::GetColor(enum Shading color) const | 
|---|
| [cee0b57] | 279 | { | 
|---|
| [9eefda] | 280 | switch (color) { | 
|---|
| [cee0b57] | 281 | case white: | 
|---|
|  | 282 | return "white"; | 
|---|
|  | 283 | break; | 
|---|
|  | 284 | case lightgray: | 
|---|
|  | 285 | return "lightgray"; | 
|---|
|  | 286 | break; | 
|---|
|  | 287 | case darkgray: | 
|---|
|  | 288 | return "darkgray"; | 
|---|
|  | 289 | break; | 
|---|
|  | 290 | case black: | 
|---|
|  | 291 | return "black"; | 
|---|
|  | 292 | break; | 
|---|
|  | 293 | default: | 
|---|
|  | 294 | return "uncolored"; | 
|---|
|  | 295 | break; | 
|---|
|  | 296 | }; | 
|---|
| [9eefda] | 297 | } | 
|---|
|  | 298 | ; | 
|---|
| [cee0b57] | 299 |  | 
|---|
| [9eefda] | 300 | /** Sets atom::GraphNr and atom::LowpointNr to BFSAccounting::CurrentGraphNr. | 
|---|
|  | 301 | * \param *out output stream for debugging | 
|---|
|  | 302 | * \param *Walker current node | 
|---|
|  | 303 | * \param &BFS structure with accounting data for BFS | 
|---|
|  | 304 | */ | 
|---|
| [e138de] | 305 | void DepthFirstSearchAnalysis_SetWalkersGraphNr(atom *&Walker, struct DFSAccounting &DFS) | 
|---|
| [174e0e] | 306 | { | 
|---|
| [9eefda] | 307 | if (!DFS.BackStepping) { // if we don't just return from (8) | 
|---|
|  | 308 | Walker->GraphNr = DFS.CurrentGraphNr; | 
|---|
|  | 309 | Walker->LowpointNr = DFS.CurrentGraphNr; | 
|---|
| [e138de] | 310 | Log() << Verbose(1) << "Setting Walker[" << Walker->Name << "]'s number to " << Walker->GraphNr << " with Lowpoint " << Walker->LowpointNr << "." << endl; | 
|---|
| [9eefda] | 311 | DFS.AtomStack->Push(Walker); | 
|---|
|  | 312 | DFS.CurrentGraphNr++; | 
|---|
| [174e0e] | 313 | } | 
|---|
| [9eefda] | 314 | } | 
|---|
|  | 315 | ; | 
|---|
| [174e0e] | 316 |  | 
|---|
| [9eefda] | 317 | /** During DFS goes along unvisited bond and touches other atom. | 
|---|
|  | 318 | * Sets bond::type, if | 
|---|
|  | 319 | *  -# BackEdge: set atom::LowpointNr and push on \a BackEdgeStack | 
|---|
|  | 320 | *  -# TreeEgde: set atom::Ancestor and continue with Walker along this edge | 
|---|
|  | 321 | * Continue until molecule::FindNextUnused() finds no more unused bonds. | 
|---|
|  | 322 | * \param *out output stream for debugging | 
|---|
|  | 323 | * \param *mol molecule with atoms and finding unused bonds | 
|---|
|  | 324 | * \param *&Binder current edge | 
|---|
|  | 325 | * \param &DFS DFS accounting data | 
|---|
|  | 326 | */ | 
|---|
| [e138de] | 327 | void DepthFirstSearchAnalysis_ProbeAlongUnusedBond(const molecule * const mol, atom *&Walker, bond *&Binder, struct DFSAccounting &DFS) | 
|---|
| [174e0e] | 328 | { | 
|---|
|  | 329 | atom *OtherAtom = NULL; | 
|---|
|  | 330 |  | 
|---|
|  | 331 | do { // (3) if Walker has no unused egdes, go to (5) | 
|---|
| [9eefda] | 332 | DFS.BackStepping = false; // reset backstepping flag for (8) | 
|---|
| [174e0e] | 333 | if (Binder == NULL) // if we don't just return from (11), Binder is already set to next unused | 
|---|
|  | 334 | Binder = mol->FindNextUnused(Walker); | 
|---|
|  | 335 | if (Binder == NULL) | 
|---|
|  | 336 | break; | 
|---|
| [e138de] | 337 | Log() << Verbose(2) << "Current Unused Bond is " << *Binder << "." << endl; | 
|---|
| [174e0e] | 338 | // (4) Mark Binder used, ... | 
|---|
|  | 339 | Binder->MarkUsed(black); | 
|---|
|  | 340 | OtherAtom = Binder->GetOtherAtom(Walker); | 
|---|
| [e138de] | 341 | Log() << Verbose(2) << "(4) OtherAtom is " << OtherAtom->Name << "." << endl; | 
|---|
| [174e0e] | 342 | if (OtherAtom->GraphNr != -1) { | 
|---|
|  | 343 | // (4a) ... if "other" atom has been visited (GraphNr != 0), set lowpoint to minimum of both, go to (3) | 
|---|
|  | 344 | Binder->Type = BackEdge; | 
|---|
| [9eefda] | 345 | DFS.BackEdgeStack->Push(Binder); | 
|---|
|  | 346 | Walker->LowpointNr = (Walker->LowpointNr < OtherAtom->GraphNr) ? Walker->LowpointNr : OtherAtom->GraphNr; | 
|---|
| [e138de] | 347 | Log() << Verbose(3) << "(4a) Visited: Setting Lowpoint of Walker[" << Walker->Name << "] to " << Walker->LowpointNr << "." << endl; | 
|---|
| [174e0e] | 348 | } else { | 
|---|
|  | 349 | // (4b) ... otherwise set OtherAtom as Ancestor of Walker and Walker as OtherAtom, go to (2) | 
|---|
|  | 350 | Binder->Type = TreeEdge; | 
|---|
|  | 351 | OtherAtom->Ancestor = Walker; | 
|---|
|  | 352 | Walker = OtherAtom; | 
|---|
| [e138de] | 353 | Log() << Verbose(3) << "(4b) Not Visited: OtherAtom[" << OtherAtom->Name << "]'s Ancestor is now " << OtherAtom->Ancestor->Name << ", Walker is OtherAtom " << OtherAtom->Name << "." << endl; | 
|---|
| [174e0e] | 354 | break; | 
|---|
|  | 355 | } | 
|---|
|  | 356 | Binder = NULL; | 
|---|
| [9eefda] | 357 | } while (1); // (3) | 
|---|
|  | 358 | } | 
|---|
|  | 359 | ; | 
|---|
| [174e0e] | 360 |  | 
|---|
| [9eefda] | 361 | /** Checks whether we have a new component. | 
|---|
|  | 362 | * if atom::LowpointNr of \a *&Walker is greater than atom::GraphNr of its atom::Ancestor, we have a new component. | 
|---|
|  | 363 | * Meaning that if we touch upon a node who suddenly has a smaller atom::LowpointNr than its ancestor, then we | 
|---|
|  | 364 | * have a found a new branch in the graph tree. | 
|---|
|  | 365 | * \param *out output stream for debugging | 
|---|
|  | 366 | * \param *mol molecule with atoms and finding unused bonds | 
|---|
|  | 367 | * \param *&Walker current node | 
|---|
|  | 368 | * \param &DFS DFS accounting data | 
|---|
|  | 369 | */ | 
|---|
| [e138de] | 370 | void DepthFirstSearchAnalysis_CheckForaNewComponent(const molecule * const mol, atom *&Walker, struct DFSAccounting &DFS, MoleculeLeafClass *&LeafWalker) | 
|---|
| [174e0e] | 371 | { | 
|---|
|  | 372 | atom *OtherAtom = NULL; | 
|---|
|  | 373 |  | 
|---|
|  | 374 | // (5) if Ancestor of Walker is ... | 
|---|
| [e138de] | 375 | Log() << Verbose(1) << "(5) Number of Walker[" << Walker->Name << "]'s Ancestor[" << Walker->Ancestor->Name << "] is " << Walker->Ancestor->GraphNr << "." << endl; | 
|---|
| [174e0e] | 376 |  | 
|---|
| [9eefda] | 377 | if (Walker->Ancestor->GraphNr != DFS.Root->GraphNr) { | 
|---|
| [174e0e] | 378 | // (6)  (Ancestor of Walker is not Root) | 
|---|
|  | 379 | if (Walker->LowpointNr < Walker->Ancestor->GraphNr) { | 
|---|
|  | 380 | // (6a) set Ancestor's Lowpoint number to minimum of of its Ancestor and itself, go to Step(8) | 
|---|
|  | 381 | Walker->Ancestor->LowpointNr = (Walker->Ancestor->LowpointNr < Walker->LowpointNr) ? Walker->Ancestor->LowpointNr : Walker->LowpointNr; | 
|---|
| [e138de] | 382 | Log() << Verbose(2) << "(6) Setting Walker[" << Walker->Name << "]'s Ancestor[" << Walker->Ancestor->Name << "]'s Lowpoint to " << Walker->Ancestor->LowpointNr << "." << endl; | 
|---|
| [174e0e] | 383 | } else { | 
|---|
|  | 384 | // (7) (Ancestor of Walker is a separating vertex, remove all from stack till Walker (including), these and Ancestor form a component | 
|---|
|  | 385 | Walker->Ancestor->SeparationVertex = true; | 
|---|
| [e138de] | 386 | Log() << Verbose(2) << "(7) Walker[" << Walker->Name << "]'s Ancestor[" << Walker->Ancestor->Name << "]'s is a separating vertex, creating component." << endl; | 
|---|
| [9eefda] | 387 | mol->SetNextComponentNumber(Walker->Ancestor, DFS.ComponentNumber); | 
|---|
| [e138de] | 388 | Log() << Verbose(3) << "(7) Walker[" << Walker->Name << "]'s Ancestor's Compont is " << DFS.ComponentNumber << "." << endl; | 
|---|
| [9eefda] | 389 | mol->SetNextComponentNumber(Walker, DFS.ComponentNumber); | 
|---|
| [e138de] | 390 | Log() << Verbose(3) << "(7) Walker[" << Walker->Name << "]'s Compont is " << DFS.ComponentNumber << "." << endl; | 
|---|
| [174e0e] | 391 | do { | 
|---|
| [9eefda] | 392 | OtherAtom = DFS.AtomStack->PopLast(); | 
|---|
| [174e0e] | 393 | LeafWalker->Leaf->AddCopyAtom(OtherAtom); | 
|---|
| [9eefda] | 394 | mol->SetNextComponentNumber(OtherAtom, DFS.ComponentNumber); | 
|---|
| [e138de] | 395 | Log() << Verbose(3) << "(7) Other[" << OtherAtom->Name << "]'s Compont is " << DFS.ComponentNumber << "." << endl; | 
|---|
| [174e0e] | 396 | } while (OtherAtom != Walker); | 
|---|
| [9eefda] | 397 | DFS.ComponentNumber++; | 
|---|
| [174e0e] | 398 | } | 
|---|
|  | 399 | // (8) Walker becomes its Ancestor, go to (3) | 
|---|
| [e138de] | 400 | Log() << Verbose(2) << "(8) Walker[" << Walker->Name << "] is now its Ancestor " << Walker->Ancestor->Name << ", backstepping. " << endl; | 
|---|
| [174e0e] | 401 | Walker = Walker->Ancestor; | 
|---|
| [9eefda] | 402 | DFS.BackStepping = true; | 
|---|
| [174e0e] | 403 | } | 
|---|
| [9eefda] | 404 | } | 
|---|
|  | 405 | ; | 
|---|
| [174e0e] | 406 |  | 
|---|
| [9eefda] | 407 | /** Cleans the root stack when we have found a component. | 
|---|
|  | 408 | * If we are not DFSAccounting::BackStepping, then we clear the root stack by putting everything into a | 
|---|
|  | 409 | * component down till we meet DFSAccounting::Root. | 
|---|
|  | 410 | * \param *out output stream for debugging | 
|---|
|  | 411 | * \param *mol molecule with atoms and finding unused bonds | 
|---|
|  | 412 | * \param *&Walker current node | 
|---|
|  | 413 | * \param *&Binder current edge | 
|---|
|  | 414 | * \param &DFS DFS accounting data | 
|---|
|  | 415 | */ | 
|---|
| [e138de] | 416 | void DepthFirstSearchAnalysis_CleanRootStackDownTillWalker(const molecule * const mol, atom *&Walker, bond *&Binder, struct DFSAccounting &DFS, MoleculeLeafClass *&LeafWalker) | 
|---|
| [174e0e] | 417 | { | 
|---|
|  | 418 | atom *OtherAtom = NULL; | 
|---|
|  | 419 |  | 
|---|
| [9eefda] | 420 | if (!DFS.BackStepping) { // coming from (8) want to go to (3) | 
|---|
| [174e0e] | 421 | // (9) remove all from stack till Walker (including), these and Root form a component | 
|---|
| [99593f] | 422 | //DFS.AtomStack->Output(out); | 
|---|
| [9eefda] | 423 | mol->SetNextComponentNumber(DFS.Root, DFS.ComponentNumber); | 
|---|
| [e138de] | 424 | Log() << Verbose(3) << "(9) Root[" << DFS.Root->Name << "]'s Component is " << DFS.ComponentNumber << "." << endl; | 
|---|
| [9eefda] | 425 | mol->SetNextComponentNumber(Walker, DFS.ComponentNumber); | 
|---|
| [e138de] | 426 | Log() << Verbose(3) << "(9) Walker[" << Walker->Name << "]'s Component is " << DFS.ComponentNumber << "." << endl; | 
|---|
| [174e0e] | 427 | do { | 
|---|
| [9eefda] | 428 | OtherAtom = DFS.AtomStack->PopLast(); | 
|---|
| [174e0e] | 429 | LeafWalker->Leaf->AddCopyAtom(OtherAtom); | 
|---|
| [9eefda] | 430 | mol->SetNextComponentNumber(OtherAtom, DFS.ComponentNumber); | 
|---|
| [e138de] | 431 | Log() << Verbose(3) << "(7) Other[" << OtherAtom->Name << "]'s Compont is " << DFS.ComponentNumber << "." << endl; | 
|---|
| [174e0e] | 432 | } while (OtherAtom != Walker); | 
|---|
| [9eefda] | 433 | DFS.ComponentNumber++; | 
|---|
| [174e0e] | 434 |  | 
|---|
|  | 435 | // (11) Root is separation vertex,  set Walker to Root and go to (4) | 
|---|
| [9eefda] | 436 | Walker = DFS.Root; | 
|---|
| [174e0e] | 437 | Binder = mol->FindNextUnused(Walker); | 
|---|
| [e138de] | 438 | Log() << Verbose(1) << "(10) Walker is Root[" << DFS.Root->Name << "], next Unused Bond is " << Binder << "." << endl; | 
|---|
| [174e0e] | 439 | if (Binder != NULL) { // Root is separation vertex | 
|---|
| [e138de] | 440 | Log() << Verbose(1) << "(11) Root is a separation vertex." << endl; | 
|---|
| [174e0e] | 441 | Walker->SeparationVertex = true; | 
|---|
|  | 442 | } | 
|---|
|  | 443 | } | 
|---|
| [9eefda] | 444 | } | 
|---|
|  | 445 | ; | 
|---|
|  | 446 |  | 
|---|
|  | 447 | /** Initializes DFSAccounting structure. | 
|---|
|  | 448 | * \param *out output stream for debugging | 
|---|
|  | 449 | * \param &DFS accounting structure to allocate | 
|---|
| [7218f8] | 450 | * \param *mol molecule with AtomCount, BondCount and all atoms | 
|---|
| [9eefda] | 451 | */ | 
|---|
| [e138de] | 452 | void DepthFirstSearchAnalysis_Init(struct DFSAccounting &DFS, const molecule * const mol) | 
|---|
| [9eefda] | 453 | { | 
|---|
| [7218f8] | 454 | DFS.AtomStack = new StackClass<atom *> (mol->AtomCount); | 
|---|
| [9eefda] | 455 | DFS.CurrentGraphNr = 0; | 
|---|
|  | 456 | DFS.ComponentNumber = 0; | 
|---|
|  | 457 | DFS.BackStepping = false; | 
|---|
| [7218f8] | 458 | mol->ResetAllBondsToUnused(); | 
|---|
|  | 459 | mol->SetAtomValueToValue(-1, &atom::GraphNr); | 
|---|
|  | 460 | mol->ActOnAllAtoms(&atom::InitComponentNr); | 
|---|
|  | 461 | DFS.BackEdgeStack->ClearStack(); | 
|---|
| [9eefda] | 462 | } | 
|---|
|  | 463 | ; | 
|---|
| [174e0e] | 464 |  | 
|---|
| [9eefda] | 465 | /** Free's DFSAccounting structure. | 
|---|
|  | 466 | * \param *out output stream for debugging | 
|---|
|  | 467 | * \param &DFS accounting structure to free | 
|---|
|  | 468 | */ | 
|---|
| [e138de] | 469 | void DepthFirstSearchAnalysis_Finalize(struct DFSAccounting &DFS) | 
|---|
| [9eefda] | 470 | { | 
|---|
|  | 471 | delete (DFS.AtomStack); | 
|---|
| [7218f8] | 472 | // delete (DFS.BackEdgeStack); // DON'T free, see DepthFirstSearchAnalysis(), is returned as allocated | 
|---|
| [9eefda] | 473 | } | 
|---|
|  | 474 | ; | 
|---|
| [174e0e] | 475 |  | 
|---|
| [cee0b57] | 476 | /** Performs a Depth-First search on this molecule. | 
|---|
|  | 477 | * Marks bonds in molecule as cyclic, bridge, ... and atoms as | 
|---|
|  | 478 | * articulations points, ... | 
|---|
|  | 479 | * We use the algorithm from [Even, Graph Algorithms, p.62]. | 
|---|
|  | 480 | * \param *out output stream for debugging | 
|---|
|  | 481 | * \param *&BackEdgeStack NULL pointer to StackClass with all the found back edges, allocated and filled on return | 
|---|
|  | 482 | * \return list of each disconnected subgraph as an individual molecule class structure | 
|---|
|  | 483 | */ | 
|---|
| [e138de] | 484 | MoleculeLeafClass * molecule::DepthFirstSearchAnalysis(class StackClass<bond *> *&BackEdgeStack) const | 
|---|
| [cee0b57] | 485 | { | 
|---|
| [9eefda] | 486 | struct DFSAccounting DFS; | 
|---|
| [cee0b57] | 487 | BackEdgeStack = new StackClass<bond *> (BondCount); | 
|---|
| [9eefda] | 488 | DFS.BackEdgeStack = BackEdgeStack; | 
|---|
| [cee0b57] | 489 | MoleculeLeafClass *SubGraphs = new MoleculeLeafClass(NULL); | 
|---|
|  | 490 | MoleculeLeafClass *LeafWalker = SubGraphs; | 
|---|
| [9eefda] | 491 | int OldGraphNr = 0; | 
|---|
| [174e0e] | 492 | atom *Walker = NULL; | 
|---|
| [cee0b57] | 493 | bond *Binder = NULL; | 
|---|
|  | 494 |  | 
|---|
| [e138de] | 495 | Log() << Verbose(0) << "Begin of DepthFirstSearchAnalysis" << endl; | 
|---|
|  | 496 | DepthFirstSearchAnalysis_Init(DFS, this); | 
|---|
| [cee0b57] | 497 |  | 
|---|
| [7218f8] | 498 | DFS.Root = start->next; | 
|---|
| [9eefda] | 499 | while (DFS.Root != end) { // if there any atoms at all | 
|---|
| [7218f8] | 500 | // (1) mark all edges unused, empty stack, set atom->GraphNr = -1 for all | 
|---|
| [9eefda] | 501 | DFS.AtomStack->ClearStack(); | 
|---|
| [cee0b57] | 502 |  | 
|---|
|  | 503 | // put into new subgraph molecule and add this to list of subgraphs | 
|---|
|  | 504 | LeafWalker = new MoleculeLeafClass(LeafWalker); | 
|---|
|  | 505 | LeafWalker->Leaf = new molecule(elemente); | 
|---|
| [9eefda] | 506 | LeafWalker->Leaf->AddCopyAtom(DFS.Root); | 
|---|
| [cee0b57] | 507 |  | 
|---|
| [9eefda] | 508 | OldGraphNr = DFS.CurrentGraphNr; | 
|---|
|  | 509 | Walker = DFS.Root; | 
|---|
| [cee0b57] | 510 | do { // (10) | 
|---|
|  | 511 | do { // (2) set number and Lowpoint of Atom to i, increase i, push current atom | 
|---|
| [e138de] | 512 | DepthFirstSearchAnalysis_SetWalkersGraphNr(Walker, DFS); | 
|---|
| [174e0e] | 513 |  | 
|---|
| [e138de] | 514 | DepthFirstSearchAnalysis_ProbeAlongUnusedBond(this, Walker, Binder, DFS); | 
|---|
| [174e0e] | 515 |  | 
|---|
| [cee0b57] | 516 | if (Binder == NULL) { | 
|---|
| [e138de] | 517 | Log() << Verbose(2) << "No more Unused Bonds." << endl; | 
|---|
| [cee0b57] | 518 | break; | 
|---|
|  | 519 | } else | 
|---|
|  | 520 | Binder = NULL; | 
|---|
| [9eefda] | 521 | } while (1); // (2) | 
|---|
| [cee0b57] | 522 |  | 
|---|
|  | 523 | // if we came from backstepping, yet there were no more unused bonds, we end up here with no Ancestor, because Walker is Root! Then we are finished! | 
|---|
| [9eefda] | 524 | if ((Walker == DFS.Root) && (Binder == NULL)) | 
|---|
| [cee0b57] | 525 | break; | 
|---|
|  | 526 |  | 
|---|
| [e138de] | 527 | DepthFirstSearchAnalysis_CheckForaNewComponent(this, Walker, DFS, LeafWalker); | 
|---|
| [174e0e] | 528 |  | 
|---|
| [e138de] | 529 | DepthFirstSearchAnalysis_CleanRootStackDownTillWalker(this, Walker, Binder, DFS, LeafWalker); | 
|---|
| [174e0e] | 530 |  | 
|---|
| [9eefda] | 531 | } while ((DFS.BackStepping) || (Binder != NULL)); // (10) halt only if Root has no unused edges | 
|---|
| [cee0b57] | 532 |  | 
|---|
|  | 533 | // From OldGraphNr to CurrentGraphNr ranges an disconnected subgraph | 
|---|
| [e138de] | 534 | Log() << Verbose(0) << "Disconnected subgraph ranges from " << OldGraphNr << " to " << DFS.CurrentGraphNr << "." << endl; | 
|---|
|  | 535 | LeafWalker->Leaf->Output((ofstream *)&cout); | 
|---|
|  | 536 | Log() << Verbose(0) << endl; | 
|---|
| [cee0b57] | 537 |  | 
|---|
|  | 538 | // step on to next root | 
|---|
| [9eefda] | 539 | while ((DFS.Root != end) && (DFS.Root->GraphNr != -1)) { | 
|---|
| [e138de] | 540 | //Log() << Verbose(1) << "Current next subgraph root candidate is " << Root->Name << "." << endl; | 
|---|
| [9eefda] | 541 | if (DFS.Root->GraphNr != -1) // if already discovered, step on | 
|---|
|  | 542 | DFS.Root = DFS.Root->next; | 
|---|
| [cee0b57] | 543 | } | 
|---|
|  | 544 | } | 
|---|
|  | 545 | // set cyclic bond criterium on "same LP" basis | 
|---|
| [266237] | 546 | CyclicBondAnalysis(); | 
|---|
|  | 547 |  | 
|---|
| [e138de] | 548 | OutputGraphInfoPerAtom(); | 
|---|
| [266237] | 549 |  | 
|---|
| [e138de] | 550 | OutputGraphInfoPerBond(); | 
|---|
| [266237] | 551 |  | 
|---|
|  | 552 | // free all and exit | 
|---|
| [e138de] | 553 | DepthFirstSearchAnalysis_Finalize(DFS); | 
|---|
|  | 554 | Log() << Verbose(0) << "End of DepthFirstSearchAnalysis" << endl; | 
|---|
| [266237] | 555 | return SubGraphs; | 
|---|
| [9eefda] | 556 | } | 
|---|
|  | 557 | ; | 
|---|
| [266237] | 558 |  | 
|---|
|  | 559 | /** Scans through all bonds and set bond::Cyclic to true where atom::LowpointNr of both ends is equal: LP criterion. | 
|---|
|  | 560 | */ | 
|---|
| [fa649a] | 561 | void molecule::CyclicBondAnalysis() const | 
|---|
| [266237] | 562 | { | 
|---|
|  | 563 | NoCyclicBonds = 0; | 
|---|
|  | 564 | bond *Binder = first; | 
|---|
| [9eefda] | 565 | while (Binder->next != last) { | 
|---|
| [cee0b57] | 566 | Binder = Binder->next; | 
|---|
|  | 567 | if (Binder->rightatom->LowpointNr == Binder->leftatom->LowpointNr) { // cyclic ?? | 
|---|
|  | 568 | Binder->Cyclic = true; | 
|---|
|  | 569 | NoCyclicBonds++; | 
|---|
|  | 570 | } | 
|---|
|  | 571 | } | 
|---|
| [9eefda] | 572 | } | 
|---|
|  | 573 | ; | 
|---|
| [cee0b57] | 574 |  | 
|---|
| [266237] | 575 | /** Output graph information per atom. | 
|---|
|  | 576 | * \param *out output stream | 
|---|
|  | 577 | */ | 
|---|
| [e138de] | 578 | void molecule::OutputGraphInfoPerAtom() const | 
|---|
| [266237] | 579 | { | 
|---|
| [e138de] | 580 | Log() << Verbose(1) << "Final graph info for each atom is:" << endl; | 
|---|
|  | 581 | ActOnAllAtoms( &atom::OutputGraphInfo ); | 
|---|
| [9eefda] | 582 | } | 
|---|
|  | 583 | ; | 
|---|
| [cee0b57] | 584 |  | 
|---|
| [266237] | 585 | /** Output graph information per bond. | 
|---|
|  | 586 | * \param *out output stream | 
|---|
|  | 587 | */ | 
|---|
| [e138de] | 588 | void molecule::OutputGraphInfoPerBond() const | 
|---|
| [266237] | 589 | { | 
|---|
| [e138de] | 590 | Log() << Verbose(1) << "Final graph info for each bond is:" << endl; | 
|---|
| [266237] | 591 | bond *Binder = first; | 
|---|
| [9eefda] | 592 | while (Binder->next != last) { | 
|---|
| [cee0b57] | 593 | Binder = Binder->next; | 
|---|
| [e138de] | 594 | Log() << Verbose(2) << ((Binder->Type == TreeEdge) ? "TreeEdge " : "BackEdge ") << *Binder << ": <"; | 
|---|
|  | 595 | Log() << Verbose(0) << ((Binder->leftatom->SeparationVertex) ? "SP," : "") << "L" << Binder->leftatom->LowpointNr << " G" << Binder->leftatom->GraphNr << " Comp."; | 
|---|
|  | 596 | Binder->leftatom->OutputComponentNumber(); | 
|---|
|  | 597 | Log() << Verbose(0) << " ===  "; | 
|---|
|  | 598 | Log() << Verbose(0) << ((Binder->rightatom->SeparationVertex) ? "SP," : "") << "L" << Binder->rightatom->LowpointNr << " G" << Binder->rightatom->GraphNr << " Comp."; | 
|---|
|  | 599 | Binder->rightatom->OutputComponentNumber(); | 
|---|
|  | 600 | Log() << Verbose(0) << ">." << endl; | 
|---|
| [cee0b57] | 601 | if (Binder->Cyclic) // cyclic ?? | 
|---|
| [e138de] | 602 | Log() << Verbose(3) << "Lowpoint at each side are equal: CYCLIC!" << endl; | 
|---|
| [cee0b57] | 603 | } | 
|---|
| [9eefda] | 604 | } | 
|---|
|  | 605 | ; | 
|---|
|  | 606 |  | 
|---|
|  | 607 | /** Initialise each vertex as white with no predecessor, empty queue, color Root lightgray. | 
|---|
|  | 608 | * \param *out output stream for debugging | 
|---|
|  | 609 | * \param &BFS accounting structure | 
|---|
|  | 610 | * \param AtomCount number of entries in the array to allocate | 
|---|
|  | 611 | */ | 
|---|
| [e138de] | 612 | void InitializeBFSAccounting(struct BFSAccounting &BFS, int AtomCount) | 
|---|
| [9eefda] | 613 | { | 
|---|
|  | 614 | BFS.AtomCount = AtomCount; | 
|---|
| [7218f8] | 615 | BFS.PredecessorList = Calloc<atom*> (AtomCount, "molecule::BreadthFirstSearchAdd_Init: **PredecessorList"); | 
|---|
| [9eefda] | 616 | BFS.ShortestPathList = Malloc<int> (AtomCount, "molecule::BreadthFirstSearchAdd_Init: *ShortestPathList"); | 
|---|
| [7218f8] | 617 | BFS.ColorList = Calloc<enum Shading> (AtomCount, "molecule::BreadthFirstSearchAdd_Init: *ColorList"); | 
|---|
| [9eefda] | 618 | BFS.BFSStack = new StackClass<atom *> (AtomCount); | 
|---|
|  | 619 |  | 
|---|
| [7218f8] | 620 | for (int i = AtomCount; i--;) | 
|---|
| [9eefda] | 621 | BFS.ShortestPathList[i] = -1; | 
|---|
| [cee0b57] | 622 | }; | 
|---|
|  | 623 |  | 
|---|
| [9eefda] | 624 | /** Free's accounting structure. | 
|---|
|  | 625 | * \param *out output stream for debugging | 
|---|
|  | 626 | * \param &BFS accounting structure | 
|---|
|  | 627 | */ | 
|---|
| [e138de] | 628 | void FinalizeBFSAccounting(struct BFSAccounting &BFS) | 
|---|
| [9eefda] | 629 | { | 
|---|
|  | 630 | Free(&BFS.PredecessorList); | 
|---|
|  | 631 | Free(&BFS.ShortestPathList); | 
|---|
|  | 632 | Free(&BFS.ColorList); | 
|---|
|  | 633 | delete (BFS.BFSStack); | 
|---|
|  | 634 | BFS.AtomCount = 0; | 
|---|
|  | 635 | }; | 
|---|
|  | 636 |  | 
|---|
|  | 637 | /** Clean the accounting structure. | 
|---|
|  | 638 | * \param *out output stream for debugging | 
|---|
|  | 639 | * \param &BFS accounting structure | 
|---|
| [ef9aae] | 640 | */ | 
|---|
| [e138de] | 641 | void CleanBFSAccounting(struct BFSAccounting &BFS) | 
|---|
| [ef9aae] | 642 | { | 
|---|
| [9eefda] | 643 | atom *Walker = NULL; | 
|---|
|  | 644 | while (!BFS.TouchedStack->IsEmpty()) { | 
|---|
|  | 645 | Walker = BFS.TouchedStack->PopFirst(); | 
|---|
|  | 646 | BFS.PredecessorList[Walker->nr] = NULL; | 
|---|
|  | 647 | BFS.ShortestPathList[Walker->nr] = -1; | 
|---|
|  | 648 | BFS.ColorList[Walker->nr] = white; | 
|---|
| [ef9aae] | 649 | } | 
|---|
|  | 650 | }; | 
|---|
|  | 651 |  | 
|---|
| [9eefda] | 652 | /** Resets shortest path list and BFSStack. | 
|---|
|  | 653 | * \param *out output stream for debugging | 
|---|
|  | 654 | * \param *&Walker current node, pushed onto BFSAccounting::BFSStack and BFSAccounting::TouchedStack | 
|---|
|  | 655 | * \param &BFS accounting structure | 
|---|
|  | 656 | */ | 
|---|
| [e138de] | 657 | void ResetBFSAccounting(atom *&Walker, struct BFSAccounting &BFS) | 
|---|
| [ef9aae] | 658 | { | 
|---|
| [9eefda] | 659 | BFS.ShortestPathList[Walker->nr] = 0; | 
|---|
|  | 660 | BFS.BFSStack->ClearStack(); // start with empty BFS stack | 
|---|
|  | 661 | BFS.BFSStack->Push(Walker); | 
|---|
|  | 662 | BFS.TouchedStack->Push(Walker); | 
|---|
| [ef9aae] | 663 | }; | 
|---|
|  | 664 |  | 
|---|
| [9eefda] | 665 | /** Performs a BFS from \a *Root, trying to find the same node and hence a cycle. | 
|---|
|  | 666 | * \param *out output stream for debugging | 
|---|
|  | 667 | * \param *&BackEdge the edge from root that we don't want to move along | 
|---|
|  | 668 | * \param &BFS accounting structure | 
|---|
|  | 669 | */ | 
|---|
| [e138de] | 670 | void CyclicStructureAnalysis_CyclicBFSFromRootToRoot(bond *&BackEdge, struct BFSAccounting &BFS) | 
|---|
| [ef9aae] | 671 | { | 
|---|
|  | 672 | atom *Walker = NULL; | 
|---|
|  | 673 | atom *OtherAtom = NULL; | 
|---|
| [9eefda] | 674 | do { // look for Root | 
|---|
|  | 675 | Walker = BFS.BFSStack->PopFirst(); | 
|---|
| [e138de] | 676 | Log() << Verbose(2) << "Current Walker is " << *Walker << ", we look for SP to Root " << *BFS.Root << "." << endl; | 
|---|
| [ef9aae] | 677 | for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) { | 
|---|
|  | 678 | if ((*Runner) != BackEdge) { // only walk along DFS spanning tree (otherwise we always find SP of one being backedge Binder) | 
|---|
|  | 679 | OtherAtom = (*Runner)->GetOtherAtom(Walker); | 
|---|
| [9eefda] | 680 | #ifdef ADDHYDROGEN | 
|---|
| [ef9aae] | 681 | if (OtherAtom->type->Z != 1) { | 
|---|
| [9eefda] | 682 | #endif | 
|---|
| [e138de] | 683 | Log() << Verbose(2) << "Current OtherAtom is: " << OtherAtom->Name << " for bond " << *(*Runner) << "." << endl; | 
|---|
| [9eefda] | 684 | if (BFS.ColorList[OtherAtom->nr] == white) { | 
|---|
|  | 685 | BFS.TouchedStack->Push(OtherAtom); | 
|---|
|  | 686 | BFS.ColorList[OtherAtom->nr] = lightgray; | 
|---|
|  | 687 | BFS.PredecessorList[OtherAtom->nr] = Walker; // Walker is the predecessor | 
|---|
|  | 688 | BFS.ShortestPathList[OtherAtom->nr] = BFS.ShortestPathList[Walker->nr] + 1; | 
|---|
| [e138de] | 689 | Log() << Verbose(2) << "Coloring OtherAtom " << OtherAtom->Name << " lightgray, its predecessor is " << Walker->Name << " and its Shortest Path is " << BFS.ShortestPathList[OtherAtom->nr] << " egde(s) long." << endl; | 
|---|
| [9eefda] | 690 | //if (BFS.ShortestPathList[OtherAtom->nr] < MinimumRingSize[Walker->GetTrueFather()->nr]) { // Check for maximum distance | 
|---|
| [e138de] | 691 | Log() << Verbose(3) << "Putting OtherAtom into queue." << endl; | 
|---|
| [9eefda] | 692 | BFS.BFSStack->Push(OtherAtom); | 
|---|
|  | 693 | //} | 
|---|
| [ef9aae] | 694 | } else { | 
|---|
| [e138de] | 695 | Log() << Verbose(3) << "Not Adding, has already been visited." << endl; | 
|---|
| [ef9aae] | 696 | } | 
|---|
| [9eefda] | 697 | if (OtherAtom == BFS.Root) | 
|---|
|  | 698 | break; | 
|---|
|  | 699 | #ifdef ADDHYDROGEN | 
|---|
|  | 700 | } else { | 
|---|
| [e138de] | 701 | Log() << Verbose(2) << "Skipping hydrogen atom " << *OtherAtom << "." << endl; | 
|---|
| [9eefda] | 702 | BFS.ColorList[OtherAtom->nr] = black; | 
|---|
|  | 703 | } | 
|---|
|  | 704 | #endif | 
|---|
| [ef9aae] | 705 | } else { | 
|---|
| [e138de] | 706 | Log() << Verbose(2) << "Bond " << *(*Runner) << " not Visiting, is the back edge." << endl; | 
|---|
| [ef9aae] | 707 | } | 
|---|
|  | 708 | } | 
|---|
| [9eefda] | 709 | BFS.ColorList[Walker->nr] = black; | 
|---|
| [e138de] | 710 | Log() << Verbose(1) << "Coloring Walker " << Walker->Name << " black." << endl; | 
|---|
| [9eefda] | 711 | if (OtherAtom == BFS.Root) { // if we have found the root, check whether this cycle wasn't already found beforehand | 
|---|
| [ef9aae] | 712 | // step through predecessor list | 
|---|
|  | 713 | while (OtherAtom != BackEdge->rightatom) { | 
|---|
| [9eefda] | 714 | if (!OtherAtom->GetTrueFather()->IsCyclic) // if one bond in the loop is not marked as cyclic, we haven't found this cycle yet | 
|---|
| [ef9aae] | 715 | break; | 
|---|
|  | 716 | else | 
|---|
| [9eefda] | 717 | OtherAtom = BFS.PredecessorList[OtherAtom->nr]; | 
|---|
| [ef9aae] | 718 | } | 
|---|
|  | 719 | if (OtherAtom == BackEdge->rightatom) { // if each atom in found cycle is cyclic, loop's been found before already | 
|---|
| [e138de] | 720 | Log() << Verbose(3) << "This cycle was already found before, skipping and removing seeker from search." << endl; | 
|---|
| [ef9aae] | 721 | do { | 
|---|
| [9eefda] | 722 | OtherAtom = BFS.TouchedStack->PopLast(); | 
|---|
|  | 723 | if (BFS.PredecessorList[OtherAtom->nr] == Walker) { | 
|---|
| [e138de] | 724 | Log() << Verbose(4) << "Removing " << *OtherAtom << " from lists and stacks." << endl; | 
|---|
| [9eefda] | 725 | BFS.PredecessorList[OtherAtom->nr] = NULL; | 
|---|
|  | 726 | BFS.ShortestPathList[OtherAtom->nr] = -1; | 
|---|
|  | 727 | BFS.ColorList[OtherAtom->nr] = white; | 
|---|
|  | 728 | BFS.BFSStack->RemoveItem(OtherAtom); | 
|---|
| [ef9aae] | 729 | } | 
|---|
| [9eefda] | 730 | } while ((!BFS.TouchedStack->IsEmpty()) && (BFS.PredecessorList[OtherAtom->nr] == NULL)); | 
|---|
|  | 731 | BFS.TouchedStack->Push(OtherAtom); // last was wrongly popped | 
|---|
| [ef9aae] | 732 | OtherAtom = BackEdge->rightatom; // set to not Root | 
|---|
|  | 733 | } else | 
|---|
| [9eefda] | 734 | OtherAtom = BFS.Root; | 
|---|
| [ef9aae] | 735 | } | 
|---|
| [9eefda] | 736 | } while ((!BFS.BFSStack->IsEmpty()) && (OtherAtom != BFS.Root) && (OtherAtom != NULL)); // || (ShortestPathList[OtherAtom->nr] < MinimumRingSize[Walker->GetTrueFather()->nr]))); | 
|---|
| [ef9aae] | 737 | }; | 
|---|
|  | 738 |  | 
|---|
| [9eefda] | 739 | /** Climb back the BFSAccounting::PredecessorList and find cycle members. | 
|---|
|  | 740 | * \param *out output stream for debugging | 
|---|
|  | 741 | * \param *&OtherAtom | 
|---|
|  | 742 | * \param *&BackEdge denotes the edge we did not want to travel along when doing CyclicBFSFromRootToRoot() | 
|---|
|  | 743 | * \param &BFS accounting structure | 
|---|
|  | 744 | * \param *&MinimumRingSize minimum distance from this node possible without encountering oneself, set on return for each atom | 
|---|
|  | 745 | * \param &MinRingSize global minimum distance from one node without encountering oneself, set on return | 
|---|
|  | 746 | */ | 
|---|
| [e138de] | 747 | void CyclicStructureAnalysis_RetrieveCycleMembers(atom *&OtherAtom, bond *&BackEdge, struct BFSAccounting &BFS, int *&MinimumRingSize, int &MinRingSize) | 
|---|
| [ef9aae] | 748 | { | 
|---|
|  | 749 | atom *Walker = NULL; | 
|---|
|  | 750 | int NumCycles = 0; | 
|---|
|  | 751 | int RingSize = -1; | 
|---|
|  | 752 |  | 
|---|
| [9eefda] | 753 | if (OtherAtom == BFS.Root) { | 
|---|
| [ef9aae] | 754 | // now climb back the predecessor list and thus find the cycle members | 
|---|
|  | 755 | NumCycles++; | 
|---|
|  | 756 | RingSize = 1; | 
|---|
| [9eefda] | 757 | BFS.Root->GetTrueFather()->IsCyclic = true; | 
|---|
| [e138de] | 758 | Log() << Verbose(1) << "Found ring contains: "; | 
|---|
| [9eefda] | 759 | Walker = BFS.Root; | 
|---|
| [ef9aae] | 760 | while (Walker != BackEdge->rightatom) { | 
|---|
| [e138de] | 761 | Log() << Verbose(0) << Walker->Name << " <-> "; | 
|---|
| [9eefda] | 762 | Walker = BFS.PredecessorList[Walker->nr]; | 
|---|
| [ef9aae] | 763 | Walker->GetTrueFather()->IsCyclic = true; | 
|---|
|  | 764 | RingSize++; | 
|---|
|  | 765 | } | 
|---|
| [e138de] | 766 | Log() << Verbose(0) << Walker->Name << "  with a length of " << RingSize << "." << endl << endl; | 
|---|
| [ef9aae] | 767 | // walk through all and set MinimumRingSize | 
|---|
| [9eefda] | 768 | Walker = BFS.Root; | 
|---|
| [ef9aae] | 769 | MinimumRingSize[Walker->GetTrueFather()->nr] = RingSize; | 
|---|
|  | 770 | while (Walker != BackEdge->rightatom) { | 
|---|
| [9eefda] | 771 | Walker = BFS.PredecessorList[Walker->nr]; | 
|---|
| [ef9aae] | 772 | if (RingSize < MinimumRingSize[Walker->GetTrueFather()->nr]) | 
|---|
|  | 773 | MinimumRingSize[Walker->GetTrueFather()->nr] = RingSize; | 
|---|
|  | 774 | } | 
|---|
|  | 775 | if ((RingSize < MinRingSize) || (MinRingSize == -1)) | 
|---|
|  | 776 | MinRingSize = RingSize; | 
|---|
|  | 777 | } else { | 
|---|
| [e138de] | 778 | Log() << Verbose(1) << "No ring containing " << *BFS.Root << " with length equal to or smaller than " << MinimumRingSize[Walker->GetTrueFather()->nr] << " found." << endl; | 
|---|
| [ef9aae] | 779 | } | 
|---|
|  | 780 | }; | 
|---|
|  | 781 |  | 
|---|
| [9eefda] | 782 | /** From a given node performs a BFS to touch the next cycle, for whose nodes \a *&MinimumRingSize is set and set it accordingly. | 
|---|
|  | 783 | * \param *out output stream for debugging | 
|---|
|  | 784 | * \param *&Root node to look for closest cycle from, i.e. \a *&MinimumRingSize is set for this node | 
|---|
|  | 785 | * \param *&MinimumRingSize minimum distance from this node possible without encountering oneself, set on return for each atom | 
|---|
|  | 786 | * \param AtomCount number of nodes in graph | 
|---|
|  | 787 | */ | 
|---|
| [e138de] | 788 | void CyclicStructureAnalysis_BFSToNextCycle(atom *&Root, atom *&Walker, int *&MinimumRingSize, int AtomCount) | 
|---|
| [ef9aae] | 789 | { | 
|---|
| [9eefda] | 790 | struct BFSAccounting BFS; | 
|---|
| [ef9aae] | 791 | atom *OtherAtom = Walker; | 
|---|
|  | 792 |  | 
|---|
| [e138de] | 793 | InitializeBFSAccounting(BFS, AtomCount); | 
|---|
| [ef9aae] | 794 |  | 
|---|
| [e138de] | 795 | ResetBFSAccounting(Walker, BFS); | 
|---|
| [9eefda] | 796 | while (OtherAtom != NULL) { // look for Root | 
|---|
|  | 797 | Walker = BFS.BFSStack->PopFirst(); | 
|---|
| [e138de] | 798 | //Log() << Verbose(2) << "Current Walker is " << *Walker << ", we look for SP to Root " << *Root << "." << endl; | 
|---|
| [ef9aae] | 799 | for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) { | 
|---|
| [9eefda] | 800 | // "removed (*Runner) != BackEdge) || " from next if, is u | 
|---|
|  | 801 | if ((Walker->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 | 
|---|
| [ef9aae] | 802 | OtherAtom = (*Runner)->GetOtherAtom(Walker); | 
|---|
| [e138de] | 803 | //Log() << Verbose(2) << "Current OtherAtom is: " << OtherAtom->Name << " for bond " << *Binder << "." << endl; | 
|---|
| [9eefda] | 804 | if (BFS.ColorList[OtherAtom->nr] == white) { | 
|---|
|  | 805 | BFS.TouchedStack->Push(OtherAtom); | 
|---|
|  | 806 | BFS.ColorList[OtherAtom->nr] = lightgray; | 
|---|
|  | 807 | BFS.PredecessorList[OtherAtom->nr] = Walker; // Walker is the predecessor | 
|---|
|  | 808 | BFS.ShortestPathList[OtherAtom->nr] = BFS.ShortestPathList[Walker->nr] + 1; | 
|---|
| [e138de] | 809 | //Log() << Verbose(2) << "Coloring OtherAtom " << OtherAtom->Name << " lightgray, its predecessor is " << Walker->Name << " and its Shortest Path is " << ShortestPathList[OtherAtom->nr] << " egde(s) long." << endl; | 
|---|
| [ef9aae] | 810 | if (OtherAtom->GetTrueFather()->IsCyclic) { // if the other atom is connected to a ring | 
|---|
| [9eefda] | 811 | MinimumRingSize[Root->GetTrueFather()->nr] = BFS.ShortestPathList[OtherAtom->nr] + MinimumRingSize[OtherAtom->GetTrueFather()->nr]; | 
|---|
| [ef9aae] | 812 | OtherAtom = NULL; //break; | 
|---|
|  | 813 | break; | 
|---|
|  | 814 | } else | 
|---|
| [9eefda] | 815 | BFS.BFSStack->Push(OtherAtom); | 
|---|
| [ef9aae] | 816 | } else { | 
|---|
| [e138de] | 817 | //Log() << Verbose(3) << "Not Adding, has already been visited." << endl; | 
|---|
| [ef9aae] | 818 | } | 
|---|
|  | 819 | } else { | 
|---|
| [e138de] | 820 | //Log() << Verbose(3) << "Not Visiting, is a back edge." << endl; | 
|---|
| [ef9aae] | 821 | } | 
|---|
|  | 822 | } | 
|---|
| [9eefda] | 823 | BFS.ColorList[Walker->nr] = black; | 
|---|
| [e138de] | 824 | //Log() << Verbose(1) << "Coloring Walker " << Walker->Name << " black." << endl; | 
|---|
| [ef9aae] | 825 | } | 
|---|
|  | 826 | //CleanAccountingLists(TouchedStack, PredecessorList, ShortestPathList, ColorList); | 
|---|
|  | 827 |  | 
|---|
| [e138de] | 828 | FinalizeBFSAccounting(BFS); | 
|---|
| [9eefda] | 829 | } | 
|---|
|  | 830 | ; | 
|---|
| [ef9aae] | 831 |  | 
|---|
| [9eefda] | 832 | /** All nodes that are not in cycles get assigned a \a *&MinimumRingSizeby BFS to next cycle. | 
|---|
|  | 833 | * \param *out output stream for debugging | 
|---|
|  | 834 | * \param *&MinimumRingSize array with minimum distance without encountering onself for each atom | 
|---|
|  | 835 | * \param &MinRingSize global minium distance | 
|---|
|  | 836 | * \param &NumCyles number of cycles in graph | 
|---|
|  | 837 | * \param *mol molecule with atoms | 
|---|
|  | 838 | */ | 
|---|
| [e138de] | 839 | void CyclicStructureAnalysis_AssignRingSizetoNonCycleMembers(int *&MinimumRingSize, int &MinRingSize, int &NumCycles, const molecule * const mol) | 
|---|
| [ef9aae] | 840 | { | 
|---|
| [9eefda] | 841 | atom *Root = NULL; | 
|---|
| [ef9aae] | 842 | atom *Walker = NULL; | 
|---|
|  | 843 | if (MinRingSize != -1) { // if rings are present | 
|---|
|  | 844 | // go over all atoms | 
|---|
|  | 845 | Root = mol->start; | 
|---|
| [9eefda] | 846 | while (Root->next != mol->end) { | 
|---|
| [ef9aae] | 847 | Root = Root->next; | 
|---|
|  | 848 |  | 
|---|
|  | 849 | if (MinimumRingSize[Root->GetTrueFather()->nr] == mol->AtomCount) { // check whether MinimumRingSize is set, if not BFS to next where it is | 
|---|
|  | 850 | Walker = Root; | 
|---|
|  | 851 |  | 
|---|
| [e138de] | 852 | //Log() << Verbose(1) << "---------------------------------------------------------------------------------------------------------" << endl; | 
|---|
|  | 853 | CyclicStructureAnalysis_BFSToNextCycle(Root, Walker, MinimumRingSize, mol->AtomCount); | 
|---|
| [ef9aae] | 854 |  | 
|---|
|  | 855 | } | 
|---|
| [e138de] | 856 | Log() << Verbose(1) << "Minimum ring size of " << *Root << " is " << MinimumRingSize[Root->GetTrueFather()->nr] << "." << endl; | 
|---|
| [ef9aae] | 857 | } | 
|---|
| [e138de] | 858 | Log() << Verbose(1) << "Minimum ring size is " << MinRingSize << ", over " << NumCycles << " cycles total." << endl; | 
|---|
| [ef9aae] | 859 | } else | 
|---|
| [e138de] | 860 | Log() << Verbose(1) << "No rings were detected in the molecular structure." << endl; | 
|---|
| [9eefda] | 861 | } | 
|---|
|  | 862 | ; | 
|---|
| [ef9aae] | 863 |  | 
|---|
| [cee0b57] | 864 | /** Analyses the cycles found and returns minimum of all cycle lengths. | 
|---|
|  | 865 | * We begin with a list of Back edges found during DepthFirstSearchAnalysis(). We go through this list - one end is the Root, | 
|---|
|  | 866 | * the other our initial Walker - and do a Breadth First Search for the Root. We mark down each Predecessor and as soon as | 
|---|
|  | 867 | * we have found the Root via BFS, we may climb back the closed cycle via the Predecessors. Thereby we mark atoms and bonds | 
|---|
|  | 868 | * as cyclic and print out the cycles. | 
|---|
|  | 869 | * \param *out output stream for debugging | 
|---|
|  | 870 | * \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! | 
|---|
|  | 871 | * \param *&MinimumRingSize contains smallest ring size in molecular structure on return or -1 if no rings were found, if set is maximum search distance | 
|---|
|  | 872 | * \todo BFS from the not-same-LP to find back to starting point of tributary cycle over more than one bond | 
|---|
|  | 873 | */ | 
|---|
| [e138de] | 874 | void molecule::CyclicStructureAnalysis(class StackClass<bond *> * BackEdgeStack, int *&MinimumRingSize) const | 
|---|
| [cee0b57] | 875 | { | 
|---|
| [9eefda] | 876 | struct BFSAccounting BFS; | 
|---|
| [ef9aae] | 877 | atom *Walker = NULL; | 
|---|
|  | 878 | atom *OtherAtom = NULL; | 
|---|
|  | 879 | bond *BackEdge = NULL; | 
|---|
|  | 880 | int NumCycles = 0; | 
|---|
|  | 881 | int MinRingSize = -1; | 
|---|
| [cee0b57] | 882 |  | 
|---|
| [e138de] | 883 | InitializeBFSAccounting(BFS, AtomCount); | 
|---|
| [cee0b57] | 884 |  | 
|---|
| [e138de] | 885 | //Log() << Verbose(1) << "Back edge list - "; | 
|---|
| [99593f] | 886 | //BackEdgeStack->Output(out); | 
|---|
| [cee0b57] | 887 |  | 
|---|
| [e138de] | 888 | Log() << Verbose(1) << "Analysing cycles ... " << endl; | 
|---|
| [cee0b57] | 889 | NumCycles = 0; | 
|---|
|  | 890 | while (!BackEdgeStack->IsEmpty()) { | 
|---|
|  | 891 | BackEdge = BackEdgeStack->PopFirst(); | 
|---|
|  | 892 | // this is the target | 
|---|
| [9eefda] | 893 | BFS.Root = BackEdge->leftatom; | 
|---|
| [cee0b57] | 894 | // this is the source point | 
|---|
|  | 895 | Walker = BackEdge->rightatom; | 
|---|
|  | 896 |  | 
|---|
| [e138de] | 897 | ResetBFSAccounting(Walker, BFS); | 
|---|
| [cee0b57] | 898 |  | 
|---|
| [e138de] | 899 | Log() << Verbose(1) << "---------------------------------------------------------------------------------------------------------" << endl; | 
|---|
| [ef9aae] | 900 | OtherAtom = NULL; | 
|---|
| [e138de] | 901 | CyclicStructureAnalysis_CyclicBFSFromRootToRoot(BackEdge, BFS); | 
|---|
| [cee0b57] | 902 |  | 
|---|
| [e138de] | 903 | CyclicStructureAnalysis_RetrieveCycleMembers(OtherAtom, BackEdge, BFS, MinimumRingSize, MinRingSize); | 
|---|
| [cee0b57] | 904 |  | 
|---|
| [e138de] | 905 | CleanBFSAccounting(BFS); | 
|---|
| [ef9aae] | 906 | } | 
|---|
| [e138de] | 907 | FinalizeBFSAccounting(BFS); | 
|---|
| [ef9aae] | 908 |  | 
|---|
| [e138de] | 909 | CyclicStructureAnalysis_AssignRingSizetoNonCycleMembers(MinimumRingSize, MinRingSize, NumCycles, this); | 
|---|
| [fa649a] | 910 | }; | 
|---|
| [cee0b57] | 911 |  | 
|---|
|  | 912 | /** Sets the next component number. | 
|---|
|  | 913 | * This is O(N) as the number of bonds per atom is bound. | 
|---|
|  | 914 | * \param *vertex atom whose next atom::*ComponentNr is to be set | 
|---|
|  | 915 | * \param nr number to use | 
|---|
|  | 916 | */ | 
|---|
| [fa649a] | 917 | void molecule::SetNextComponentNumber(atom *vertex, int nr) const | 
|---|
| [cee0b57] | 918 | { | 
|---|
| [9eefda] | 919 | size_t i = 0; | 
|---|
| [cee0b57] | 920 | if (vertex != NULL) { | 
|---|
| [9eefda] | 921 | for (; i < vertex->ListOfBonds.size(); i++) { | 
|---|
|  | 922 | if (vertex->ComponentNr[i] == -1) { // check if not yet used | 
|---|
| [cee0b57] | 923 | vertex->ComponentNr[i] = nr; | 
|---|
|  | 924 | break; | 
|---|
| [9eefda] | 925 | } else if (vertex->ComponentNr[i] == nr) // if number is already present, don't add another time | 
|---|
|  | 926 | break; // breaking here will not cause error! | 
|---|
| [cee0b57] | 927 | } | 
|---|
| [e359a8] | 928 | if (i == vertex->ListOfBonds.size()) { | 
|---|
| [e138de] | 929 | eLog() << Verbose(0) << "Error: All Component entries are already occupied!" << endl; | 
|---|
| [e359a8] | 930 | performCriticalExit(); | 
|---|
|  | 931 | } | 
|---|
|  | 932 | } else { | 
|---|
| [e138de] | 933 | eLog() << Verbose(0) << "Error: Given vertex is NULL!" << endl; | 
|---|
| [e359a8] | 934 | performCriticalExit(); | 
|---|
|  | 935 | } | 
|---|
| [9eefda] | 936 | } | 
|---|
|  | 937 | ; | 
|---|
| [cee0b57] | 938 |  | 
|---|
|  | 939 | /** Returns next unused bond for this atom \a *vertex or NULL of none exists. | 
|---|
|  | 940 | * \param *vertex atom to regard | 
|---|
|  | 941 | * \return bond class or NULL | 
|---|
|  | 942 | */ | 
|---|
| [fa649a] | 943 | bond * molecule::FindNextUnused(atom *vertex) const | 
|---|
| [cee0b57] | 944 | { | 
|---|
| [266237] | 945 | for (BondList::const_iterator Runner = vertex->ListOfBonds.begin(); Runner != vertex->ListOfBonds.end(); (++Runner)) | 
|---|
|  | 946 | if ((*Runner)->IsUsed() == white) | 
|---|
| [9eefda] | 947 | return ((*Runner)); | 
|---|
| [cee0b57] | 948 | return NULL; | 
|---|
| [9eefda] | 949 | } | 
|---|
|  | 950 | ; | 
|---|
| [cee0b57] | 951 |  | 
|---|
|  | 952 | /** Resets bond::Used flag of all bonds in this molecule. | 
|---|
|  | 953 | * \return true - success, false - -failure | 
|---|
|  | 954 | */ | 
|---|
| [fa649a] | 955 | void molecule::ResetAllBondsToUnused() const | 
|---|
| [cee0b57] | 956 | { | 
|---|
|  | 957 | bond *Binder = first; | 
|---|
|  | 958 | while (Binder->next != last) { | 
|---|
|  | 959 | Binder = Binder->next; | 
|---|
|  | 960 | Binder->ResetUsed(); | 
|---|
|  | 961 | } | 
|---|
| [9eefda] | 962 | } | 
|---|
|  | 963 | ; | 
|---|
| [cee0b57] | 964 |  | 
|---|
|  | 965 | /** Output a list of flags, stating whether the bond was visited or not. | 
|---|
|  | 966 | * \param *out output stream for debugging | 
|---|
|  | 967 | * \param *list | 
|---|
|  | 968 | */ | 
|---|
| [e138de] | 969 | void OutputAlreadyVisited(int *list) | 
|---|
| [cee0b57] | 970 | { | 
|---|
| [e138de] | 971 | Log() << Verbose(4) << "Already Visited Bonds:\t"; | 
|---|
| [9eefda] | 972 | for (int i = 1; i <= list[0]; i++) | 
|---|
| [e138de] | 973 | Log() << Verbose(0) << list[i] << "  "; | 
|---|
|  | 974 | Log() << Verbose(0) << endl; | 
|---|
| [9eefda] | 975 | } | 
|---|
|  | 976 | ; | 
|---|
| [cee0b57] | 977 |  | 
|---|
|  | 978 | /** Storing the bond structure of a molecule to file. | 
|---|
|  | 979 | * Simply stores Atom::nr and then the Atom::nr of all bond partners per line. | 
|---|
|  | 980 | * \param *out output stream for debugging | 
|---|
|  | 981 | * \param *path path to file | 
|---|
|  | 982 | * \return true - file written successfully, false - writing failed | 
|---|
|  | 983 | */ | 
|---|
| [e138de] | 984 | bool molecule::StoreAdjacencyToFile(char *path) | 
|---|
| [cee0b57] | 985 | { | 
|---|
|  | 986 | ofstream AdjacencyFile; | 
|---|
|  | 987 | stringstream line; | 
|---|
|  | 988 | bool status = true; | 
|---|
|  | 989 |  | 
|---|
|  | 990 | line << path << "/" << FRAGMENTPREFIX << ADJACENCYFILE; | 
|---|
|  | 991 | AdjacencyFile.open(line.str().c_str(), ios::out); | 
|---|
| [e138de] | 992 | Log() << Verbose(1) << "Saving adjacency list ... "; | 
|---|
| [cee0b57] | 993 | if (AdjacencyFile != NULL) { | 
|---|
| [9eefda] | 994 | ActOnAllAtoms(&atom::OutputAdjacency, &AdjacencyFile); | 
|---|
| [cee0b57] | 995 | AdjacencyFile.close(); | 
|---|
| [e138de] | 996 | Log() << Verbose(1) << "done." << endl; | 
|---|
| [cee0b57] | 997 | } else { | 
|---|
| [e138de] | 998 | Log() << Verbose(1) << "failed to open file " << line.str() << "." << endl; | 
|---|
| [cee0b57] | 999 | status = false; | 
|---|
|  | 1000 | } | 
|---|
|  | 1001 |  | 
|---|
|  | 1002 | return status; | 
|---|
| [9eefda] | 1003 | } | 
|---|
|  | 1004 | ; | 
|---|
| [cee0b57] | 1005 |  | 
|---|
| [e138de] | 1006 | bool CheckAdjacencyFileAgainstMolecule_Init(char *path, ifstream &File, int *&CurrentBonds) | 
|---|
| [ba4170] | 1007 | { | 
|---|
|  | 1008 | stringstream filename; | 
|---|
|  | 1009 | filename << path << "/" << FRAGMENTPREFIX << ADJACENCYFILE; | 
|---|
|  | 1010 | File.open(filename.str().c_str(), ios::out); | 
|---|
| [e138de] | 1011 | Log() << Verbose(1) << "Looking at bond structure stored in adjacency file and comparing to present one ... "; | 
|---|
| [ba4170] | 1012 | if (File == NULL) | 
|---|
|  | 1013 | return false; | 
|---|
|  | 1014 |  | 
|---|
|  | 1015 | // allocate storage structure | 
|---|
| [7218f8] | 1016 | CurrentBonds = Calloc<int> (8, "molecule::CheckAdjacencyFileAgainstMolecule - CurrentBonds"); // contains parsed bonds of current atom | 
|---|
| [ba4170] | 1017 | return true; | 
|---|
| [9eefda] | 1018 | } | 
|---|
|  | 1019 | ; | 
|---|
| [ba4170] | 1020 |  | 
|---|
| [e138de] | 1021 | void CheckAdjacencyFileAgainstMolecule_Finalize(ifstream &File, int *&CurrentBonds) | 
|---|
| [ba4170] | 1022 | { | 
|---|
|  | 1023 | File.close(); | 
|---|
|  | 1024 | File.clear(); | 
|---|
|  | 1025 | Free(&CurrentBonds); | 
|---|
| [9eefda] | 1026 | } | 
|---|
|  | 1027 | ; | 
|---|
| [ba4170] | 1028 |  | 
|---|
| [e138de] | 1029 | void CheckAdjacencyFileAgainstMolecule_CompareBonds(bool &status, int &NonMatchNumber, atom *&Walker, size_t &CurrentBondsOfAtom, int AtomNr, int *&CurrentBonds, atom **ListOfAtoms) | 
|---|
| [ba4170] | 1030 | { | 
|---|
|  | 1031 | size_t j = 0; | 
|---|
|  | 1032 | int id = -1; | 
|---|
|  | 1033 |  | 
|---|
| [e138de] | 1034 | //Log() << Verbose(2) << "Walker is " << *Walker << ", bond partners: "; | 
|---|
| [ba4170] | 1035 | if (CurrentBondsOfAtom == Walker->ListOfBonds.size()) { | 
|---|
|  | 1036 | for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) { | 
|---|
|  | 1037 | id = (*Runner)->GetOtherAtom(Walker)->nr; | 
|---|
|  | 1038 | j = 0; | 
|---|
| [9eefda] | 1039 | for (; (j < CurrentBondsOfAtom) && (CurrentBonds[j++] != id);) | 
|---|
| [ba4170] | 1040 | ; // check against all parsed bonds | 
|---|
| [9eefda] | 1041 | if (CurrentBonds[j - 1] != id) { // no match ? Then mark in ListOfAtoms | 
|---|
| [ba4170] | 1042 | ListOfAtoms[AtomNr] = NULL; | 
|---|
|  | 1043 | NonMatchNumber++; | 
|---|
|  | 1044 | status = false; | 
|---|
| [e138de] | 1045 | //Log() << Verbose(0) << "[" << id << "]\t"; | 
|---|
| [ba4170] | 1046 | } else { | 
|---|
| [e138de] | 1047 | //Log() << Verbose(0) << id << "\t"; | 
|---|
| [ba4170] | 1048 | } | 
|---|
|  | 1049 | } | 
|---|
| [e138de] | 1050 | //Log() << Verbose(0) << endl; | 
|---|
| [ba4170] | 1051 | } else { | 
|---|
| [e138de] | 1052 | Log() << Verbose(0) << "Number of bonds for Atom " << *Walker << " does not match, parsed " << CurrentBondsOfAtom << " against " << Walker->ListOfBonds.size() << "." << endl; | 
|---|
| [ba4170] | 1053 | status = false; | 
|---|
|  | 1054 | } | 
|---|
| [9eefda] | 1055 | } | 
|---|
|  | 1056 | ; | 
|---|
| [ba4170] | 1057 |  | 
|---|
| [cee0b57] | 1058 | /** Checks contents of adjacency file against bond structure in structure molecule. | 
|---|
|  | 1059 | * \param *out output stream for debugging | 
|---|
|  | 1060 | * \param *path path to file | 
|---|
|  | 1061 | * \param **ListOfAtoms allocated (molecule::AtomCount) and filled lookup table for ids (Atom::nr) to *Atom | 
|---|
|  | 1062 | * \return true - structure is equal, false - not equivalence | 
|---|
|  | 1063 | */ | 
|---|
| [e138de] | 1064 | bool molecule::CheckAdjacencyFileAgainstMolecule(char *path, atom **ListOfAtoms) | 
|---|
| [cee0b57] | 1065 | { | 
|---|
|  | 1066 | ifstream File; | 
|---|
|  | 1067 | bool status = true; | 
|---|
| [266237] | 1068 | atom *Walker = NULL; | 
|---|
| [ba4170] | 1069 | char *buffer = NULL; | 
|---|
|  | 1070 | int *CurrentBonds = NULL; | 
|---|
| [9eefda] | 1071 | int NonMatchNumber = 0; // will number of atoms with differing bond structure | 
|---|
| [ba4170] | 1072 | size_t CurrentBondsOfAtom = -1; | 
|---|
| [cee0b57] | 1073 |  | 
|---|
| [e138de] | 1074 | if (!CheckAdjacencyFileAgainstMolecule_Init(path, File, CurrentBonds)) { | 
|---|
|  | 1075 | Log() << Verbose(1) << "Adjacency file not found." << endl; | 
|---|
| [ba4170] | 1076 | return true; | 
|---|
|  | 1077 | } | 
|---|
|  | 1078 |  | 
|---|
| [9eefda] | 1079 | buffer = Malloc<char> (MAXSTRINGSIZE, "molecule::CheckAdjacencyFileAgainstMolecule: *buffer"); | 
|---|
| [ba4170] | 1080 | // Parse the file line by line and count the bonds | 
|---|
|  | 1081 | while (!File.eof()) { | 
|---|
|  | 1082 | File.getline(buffer, MAXSTRINGSIZE); | 
|---|
|  | 1083 | stringstream line; | 
|---|
|  | 1084 | line.str(buffer); | 
|---|
|  | 1085 | int AtomNr = -1; | 
|---|
|  | 1086 | line >> AtomNr; | 
|---|
|  | 1087 | CurrentBondsOfAtom = -1; // we count one too far due to line end | 
|---|
|  | 1088 | // parse into structure | 
|---|
|  | 1089 | if ((AtomNr >= 0) && (AtomNr < AtomCount)) { | 
|---|
|  | 1090 | Walker = ListOfAtoms[AtomNr]; | 
|---|
|  | 1091 | while (!line.eof()) | 
|---|
| [9eefda] | 1092 | line >> CurrentBonds[++CurrentBondsOfAtom]; | 
|---|
| [ba4170] | 1093 | // compare against present bonds | 
|---|
| [e138de] | 1094 | CheckAdjacencyFileAgainstMolecule_CompareBonds(status, NonMatchNumber, Walker, CurrentBondsOfAtom, AtomNr, CurrentBonds, ListOfAtoms); | 
|---|
| [ba4170] | 1095 | } | 
|---|
| [cee0b57] | 1096 | } | 
|---|
|  | 1097 | Free(&buffer); | 
|---|
| [e138de] | 1098 | CheckAdjacencyFileAgainstMolecule_Finalize(File, CurrentBonds); | 
|---|
| [cee0b57] | 1099 |  | 
|---|
| [ba4170] | 1100 | if (status) { // if equal we parse the KeySetFile | 
|---|
| [e138de] | 1101 | Log() << Verbose(1) << "done: Equal." << endl; | 
|---|
| [ba4170] | 1102 | } else | 
|---|
| [e138de] | 1103 | Log() << Verbose(1) << "done: Not equal by " << NonMatchNumber << " atoms." << endl; | 
|---|
| [cee0b57] | 1104 | return status; | 
|---|
| [9eefda] | 1105 | } | 
|---|
|  | 1106 | ; | 
|---|
| [cee0b57] | 1107 |  | 
|---|
|  | 1108 | /** Picks from a global stack with all back edges the ones in the fragment. | 
|---|
|  | 1109 | * \param *out output stream for debugging | 
|---|
|  | 1110 | * \param **ListOfLocalAtoms array of father atom::nr to local atom::nr (reverse of atom::father) | 
|---|
|  | 1111 | * \param *ReferenceStack stack with all the back egdes | 
|---|
|  | 1112 | * \param *LocalStack stack to be filled | 
|---|
|  | 1113 | * \return true - everything ok, false - ReferenceStack was empty | 
|---|
|  | 1114 | */ | 
|---|
| [e138de] | 1115 | bool molecule::PickLocalBackEdges(atom **ListOfLocalAtoms, class StackClass<bond *> *&ReferenceStack, class StackClass<bond *> *&LocalStack) const | 
|---|
| [cee0b57] | 1116 | { | 
|---|
|  | 1117 | bool status = true; | 
|---|
|  | 1118 | if (ReferenceStack->IsEmpty()) { | 
|---|
| [e138de] | 1119 | eLog() << Verbose(0) << "ReferenceStack is empty!" << endl; | 
|---|
| [e359a8] | 1120 | performCriticalExit(); | 
|---|
| [cee0b57] | 1121 | return false; | 
|---|
|  | 1122 | } | 
|---|
|  | 1123 | bond *Binder = ReferenceStack->PopFirst(); | 
|---|
| [9eefda] | 1124 | bond *FirstBond = Binder; // mark the first bond, so that we don't loop through the stack indefinitely | 
|---|
| [cee0b57] | 1125 | atom *Walker = NULL, *OtherAtom = NULL; | 
|---|
|  | 1126 | ReferenceStack->Push(Binder); | 
|---|
|  | 1127 |  | 
|---|
| [9eefda] | 1128 | do { // go through all bonds and push local ones | 
|---|
|  | 1129 | Walker = ListOfLocalAtoms[Binder->leftatom->nr]; // get one atom in the reference molecule | 
|---|
| [cee0b57] | 1130 | if (Walker != NULL) // if this Walker exists in the subgraph ... | 
|---|
| [266237] | 1131 | for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) { | 
|---|
|  | 1132 | OtherAtom = (*Runner)->GetOtherAtom(Walker); | 
|---|
|  | 1133 | if (OtherAtom == ListOfLocalAtoms[(*Runner)->rightatom->nr]) { // found the bond | 
|---|
|  | 1134 | LocalStack->Push((*Runner)); | 
|---|
| [e138de] | 1135 | Log() << Verbose(3) << "Found local edge " << *(*Runner) << "." << endl; | 
|---|
| [cee0b57] | 1136 | break; | 
|---|
|  | 1137 | } | 
|---|
|  | 1138 | } | 
|---|
| [9eefda] | 1139 | Binder = ReferenceStack->PopFirst(); // loop the stack for next item | 
|---|
| [e138de] | 1140 | Log() << Verbose(3) << "Current candidate edge " << Binder << "." << endl; | 
|---|
| [cee0b57] | 1141 | ReferenceStack->Push(Binder); | 
|---|
|  | 1142 | } while (FirstBond != Binder); | 
|---|
|  | 1143 |  | 
|---|
|  | 1144 | return status; | 
|---|
| [9eefda] | 1145 | } | 
|---|
|  | 1146 | ; | 
|---|
| [ce7cc5] | 1147 |  | 
|---|
|  | 1148 | void BreadthFirstSearchAdd_Init(struct BFSAccounting &BFS, atom *&Root, int AtomCount, int BondOrder, atom **AddedAtomList = NULL) | 
|---|
|  | 1149 | { | 
|---|
|  | 1150 | BFS.AtomCount = AtomCount; | 
|---|
|  | 1151 | BFS.BondOrder = BondOrder; | 
|---|
| [7218f8] | 1152 | BFS.PredecessorList = Calloc<atom*> (AtomCount, "molecule::BreadthFirstSearchAdd_Init: **PredecessorList"); | 
|---|
|  | 1153 | BFS.ShortestPathList = Calloc<int> (AtomCount, "molecule::BreadthFirstSearchAdd_Init: *ShortestPathList"); | 
|---|
| [9eefda] | 1154 | BFS.ColorList = Malloc<enum Shading> (AtomCount, "molecule::BreadthFirstSearchAdd_Init: *ColorList"); | 
|---|
|  | 1155 | BFS.BFSStack = new StackClass<atom *> (AtomCount); | 
|---|
| [ce7cc5] | 1156 |  | 
|---|
|  | 1157 | BFS.Root = Root; | 
|---|
| [9eefda] | 1158 | BFS.BFSStack->ClearStack(); | 
|---|
|  | 1159 | BFS.BFSStack->Push(Root); | 
|---|
| [ce7cc5] | 1160 |  | 
|---|
|  | 1161 | // initialise each vertex as white with no predecessor, empty queue, color Root lightgray | 
|---|
| [9eefda] | 1162 | for (int i = AtomCount; i--;) { | 
|---|
| [ce7cc5] | 1163 | BFS.ShortestPathList[i] = -1; | 
|---|
|  | 1164 | if ((AddedAtomList != NULL) && (AddedAtomList[i] != NULL)) // mark already present atoms (i.e. Root and maybe others) as visited | 
|---|
|  | 1165 | BFS.ColorList[i] = lightgray; | 
|---|
|  | 1166 | else | 
|---|
|  | 1167 | BFS.ColorList[i] = white; | 
|---|
|  | 1168 | } | 
|---|
| [7218f8] | 1169 | //BFS.ShortestPathList[Root->nr] = 0; //is set due to Calloc() | 
|---|
| [9eefda] | 1170 | } | 
|---|
|  | 1171 | ; | 
|---|
| [ce7cc5] | 1172 |  | 
|---|
|  | 1173 | void BreadthFirstSearchAdd_Free(struct BFSAccounting &BFS) | 
|---|
|  | 1174 | { | 
|---|
|  | 1175 | Free(&BFS.PredecessorList); | 
|---|
|  | 1176 | Free(&BFS.ShortestPathList); | 
|---|
|  | 1177 | Free(&BFS.ColorList); | 
|---|
| [9eefda] | 1178 | delete (BFS.BFSStack); | 
|---|
| [ce7cc5] | 1179 | BFS.AtomCount = 0; | 
|---|
| [9eefda] | 1180 | } | 
|---|
|  | 1181 | ; | 
|---|
| [ce7cc5] | 1182 |  | 
|---|
| [e138de] | 1183 | void BreadthFirstSearchAdd_UnvisitedNode(molecule *Mol, struct BFSAccounting &BFS, atom *&Walker, atom *&OtherAtom, bond *&Binder, bond *&Bond, atom **&AddedAtomList, bond **&AddedBondList, bool IsAngstroem) | 
|---|
| [ce7cc5] | 1184 | { | 
|---|
|  | 1185 | if (Binder != Bond) // let other atom white if it's via Root bond. In case it's cyclic it has to be reached again (yet Root is from OtherAtom already black, thus no problem) | 
|---|
|  | 1186 | BFS.ColorList[OtherAtom->nr] = lightgray; | 
|---|
| [9eefda] | 1187 | BFS.PredecessorList[OtherAtom->nr] = Walker; // Walker is the predecessor | 
|---|
|  | 1188 | BFS.ShortestPathList[OtherAtom->nr] = BFS.ShortestPathList[Walker->nr] + 1; | 
|---|
| [e138de] | 1189 | Log() << Verbose(2) << "Coloring OtherAtom " << OtherAtom->Name << " " << ((BFS.ColorList[OtherAtom->nr] == white) ? "white" : "lightgray") << ", its predecessor is " << Walker->Name << " and its Shortest Path is " << BFS.ShortestPathList[OtherAtom->nr] << " egde(s) long." << endl; | 
|---|
| [9eefda] | 1190 | if ((((BFS.ShortestPathList[OtherAtom->nr] < BFS.BondOrder) && (Binder != Bond)))) { // Check for maximum distance | 
|---|
| [e138de] | 1191 | Log() << Verbose(3); | 
|---|
| [ce7cc5] | 1192 | if (AddedAtomList[OtherAtom->nr] == NULL) { // add if it's not been so far | 
|---|
|  | 1193 | AddedAtomList[OtherAtom->nr] = Mol->AddCopyAtom(OtherAtom); | 
|---|
| [e138de] | 1194 | Log() << Verbose(0) << "Added OtherAtom " << OtherAtom->Name; | 
|---|
| [ce7cc5] | 1195 | AddedBondList[Binder->nr] = Mol->CopyBond(AddedAtomList[Walker->nr], AddedAtomList[OtherAtom->nr], Binder); | 
|---|
| [e138de] | 1196 | Log() << Verbose(0) << " and bond " << *(AddedBondList[Binder->nr]) << ", "; | 
|---|
| [9eefda] | 1197 | } else { // this code should actually never come into play (all white atoms are not yet present in BondMolecule, that's why they are white in the first place) | 
|---|
| [e138de] | 1198 | Log() << Verbose(0) << "Not adding OtherAtom " << OtherAtom->Name; | 
|---|
| [ce7cc5] | 1199 | if (AddedBondList[Binder->nr] == NULL) { | 
|---|
|  | 1200 | AddedBondList[Binder->nr] = Mol->CopyBond(AddedAtomList[Walker->nr], AddedAtomList[OtherAtom->nr], Binder); | 
|---|
| [e138de] | 1201 | Log() << Verbose(0) << ", added Bond " << *(AddedBondList[Binder->nr]); | 
|---|
| [ce7cc5] | 1202 | } else | 
|---|
| [e138de] | 1203 | Log() << Verbose(0) << ", not added Bond "; | 
|---|
| [ce7cc5] | 1204 | } | 
|---|
| [e138de] | 1205 | Log() << Verbose(0) << ", putting OtherAtom into queue." << endl; | 
|---|
| [9eefda] | 1206 | BFS.BFSStack->Push(OtherAtom); | 
|---|
| [ce7cc5] | 1207 | } else { // out of bond order, then replace | 
|---|
|  | 1208 | if ((AddedAtomList[OtherAtom->nr] == NULL) && (Binder->Cyclic)) | 
|---|
|  | 1209 | BFS.ColorList[OtherAtom->nr] = white; // unmark if it has not been queued/added, to make it available via its other bonds (cyclic) | 
|---|
|  | 1210 | if (Binder == Bond) | 
|---|
| [e138de] | 1211 | Log() << Verbose(3) << "Not Queueing, is the Root bond"; | 
|---|
| [ce7cc5] | 1212 | else if (BFS.ShortestPathList[OtherAtom->nr] >= BFS.BondOrder) | 
|---|
| [e138de] | 1213 | Log() << Verbose(3) << "Not Queueing, is out of Bond Count of " << BFS.BondOrder; | 
|---|
| [ce7cc5] | 1214 | if (!Binder->Cyclic) | 
|---|
| [e138de] | 1215 | Log() << Verbose(0) << ", is not part of a cyclic bond, saturating bond with Hydrogen." << endl; | 
|---|
| [ce7cc5] | 1216 | if (AddedBondList[Binder->nr] == NULL) { | 
|---|
|  | 1217 | if ((AddedAtomList[OtherAtom->nr] != NULL)) { // .. whether we add or saturate | 
|---|
|  | 1218 | AddedBondList[Binder->nr] = Mol->CopyBond(AddedAtomList[Walker->nr], AddedAtomList[OtherAtom->nr], Binder); | 
|---|
|  | 1219 | } else { | 
|---|
| [9eefda] | 1220 | #ifdef ADDHYDROGEN | 
|---|
| [e138de] | 1221 | if (!Mol->AddHydrogenReplacementAtom(Binder, AddedAtomList[Walker->nr], Walker, OtherAtom, IsAngstroem)) | 
|---|
| [9eefda] | 1222 | exit(1); | 
|---|
|  | 1223 | #endif | 
|---|
| [ce7cc5] | 1224 | } | 
|---|
|  | 1225 | } | 
|---|
|  | 1226 | } | 
|---|
| [9eefda] | 1227 | } | 
|---|
|  | 1228 | ; | 
|---|
| [ce7cc5] | 1229 |  | 
|---|
| [e138de] | 1230 | void BreadthFirstSearchAdd_VisitedNode(molecule *Mol, struct BFSAccounting &BFS, atom *&Walker, atom *&OtherAtom, bond *&Binder, bond *&Bond, atom **&AddedAtomList, bond **&AddedBondList, bool IsAngstroem) | 
|---|
| [ce7cc5] | 1231 | { | 
|---|
| [e138de] | 1232 | Log() << Verbose(3) << "Not Adding, has already been visited." << endl; | 
|---|
| [ce7cc5] | 1233 | // This has to be a cyclic bond, check whether it's present ... | 
|---|
|  | 1234 | if (AddedBondList[Binder->nr] == NULL) { | 
|---|
| [9eefda] | 1235 | if ((Binder != Bond) && (Binder->Cyclic) && (((BFS.ShortestPathList[Walker->nr] + 1) < BFS.BondOrder))) { | 
|---|
| [ce7cc5] | 1236 | AddedBondList[Binder->nr] = Mol->CopyBond(AddedAtomList[Walker->nr], AddedAtomList[OtherAtom->nr], Binder); | 
|---|
|  | 1237 | } else { // if it's root bond it has to broken (otherwise we would not create the fragments) | 
|---|
| [9eefda] | 1238 | #ifdef ADDHYDROGEN | 
|---|
| [e138de] | 1239 | if(!Mol->AddHydrogenReplacementAtom(Binder, AddedAtomList[Walker->nr], Walker, OtherAtom, IsAngstroem)) | 
|---|
| [9eefda] | 1240 | exit(1); | 
|---|
|  | 1241 | #endif | 
|---|
| [ce7cc5] | 1242 | } | 
|---|
|  | 1243 | } | 
|---|
| [9eefda] | 1244 | } | 
|---|
|  | 1245 | ; | 
|---|
| [cee0b57] | 1246 |  | 
|---|
|  | 1247 | /** Adds atoms up to \a BondCount distance from \a *Root and notes them down in \a **AddedAtomList. | 
|---|
|  | 1248 | * Gray vertices are always enqueued in an StackClass<atom *> FIFO queue, the rest is usual BFS with adding vertices found was | 
|---|
|  | 1249 | * white and putting into queue. | 
|---|
|  | 1250 | * \param *out output stream for debugging | 
|---|
|  | 1251 | * \param *Mol Molecule class to add atoms to | 
|---|
|  | 1252 | * \param **AddedAtomList list with added atom pointers, index is atom father's number | 
|---|
|  | 1253 | * \param **AddedBondList list with added bond pointers, index is bond father's number | 
|---|
|  | 1254 | * \param *Root root vertex for BFS | 
|---|
|  | 1255 | * \param *Bond bond not to look beyond | 
|---|
|  | 1256 | * \param BondOrder maximum distance for vertices to add | 
|---|
|  | 1257 | * \param IsAngstroem lengths are in angstroem or bohrradii | 
|---|
|  | 1258 | */ | 
|---|
| [e138de] | 1259 | void molecule::BreadthFirstSearchAdd(molecule *Mol, atom **&AddedAtomList, bond **&AddedBondList, atom *Root, bond *Bond, int BondOrder, bool IsAngstroem) | 
|---|
| [cee0b57] | 1260 | { | 
|---|
| [ce7cc5] | 1261 | struct BFSAccounting BFS; | 
|---|
| [cee0b57] | 1262 | atom *Walker = NULL, *OtherAtom = NULL; | 
|---|
| [ce7cc5] | 1263 | bond *Binder = NULL; | 
|---|
| [cee0b57] | 1264 |  | 
|---|
|  | 1265 | // add Root if not done yet | 
|---|
| [9eefda] | 1266 | if (AddedAtomList[Root->nr] == NULL) // add Root if not yet present | 
|---|
| [cee0b57] | 1267 | AddedAtomList[Root->nr] = Mol->AddCopyAtom(Root); | 
|---|
|  | 1268 |  | 
|---|
| [ce7cc5] | 1269 | BreadthFirstSearchAdd_Init(BFS, Root, BondOrder, AtomCount, AddedAtomList); | 
|---|
| [cee0b57] | 1270 |  | 
|---|
|  | 1271 | // and go on ... Queue always contains all lightgray vertices | 
|---|
| [9eefda] | 1272 | while (!BFS.BFSStack->IsEmpty()) { | 
|---|
| [cee0b57] | 1273 | // we have to pop the oldest atom from stack. This keeps the atoms on the stack always of the same ShortestPath distance. | 
|---|
|  | 1274 | // e.g. if current atom is 2, push to end of stack are of length 3, but first all of length 2 would be popped. They again | 
|---|
|  | 1275 | // append length of 3 (their neighbours). Thus on stack we have always atoms of a certain length n at bottom of stack and | 
|---|
|  | 1276 | // followed by n+1 till top of stack. | 
|---|
| [9eefda] | 1277 | Walker = BFS.BFSStack->PopFirst(); // pop oldest added | 
|---|
| [e138de] | 1278 | Log() << Verbose(1) << "Current Walker is: " << Walker->Name << ", and has " << Walker->ListOfBonds.size() << " bonds." << endl; | 
|---|
| [266237] | 1279 | for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) { | 
|---|
|  | 1280 | if ((*Runner) != NULL) { // don't look at bond equal NULL | 
|---|
| [ce7cc5] | 1281 | Binder = (*Runner); | 
|---|
| [266237] | 1282 | OtherAtom = (*Runner)->GetOtherAtom(Walker); | 
|---|
| [e138de] | 1283 | Log() << Verbose(2) << "Current OtherAtom is: " << OtherAtom->Name << " for bond " << *(*Runner) << "." << endl; | 
|---|
| [ce7cc5] | 1284 | if (BFS.ColorList[OtherAtom->nr] == white) { | 
|---|
| [e138de] | 1285 | BreadthFirstSearchAdd_UnvisitedNode(Mol, BFS, Walker, OtherAtom, Binder, Bond, AddedAtomList, AddedBondList, IsAngstroem); | 
|---|
| [cee0b57] | 1286 | } else { | 
|---|
| [e138de] | 1287 | BreadthFirstSearchAdd_VisitedNode(Mol, BFS, Walker, OtherAtom, Binder, Bond, AddedAtomList, AddedBondList, IsAngstroem); | 
|---|
| [cee0b57] | 1288 | } | 
|---|
|  | 1289 | } | 
|---|
|  | 1290 | } | 
|---|
| [ce7cc5] | 1291 | BFS.ColorList[Walker->nr] = black; | 
|---|
| [e138de] | 1292 | Log() << Verbose(1) << "Coloring Walker " << Walker->Name << " black." << endl; | 
|---|
| [cee0b57] | 1293 | } | 
|---|
| [ce7cc5] | 1294 | BreadthFirstSearchAdd_Free(BFS); | 
|---|
| [9eefda] | 1295 | } | 
|---|
|  | 1296 | ; | 
|---|
| [cee0b57] | 1297 |  | 
|---|
| [266237] | 1298 | /** Adds a bond as a copy to a given one | 
|---|
|  | 1299 | * \param *left leftatom of new bond | 
|---|
|  | 1300 | * \param *right rightatom of new bond | 
|---|
|  | 1301 | * \param *CopyBond rest of fields in bond are copied from this | 
|---|
|  | 1302 | * \return pointer to new bond | 
|---|
|  | 1303 | */ | 
|---|
|  | 1304 | bond * molecule::CopyBond(atom *left, atom *right, bond *CopyBond) | 
|---|
|  | 1305 | { | 
|---|
|  | 1306 | bond *Binder = AddBond(left, right, CopyBond->BondDegree); | 
|---|
|  | 1307 | Binder->Cyclic = CopyBond->Cyclic; | 
|---|
|  | 1308 | Binder->Type = CopyBond->Type; | 
|---|
|  | 1309 | return Binder; | 
|---|
| [9eefda] | 1310 | } | 
|---|
|  | 1311 | ; | 
|---|
| [266237] | 1312 |  | 
|---|
| [e138de] | 1313 | void BuildInducedSubgraph_Init(atom **&ParentList, int AtomCount) | 
|---|
| [cee0b57] | 1314 | { | 
|---|
|  | 1315 | // reset parent list | 
|---|
| [7218f8] | 1316 | ParentList = Calloc<atom*> (AtomCount, "molecule::BuildInducedSubgraph_Init: **ParentList"); | 
|---|
| [e138de] | 1317 | Log() << Verbose(3) << "Resetting ParentList." << endl; | 
|---|
| [9eefda] | 1318 | } | 
|---|
|  | 1319 | ; | 
|---|
| [cee0b57] | 1320 |  | 
|---|
| [e138de] | 1321 | void BuildInducedSubgraph_FillParentList(const molecule *mol, const molecule *Father, atom **&ParentList) | 
|---|
| [43587e] | 1322 | { | 
|---|
| [cee0b57] | 1323 | // fill parent list with sons | 
|---|
| [e138de] | 1324 | Log() << Verbose(3) << "Filling Parent List." << endl; | 
|---|
| [43587e] | 1325 | atom *Walker = mol->start; | 
|---|
|  | 1326 | while (Walker->next != mol->end) { | 
|---|
| [cee0b57] | 1327 | Walker = Walker->next; | 
|---|
|  | 1328 | ParentList[Walker->father->nr] = Walker; | 
|---|
|  | 1329 | // Outputting List for debugging | 
|---|
| [e138de] | 1330 | Log() << Verbose(4) << "Son[" << Walker->father->nr << "] of " << Walker->father << " is " << ParentList[Walker->father->nr] << "." << endl; | 
|---|
| [cee0b57] | 1331 | } | 
|---|
|  | 1332 |  | 
|---|
| [9eefda] | 1333 | } | 
|---|
|  | 1334 | ; | 
|---|
| [43587e] | 1335 |  | 
|---|
| [e138de] | 1336 | void BuildInducedSubgraph_Finalize(atom **&ParentList) | 
|---|
| [43587e] | 1337 | { | 
|---|
|  | 1338 | Free(&ParentList); | 
|---|
| [9eefda] | 1339 | } | 
|---|
|  | 1340 | ; | 
|---|
| [43587e] | 1341 |  | 
|---|
| [e138de] | 1342 | bool BuildInducedSubgraph_CreateBondsFromParent(molecule *mol, const molecule *Father, atom **&ParentList) | 
|---|
| [43587e] | 1343 | { | 
|---|
|  | 1344 | bool status = true; | 
|---|
|  | 1345 | atom *Walker = NULL; | 
|---|
|  | 1346 | atom *OtherAtom = NULL; | 
|---|
| [cee0b57] | 1347 | // check each entry of parent list and if ok (one-to-and-onto matching) create bonds | 
|---|
| [e138de] | 1348 | Log() << Verbose(3) << "Creating bonds." << endl; | 
|---|
| [cee0b57] | 1349 | Walker = Father->start; | 
|---|
|  | 1350 | while (Walker->next != Father->end) { | 
|---|
|  | 1351 | Walker = Walker->next; | 
|---|
|  | 1352 | if (ParentList[Walker->nr] != NULL) { | 
|---|
|  | 1353 | if (ParentList[Walker->nr]->father != Walker) { | 
|---|
|  | 1354 | status = false; | 
|---|
|  | 1355 | } else { | 
|---|
| [266237] | 1356 | for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) { | 
|---|
|  | 1357 | OtherAtom = (*Runner)->GetOtherAtom(Walker); | 
|---|
| [cee0b57] | 1358 | if (ParentList[OtherAtom->nr] != NULL) { // if otheratom is also a father of an atom on this molecule, create the bond | 
|---|
| [e138de] | 1359 | Log() << Verbose(4) << "Endpoints of Bond " << (*Runner) << " are both present: " << ParentList[Walker->nr]->Name << " and " << ParentList[OtherAtom->nr]->Name << "." << endl; | 
|---|
| [43587e] | 1360 | mol->AddBond(ParentList[Walker->nr], ParentList[OtherAtom->nr], (*Runner)->BondDegree); | 
|---|
| [cee0b57] | 1361 | } | 
|---|
|  | 1362 | } | 
|---|
|  | 1363 | } | 
|---|
|  | 1364 | } | 
|---|
|  | 1365 | } | 
|---|
| [43587e] | 1366 | return status; | 
|---|
| [9eefda] | 1367 | } | 
|---|
|  | 1368 | ; | 
|---|
| [cee0b57] | 1369 |  | 
|---|
| [43587e] | 1370 | /** Adds bond structure to this molecule from \a Father molecule. | 
|---|
|  | 1371 | * This basically causes this molecule to become an induced subgraph of the \a Father, i.e. for every bond in Father | 
|---|
|  | 1372 | * with end points present in this molecule, bond is created in this molecule. | 
|---|
|  | 1373 | * Special care was taken to ensure that this is of complexity O(N), where N is the \a Father's molecule::AtomCount. | 
|---|
|  | 1374 | * \param *out output stream for debugging | 
|---|
|  | 1375 | * \param *Father father molecule | 
|---|
|  | 1376 | * \return true - is induced subgraph, false - there are atoms with fathers not in \a Father | 
|---|
|  | 1377 | * \todo not checked, not fully working probably | 
|---|
|  | 1378 | */ | 
|---|
| [e138de] | 1379 | bool molecule::BuildInducedSubgraph(const molecule *Father) | 
|---|
| [43587e] | 1380 | { | 
|---|
|  | 1381 | bool status = true; | 
|---|
|  | 1382 | atom **ParentList = NULL; | 
|---|
|  | 1383 |  | 
|---|
| [e138de] | 1384 | Log() << Verbose(2) << "Begin of BuildInducedSubgraph." << endl; | 
|---|
|  | 1385 | BuildInducedSubgraph_Init(ParentList, Father->AtomCount); | 
|---|
|  | 1386 | BuildInducedSubgraph_FillParentList(this, Father, ParentList); | 
|---|
|  | 1387 | status = BuildInducedSubgraph_CreateBondsFromParent(this, Father, ParentList); | 
|---|
|  | 1388 | BuildInducedSubgraph_Finalize(ParentList); | 
|---|
|  | 1389 | Log() << Verbose(2) << "End of BuildInducedSubgraph." << endl; | 
|---|
| [cee0b57] | 1390 | return status; | 
|---|
| [9eefda] | 1391 | } | 
|---|
|  | 1392 | ; | 
|---|
| [cee0b57] | 1393 |  | 
|---|
|  | 1394 | /** For a given keyset \a *Fragment, checks whether it is connected in the current molecule. | 
|---|
|  | 1395 | * \param *out output stream for debugging | 
|---|
|  | 1396 | * \param *Fragment Keyset of fragment's vertices | 
|---|
|  | 1397 | * \return true - connected, false - disconnected | 
|---|
|  | 1398 | * \note this is O(n^2) for it's just a bug checker not meant for permanent use! | 
|---|
|  | 1399 | */ | 
|---|
| [e138de] | 1400 | bool molecule::CheckForConnectedSubgraph(KeySet *Fragment) | 
|---|
| [cee0b57] | 1401 | { | 
|---|
|  | 1402 | atom *Walker = NULL, *Walker2 = NULL; | 
|---|
|  | 1403 | bool BondStatus = false; | 
|---|
|  | 1404 | int size; | 
|---|
|  | 1405 |  | 
|---|
| [e138de] | 1406 | Log() << Verbose(1) << "Begin of CheckForConnectedSubgraph" << endl; | 
|---|
|  | 1407 | Log() << Verbose(2) << "Disconnected atom: "; | 
|---|
| [cee0b57] | 1408 |  | 
|---|
|  | 1409 | // count number of atoms in graph | 
|---|
|  | 1410 | size = 0; | 
|---|
| [9eefda] | 1411 | for (KeySet::iterator runner = Fragment->begin(); runner != Fragment->end(); runner++) | 
|---|
| [cee0b57] | 1412 | size++; | 
|---|
|  | 1413 | if (size > 1) | 
|---|
| [9eefda] | 1414 | for (KeySet::iterator runner = Fragment->begin(); runner != Fragment->end(); runner++) { | 
|---|
| [cee0b57] | 1415 | Walker = FindAtom(*runner); | 
|---|
|  | 1416 | BondStatus = false; | 
|---|
| [9eefda] | 1417 | for (KeySet::iterator runners = Fragment->begin(); runners != Fragment->end(); runners++) { | 
|---|
| [cee0b57] | 1418 | Walker2 = FindAtom(*runners); | 
|---|
| [266237] | 1419 | for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) { | 
|---|
|  | 1420 | if ((*Runner)->GetOtherAtom(Walker) == Walker2) { | 
|---|
| [cee0b57] | 1421 | BondStatus = true; | 
|---|
|  | 1422 | break; | 
|---|
|  | 1423 | } | 
|---|
|  | 1424 | if (BondStatus) | 
|---|
|  | 1425 | break; | 
|---|
|  | 1426 | } | 
|---|
|  | 1427 | } | 
|---|
|  | 1428 | if (!BondStatus) { | 
|---|
| [e138de] | 1429 | Log() << Verbose(0) << (*Walker) << endl; | 
|---|
| [cee0b57] | 1430 | return false; | 
|---|
|  | 1431 | } | 
|---|
|  | 1432 | } | 
|---|
|  | 1433 | else { | 
|---|
| [e138de] | 1434 | Log() << Verbose(0) << "none." << endl; | 
|---|
| [cee0b57] | 1435 | return true; | 
|---|
|  | 1436 | } | 
|---|
| [e138de] | 1437 | Log() << Verbose(0) << "none." << endl; | 
|---|
| [cee0b57] | 1438 |  | 
|---|
| [e138de] | 1439 | Log() << Verbose(1) << "End of CheckForConnectedSubgraph" << endl; | 
|---|
| [cee0b57] | 1440 |  | 
|---|
|  | 1441 | return true; | 
|---|
|  | 1442 | } | 
|---|