| [13a953] | 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/>.
 | 
|---|
| [13a953] | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | /*
 | 
|---|
| [0fad93] | 24 |  * AdjacencyList.cpp
 | 
|---|
| [13a953] | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Mar 3, 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 <iostream>
 | 
|---|
| [42c9e2] | 38 | #include <map>
 | 
|---|
 | 39 | #include <set>
 | 
|---|
 | 40 | #include <utility>
 | 
|---|
| [13a953] | 41 | 
 | 
|---|
| [0fad93] | 42 | #include "AdjacencyList.hpp"
 | 
|---|
| [13a953] | 43 | 
 | 
|---|
| [6f0841] | 44 | #include "Atom/atom.hpp"
 | 
|---|
| [13a953] | 45 | #include "Bond/bond.hpp"
 | 
|---|
 | 46 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [255829] | 47 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [ec87e4] | 48 | #include "CodePatterns/Range.hpp"
 | 
|---|
 | 49 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| [255829] | 50 | #include "Helpers/defs.hpp"
 | 
|---|
| [ec87e4] | 51 | #include "World.hpp"
 | 
|---|
| [13a953] | 52 | 
 | 
|---|
| [0fad93] | 53 | /** Constructor of class AdjacencyList.
 | 
|---|
| [3501d2] | 54 |  *
 | 
|---|
 | 55 |  * \param File file to parser
 | 
|---|
 | 56 |  */
 | 
|---|
| [3aa8a5] | 57 | AdjacencyList::AdjacencyList(std::istream &File)
 | 
|---|
| [13a953] | 58 | {
 | 
|---|
| [3aa8a5] | 59 |   bool status = ParseFromFile(File);
 | 
|---|
| [b6b5b8] | 60 | //  ASSERT( status,
 | 
|---|
 | 61 | //      "AdjacencyList::AdjacencyList() - File's contents was nor parsable");
 | 
|---|
| [06f41f3] | 62 |   if (!status) // remove map if failed to parse
 | 
|---|
| [3aa8a5] | 63 |     atombondmap.clear();
 | 
|---|
 | 64 | }
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 | /** Constructor of class AdjacencyList.
 | 
|---|
 | 67 |  *
 | 
|---|
 | 68 |  * \param File file to parser
 | 
|---|
 | 69 |  */
 | 
|---|
 | 70 | AdjacencyList::AdjacencyList(const atomids_t &atomids)
 | 
|---|
 | 71 | {
 | 
|---|
 | 72 |   CreateMap(atomids);
 | 
|---|
| [13a953] | 73 | }
 | 
|---|
 | 74 | 
 | 
|---|
| [0fad93] | 75 | AdjacencyList::~AdjacencyList()
 | 
|---|
| [06f41f3] | 76 | {}
 | 
|---|
| [13a953] | 77 | 
 | 
|---|
| [3aa8a5] | 78 | /** Parses the bond partners of each atom from an external file into AdjacencyList::atombondmap.
 | 
|---|
| [ec87e4] | 79 |  *
 | 
|---|
 | 80 |  * @param File file to parse
 | 
|---|
 | 81 |  * @return true - everything ok, false - error while parsing
 | 
|---|
| [13a953] | 82 |  */
 | 
|---|
| [3aa8a5] | 83 | bool AdjacencyList::ParseFromFile(std::istream &File)
 | 
