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