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