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