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