| [212c179] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [94d5ac6] | 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/>.
 | 
|---|
| [212c179] | 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 | 
 | 
|---|
| [47d041] | 39 | #include <sstream>
 | 
|---|
 | 40 | 
 | 
|---|
| [212c179] | 41 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 42 | 
 | 
|---|
| [6f0841] | 43 | #include "Atom/atom.hpp"
 | 
|---|
| [212c179] | 44 | #include "Bond/bond.hpp"
 | 
|---|
 | 45 | #include "Element/element.hpp"
 | 
|---|
| [f0674a] | 46 | #include "Fragmentation/KeySet.hpp"
 | 
|---|
| [212c179] | 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;
 | 
|---|
| [7d82a5] | 92 |   bond::ptr Binder(new bond(_Root, _Root));
 | 
|---|
| [212c179] | 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 | {
 | 
|---|
| [8ac66b4] | 101 |   LOG(1, "INFO: Free'ing all found lists and resetting index lists");
 | 
|---|
| [212c179] | 102 |   for(int i=Order;i--;) {
 | 
|---|
| [d760bb] | 103 |     std::stringstream output;
 | 
|---|
| [47d041] | 104 |     output << "Current SP level is " << i << ": ";
 | 
|---|
| [212c179] | 105 |     // delete added bonds
 | 
|---|
 | 106 |     for (BondsPerSP::iterator iter = BondsPerSPList[i].begin();
 | 
|---|
 | 107 |         iter != BondsPerSPList[i].end();
 | 
|---|
 | 108 |         ++iter) {
 | 
|---|
| [e85169] | 109 |       // TODO: Hack because we have not registered bond's in BondsPerSPList with atoms
 | 
|---|
 | 110 |       (*iter)->leftatom = NULL;
 | 
|---|
 | 111 |       (*iter)->rightatom = NULL;
 | 
|---|
| [212c179] | 112 |     }
 | 
|---|
 | 113 |     BondsPerSPList[i].clear();
 | 
|---|
 | 114 |     // also start and end node
 | 
|---|
| [47d041] | 115 |     output << "cleaned.";
 | 
|---|
| [d760bb] | 116 |     LOG(1, output.str());
 | 
|---|
| [212c179] | 117 |   }
 | 
|---|
 | 118 | };
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 | 
 | 
|---|
 | 121 | /** Fills the Bonds per Shortest Path List and set the vertex labels.
 | 
|---|
 | 122 |  * \param _RootKeyNr index of root node
 | 
|---|
 | 123 |  * \param RestrictedKeySet Restricted vertex set to use in context of molecule
 | 
|---|
| [9291d04] | 124 |  * \param treatment this tells whether to treat hydrogen special or not.
 | 
|---|
| [212c179] | 125 |  */
 | 
|---|
| [9291d04] | 126 | void BondsPerShortestPath::FillSPListandLabelVertices(int _RootKeyNr, KeySet &RestrictedKeySet, const enum HydrogenTreatment treatment)
 | 
