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