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