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