|---|
| [13a953] | 84 | {
 | 
|---|
 | 85 |   if (File.fail()) {
 | 
|---|
 | 86 |     LOG(1, "STATUS: Adjacency file not found." << endl);
 | 
|---|
 | 87 |     return false;
 | 
|---|
 | 88 |   }
 | 
|---|
 | 89 | 
 | 
|---|
| [5197e5] | 90 |   atombondmap.clear();
 | 
|---|
| [13a953] | 91 |   char buffer[MAXSTRINGSIZE];
 | 
|---|
 | 92 |   int tmp;
 | 
|---|
 | 93 |   // Parse the file line by line and count the bonds
 | 
|---|
| [3aa8a5] | 94 |   while (File.good()) {
 | 
|---|
| [13a953] | 95 |     File.getline(buffer, MAXSTRINGSIZE);
 | 
|---|
 | 96 |     stringstream line;
 | 
|---|
 | 97 |     line.str(buffer);
 | 
|---|
 | 98 |     int AtomNr = -1;
 | 
|---|
 | 99 |     line >> AtomNr;
 | 
|---|
 | 100 |     // parse into structure
 | 
|---|
| [52ed5b] | 101 |     if (AtomNr > 0) {
 | 
|---|
| [3aa8a5] | 102 |       const atomId_t WalkerId = AtomNr-1;
 | 
|---|
| [ec87e4] | 103 |       // parse bond partner ids associated to AtomNr
 | 
|---|
| [13a953] | 104 |       while (line >> ws >> tmp) {
 | 
|---|
| [06f41f3] | 105 |         LOG(3, "INFO: Recognized bond partner " << tmp-1 << " for " << WalkerId << ".");
 | 
|---|
| [5197e5] | 106 |         atombondmap.insert( std::make_pair(WalkerId, tmp-1) );
 | 
|---|
| [13a953] | 107 |       }
 | 
|---|
 | 108 |     } else {
 | 
|---|
| [ec87e4] | 109 |       if (AtomNr != -1) {
 | 
|---|
| [13a953] | 110 |         ELOG(2, AtomNr << " is negative.");
 | 
|---|
| [ec87e4] | 111 |         return false;
 | 
|---|
 | 112 |       }
 | 
|---|
 | 113 |     }
 | 
|---|
 | 114 |   }
 | 
|---|
 | 115 |   return true;
 | 
|---|
 | 116 | }
 | 
|---|
 | 117 | 
 | 
|---|
| [3aa8a5] | 118 | /** Fills the AdjacencyList::atombondmap from the atoms given by the two iterators.
 | 
|---|
| [ec87e4] | 119 |  *
 | 
|---|
| [06f41f3] | 120 |  * @param atomids set of atomic ids to check (must be global ids, i.e. from atom::getId())
 | 
|---|
| [ec87e4] | 121 |  */
 | 
|---|
| [3aa8a5] | 122 | void AdjacencyList::CreateMap(atomids_t atomids)
 | 
|---|
| [ec87e4] | 123 | {
 | 
|---|
| [5197e5] | 124 |   atombondmap.clear();
 | 
|---|
| [3aa8a5] | 125 |   std::sort(atomids.begin(), atomids.end());
 | 
|---|
| [ec87e4] | 126 |   // go through each atom in the list
 | 
|---|
| [06f41f3] | 127 |   for (atomids_t::const_iterator iter = atomids.begin(); iter != atomids.end(); ++iter) {
 | 
|---|
 | 128 |     const atomId_t WalkerId = *iter;
 | 
|---|
| [ec87e4] | 129 |     ASSERT(WalkerId != (size_t)-1,
 | 
|---|
| [5197e5] | 130 |         "AdjacencyList::CreateMap() - Walker has no id.");
 | 
|---|
| [06f41f3] | 131 |     const atom *Walker = World::getInstance().getAtom(AtomById(WalkerId));
 | 
|---|
 | 132 |     ASSERT( Walker != NULL,
 | 
|---|
| [5197e5] | 133 |         "AdjacencyList::CreateMap() - Walker id "+toString(*iter)
 | 
|---|
| [06f41f3] | 134 |         +" is not associated to any of World's atoms.");
 | 
|---|
| [ec87e4] | 135 |     const BondList& ListOfBonds = Walker->getListOfBonds();
 | 
|---|
| [abb641] | 136 |     if (ListOfBonds.size() > 0) {
 | 
|---|
 | 137 |       // go through each of its bonds
 | 
|---|
 | 138 |       for (BondList::const_iterator Runner = ListOfBonds.begin();
 | 
|---|
 | 139 |           Runner != ListOfBonds.end();
 | 
|---|
 | 140 |           ++Runner) {
 | 
|---|
 | 141 |         const atomId_t id = (*Runner)->GetOtherAtom(Walker)->getId();
 | 
|---|
 | 142 |         ASSERT(id != (size_t)-1,
 | 
|---|
 | 143 |             "AdjacencyList::CreateMap() - OtherAtom has not id.");
 | 
|---|
 | 144 |         if (std::binary_search(atomids.begin(), atomids.end(), id))
 | 
|---|
 | 145 |           atombondmap.insert( std::make_pair(WalkerId, id) );
 | 
|---|
 | 146 |       }
 | 
|---|
 | 147 |     } else {
 | 
|---|
 | 148 |       // insert self-bond if atom is lonesome cowboy
 | 
|---|
 | 149 |       atombondmap.insert( std::make_pair(WalkerId, WalkerId) );
 | 
|---|
| [13a953] | 150 |     }
 | 
|---|
 | 151 |   }
 | 
|---|
| [ec87e4] | 152 | }
 | 
