| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * 
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *   This file is part of MoleCuilder.
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 12 |  *    (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  *    GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * BondsPerShortestPath.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Oct 18, 2011
 | 
|---|
| 27 |  *      Author: heber
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include "BondsPerShortestPath.hpp"
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #include <sstream>
 | 
|---|
| 40 | 
 | 
|---|
| 41 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #include "Atom/atom.hpp"
 | 
|---|
| 44 | #include "Bond/bond.hpp"
 | 
|---|
| 45 | #include "Element/element.hpp"
 | 
|---|
| 46 | #include "Fragmentation/KeySet.hpp"
 | 
|---|
| 47 | 
 | 
|---|
| 48 | BondsPerShortestPath::BondsPerShortestPath(int _Order) :
 | 
|---|
| 49 |   Order(_Order)
 | 
|---|
| 50 | {
 | 
|---|
| 51 |   InitialiseSPList();
 | 
|---|
| 52 | }
 | 
|---|
| 53 | 
 | 
|---|
| 54 | BondsPerShortestPath::~BondsPerShortestPath()
 | 
|---|
| 55 | {
 | 
|---|
| 56 |   // free Order-dependent entries of UniqueFragments structure for next loop cycle
 | 
|---|
| 57 |   FreeSPList();
 | 
|---|
| 58 | }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | /** Allocates memory for BondsPerShortestPath::BondsPerSPList.
 | 
|---|
| 61 |  * \sa BondsPerShortestPath::FreeSPList()
 | 
|---|
| 62 |  */
 | 
|---|
| 63 | void BondsPerShortestPath::InitialiseSPList()
 | 
|---|
| 64 | {
 | 
|---|
| 65 |   BondsPerSPList.resize(Order);
 | 
|---|
| 66 |   BondsPerSPCount = new int[Order];
 | 
|---|
| 67 |   for (int i=Order;i--;) {
 | 
|---|
| 68 |     BondsPerSPCount[i] = 0;
 | 
|---|
| 69 |   }
 | 
|---|
| 70 | };
 | 
|---|
| 71 | 
 | 
|---|
| 72 | /** Free's memory for for BondsPerShortestPath::BondsPerSPList.
 | 
|---|
| 73 |  * \sa BondsPerShortestPath::InitialiseSPList()
 | 
|---|
| 74 |  */
 | 
|---|
| 75 | void BondsPerShortestPath::FreeSPList()
 | 
|---|
| 76 | {
 | 
|---|
| 77 |   delete[](BondsPerSPCount);
 | 
|---|
| 78 | };
 | 
|---|
| 79 | 
 | 
|---|
| 80 | /** Sets FragmenSearch to initial value.
 | 
|---|
| 81 |  * Sets BondsPerShortestPath::ShortestPathList entries to zero, BondsPerShortestPath::BondsPerSPCount to zero (except zero level to 1) and
 | 
|---|
| 82 |  * adds initial bond BondsPerShortestPath::Root to BondsPerShortestPath::Root to BondsPerShortestPath::BondsPerSPList
 | 
|---|
| 83 |  * \param *_Root root node, self loop becomes first bond
 | 
|---|
| 84 |  * \sa BondsPerShortestPath::FreeSPList()
 | 
|---|
| 85 |  */
 | 
|---|
| 86 | void BondsPerShortestPath::SetSPList(atom *_Root)
 | 
|---|
| 87 | {
 | 
|---|
| 88 |   // prepare root level (SP = 0) and a loop bond denoting Root
 | 
|---|
| 89 |   for (int i=Order;i--;)
 | 
|---|
| 90 |     BondsPerSPCount[i] = 0;
 | 
|---|
| 91 |   BondsPerSPCount[0] = 1;
 | 
|---|
| 92 |   bond *Binder = new bond(_Root, _Root);
 | 
|---|
| 93 |   BondsPerSPList[0].push_back(Binder);
 | 
|---|
| 94 | };
 | 
|---|
| 95 | 
 | 
|---|
| 96 | /** Resets BondsPerShortestPath::ShortestPathList and cleans bonds from BondsPerShortestPath::BondsPerSPList.
 | 
|---|
| 97 |  * \sa BondsPerShortestPath::InitialiseSPList()
 | 
|---|
| 98 |  */
 | 
|---|
| 99 | void BondsPerShortestPath::ResetSPList()
 | 
|---|
| 100 | {
 | 
|---|
| 101 |   LOG(0, "Free'ing all found lists. and resetting index lists");
 | 
|---|
| 102 |   std::stringstream output;
 | 
|---|
| 103 |   for(int i=Order;i--;) {
 | 
|---|
| 104 |     output << "Current SP level is " << i << ": ";
 | 
|---|
| 105 |     // delete added bonds
 | 
|---|
| 106 |     for (BondsPerSP::iterator iter = BondsPerSPList[i].begin();
 | 
|---|
| 107 |         iter != BondsPerSPList[i].end();
 | 
|---|
| 108 |         ++iter) {
 | 
|---|
| 109 |       delete(*iter);
 | 
|---|
| 110 |     }
 | 
|---|
| 111 |     BondsPerSPList[i].clear();
 | 
|---|
| 112 |     // also start and end node
 | 
|---|
| 113 |     output << "cleaned.";
 | 
|---|
| 114 |   }
 | 
|---|
| 115 |   LOG(1, output.str());
 | 
|---|
| 116 | };
 | 
|---|
| 117 | 
 | 
|---|
| 118 | 
 | 
|---|
| 119 | /** Fills the Bonds per Shortest Path List and set the vertex labels.
 | 
|---|
| 120 |  * \param _RootKeyNr index of root node
 | 
|---|
| 121 |  * \param RestrictedKeySet Restricted vertex set to use in context of molecule
 | 
|---|
| 122 |  * \param saturation this tells whether to treat hydrogen special or not.
 | 
|---|
| 123 |  */
 | 
|---|
| 124 | void BondsPerShortestPath::FillSPListandLabelVertices(int _RootKeyNr, KeySet &RestrictedKeySet, const enum HydrogenSaturation saturation)
 | 
|---|
| 125 | {
 | 
|---|
| 126 |   // Actually, we should construct a spanning tree vom the root atom and select all edges therefrom and put them into
 | 
|---|
| 127 |   // according shortest path lists. However, we don't. Rather we fill these lists right away, as they do form a spanning
 | 
|---|
| 128 |   // tree already sorted into various SP levels. That's why we just do loops over the depth (CurrentSP) and breadth
 | 
|---|
| 129 |   // (EdgeinSPLevel) of this tree ...
 | 
|---|
| 130 |   // In another picture, the bonds always contain a direction by rightatom being the one more distant from root and hence
 | 
|---|
| 131 |   // naturally leftatom forming its predecessor, preventing the BFS"seeker" from continuing in the wrong direction.
 | 
|---|
| 132 |   int AtomKeyNr = -1;
 | 
|---|
| 133 |   atom *Walker = NULL;
 | 
|---|
| 134 |   atom *OtherWalker = NULL;
 | 
|---|
| 135 |   atom *Predecessor = NULL;
 | 
|---|
| 136 |   bond *Binder = NULL;
 | 
|---|
| 137 |   int RootKeyNr = _RootKeyNr;
 | 
|---|
| 138 |   int RemainingWalkers = -1;
 | 
|---|
| 139 |   int SP = -1;
 | 
|---|
| 140 | 
 | 
|---|
| 141 |   LOG(0, "Starting BFS analysis ...");
 | 
|---|
| 142 |   for (SP = 0; SP < (Order-1); SP++) {
 | 
|---|
| 143 |     {
 | 
|---|
| 144 |       std::stringstream output;
 | 
|---|
| 145 |       output << "New SP level reached: " << SP << ", creating new SP list with " << BondsPerSPCount[SP] << " item(s)";
 | 
|---|
| 146 |       if (SP > 0) {
 | 
|---|
| 147 |         output << ", old level closed with " << BondsPerSPCount[SP-1] << " item(s).";
 | 
|---|
| 148 |         BondsPerSPCount[SP] = 0;
 | 
|---|
| 149 |       } else
 | 
|---|
| 150 |         output << ".";
 | 
|---|
| 151 |       LOG(1, output.str());
 | 
|---|
| 152 |     }
 | 
|---|
| 153 | 
 | 
|---|
| 154 |     RemainingWalkers = BondsPerSPCount[SP];
 | 
|---|
| 155 |     for (BondsPerSP::const_iterator CurrentEdge = BondsPerSPList[SP].begin();
 | 
|---|
| 156 |         CurrentEdge != BondsPerSPList[SP].end();
 | 
|---|
| 157 |         ++CurrentEdge) { /// start till end of this SP level's list
 | 
|---|
| 158 |       RemainingWalkers--;
 | 
|---|
| 159 |       Walker = (*CurrentEdge)->rightatom;    // rightatom is always the one more distant
 | 
|---|
| 160 |       Predecessor = (*CurrentEdge)->leftatom;    // ... and leftatom is predecessor
 | 
|---|
| 161 |       AtomKeyNr = Walker->getNr();
 | 
|---|
| 162 |       LOG(0, "Current Walker is: " << *Walker << " with nr " << Walker->getNr() << " and SP of " << SP << ", with " << RemainingWalkers << " remaining walkers on this level.");
 | 
|---|
| 163 |       // check for new sp level
 | 
|---|
| 164 |       // go through all its bonds
 | 
|---|
| 165 |       LOG(1, "Going through all bonds of Walker.");
 | 
|---|
| 166 |       const BondList& ListOfBonds = Walker->getListOfBonds();
 | 
|---|
| 167 |       for (BondList::const_iterator Runner = ListOfBonds.begin();
 | 
|---|
| 168 |           Runner != ListOfBonds.end();
 | 
|---|
| 169 |           ++Runner) {
 | 
|---|
| 170 |         OtherWalker = (*Runner)->GetOtherAtom(Walker);
 | 
|---|
| 171 |         if ((RestrictedKeySet.find(OtherWalker->getNr()) != RestrictedKeySet.end())
 | 
|---|
| 172 |             // skip hydrogens if desired and restrict to fragment
 | 
|---|
| 173 |             && ((saturation == DontSaturate) || (OtherWalker->getType()->getAtomicNumber() != 1))) {
 | 
|---|
| 174 |           LOG(2, "Current partner is " << *OtherWalker << " with nr " << OtherWalker->getNr() << " in bond " << *(*Runner) << ".");
 | 
|---|
| 175 |           // set the label if not set (and push on root stack as well)
 | 
|---|
| 176 |           if ((OtherWalker != Predecessor) && (OtherWalker->GetTrueFather()->getNr() > RootKeyNr)) { // only pass through those with label bigger than Root's
 | 
|---|
| 177 |             // add the bond in between to the SP list
 | 
|---|
| 178 |             Binder = new bond(Walker, OtherWalker); // create a new bond in such a manner, that bond::rightatom is always the one more distant
 | 
|---|
| 179 |             BondsPerSPList[SP+1].push_back(Binder);
 | 
|---|
| 180 |             BondsPerSPCount[SP+1]++;
 | 
|---|
| 181 |             LOG(3, "Added its bond to SP list, having now " << BondsPerSPCount[SP+1] << " item(s).");
 | 
|---|
| 182 |           } else {
 | 
|---|
| 183 |             if (OtherWalker != Predecessor)
 | 
|---|
| 184 |               LOG(3, "Not passing on, as index of " << *OtherWalker << " " << OtherWalker->GetTrueFather()->getNr() << " is smaller than that of Root " << RootKeyNr << ".");
 | 
|---|
| 185 |             else
 | 
|---|
| 186 |               LOG(3, "This is my predecessor " << *Predecessor << ".");
 | 
|---|
| 187 |           }
 | 
|---|
| 188 |         } else LOG(2, "Is not in the restricted keyset or skipping hydrogen " << *OtherWalker << ".");
 | 
|---|
| 189 |       }
 | 
|---|
| 190 |     }
 | 
|---|
| 191 |   }
 | 
|---|
| 192 | };
 | 
|---|
| 193 | 
 | 
|---|
| 194 | /** prints the Bonds per Shortest Path list in BondsPerShortestPath.
 | 
|---|
| 195 |  */
 | 
|---|
| 196 | void BondsPerShortestPath::OutputSPList()
 | 
|---|
| 197 | {
 | 
|---|
| 198 |   LOG(0, "Printing all found lists.");
 | 
|---|
| 199 |   for(int i=1;i<Order;i++) {    // skip the root edge in the printing
 | 
|---|
| 200 |     LOG(1, "Current SP level is " << i << ".");
 | 
|---|
| 201 |     for (BondsPerShortestPath::BondsPerSP::const_iterator Binder = BondsPerSPList[i].begin();
 | 
|---|
| 202 |         Binder != BondsPerSPList[i].end();
 | 
|---|
| 203 |         ++Binder) {
 | 
|---|
| 204 |       LOG(2, *Binder);
 | 
|---|
| 205 |     }
 | 
|---|
| 206 |   }
 | 
|---|
| 207 | };
 | 
|---|
| 208 | 
 | 
|---|
| 209 | /** Simply counts all bonds in all BondsPerShortestPath::BondsPerSPList lists.
 | 
|---|
| 210 |  */
 | 
|---|
| 211 | int BondsPerShortestPath::CountNumbersInBondsList()
 | 
|---|
| 212 | {
 | 
|---|
| 213 |   int SP = -1;  // the Root <-> Root edge must be subtracted!
 | 
|---|
| 214 |   for(int i=Order;i--;) { // sum up all found edges
 | 
|---|
| 215 |     for (BondsPerShortestPath::BondsPerSP::const_iterator Binder = BondsPerSPList[i].begin();
 | 
|---|
| 216 |         Binder != BondsPerSPList[i].end();
 | 
|---|
| 217 |         ++Binder) {
 | 
|---|
| 218 |       SP++;
 | 
|---|
| 219 |     }
 | 
|---|
| 220 |   }
 | 
|---|
| 221 |   return SP;
 | 
|---|
| 222 | };
 | 
|---|
| 223 | 
 | 
|---|
| 224 | /** Getter for BondsPerShortestPath::Order.
 | 
|---|
| 225 |  *
 | 
|---|
| 226 |  * @return returns BondsPerShortestPath::Order
 | 
|---|
| 227 |  */
 | 
|---|
| 228 | int BondsPerShortestPath::getOrder() const
 | 
|---|
| 229 | {
 | 
|---|
| 230 |   return Order;
 | 
|---|
| 231 | }
 | 
|---|