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