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