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