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 | /** \file FormatParserStorage.cpp
|
---|
9 | *
|
---|
10 | * date: Jun, 22 2010
|
---|
11 | * author: heber
|
---|
12 | *
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "Helpers/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include <iostream>
|
---|
23 | #include <fstream>
|
---|
24 |
|
---|
25 | #include "Parser/FormatParserStorage.hpp"
|
---|
26 |
|
---|
27 | #include "Parser/FormatParser.hpp"
|
---|
28 | #include "Parser/MpqcParser.hpp"
|
---|
29 | #include "Parser/PcpParser.hpp"
|
---|
30 | #include "Parser/PdbParser.hpp"
|
---|
31 | #include "Parser/TremoloParser.hpp"
|
---|
32 | #include "Parser/XyzParser.hpp"
|
---|
33 |
|
---|
34 | #include "Helpers/Log.hpp"
|
---|
35 | #include "Helpers/Verbose.hpp"
|
---|
36 |
|
---|
37 | #include "Helpers/Assert.hpp"
|
---|
38 |
|
---|
39 | #include "molecule.hpp"
|
---|
40 |
|
---|
41 | #include "Patterns/Singleton_impl.hpp"
|
---|
42 |
|
---|
43 | /** Increment operator for the enumeration ParserTypes to allow loops.
|
---|
44 | * \param &type value
|
---|
45 | * \return value incremented by one
|
---|
46 | */
|
---|
47 | ParserTypes &operator++(ParserTypes &type)
|
---|
48 | {
|
---|
49 | return type = ParserTypes(type+1);
|
---|
50 | }
|
---|
51 |
|
---|
52 | /** Constructor of class FormatParserStorage.
|
---|
53 | */
|
---|
54 | FormatParserStorage::FormatParserStorage()
|
---|
55 | {
|
---|
56 | ParserList.resize(ParserTypes_end, NULL);
|
---|
57 | ParserStream.resize(ParserTypes_end, NULL);
|
---|
58 | ParserPresent.resize(ParserTypes_end, false);
|
---|
59 |
|
---|
60 | ParserNames[mpqc] = "mpqc";
|
---|
61 | ParserNames[pcp] = "pcp";
|
---|
62 | ParserNames[pdb] = "pdb";
|
---|
63 | ParserNames[tremolo] = "tremolo";
|
---|
64 | ParserNames[xyz] = "xyz";
|
---|
65 |
|
---|
66 | for (std::map<ParserTypes, std::string>::const_iterator it = ParserNames.begin(); it != ParserNames.end(); ++it)
|
---|
67 | ParserLookupNames.insert(pair<std::string, ParserTypes>(it->second,it->first) );
|
---|
68 |
|
---|
69 | ParserSuffixes[mpqc] = "in";
|
---|
70 | ParserSuffixes[pcp] = "conf";
|
---|
71 | ParserSuffixes[pdb] = "pdb";
|
---|
72 | ParserSuffixes[tremolo] = "data";
|
---|
73 | ParserSuffixes[xyz] = "xyz";
|
---|
74 |
|
---|
75 | for (std::map<ParserTypes, std::string>::const_iterator it = ParserSuffixes.begin(); it != ParserSuffixes.end(); ++it)
|
---|
76 | ParserLookupSuffixes.insert(pair<std::string, ParserTypes>(it->second,it->first) );
|
---|
77 |
|
---|
78 | ParserAddFunction[mpqc] = &FormatParserStorage::addMpqc;
|
---|
79 | ParserAddFunction[pcp] = &FormatParserStorage::addPcp;
|
---|
80 | ParserAddFunction[pdb] = &FormatParserStorage::addPdb;
|
---|
81 | ParserAddFunction[tremolo] = &FormatParserStorage::addTremolo;
|
---|
82 | ParserAddFunction[xyz] = &FormatParserStorage::addXyz;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** Destructor of class FormatParserStorage.
|
---|
86 | * Free all stored FormatParsers.
|
---|
87 | * Save on Exit.
|
---|
88 | */
|
---|
89 | FormatParserStorage::~FormatParserStorage()
|
---|
90 | {
|
---|
91 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
92 | if (ParserPresent[iter]) {
|
---|
93 | if (ParserStream[iter]->is_open())
|
---|
94 | ParserStream[iter]->close();
|
---|
95 | delete ParserStream[iter];
|
---|
96 | delete ParserList[iter];
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Sets the filename of all current parsers in storage to prefix.suffix.
|
---|
101 | * \param &prefix prefix to use.
|
---|
102 | */
|
---|
103 | void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
|
---|
104 | {
|
---|
105 | prefix=_prefix;
|
---|
106 | };
|
---|
107 |
|
---|
108 |
|
---|
109 | void FormatParserStorage::SaveAll()
|
---|
110 | {
|
---|
111 | std::string filename;
|
---|
112 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
113 | if (ParserPresent[iter]) {
|
---|
114 | filename = prefix;
|
---|
115 | filename += ".";
|
---|
116 | filename += ParserSuffixes[iter];
|
---|
117 | ParserStream[iter] = new std::ofstream(filename.c_str());
|
---|
118 | ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | /** Adds an MpqcParser to the storage.
|
---|
124 | */
|
---|
125 | void FormatParserStorage::addMpqc()
|
---|
126 | {
|
---|
127 | if (!ParserPresent[mpqc]) {
|
---|
128 | ParserList[mpqc] = dynamic_cast<FormatParser *>(new MpqcParser);
|
---|
129 | ParserPresent[mpqc] = true;
|
---|
130 | }
|
---|
131 | else
|
---|
132 | DoeLog(1) && (eLog() << Verbose(1) << "Parser mpqc is already present." << endl);
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | /** Adds an PcpParser to the storage.
|
---|
137 | */
|
---|
138 | void FormatParserStorage::addPcp()
|
---|
139 | {
|
---|
140 | if (!ParserPresent[pcp]) {
|
---|
141 | ParserList[pcp] = new PcpParser();
|
---|
142 | ParserPresent[pcp] = true;
|
---|
143 | } else
|
---|
144 | DoeLog(1) && (eLog() << Verbose(1) << "Parser pcp is already present." << endl);
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | /** Adds an PdbParser to the storage.
|
---|
149 | */
|
---|
150 | void FormatParserStorage::addPdb()
|
---|
151 | {
|
---|
152 | if (!ParserPresent[pdb]) {
|
---|
153 | ParserList[pdb] = new PdbParser();
|
---|
154 | ParserPresent[pdb] = true;
|
---|
155 | } else
|
---|
156 | DoeLog(1) && (eLog() << Verbose(1) << "Parser pdb is already present." << endl);
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | /** Adds an TremoloParser to the storage.
|
---|
161 | */
|
---|
162 | void FormatParserStorage::addTremolo()
|
---|
163 | {
|
---|
164 | if (!ParserPresent[tremolo]) {
|
---|
165 | ParserList[tremolo] = new TremoloParser();
|
---|
166 | ParserPresent[tremolo] = true;
|
---|
167 | } else
|
---|
168 | DoeLog(1) && (eLog() << Verbose(1) << "Parser tremolo is already present." << endl);
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | /** Adds an XyzParser to the storage.
|
---|
173 | */
|
---|
174 | void FormatParserStorage::addXyz()
|
---|
175 | {
|
---|
176 | if (!ParserPresent[xyz]) {
|
---|
177 | ParserList[xyz] = new XyzParser();
|
---|
178 | ParserPresent[xyz] = true;
|
---|
179 | } else
|
---|
180 | DoeLog(1) && (eLog() << Verbose(1) << "Parser xyz is already present." << endl);
|
---|
181 | }
|
---|
182 |
|
---|
183 | ParserTypes FormatParserStorage::getTypeFromName(std::string type)
|
---|
184 | {
|
---|
185 | if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
|
---|
186 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
|
---|
187 | return ParserTypes_end;
|
---|
188 | } else
|
---|
189 | return ParserLookupNames[type];
|
---|
190 | }
|
---|
191 |
|
---|
192 | ParserTypes FormatParserStorage::getTypeFromSuffix(std::string type)
|
---|
193 | {
|
---|
194 | if (ParserLookupSuffixes.find(type) == ParserLookupSuffixes.end()) {
|
---|
195 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
|
---|
196 | return ParserTypes_end;
|
---|
197 | } else
|
---|
198 | return ParserLookupSuffixes[type];
|
---|
199 | }
|
---|
200 |
|
---|
201 | bool FormatParserStorage::add(ParserTypes ptype)
|
---|
202 | {
|
---|
203 | if (ptype != ParserTypes_end) {
|
---|
204 | if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
|
---|
205 | DoLog(0) && (Log() << Verbose(0) << "Adding " << ParserNames[ptype] << " type to output." << endl);
|
---|
206 | (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
|
---|
207 | return true;
|
---|
208 | } else {
|
---|
209 | DoeLog(1) && (eLog() << Verbose(1) << "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?" << endl);
|
---|
210 | return false;
|
---|
211 | }
|
---|
212 | } else {
|
---|
213 | return false;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | bool FormatParserStorage::add(std::string type)
|
---|
218 | {
|
---|
219 | return add(getTypeFromName(type));
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | /** Parses an istream depending on its suffix
|
---|
224 | * \param &input input stream
|
---|
225 | * \param suffix
|
---|
226 | * \return true - parsing ok, false - suffix unknown
|
---|
227 | */
|
---|
228 | bool FormatParserStorage::load(std::istream &input, std::string suffix)
|
---|
229 | {
|
---|
230 | if (suffix == ParserSuffixes[mpqc]) {
|
---|
231 | getMpqc().load(&input);
|
---|
232 | } else if (suffix == ParserSuffixes[pcp]) {
|
---|
233 | getPcp().load(&input);
|
---|
234 | } else if (suffix == ParserSuffixes[pdb]) {
|
---|
235 | getPdb().load(&input);
|
---|
236 | } else if (suffix == ParserSuffixes[tremolo]) {
|
---|
237 | getTremolo().load(&input);
|
---|
238 | } else if (suffix == ParserSuffixes[xyz]) {
|
---|
239 | getXyz().load(&input);
|
---|
240 | } else {
|
---|
241 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::get()." << endl);
|
---|
242 | return false;
|
---|
243 | }
|
---|
244 | return true;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /** Stores all selected atoms in an ostream depending on its suffix
|
---|
248 | * \param &output output stream
|
---|
249 | * \param suffix
|
---|
250 | * \return true - storing ok, false - suffix unknown
|
---|
251 | */
|
---|
252 | bool FormatParserStorage::saveSelectedAtoms(std::ostream &output, std::string suffix)
|
---|
253 | {
|
---|
254 | std::vector<atom *> atoms = World::getInstance().getSelectedAtoms();
|
---|
255 | return save(output, suffix, atoms);
|
---|
256 | }
|
---|
257 |
|
---|
258 | /** Stores all selected atoms in an ostream depending on its suffix
|
---|
259 | * We store in the order of the atomic ids, not in the order they appear in the molecules.
|
---|
260 | * Hence, we first create a vector from all selected molecules' atoms.
|
---|
261 | * \param &output output stream
|
---|
262 | * \param suffix
|
---|
263 | * \return true - storing ok, false - suffix unknown
|
---|
264 | */
|
---|
265 | bool FormatParserStorage::saveSelectedMolecules(std::ostream &output, std::string suffix)
|
---|
266 | {
|
---|
267 | std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
|
---|
268 | std::map<size_t, atom *> IdAtoms;
|
---|
269 | for (std::vector<molecule *>::const_iterator MolIter = molecules.begin();
|
---|
270 | MolIter != molecules.end();
|
---|
271 | ++MolIter) {
|
---|
272 | for(molecule::atomSet::const_iterator AtomIter = (*MolIter)->begin();
|
---|
273 | AtomIter != (*MolIter)->end();
|
---|
274 | ++AtomIter) {
|
---|
275 | IdAtoms.insert( make_pair((*AtomIter)->getId(), (*AtomIter)) );
|
---|
276 | }
|
---|
277 | }
|
---|
278 | std::vector<atom *> atoms;
|
---|
279 | atoms.reserve(IdAtoms.size());
|
---|
280 | for (std::map<size_t, atom *>::const_iterator iter = IdAtoms.begin();
|
---|
281 | iter != IdAtoms.end();
|
---|
282 | ++iter) {
|
---|
283 | atoms.push_back(iter->second);
|
---|
284 | }
|
---|
285 | return save(output, suffix, atoms);
|
---|
286 | }
|
---|
287 |
|
---|
288 | /** Stores world in an ostream depending on its suffix
|
---|
289 | * \param &output output stream
|
---|
290 | * \param suffix
|
---|
291 | * \return true - storing ok, false - suffix unknown
|
---|
292 | */
|
---|
293 | bool FormatParserStorage::saveWorld(std::ostream &output, std::string suffix)
|
---|
294 | {
|
---|
295 | std::vector<atom *> atoms = World::getInstance().getAllAtoms();
|
---|
296 | return save(output, suffix, atoms);
|
---|
297 | }
|
---|
298 |
|
---|
299 | /** Stores a given vector of \a atoms in an ostream depending on its suffix
|
---|
300 | * \param &output output stream
|
---|
301 | * \param suffix
|
---|
302 | * \return true - storing ok, false - suffix unknown
|
---|
303 | */
|
---|
304 | bool FormatParserStorage::save(std::ostream &output, std::string suffix, const std::vector<atom *> &atoms)
|
---|
305 | {
|
---|
306 | if (suffix == ParserSuffixes[mpqc]) {
|
---|
307 | getMpqc().save(&output, atoms);
|
---|
308 | } else if (suffix == ParserSuffixes[pcp]) {
|
---|
309 | getPcp().save(&output, atoms);
|
---|
310 | } else if (suffix == ParserSuffixes[pdb]) {
|
---|
311 | getPdb().save(&output, atoms);
|
---|
312 | } else if (suffix == ParserSuffixes[tremolo]) {
|
---|
313 | getTremolo().save(&output, atoms);
|
---|
314 | } else if (suffix == ParserSuffixes[xyz]) {
|
---|
315 | getXyz().save(&output, atoms);
|
---|
316 | } else {
|
---|
317 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::put()." << endl);
|
---|
318 | return false;
|
---|
319 | }
|
---|
320 | return true;
|
---|
321 | }
|
---|
322 |
|
---|
323 | /** Returns reference to the output MpqcParser, adds if not present.
|
---|
324 | * \return reference to the output MpqcParser
|
---|
325 | */
|
---|
326 | MpqcParser &FormatParserStorage::getMpqc()
|
---|
327 | {
|
---|
328 | if (!ParserPresent[mpqc])
|
---|
329 | addMpqc();
|
---|
330 | return dynamic_cast<MpqcParser &>(*ParserList[mpqc]);
|
---|
331 | }
|
---|
332 |
|
---|
333 | /** Returns reference to the output PcpParser, adds if not present.
|
---|
334 | * \return reference to the output PcpParser
|
---|
335 | */
|
---|
336 | PcpParser &FormatParserStorage::getPcp()
|
---|
337 | {
|
---|
338 | if (!ParserPresent[pcp])
|
---|
339 | addPcp();
|
---|
340 | return dynamic_cast<PcpParser &>(*ParserList[pcp]);
|
---|
341 | }
|
---|
342 |
|
---|
343 | /** Returns reference to the output PdbParser, adds if not present.
|
---|
344 | * \return reference to the output PdbParser
|
---|
345 | */
|
---|
346 | PdbParser &FormatParserStorage::getPdb()
|
---|
347 | {
|
---|
348 | if (!ParserPresent[pdb])
|
---|
349 | addPdb();
|
---|
350 | return dynamic_cast<PdbParser &>(*ParserList[pdb]);
|
---|
351 | }
|
---|
352 |
|
---|
353 | /** Returns reference to the output TremoloParser, adds if not present.
|
---|
354 | * \return reference to the output TremoloParser
|
---|
355 | */
|
---|
356 | TremoloParser &FormatParserStorage::getTremolo()
|
---|
357 | {
|
---|
358 | if (!ParserPresent[tremolo])
|
---|
359 | addTremolo();
|
---|
360 | return dynamic_cast<TremoloParser &>(*ParserList[tremolo]);
|
---|
361 | }
|
---|
362 |
|
---|
363 | /** Returns reference to the output XyzParser, adds if not present.
|
---|
364 | * \return reference to the output XyzParser
|
---|
365 | */
|
---|
366 | XyzParser &FormatParserStorage::getXyz()
|
---|
367 | {
|
---|
368 | if (!ParserPresent[xyz])
|
---|
369 | addXyz();
|
---|
370 | return dynamic_cast<XyzParser &>(*ParserList[xyz]);
|
---|
371 | }
|
---|
372 |
|
---|
373 |
|
---|
374 |
|
---|
375 | CONSTRUCT_SINGLETON(FormatParserStorage)
|
---|