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