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(Graph &graph, int *counter)
|
---|
67 | {
|
---|
68 | GraphTestPair testGraphInsert;
|
---|
69 |
|
---|
70 | for(Graph::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)
|
---|
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::iterator runner = begin(); runner != end(); runner++) {
|
---|
144 | for (KeySet::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 | output.clear();
|
---|
159 |
|
---|
160 | return status;
|
---|
161 | };
|
---|
162 |
|
---|
163 | /** Parses the TE factors file and fills \a *FragmentList from the known molecule structure.
|
---|
164 | * -# Scans TEFactors file and sets the TEFactor of each key set in the temporary graph accordingly
|
---|
165 | * \param *path path to file
|
---|
166 | * \return true - parsing successfully, false - failure on parsing
|
---|
167 | */
|
---|
168 | bool Graph::ParseTEFactorsFile(char *path)
|
---|
169 | {
|
---|
170 | bool status = true;
|
---|
171 | std::ifstream InputFile;
|
---|
172 | std::stringstream line;
|
---|
173 | GraphTestPair testGraphInsert;
|
---|
174 | int NumberOfFragments = 0;
|
---|
175 | double TEFactor;
|
---|
176 | char filename[MAXSTRINGSIZE];
|
---|
177 |
|
---|
178 | // 2nd pass: open TEFactors file and read
|
---|
179 | LOG(1, "INFO: Parsing the TEFactors file ... ");
|
---|
180 | sprintf(filename, "%s/%s%s", path, FRAGMENTPREFIX, TEFACTORSFILE);
|
---|
181 | InputFile.open(filename);
|
---|
182 | if (InputFile != NULL) {
|
---|
183 | // 3. add found TEFactors to each keyset
|
---|
184 | NumberOfFragments = 0;
|
---|
185 | for(Graph::iterator runner = begin();runner != end(); runner++) {
|
---|
186 | if (!InputFile.eof()) {
|
---|
187 | InputFile >> TEFactor;
|
---|
188 | (*runner).second.second = TEFactor;
|
---|
189 | LOG(2, "INFO: Setting " << ++NumberOfFragments << " fragment's TEFactor to " << (*runner).second.second << ".");
|
---|
190 | } else {
|
---|
191 | status = false;
|
---|
192 | break;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | // 4. Free and done
|
---|
196 | InputFile.close();
|
---|
197 | LOG(1, "INFO: done.");
|
---|
198 | } else {
|
---|
199 | LOG(1, "INFO: File " << filename << " not found.");
|
---|
200 | status = false;
|
---|
201 | }
|
---|
202 |
|
---|
203 | return status;
|
---|
204 | };
|
---|
205 |
|
---|
206 | /** Stores TEFactors to file.
|
---|
207 | * \param *out output stream for debugging
|
---|
208 | * \param KeySetList Graph with factors
|
---|
209 | * \param *path path to file
|
---|
210 | * \return true - file written successfully, false - writing failed
|
---|
211 | */
|
---|
212 | bool Graph::StoreTEFactorsFile(char *path)
|
---|
213 | {
|
---|
214 | ofstream output;
|
---|
215 | bool status = true;
|
---|
216 | string line;
|
---|
217 |
|
---|
218 | // open TEFactors file
|
---|
219 | line = path;
|
---|
220 | line.append("/");
|
---|
221 | line += FRAGMENTPREFIX;
|
---|
222 | line += TEFACTORSFILE;
|
---|
223 | output.open(line.c_str(), ios::out);
|
---|
224 | LOG(1, "INFO: Saving TEFactors of the total graph ... ");
|
---|
225 | if(output != NULL) {
|
---|
226 | for(Graph::iterator runner = begin(); runner != end(); runner++)
|
---|
227 | output << (*runner).second.second << endl;
|
---|
228 | LOG(1, "INFO: done." << endl);
|
---|
229 | } else {
|
---|
230 | ELOG(2, "INFO: failed to open " << line << "." << endl);
|
---|
231 | status = false;
|
---|
232 | }
|
---|
233 | output.close();
|
---|
234 |
|
---|
235 | return status;
|
---|
236 | };
|
---|
237 |
|
---|
238 | /** For a given graph, sorts KeySets into a (index, keyset) map.
|
---|
239 | * \return ref to allocated map from index to keyset
|
---|
240 | */
|
---|
241 | AdaptivityMap * Graph::GraphToAdaptivityMap() const
|
---|
242 | {
|
---|
243 | AdaptivityMap *IndexKeySetList = new AdaptivityMap;
|
---|
244 | for(const_iterator runner = begin(); runner != end(); runner++) {
|
---|
245 | IndexKeySetList->insert( pair<int,KeySet>(runner->second.first,runner->first) );
|
---|
246 | }
|
---|
247 | return IndexKeySetList;
|
---|
248 | };
|
---|