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