|---|
 | 153 | 
 | 
|---|
| [0fad93] | 154 | AdjacencyList::KeysSet AdjacencyList::getKeys(const AdjacencyList::AtomBondRange &_range) const
 | 
|---|
| [ec87e4] | 155 | {
 | 
|---|
| [42c9e2] | 156 |   KeysSet Keys;
 | 
|---|
 | 157 |   for (AtomBondMap::const_iterator iter = _range.first;
 | 
|---|
| [ec87e4] | 158 |       iter != _range.second;
 | 
|---|
 | 159 |       ++iter) {
 | 
|---|
 | 160 |     Keys.insert( iter->first );
 | 
|---|
 | 161 |   }
 | 
|---|
 | 162 |   return Keys;
 | 
|---|
 | 163 | }
 | 
|---|
 | 164 | 
 | 
|---|
| [0fad93] | 165 | AdjacencyList::ValuesSet AdjacencyList::getValues(const AdjacencyList::AtomBondRange&_range) const
 | 
|---|
| [ec87e4] | 166 | {
 | 
|---|
| [42c9e2] | 167 |   ValuesSet Values;
 | 
|---|
 | 168 |   for (AtomBondMap::const_iterator iter = _range.first;
 | 
|---|
| [ec87e4] | 169 |       iter != _range.second;
 | 
|---|
 | 170 |       ++iter) {
 | 
|---|
 | 171 |     Values.insert( iter->second );
 | 
|---|
 | 172 |   }
 | 
|---|
 | 173 |   return Values;
 | 
|---|
 | 174 | }
 | 
|---|
 | 175 | 
 | 
|---|
| [06f41f3] | 176 | /** Counts the number of items in the second set not present in the first set.
 | 
|---|
 | 177 |  *
 | 
|---|
 | 178 |  * \note We assume that the sets are sorted.
 | 
|---|
| [ec87e4] | 179 |  *
 | 
|---|
| [06f41f3] | 180 |  * @param firstset check set
 | 
|---|
 | 181 |  * @param secondset reference set
 | 
|---|
 | 182 |  * @return number of items in the first set that are missing in the second
 | 
|---|
| [ec87e4] | 183 |  */
 | 
|---|
 | 184 | template <class T>
 | 
|---|
| [06f41f3] | 185 | size_t getMissingItems(const T &firstset, const T &secondset)
 | 
|---|
| [ec87e4] | 186 | {
 | 
|---|
 | 187 |   size_t Mismatch = 0;
 | 
|---|
 | 188 |   typename T::const_iterator firstiter = firstset.begin();
 | 
|---|
 | 189 |   typename T::const_iterator seconditer = secondset.begin();
 | 
|---|
| [06f41f3] | 190 |   for (; (firstiter != firstset.end()) && (seconditer != secondset.end());) {
 | 
|---|
 | 191 |     if (*firstiter > *seconditer)
 | 
|---|
 | 192 |       ++seconditer;
 | 
|---|
 | 193 |     else {
 | 
|---|
 | 194 |       if (*firstiter < *seconditer)
 | 
|---|
 | 195 |         ++Mismatch;
 | 
|---|
 | 196 |       ++firstiter;
 | 
|---|
 | 197 |     }
 | 
|---|
| [ec87e4] | 198 |   }
 | 
|---|
 | 199 |   return Mismatch;
 | 
|---|
 | 200 | }
 | 
