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