| 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 |  * Graph.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Oct 20, 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 <fstream>
 | 
|---|
| 38 | #include <iostream>
 | 
|---|
| 39 | #include <sstream>
 | 
|---|
| 40 | #include <string>
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include "Graph.hpp"
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include "Fragmentation/AdaptivityMap.hpp"
 | 
|---|
| 47 | #include "Helpers/defs.hpp"
 | 
|---|
| 48 | #include "Helpers/helpers.hpp"
 | 
|---|
| 49 | 
 | 
|---|
| 50 | /** Constructor for class Graph.
 | 
|---|
| 51 |  *
 | 
|---|
| 52 |  */
 | 
|---|
| 53 | Graph::Graph()
 | 
|---|
| 54 | {}
 | 
|---|
| 55 | 
 | 
|---|
| 56 | /** Destructor for class Graph.
 | 
|---|
| 57 |  *
 | 
|---|
| 58 |  */
 | 
|---|
| 59 | Graph::~Graph()
 | 
|---|
| 60 | {}
 | 
|---|
| 61 | 
 | 
|---|
| 62 | /** Inserts each KeySet in \a graph into \a this.
 | 
|---|
| 63 |  * \param graph1 graph whose KeySet are inserted into \this graph
 | 
|---|
| 64 |  * \param *counter keyset counter that gets increased
 | 
|---|
| 65 |  */
 | 
|---|
| 66 | void Graph::InsertGraph(const Graph &graph, int &counter)
 | 
|---|
| 67 | {
 | 
|---|
| 68 |   GraphTestPair testGraphInsert;
 | 
|---|
| 69 | 
 | 
|---|
| 70 |   for(Graph::const_iterator runner = graph.begin(); runner != graph.end(); runner++) {
 | 
|---|
| 71 |     testGraphInsert = insert(GraphPair ((*runner).first,pair<int,double>(++counter,((*runner).second).second)));  // store fragment number and current factor
 | 
|---|
| 72 |     if (testGraphInsert.second) {
 | 
|---|
| 73 |       LOG(2, "INFO: KeySet " << counter-1 << " successfully inserted.");
 | 
|---|
| 74 |     } else {
 | 
|---|
| 75 |       LOG(2, "INFO: KeySet " << counter-1 << " failed to insert, present fragment is " << ((*(testGraphInsert.first)).second).first);
 | 
|---|
| 76 |       ((*(testGraphInsert.first)).second).second += (*runner).second.second;
 | 
|---|
| 77 |       LOG(2, "INFO: New factor is " << (*(testGraphInsert.first)).second.second << ".");
 | 
|---|
| 78 |     }
 | 
|---|
| 79 |   }
 | 
|---|
| 80 | };
 | 
|---|
| 81 | 
 | 
|---|
| 82 | /** Parses the KeySet file and fills \a this from the known molecule structure.
 | 
|---|
| 83 |  * Does two-pass scanning:
 | 
|---|
| 84 |  * -# Scans the keyset file and initialises a temporary graph
 | 
|---|
| 85 |  * -# Scans TEFactors file and sets the TEFactor of each key set in the temporary graph accordingly
 | 
|---|
| 86 |  * Finally, the temporary graph is inserted into the given \a FragmentList for return.
 | 
|---|
| 87 |  * \param &path path to file
 | 
|---|
| 88 |  * \return true - parsing successfully, false - failure on parsing (FragmentList will be NULL)
 | 
|---|
| 89 |  */
 | 
|---|
| 90 | bool Graph::ParseKeySetFile(std::string &path)
 | 