|---|
 | 201 | 
 | 
|---|
| [3aa8a5] | 202 | /** Compares whether AdjacencyList::atombondmap in this instance is a subset of
 | 
|---|
 | 203 |  * the one in \a other.
 | 
|---|
| [ec87e4] | 204 |  *
 | 
|---|
| [3aa8a5] | 205 |  * @param other other instance of an adjacency list to compare against
 | 
|---|
 | 206 |  * @return true - this atombondmap is subset, false - else
 | 
|---|
| [ec87e4] | 207 |  */
 | 
|---|
| [3aa8a5] | 208 | bool AdjacencyList::operator<(const AdjacencyList &other) const
 | 
|---|
| [ec87e4] | 209 | {
 | 
|---|
| [3aa8a5] | 210 |   int NonMatchNumber = 0;
 | 
|---|
| [ec87e4] | 211 |   // extract keys and check whether they match
 | 
|---|
| [3aa8a5] | 212 |   const AtomBondRange Intrange(atombondmap.begin(), atombondmap.end());
 | 
|---|
 | 213 |   const AtomBondRange Extrange(other.atombondmap.begin(), other.atombondmap.end());
 | 
|---|
| [42c9e2] | 214 |   KeysSet InternalKeys( getKeys(Intrange) );
 | 
|---|
 | 215 |   KeysSet ExternalKeys( getKeys(Extrange) );
 | 
|---|
| [ec87e4] | 216 | 
 | 
|---|
 | 217 | //  std::cout << "InternalKeys: " << InternalKeys << std::endl;
 | 
|---|
 | 218 | //  std::cout << "ExternalKeys: " << ExternalKeys << std::endl;
 | 
|---|
 | 219 | 
 | 
|---|
| [3aa8a5] | 220 |   // check amount of keys
 | 
|---|
 | 221 |   if (ExternalKeys.size() < InternalKeys.size()) {
 | 
|---|
 | 222 |     NonMatchNumber = (int)InternalKeys.size() - (int)ExternalKeys.size();
 | 
|---|
 | 223 |     LOG(2, "INFO: Number of internal keys exceeds external one by " << NonMatchNumber << ".");
 | 
|---|
| [ec87e4] | 224 |     return false;
 | 
|---|
 | 225 |   }
 | 
|---|
 | 226 | 
 | 
|---|
| [06f41f3] | 227 |   // check which keys are missing in the internal set
 | 
|---|
| [3aa8a5] | 228 |   NonMatchNumber = getMissingItems(InternalKeys, ExternalKeys);
 | 
|---|
| [ec87e4] | 229 | 
 | 
|---|
 | 230 |   if (NonMatchNumber != 0) {
 | 
|---|
 | 231 |     LOG(2, "INFO: " << NonMatchNumber << " keys are not the same.");
 | 
|---|
 | 232 |     return false;
 | 
|---|
 | 233 |   }
 | 
|---|
 | 234 | 
 | 
|---|
 | 235 |   // now check each map per key
 | 
|---|
| [3aa8a5] | 236 |   for (KeysSet::const_iterator keyIter = InternalKeys.begin();
 | 
|---|
 | 237 |       keyIter != InternalKeys.end();
 | 
|---|
| [ec87e4] | 238 |       ++keyIter) {
 | 
|---|
 | 239 | //    std::cout << "Current key is " << *keyIter << std::endl;
 | 
|---|
| [3aa8a5] | 240 |     const AtomBondRange IntRange( atombondmap.equal_range(*keyIter) );
 | 
|---|
 | 241 |     const AtomBondRange ExtRange( other.atombondmap.equal_range(*keyIter) );
 | 
|---|
| [42c9e2] | 242 |     ValuesSet InternalValues( getValues(IntRange) );
 | 
|---|
 | 243 |     ValuesSet ExternalValues( getValues(ExtRange) );
 | 
|---|
| [06f41f3] | 244 |     // throw out all values not present in ExternalKeys
 | 
|---|
 | 245 |     ValuesSet ExternalValues_temp( ExternalValues );
 | 
|---|
| [3aa8a5] | 246 |     for(KeysSet::const_iterator iter = InternalKeys.begin();
 | 
|---|
 | 247 |         iter != InternalKeys.end(); ++iter)
 | 
|---|
| [06f41f3] | 248 |       ExternalValues_temp.erase(*iter);
 | 
|---|
 | 249 |     // all remaining values must be masked out
 | 
|---|
 | 250 |     for (ValuesSet::const_iterator iter = ExternalValues_temp.begin();
 | 
|---|
 | 251 |         iter != ExternalValues_temp.end(); ++iter)
 | 
|---|
 | 252 |       ExternalValues.erase(*iter);
 | 
|---|
| [ec87e4] | 253 | //    std::cout << "InternalValues: " << InternalValues << std::endl;
 | 
|---|
 | 254 | //    std::cout << "ExternalValues: " << ExternalValues << std::endl;
 | 
|---|
| [3aa8a5] | 255 |     NonMatchNumber += getMissingItems(InternalValues, ExternalValues);
 | 
|---|
| [ec87e4] | 256 |   }
 | 
|---|
 | 257 |   if (NonMatchNumber != 0) {
 | 
|---|
 | 258 |     LOG(2, "INFO: " << NonMatchNumber << " keys are not the same.");
 | 
|---|
 | 259 |     return false;
 | 
|---|
 | 260 |   } else {
 | 
|---|
 | 261 |     LOG(2, "INFO: All keys are the same.");
 | 
|---|
 | 262 |     return true;
 | 
|---|
 | 263 |   }
 | 
|---|
 | 264 | }
 | 
