[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);
|
---|
| 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();
|
---|
| 136 | // go through each of its bonds
|
---|
| 137 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
---|
| 138 | Runner != ListOfBonds.end();
|
---|
| 139 | ++Runner) {
|
---|
| 140 | const atomId_t id = (*Runner)->GetOtherAtom(Walker)->getId();
|
---|
| 141 | ASSERT(id != (size_t)-1,
|
---|
[5197e5] | 142 | "AdjacencyList::CreateMap() - OtherAtom has not id.");
|
---|
[3aa8a5] | 143 | if (std::binary_search(atomids.begin(), atomids.end(), id))
|
---|
| 144 | atombondmap.insert( std::make_pair(WalkerId, id) );
|
---|
[13a953] | 145 | }
|
---|
| 146 | }
|
---|
[ec87e4] | 147 | }
|
---|
| 148 |
|
---|
[0fad93] | 149 | AdjacencyList::KeysSet AdjacencyList::getKeys(const AdjacencyList::AtomBondRange &_range) const
|
---|
[ec87e4] | 150 | {
|
---|
[42c9e2] | 151 | KeysSet Keys;
|
---|
| 152 | for (AtomBondMap::const_iterator iter = _range.first;
|
---|
[ec87e4] | 153 | iter != _range.second;
|
---|
| 154 | ++iter) {
|
---|
| 155 | Keys.insert( iter->first );
|
---|
| 156 | }
|
---|
| 157 | return Keys;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[0fad93] | 160 | AdjacencyList::ValuesSet AdjacencyList::getValues(const AdjacencyList::AtomBondRange&_range) const
|
---|
[ec87e4] | 161 | {
|
---|
[42c9e2] | 162 | ValuesSet Values;
|
---|
| 163 | for (AtomBondMap::const_iterator iter = _range.first;
|
---|
[ec87e4] | 164 | iter != _range.second;
|
---|
| 165 | ++iter) {
|
---|
| 166 | Values.insert( iter->second );
|
---|
| 167 | }
|
---|
| 168 | return Values;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[06f41f3] | 171 | /** Counts the number of items in the second set not present in the first set.
|
---|
| 172 | *
|
---|
| 173 | * \note We assume that the sets are sorted.
|
---|
[ec87e4] | 174 | *
|
---|
[06f41f3] | 175 | * @param firstset check set
|
---|
| 176 | * @param secondset reference set
|
---|
| 177 | * @return number of items in the first set that are missing in the second
|
---|
[ec87e4] | 178 | */
|
---|
| 179 | template <class T>
|
---|
[06f41f3] | 180 | size_t getMissingItems(const T &firstset, const T &secondset)
|
---|
[ec87e4] | 181 | {
|
---|
| 182 | size_t Mismatch = 0;
|
---|
| 183 | typename T::const_iterator firstiter = firstset.begin();
|
---|
| 184 | typename T::const_iterator seconditer = secondset.begin();
|
---|
[06f41f3] | 185 | for (; (firstiter != firstset.end()) && (seconditer != secondset.end());) {
|
---|
| 186 | if (*firstiter > *seconditer)
|
---|
| 187 | ++seconditer;
|
---|
| 188 | else {
|
---|
| 189 | if (*firstiter < *seconditer)
|
---|
| 190 | ++Mismatch;
|
---|
| 191 | ++firstiter;
|
---|
| 192 | }
|
---|
[ec87e4] | 193 | }
|
---|
| 194 | return Mismatch;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[3aa8a5] | 197 | /** Compares whether AdjacencyList::atombondmap in this instance is a subset of
|
---|
| 198 | * the one in \a other.
|
---|
[ec87e4] | 199 | *
|
---|
[3aa8a5] | 200 | * @param other other instance of an adjacency list to compare against
|
---|
| 201 | * @return true - this atombondmap is subset, false - else
|
---|
[ec87e4] | 202 | */
|
---|
[3aa8a5] | 203 | bool AdjacencyList::operator<(const AdjacencyList &other) const
|
---|
[ec87e4] | 204 | {
|
---|
[3aa8a5] | 205 | int NonMatchNumber = 0;
|
---|
[ec87e4] | 206 | // extract keys and check whether they match
|
---|
[3aa8a5] | 207 | const AtomBondRange Intrange(atombondmap.begin(), atombondmap.end());
|
---|
| 208 | const AtomBondRange Extrange(other.atombondmap.begin(), other.atombondmap.end());
|
---|
[42c9e2] | 209 | KeysSet InternalKeys( getKeys(Intrange) );
|
---|
| 210 | KeysSet ExternalKeys( getKeys(Extrange) );
|
---|
[ec87e4] | 211 |
|
---|
| 212 | // std::cout << "InternalKeys: " << InternalKeys << std::endl;
|
---|
| 213 | // std::cout << "ExternalKeys: " << ExternalKeys << std::endl;
|
---|
| 214 |
|
---|
[3aa8a5] | 215 | // check amount of keys
|
---|
| 216 | if (ExternalKeys.size() < InternalKeys.size()) {
|
---|
| 217 | NonMatchNumber = (int)InternalKeys.size() - (int)ExternalKeys.size();
|
---|
| 218 | LOG(2, "INFO: Number of internal keys exceeds external one by " << NonMatchNumber << ".");
|
---|
[ec87e4] | 219 | return false;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[06f41f3] | 222 | // check which keys are missing in the internal set
|
---|
[3aa8a5] | 223 | NonMatchNumber = getMissingItems(InternalKeys, ExternalKeys);
|
---|
[ec87e4] | 224 |
|
---|
| 225 | if (NonMatchNumber != 0) {
|
---|
| 226 | LOG(2, "INFO: " << NonMatchNumber << " keys are not the same.");
|
---|
| 227 | return false;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | // now check each map per key
|
---|
[3aa8a5] | 231 | for (KeysSet::const_iterator keyIter = InternalKeys.begin();
|
---|
| 232 | keyIter != InternalKeys.end();
|
---|
[ec87e4] | 233 | ++keyIter) {
|
---|
| 234 | // std::cout << "Current key is " << *keyIter << std::endl;
|
---|
[3aa8a5] | 235 | const AtomBondRange IntRange( atombondmap.equal_range(*keyIter) );
|
---|
| 236 | const AtomBondRange ExtRange( other.atombondmap.equal_range(*keyIter) );
|
---|
[42c9e2] | 237 | ValuesSet InternalValues( getValues(IntRange) );
|
---|
| 238 | ValuesSet ExternalValues( getValues(ExtRange) );
|
---|
[06f41f3] | 239 | // throw out all values not present in ExternalKeys
|
---|
| 240 | ValuesSet ExternalValues_temp( ExternalValues );
|
---|
[3aa8a5] | 241 | for(KeysSet::const_iterator iter = InternalKeys.begin();
|
---|
| 242 | iter != InternalKeys.end(); ++iter)
|
---|
[06f41f3] | 243 | ExternalValues_temp.erase(*iter);
|
---|
| 244 | // all remaining values must be masked out
|
---|
| 245 | for (ValuesSet::const_iterator iter = ExternalValues_temp.begin();
|
---|
| 246 | iter != ExternalValues_temp.end(); ++iter)
|
---|
| 247 | ExternalValues.erase(*iter);
|
---|
[ec87e4] | 248 | // std::cout << "InternalValues: " << InternalValues << std::endl;
|
---|
| 249 | // std::cout << "ExternalValues: " << ExternalValues << std::endl;
|
---|
[3aa8a5] | 250 | NonMatchNumber += getMissingItems(InternalValues, ExternalValues);
|
---|
[ec87e4] | 251 | }
|
---|
| 252 | if (NonMatchNumber != 0) {
|
---|
| 253 | LOG(2, "INFO: " << NonMatchNumber << " keys are not the same.");
|
---|
| 254 | return false;
|
---|
| 255 | } else {
|
---|
| 256 | LOG(2, "INFO: All keys are the same.");
|
---|
| 257 | return true;
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
[3aa8a5] | 260 |
|
---|
| 261 | /** Storing the bond structure of a molecule to file.
|
---|
| 262 | * Simply stores Atom::Nr and then the Atom::Nr of all bond partners per line.
|
---|
| 263 | * @param File output stream to write to
|
---|
| 264 | * \return true - file written successfully, false - writing failed
|
---|
| 265 | */
|
---|
| 266 | bool AdjacencyList::StoreToFile(std::ostream &File) const
|
---|
| 267 | {
|
---|
| 268 | bool status = true;
|
---|
| 269 | std::stringstream output;
|
---|
| 270 | output << "Saving adjacency list ... ";
|
---|
| 271 | if (!File.bad()) {
|
---|
| 272 | //File << "m\tn" << endl;
|
---|
| 273 | AtomBondMap::const_iterator advanceiter = atombondmap.begin();
|
---|
| 274 | for (AtomBondMap::const_iterator iter = atombondmap.begin();
|
---|
| 275 | iter != atombondmap.end();
|
---|
| 276 | iter = advanceiter) {
|
---|
| 277 | advanceiter = atombondmap.upper_bound(iter->first);
|
---|
| 278 | File << iter->first+1;
|
---|
| 279 | for (AtomBondMap::const_iterator adjacencyiter = iter;
|
---|
| 280 | adjacencyiter != advanceiter;
|
---|
| 281 | ++adjacencyiter)
|
---|
| 282 | File << " " << adjacencyiter->second+1;
|
---|
| 283 | File << std::endl;
|
---|
| 284 | }
|
---|
| 285 | output << "done.";
|
---|
| 286 | } else {
|
---|
| 287 | output << "given stream is not good.";
|
---|
| 288 | status = false;
|
---|
| 289 | }
|
---|
| 290 | LOG(1, output.str());
|
---|
| 291 |
|
---|
| 292 | return status;
|
---|
| 293 | }
|
---|