|---|
| 91 | {
 | 
|---|
| 92 |   bool status = true;
 | 
|---|
| 93 |   std::ifstream InputFile;
 | 
|---|
| 94 |   std::stringstream line;
 | 
|---|
| 95 |   GraphTestPair testGraphInsert;
 | 
|---|
| 96 |   int NumberOfFragments = 0;
 | 
|---|
| 97 |   std::string filename;
 | 
|---|
| 98 | 
 | 
|---|
| 99 |   // 1st pass: open file and read
 | 
|---|
| 100 |   LOG(1, "INFO: Parsing the KeySet file ... ");
 | 
|---|
| 101 |   filename = path + KEYSETFILE;
 | 
|---|
| 102 |   InputFile.open(filename.c_str());
 | 
|---|
| 103 |   if (InputFile.good()) {
 | 
|---|
| 104 |     // each line represents a new fragment
 | 
|---|
| 105 |     char buffer[MAXSTRINGSIZE];
 | 
|---|
| 106 |     // 1. parse keysets and insert into temp. graph
 | 
|---|
| 107 |     while (!InputFile.eof()) {
 | 
|---|
| 108 |       InputFile.getline(buffer, MAXSTRINGSIZE);
 | 
|---|
| 109 |       KeySet CurrentSet;
 | 
|---|
| 110 |       if ((strlen(buffer) > 0) && (CurrentSet.ScanBufferIntoKeySet(buffer))) {  // if at least one valid atom was added, write config
 | 
|---|
| 111 |         testGraphInsert = insert(GraphPair (CurrentSet,pair<int,double>(NumberOfFragments++,1)));  // store fragment number and current factor
 | 
|---|
| 112 |         if (!testGraphInsert.second) {
 | 
|---|
| 113 |           ELOG(0, "KeySet file must be corrupt as there are two equal key sets therein!");
 | 
|---|
| 114 |           performCriticalExit();
 | 
|---|
| 115 |         }
 | 
|---|
| 116 |       }
 | 
|---|
| 117 |     }
 | 
|---|
| 118 |     // 2. Free and done
 | 
|---|
| 119 |     InputFile.close();
 | 
|---|
| 120 |     InputFile.clear();
 | 
|---|
| 121 |     LOG(1, "INFO: ... done.");
 | 
|---|
| 122 |   } else {
 | 
|---|
| 123 |     ELOG(1, "File " << filename << " not found.");
 | 
|---|
| 124 |     status = false;
 | 
|---|
| 125 |   }
 | 
|---|
| 126 | 
 | 
|---|
| 127 |   return status;
 | 
|---|
| 128 | };
 | 
|---|
| 129 | 
 | 
|---|
| 130 | /** Stores key sets to file.
 | 
|---|
| 131 |  * \param &path path to file
 | 
|---|
| 132 |  * \return true - file written successfully, false - writing failed
 | 
|---|
| 133 |  */
 | 
|---|
| 134 | bool Graph::StoreKeySetFile(std::string &path) const
 | 
|---|
| 135 | {
 | 
|---|
| 136 |   bool status =  true;
 | 
|---|
| 137 |   std::string line = path + KEYSETFILE;
 | 
|---|
| 138 |   std::ofstream output(line.c_str());
 | 
|---|
| 139 | 
 | 
|---|
| 140 |   // open KeySet file
 | 
|---|
| 141 |   LOG(1, "INFO: Saving key sets of the total graph ... ");
 | 
|---|
| 142 |   if(output.good()) {
 | 
|---|
| 143 |     for(Graph::const_iterator runner = begin(); runner != end(); runner++) {
 | 
|---|
| 144 |       for (KeySet::const_iterator sprinter = (*runner).first.begin();sprinter != (*runner).first.end(); sprinter++) {
 | 
|---|
| 145 |         if (sprinter != (*runner).first.begin())
 | 
|---|
| 146 |           output << "\t";
 | 
|---|
| 147 |         output << *sprinter;
 | 
|---|
| 148 |       }
 | 
|---|
| 149 |       output << std::endl;
 | 
|---|
| 150 |     }
 | 
|---|
| 151 |     LOG(1, "INFO: done.");
 | 
|---|
| 152 |   } else {
 | 
|---|
| 153 |     ELOG(0, "Unable to open " << line << " for writing keysets!");
 | 
|---|
| 154 |     performCriticalExit();
 | 
|---|
| 155 |     status = false;
 | 
|---|
| 156 |   }
 | 
|---|
| 157 |   output.close();
 | 
|---|
| 158 | 
 | 
|---|
| 159 |   return status;
 | 
|---|
| 160 | };
 | 
|---|
| 161 | 
 | 
|---|
| 162 | /** Parses the TE factors file and fills \a *FragmentList from the known molecule structure.
 | 
|---|
| 163 |  * -# Scans TEFactors file and sets the TEFactor of each key set in the temporary graph accordingly
 | 
|---|
| 164 |  * \param *path path to file
 | 
|---|
| 165 |  * \return true - parsing successfully, false - failure on parsing
 | 
|---|
| 166 |  */
 | 
|---|
| 167 | bool Graph::ParseTEFactorsFile(char *path)
 | 
