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