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 "CodePatterns/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 "CodePatterns/Log.hpp"
|
---|
35 | #include "CodePatterns/Verbose.hpp"
|
---|
36 |
|
---|
37 | #include "CodePatterns/Assert.hpp"
|
---|
38 |
|
---|
39 | #include "molecule.hpp"
|
---|
40 |
|
---|
41 | #include "CodePatterns/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(2) && (eLog() << Verbose(2) << "Parser mpqc is already present." << endl
|
---|
133 | << "Note that you don't need to add '-o mpqc' if the input file is of type mpqc." << endl);
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | /** Adds an PcpParser to the storage.
|
---|
138 | */
|
---|
139 | void FormatParserStorage::addPcp()
|
---|
140 | {
|
---|
141 | if (!ParserPresent[pcp]) {
|
---|
142 | ParserList[pcp] = new PcpParser();
|
---|
143 | ParserPresent[pcp] = true;
|
---|
144 | } else
|
---|
145 | DoeLog(2) && (eLog() << Verbose(2) << "Parser pcp is already present." << endl
|
---|
146 | << "Note that you don't need to add '-o pcp' if the input file is of type pcp." << endl);
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | /** Adds an PdbParser to the storage.
|
---|
151 | */
|
---|
152 | void FormatParserStorage::addPdb()
|
---|
153 | {
|
---|
154 | if (!ParserPresent[pdb]) {
|
---|
155 | ParserList[pdb] = new PdbParser();
|
---|
156 | ParserPresent[pdb] = true;
|
---|
157 | } else
|
---|
158 | DoeLog(2) && (eLog() << Verbose(2) << "Parser pdb is already present." << endl
|
---|
159 | << "Note that you don't need to add '-o pdb' if the input file is of type pdb." << endl);
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | /** Adds an TremoloParser to the storage.
|
---|
164 | */
|
---|
165 | void FormatParserStorage::addTremolo()
|
---|
166 | {
|
---|
167 | if (!ParserPresent[tremolo]) {
|
---|
168 | ParserList[tremolo] = new TremoloParser();
|
---|
169 | ParserPresent[tremolo] = true;
|
---|
170 | } else
|
---|
171 | DoeLog(2) && (eLog() << Verbose(2) << "Parser tremolo is already present." << endl
|
---|
172 | << "Note that you don't need to add '-o tremolo' if the input file is of type tremolo." << endl);
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | /** Adds an XyzParser to the storage.
|
---|
177 | */
|
---|
178 | void FormatParserStorage::addXyz()
|
---|
179 | {
|
---|
180 | if (!ParserPresent[xyz]) {
|
---|
181 | ParserList[xyz] = new XyzParser();
|
---|
182 | ParserPresent[xyz] = true;
|
---|
183 | } else
|
---|
184 | DoeLog(2) && (eLog() << Verbose(2) << "Parser xyz is already present." << endl
|
---|
185 | << "Note that you don't need to add '-o xyz' if the input file is of type xyz." << endl);
|
---|
186 | }
|
---|
187 |
|
---|
188 | ParserTypes FormatParserStorage::getTypeFromName(std::string type)
|
---|
189 | {
|
---|
190 | if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
|
---|
191 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
|
---|
192 | return ParserTypes_end;
|
---|
193 | } else
|
---|
194 | return ParserLookupNames[type];
|
---|
195 | }
|
---|
196 |
|
---|
197 | ParserTypes FormatParserStorage::getTypeFromSuffix(std::string type)
|
---|
198 | {
|
---|
199 | if (ParserLookupSuffixes.find(type) == ParserLookupSuffixes.end()) {
|
---|
200 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
|
---|
201 | return ParserTypes_end;
|
---|
202 | } else
|
---|
203 | return ParserLookupSuffixes[type];
|
---|
204 | }
|
---|
205 |
|
---|
206 | bool FormatParserStorage::add(ParserTypes ptype)
|
---|
207 | {
|
---|
208 | if (ptype != ParserTypes_end) {
|
---|
209 | if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
|
---|
210 | DoLog(0) && (Log() << Verbose(0) << "Adding " << ParserNames[ptype] << " type to output." << endl);
|
---|
211 | (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
|
---|
212 | return true;
|
---|
213 | } else {
|
---|
214 | DoeLog(1) && (eLog() << Verbose(1) << "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?" << endl);
|
---|
215 | return false;
|
---|
216 | }
|
---|
217 | } else {
|
---|
218 | return false;
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | bool FormatParserStorage::add(std::string type)
|
---|
223 | {
|
---|
224 | return add(getTypeFromName(type));
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | /** Parses an istream depending on its suffix
|
---|
229 | * \param &input input stream
|
---|
230 | * \param suffix
|
---|
231 | * \return true - parsing ok, false - suffix unknown
|
---|
232 | */
|
---|
233 | bool FormatParserStorage::load(std::istream &input, std::string suffix)
|
---|
234 | {
|
---|
235 | if (suffix == ParserSuffixes[mpqc]) {
|
---|
236 | getMpqc().load(&input);
|
---|
237 | } else if (suffix == ParserSuffixes[pcp]) {
|
---|
238 | getPcp().load(&input);
|
---|
239 | } else if (suffix == ParserSuffixes[pdb]) {
|
---|
240 | getPdb().load(&input);
|
---|
241 | } else if (suffix == ParserSuffixes[tremolo]) {
|
---|
242 | getTremolo().load(&input);
|
---|
243 | } else if (suffix == ParserSuffixes[xyz]) {
|
---|
244 | getXyz().load(&input);
|
---|
245 | } else {
|
---|
246 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::get()." << endl);
|
---|
247 | return false;
|
---|
248 | }
|
---|
249 | return true;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /** Stores all selected atoms in an ostream depending on its suffix
|
---|
253 | * \param &output output stream
|
---|
254 | * \param suffix
|
---|
255 | * \return true - storing ok, false - suffix unknown
|
---|
256 | */
|
---|
257 | bool FormatParserStorage::saveSelectedAtoms(std::ostream &output, std::string suffix)
|
---|
258 | {
|
---|
259 | std::vector<atom *> atoms = World::getInstance().getSelectedAtoms();
|
---|
260 | return save(output, suffix, atoms);
|
---|
261 | }
|
---|
262 |
|
---|
263 | /** Stores all selected atoms in an ostream depending on its suffix
|
---|
264 | * We store in the order of the atomic ids, not in the order they appear in the molecules.
|
---|
265 | * Hence, we first create a vector from all selected molecules' atoms.
|
---|
266 | * \param &output output stream
|
---|
267 | * \param suffix
|
---|
268 | * \return true - storing ok, false - suffix unknown
|
---|
269 | */
|
---|
270 | bool FormatParserStorage::saveSelectedMolecules(std::ostream &output, std::string suffix)
|
---|
271 | {
|
---|
272 | std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
|
---|
273 | std::map<size_t, atom *> IdAtoms;
|
---|
274 | for (std::vector<molecule *>::const_iterator MolIter = molecules.begin();
|
---|
275 | MolIter != molecules.end();
|
---|
276 | ++MolIter) {
|
---|
277 | for(molecule::atomSet::const_iterator AtomIter = (*MolIter)->begin();
|
---|
278 | AtomIter != (*MolIter)->end();
|
---|
279 | ++AtomIter) {
|
---|
280 | IdAtoms.insert( make_pair((*AtomIter)->getId(), (*AtomIter)) );
|
---|
281 | }
|
---|
282 | }
|
---|
283 | std::vector<atom *> atoms;
|
---|
284 | atoms.reserve(IdAtoms.size());
|
---|
285 | for (std::map<size_t, atom *>::const_iterator iter = IdAtoms.begin();
|
---|
286 | iter != IdAtoms.end();
|
---|
287 | ++iter) {
|
---|
288 | atoms.push_back(iter->second);
|
---|
289 | }
|
---|
290 | return save(output, suffix, atoms);
|
---|
291 | }
|
---|
292 |
|
---|
293 | /** Stores world in an ostream depending on its suffix
|
---|
294 | * \param &output output stream
|
---|
295 | * \param suffix
|
---|
296 | * \return true - storing ok, false - suffix unknown
|
---|
297 | */
|
---|
298 | bool FormatParserStorage::saveWorld(std::ostream &output, std::string suffix)
|
---|
299 | {
|
---|
300 | std::vector<atom *> atoms = World::getInstance().getAllAtoms();
|
---|
301 | return save(output, suffix, atoms);
|
---|
302 | }
|
---|
303 |
|
---|
304 | /** Stores a given vector of \a atoms in an ostream depending on its suffix
|
---|
305 | * \param &output output stream
|
---|
306 | * \param suffix
|
---|
307 | * \return true - storing ok, false - suffix unknown
|
---|
308 | */
|
---|
309 | bool FormatParserStorage::save(std::ostream &output, std::string suffix, const std::vector<atom *> &atoms)
|
---|
310 | {
|
---|
311 | if (suffix == ParserSuffixes[mpqc]) {
|
---|
312 | getMpqc().save(&output, atoms);
|
---|
313 | } else if (suffix == ParserSuffixes[pcp]) {
|
---|
314 | getPcp().save(&output, atoms);
|
---|
315 | } else if (suffix == ParserSuffixes[pdb]) {
|
---|
316 | getPdb().save(&output, atoms);
|
---|
317 | } else if (suffix == ParserSuffixes[tremolo]) {
|
---|
318 | getTremolo().save(&output, atoms);
|
---|
319 | } else if (suffix == ParserSuffixes[xyz]) {
|
---|
320 | getXyz().save(&output, atoms);
|
---|
321 | } else {
|
---|
322 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::put()." << endl);
|
---|
323 | return false;
|
---|
324 | }
|
---|
325 | return true;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /** Returns reference to the output MpqcParser, adds if not present.
|
---|
329 | * \return reference to the output MpqcParser
|
---|
330 | */
|
---|
331 | MpqcParser &FormatParserStorage::getMpqc()
|
---|
332 | {
|
---|
333 | if (!ParserPresent[mpqc])
|
---|
334 | addMpqc();
|
---|
335 | return dynamic_cast<MpqcParser &>(*ParserList[mpqc]);
|
---|
336 | }
|
---|
337 |
|
---|
338 | /** Returns reference to the output PcpParser, adds if not present.
|
---|
339 | * \return reference to the output PcpParser
|
---|
340 | */
|
---|
341 | PcpParser &FormatParserStorage::getPcp()
|
---|
342 | {
|
---|
343 | if (!ParserPresent[pcp])
|
---|
344 | addPcp();
|
---|
345 | return dynamic_cast<PcpParser &>(*ParserList[pcp]);
|
---|
346 | }
|
---|
347 |
|
---|
348 | /** Returns reference to the output PdbParser, adds if not present.
|
---|
349 | * \return reference to the output PdbParser
|
---|
350 | */
|
---|
351 | PdbParser &FormatParserStorage::getPdb()
|
---|
352 | {
|
---|
353 | if (!ParserPresent[pdb])
|
---|
354 | addPdb();
|
---|
355 | return dynamic_cast<PdbParser &>(*ParserList[pdb]);
|
---|
356 | }
|
---|
357 |
|
---|
358 | /** Returns reference to the output TremoloParser, adds if not present.
|
---|
359 | * \return reference to the output TremoloParser
|
---|
360 | */
|
---|
361 | TremoloParser &FormatParserStorage::getTremolo()
|
---|
362 | {
|
---|
363 | if (!ParserPresent[tremolo])
|
---|
364 | addTremolo();
|
---|
365 | return dynamic_cast<TremoloParser &>(*ParserList[tremolo]);
|
---|
366 | }
|
---|
367 |
|
---|
368 | /** Returns reference to the output XyzParser, adds if not present.
|
---|
369 | * \return reference to the output XyzParser
|
---|
370 | */
|
---|
371 | XyzParser &FormatParserStorage::getXyz()
|
---|
372 | {
|
---|
373 | if (!ParserPresent[xyz])
|
---|
374 | addXyz();
|
---|
375 | return dynamic_cast<XyzParser &>(*ParserList[xyz]);
|
---|
376 | }
|
---|
377 |
|
---|
378 |
|
---|
379 |
|
---|
380 | CONSTRUCT_SINGLETON(FormatParserStorage)
|
---|