|---|
| 168 | {
 | 
|---|
| 169 |   bool status = true;
 | 
|---|
| 170 |   std::ifstream InputFile;
 | 
|---|
| 171 |   std::stringstream line;
 | 
|---|
| 172 |   GraphTestPair testGraphInsert;
 | 
|---|
| 173 |   int NumberOfFragments = 0;
 | 
|---|
| 174 |   double TEFactor;
 | 
|---|
| 175 |   char filename[MAXSTRINGSIZE];
 | 
|---|
| 176 | 
 | 
|---|
| 177 |   // 2nd pass: open TEFactors file and read
 | 
|---|
| 178 |   LOG(1, "INFO: Parsing the TEFactors file ... ");
 | 
|---|
| 179 |   sprintf(filename, "%s/%s%s", path, FRAGMENTPREFIX, TEFACTORSFILE);
 | 
|---|
| 180 |   InputFile.open(filename);
 | 
|---|
| 181 |   if (InputFile != NULL) {
 | 
|---|
| 182 |     // 3. add found TEFactors to each keyset
 | 
|---|
| 183 |     NumberOfFragments = 0;
 | 
|---|
| 184 |     for(Graph::iterator runner = begin();runner != end(); runner++) {
 | 
|---|
| 185 |       if (!InputFile.eof()) {
 | 
|---|
| 186 |         InputFile >> TEFactor;
 | 
|---|
| 187 |         (*runner).second.second = TEFactor;
 | 
|---|
| 188 |         LOG(2, "INFO: Setting " << ++NumberOfFragments << " fragment's TEFactor to " << (*runner).second.second << ".");
 | 
|---|
| 189 |       } else {
 | 
|---|
| 190 |         status = false;
 | 
|---|
| 191 |         break;
 | 
|---|
| 192 |       }
 | 
|---|
| 193 |     }
 | 
|---|
| 194 |     // 4. Free and done
 | 
|---|
| 195 |     InputFile.close();
 | 
|---|
| 196 |     LOG(1, "INFO: done.");
 | 
|---|
| 197 |   } else {
 | 
|---|
| 198 |     LOG(1, "INFO: File " << filename << " not found.");
 | 
|---|
| 199 |     status = false;
 | 
|---|
| 200 |   }
 | 
|---|
| 201 | 
 | 
|---|
| 202 |   return status;
 | 
|---|
| 203 | };
 | 
|---|
| 204 | 
 | 
|---|
| 205 | /** Stores TEFactors to file.
 | 
|---|
| 206 |  * \param *out output stream for debugging
 | 
|---|
| 207 |  * \param KeySetList Graph with factors
 | 
|---|
| 208 |  * \param *path path to file
 | 
|---|
| 209 |  * \return true - file written successfully, false - writing failed
 | 
|---|
| 210 |  */
 | 
|---|
| 211 | bool Graph::StoreTEFactorsFile(char *path) const
 | 
|---|
| 212 | {
 | 
|---|
| 213 |   ofstream output;
 | 
|---|
| 214 |   bool status =  true;
 | 
|---|
| 215 |   string line;
 | 
|---|
| 216 | 
 | 
|---|
| 217 |   // open TEFactors file
 | 
|---|
| 218 |   line = path;
 | 
|---|
| 219 |   line.append("/");
 | 
|---|
| 220 |   line += FRAGMENTPREFIX;
 | 
|---|
| 221 |   line += TEFACTORSFILE;
 | 
|---|
| 222 |   output.open(line.c_str(), ios::out);
 | 
|---|
| 223 |   LOG(1, "INFO: Saving TEFactors of the total graph ... ");
 | 
|---|
| 224 |   if(output != NULL) {
 | 
|---|
| 225 |     for(Graph::const_iterator runner = begin(); runner != end(); runner++)
 | 
|---|
| 226 |       output << (*runner).second.second << endl;
 | 
|---|
| 227 |     LOG(1, "INFO: done." << endl);
 | 
|---|
| 228 |   } else {
 | 
|---|
| 229 |     ELOG(2, "INFO: failed to open " << line << "." << endl);
 | 
|---|
| 230 |     status = false;
 | 
|---|
| 231 |   }
 | 
|---|
| 232 |   output.close();
 | 
|---|
| 233 | 
 | 
|---|
| 234 |   return status;
 | 
|---|
| 235 | };
 | 
|---|
| 236 | 
 | 
|---|
| 237 | /** For a given graph, sorts KeySets into a (index, keyset) map.
 | 
|---|
| 238 |  * \return ref to allocated map from index to keyset
 | 
|---|
| 239 |  */
 | 
|---|
| 240 | AdaptivityMap * Graph::GraphToAdaptivityMap() const
 | 
|---|
| 241 | {
 | 
|---|
| 242 |   AdaptivityMap *IndexKeySetList = new AdaptivityMap;
 | 
|---|
| 243 |   for(const_iterator runner = begin(); runner != end(); runner++) {
 | 
|---|
| 244 |     IndexKeySetList->insert( pair<int,KeySet>(runner->second.first,runner->first) );
 | 
|---|
| 245 |   }
 | 
|---|
| 246 |   return IndexKeySetList;
 | 
|---|
| 247 | };
 | 
|---|