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