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