|---|
| [3aa8a5] | 265 | 
 | 
|---|
 | 266 | /** Storing the bond structure of a molecule to file.
 | 
|---|
 | 267 |  * Simply stores Atom::Nr and then the Atom::Nr of all bond partners per line.
 | 
|---|
 | 268 |  * @param File output stream to write to
 | 
|---|
 | 269 |  * \return true - file written successfully, false - writing failed
 | 
|---|
 | 270 |  */
 | 
|---|
 | 271 | bool AdjacencyList::StoreToFile(std::ostream &File) const
 | 
|---|
 | 272 | {
 | 
|---|
 | 273 |   bool status = true;
 | 
|---|
 | 274 |   std::stringstream output;
 | 
|---|
 | 275 |   output << "Saving adjacency list ... ";
 | 
|---|
 | 276 |   if (!File.bad()) {
 | 
|---|
 | 277 |     //File << "m\tn" << endl;
 | 
|---|
 | 278 |     AtomBondMap::const_iterator advanceiter = atombondmap.begin();
 | 
|---|
 | 279 |     for (AtomBondMap::const_iterator iter = atombondmap.begin();
 | 
|---|
 | 280 |         iter != atombondmap.end();
 | 
|---|
 | 281 |         iter = advanceiter) {
 | 
|---|
 | 282 |       advanceiter = atombondmap.upper_bound(iter->first);
 | 
|---|
 | 283 |       File << iter->first+1;
 | 
|---|
 | 284 |       for (AtomBondMap::const_iterator adjacencyiter = iter;
 | 
|---|
 | 285 |           adjacencyiter != advanceiter;
 | 
|---|
 | 286 |           ++adjacencyiter)
 | 
|---|
 | 287 |         File << " " << adjacencyiter->second+1;
 | 
|---|
 | 288 |       File << std::endl;
 | 
|---|
 | 289 |     }
 | 
|---|
 | 290 |     output << "done.";
 | 
|---|
 | 291 |   } else {
 | 
|---|
 | 292 |     output << "given stream is not good.";
 | 
|---|
 | 293 |     status = false;
 | 
|---|
 | 294 |   }
 | 
|---|
 | 295 |   LOG(1, output.str());
 | 
|---|
 | 296 | 
 | 
|---|
 | 297 |   return status;
 | 
|---|
 | 298 | }
 | 
|---|