|---|
| [212c179] | 127 | {
 | 
|---|
 | 128 |   // Actually, we should construct a spanning tree vom the root atom and select all edges therefrom and put them into
 | 
|---|
 | 129 |   // according shortest path lists. However, we don't. Rather we fill these lists right away, as they do form a spanning
 | 
|---|
 | 130 |   // tree already sorted into various SP levels. That's why we just do loops over the depth (CurrentSP) and breadth
 | 
|---|
 | 131 |   // (EdgeinSPLevel) of this tree ...
 | 
|---|
 | 132 |   // In another picture, the bonds always contain a direction by rightatom being the one more distant from root and hence
 | 
|---|
 | 133 |   // naturally leftatom forming its predecessor, preventing the BFS"seeker" from continuing in the wrong direction.
 | 
|---|
 | 134 |   int AtomKeyNr = -1;
 | 
|---|
 | 135 |   atom *Walker = NULL;
 | 
|---|
 | 136 |   atom *OtherWalker = NULL;
 | 
|---|
 | 137 |   atom *Predecessor = NULL;
 | 
|---|
| [7d82a5] | 138 |   bond::ptr Binder;
 | 
|---|
| [212c179] | 139 |   int RootKeyNr = _RootKeyNr;
 | 
|---|
 | 140 |   int RemainingWalkers = -1;
 | 
|---|
 | 141 |   int SP = -1;
 | 
|---|
 | 142 | 
 | 
|---|
| [8ac66b4] | 143 |   LOG(0, "INFO: Starting BFS analysis ...");
 | 
|---|
| [212c179] | 144 |   for (SP = 0; SP < (Order-1); SP++) {
 | 
|---|
| [47d041] | 145 |     {
 | 
|---|
 | 146 |       std::stringstream output;
 | 
|---|
 | 147 |       output << "New SP level reached: " << SP << ", creating new SP list with " << BondsPerSPCount[SP] << " item(s)";
 | 
|---|
 | 148 |       if (SP > 0) {
 | 
|---|
 | 149 |         output << ", old level closed with " << BondsPerSPCount[SP-1] << " item(s).";
 | 
|---|
 | 150 |         BondsPerSPCount[SP] = 0;
 | 
|---|
 | 151 |       } else
 | 
|---|
 | 152 |         output << ".";
 | 
|---|
 | 153 |       LOG(1, output.str());
 | 
|---|
 | 154 |     }
 | 
|---|
| [212c179] | 155 | 
 | 
|---|
 | 156 |     RemainingWalkers = BondsPerSPCount[SP];
 | 
|---|
 | 157 |     for (BondsPerSP::const_iterator CurrentEdge = BondsPerSPList[SP].begin();
 | 
|---|
 | 158 |         CurrentEdge != BondsPerSPList[SP].end();
 | 
|---|
 | 159 |         ++CurrentEdge) { /// start till end of this SP level's list
 | 
|---|
 | 160 |       RemainingWalkers--;
 | 
|---|
 | 161 |       Walker = (*CurrentEdge)->rightatom;    // rightatom is always the one more distant
 | 
|---|
 | 162 |       Predecessor = (*CurrentEdge)->leftatom;    // ... and leftatom is predecessor
 | 
|---|
 | 163 |       AtomKeyNr = Walker->getNr();
 | 
|---|
| [8ac66b4] | 164 |       LOG(1, "INFO: Current Walker is: " << *Walker << " with nr " << Walker->getNr() << " and SP of " << SP << ", with " << RemainingWalkers << " remaining walkers on this level.");
 | 
|---|
| [212c179] | 165 |       // check for new sp level
 | 
|---|
 | 166 |       // go through all its bonds
 | 
|---|
| [8ac66b4] | 167 | //      LOG(2, "Going through all bonds of Walker.");
 | 
|---|
| [212c179] | 168 |       const BondList& ListOfBonds = Walker->getListOfBonds();
 | 
|---|
 | 169 |       for (BondList::const_iterator Runner = ListOfBonds.begin();
 | 
|---|
 | 170 |           Runner != ListOfBonds.end();
 | 
|---|
 | 171 |           ++Runner) {
 | 
|---|
 | 172 |         OtherWalker = (*Runner)->GetOtherAtom(Walker);
 | 
|---|
 | 173 |         if ((RestrictedKeySet.find(OtherWalker->getNr()) != RestrictedKeySet.end())
 | 
|---|
| [07a47e] | 174 |             // skip hydrogens if desired and restrict to fragment
 | 
|---|
| [9291d04] | 175 |             && ((treatment == IncludeHydrogen) || (OtherWalker->getType()->getAtomicNumber() != 1))) {
 | 
|---|
| [47d041] | 176 |           LOG(2, "Current partner is " << *OtherWalker << " with nr " << OtherWalker->getNr() << " in bond " << *(*Runner) << ".");
 | 
|---|
| [212c179] | 177 |           // set the label if not set (and push on root stack as well)
 | 
|---|
 | 178 |           if ((OtherWalker != Predecessor) && (OtherWalker->GetTrueFather()->getNr() > RootKeyNr)) { // only pass through those with label bigger than Root's
 | 
|---|
 | 179 |             // add the bond in between to the SP list
 | 
|---|
| [7d82a5] | 180 |             Binder.reset(new bond(Walker, OtherWalker)); // create a new bond in such a manner, that bond::rightatom is always the one more distant
 | 
|---|
| [212c179] | 181 |             BondsPerSPList[SP+1].push_back(Binder);
 | 
|---|
 | 182 |             BondsPerSPCount[SP+1]++;
 | 
|---|
| [8ac66b4] | 183 |             LOG(3, "DEBUG: Added its bond to SP list, having now " << BondsPerSPCount[SP+1] << " item(s).");
 | 
|---|
| [212c179] | 184 |           } else {
 | 
|---|
 | 185 |             if (OtherWalker != Predecessor)
 | 
|---|
| [8ac66b4] | 186 |               LOG(3, "DEBUG: Not passing on, as index of " << *OtherWalker << " " << OtherWalker->GetTrueFather()->getNr() << " is smaller than that of Root " << RootKeyNr << ".");
 | 
|---|
| [212c179] | 187 |             else
 | 
|---|
| [8ac66b4] | 188 |               LOG(3, "DEBUG: This is my predecessor " << *Predecessor << ".");
 | 
|---|
| [212c179] | 189 |           }
 | 
|---|
| [8ac66b4] | 190 |         } else LOG(2, "DEBUG: Is not in the restricted keyset or skipping hydrogen " << *OtherWalker << ".");
 | 
|---|
| [212c179] | 191 |       }
 | 
|---|
 | 192 |     }
 | 
|---|
 | 193 |   }
 | 
|---|
 | 194 | };
 | 
