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