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