|---|
 | 195 | 
 | 
|---|
 | 196 | /** prints the Bonds per Shortest Path list in BondsPerShortestPath.
 | 
|---|
 | 197 |  */
 | 
|---|
 | 198 | void BondsPerShortestPath::OutputSPList()
 | 
|---|
 | 199 | {
 | 
|---|
| [8ac66b4] | 200 |   LOG(2, "DEBUG: Printing all found lists.");
 | 
|---|
| [212c179] | 201 |   for(int i=1;i<Order;i++) {    // skip the root edge in the printing
 | 
|---|
| [8ac66b4] | 202 |     LOG(2, "\tCurrent SP level is " << i << ".");
 | 
|---|
 | 203 |     std::stringstream output;
 | 
|---|
| [212c179] | 204 |     for (BondsPerShortestPath::BondsPerSP::const_iterator Binder = BondsPerSPList[i].begin();
 | 
|---|
 | 205 |         Binder != BondsPerSPList[i].end();
 | 
|---|
 | 206 |         ++Binder) {
 | 
|---|
| [8ac66b4] | 207 |       output << "\t" << *Binder;
 | 
|---|
| [212c179] | 208 |     }
 | 
|---|
| [8ac66b4] | 209 |     output << std::endl;
 | 
|---|
 | 210 |     LOG(2, output.str());
 | 
|---|
| [212c179] | 211 |   }
 | 
|---|
 | 212 | };
 | 
|---|
 | 213 | 
 | 
|---|
 | 214 | /** Simply counts all bonds in all BondsPerShortestPath::BondsPerSPList lists.
 | 
|---|
 | 215 |  */
 | 
|---|
 | 216 | int BondsPerShortestPath::CountNumbersInBondsList()
 | 
|---|
 | 217 | {
 | 
|---|
 | 218 |   int SP = -1;  // the Root <-> Root edge must be subtracted!
 | 
|---|
 | 219 |   for(int i=Order;i--;) { // sum up all found edges
 | 
|---|
 | 220 |     for (BondsPerShortestPath::BondsPerSP::const_iterator Binder = BondsPerSPList[i].begin();
 | 
|---|
 | 221 |         Binder != BondsPerSPList[i].end();
 | 
|---|
 | 222 |         ++Binder) {
 | 
|---|
 | 223 |       SP++;
 | 
|---|
 | 224 |     }
 | 
|---|
 | 225 |   }
 | 
|---|
 | 226 |   return SP;
 | 
|---|
 | 227 | };
 | 
|---|