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