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 "CodePatterns/Log.hpp"
|
---|
28 | #include "CodePatterns/Verbose.hpp"
|
---|
29 |
|
---|
30 | #include "CodePatterns/Assert.hpp"
|
---|
31 |
|
---|
32 | #include "molecule.hpp"
|
---|
33 |
|
---|
34 | #include "CodePatterns/Singleton_impl.hpp"
|
---|
35 |
|
---|
36 | template <> enum ParserTypes getPType<MpqcParser>() { return (enum ParserTypes)mpqc; }
|
---|
37 | template <> enum ParserTypes getPType<PcpParser>() { return (enum ParserTypes)pcp; }
|
---|
38 | template <> enum ParserTypes getPType<PdbParser>() { return (enum ParserTypes)pdb; }
|
---|
39 | template <> enum ParserTypes getPType<TremoloParser>() { return (enum ParserTypes)tremolo; }
|
---|
40 | template <> enum ParserTypes getPType<XyzParser>() { return (enum ParserTypes)xyz; }
|
---|
41 |
|
---|
42 | /** Increment operator for the enumeration ParserTypes to allow loops.
|
---|
43 | * \param &type value
|
---|
44 | * \return value incremented by one
|
---|
45 | */
|
---|
46 | ParserTypes &operator++(ParserTypes &type)
|
---|
47 | {
|
---|
48 | return type = ParserTypes(type+1);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /** Constructor of class FormatParserStorage.
|
---|
52 | */
|
---|
53 | FormatParserStorage::FormatParserStorage()
|
---|
54 | {
|
---|
55 | ParserList.resize(ParserTypes_end, NULL);
|
---|
56 | ParserStream.resize(ParserTypes_end, NULL);
|
---|
57 | ParserPresent.resize(ParserTypes_end, false);
|
---|
58 |
|
---|
59 | ParserNames[mpqc] = "mpqc";
|
---|
60 | ParserNames[pcp] = "pcp";
|
---|
61 | ParserNames[pdb] = "pdb";
|
---|
62 | ParserNames[tremolo] = "tremolo";
|
---|
63 | ParserNames[xyz] = "xyz";
|
---|
64 |
|
---|
65 | for (std::map<ParserTypes, std::string>::const_iterator it = ParserNames.begin(); it != ParserNames.end(); ++it)
|
---|
66 | ParserLookupNames.insert(pair<std::string, ParserTypes>(it->second,it->first) );
|
---|
67 |
|
---|
68 | ParserSuffixes[mpqc] = "in";
|
---|
69 | ParserSuffixes[pcp] = "conf";
|
---|
70 | ParserSuffixes[pdb] = "pdb";
|
---|
71 | ParserSuffixes[tremolo] = "data";
|
---|
72 | ParserSuffixes[xyz] = "xyz";
|
---|
73 |
|
---|
74 | for (std::map<ParserTypes, std::string>::const_iterator it = ParserSuffixes.begin(); it != ParserSuffixes.end(); ++it)
|
---|
75 | ParserLookupSuffixes.insert(pair<std::string, ParserTypes>(it->second,it->first) );
|
---|
76 |
|
---|
77 | ParserAddFunction[mpqc] = &FormatParserStorage::addParser<MpqcParser>;
|
---|
78 | ParserAddFunction[pcp] = &FormatParserStorage::addParser<PcpParser>;
|
---|
79 | ParserAddFunction[pdb] = &FormatParserStorage::addParser<PdbParser>;
|
---|
80 | ParserAddFunction[tremolo] = &FormatParserStorage::addParser<TremoloParser>;
|
---|
81 | ParserAddFunction[xyz] = &FormatParserStorage::addParser<XyzParser>;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Destructor of class FormatParserStorage.
|
---|
85 | * Free all stored FormatParsers.
|
---|
86 | * Save on Exit.
|
---|
87 | */
|
---|
88 | FormatParserStorage::~FormatParserStorage()
|
---|
89 | {
|
---|
90 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
91 | if (ParserPresent[iter]) {
|
---|
92 | if (ParserStream[iter]->is_open())
|
---|
93 | ParserStream[iter]->close();
|
---|
94 | delete ParserStream[iter];
|
---|
95 | delete ParserList[iter];
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Sets the filename of all current parsers in storage to prefix.suffix.
|
---|
100 | * \param &prefix prefix to use.
|
---|
101 | */
|
---|
102 | void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
|
---|
103 | {
|
---|
104 | prefix=_prefix;
|
---|
105 | };
|
---|
106 |
|
---|
107 |
|
---|
108 | void FormatParserStorage::SaveAll()
|
---|
109 | {
|
---|
110 | std::string filename;
|
---|
111 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
112 | if (ParserPresent[iter]) {
|
---|
113 | filename = prefix;
|
---|
114 | filename += ".";
|
---|
115 | filename += ParserSuffixes[iter];
|
---|
116 | ParserStream[iter] = new std::ofstream(filename.c_str());
|
---|
117 | ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | ParserTypes FormatParserStorage::getTypeFromName(std::string type)
|
---|
123 | {
|
---|
124 | if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
|
---|
125 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
|
---|
126 | return ParserTypes_end;
|
---|
127 | } else
|
---|
128 | return ParserLookupNames[type];
|
---|
129 | }
|
---|
130 |
|
---|
131 | ParserTypes FormatParserStorage::getTypeFromSuffix(std::string type)
|
---|
132 | {
|
---|
133 | if (ParserLookupSuffixes.find(type) == ParserLookupSuffixes.end()) {
|
---|
134 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
|
---|
135 | return ParserTypes_end;
|
---|
136 | } else
|
---|
137 | return ParserLookupSuffixes[type];
|
---|
138 | }
|
---|
139 |
|
---|
140 | bool FormatParserStorage::add(ParserTypes ptype)
|
---|
141 | {
|
---|
142 | if (ptype != ParserTypes_end) {
|
---|
143 | if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
|
---|
144 | DoLog(0) && (Log() << Verbose(0) << "Adding " << ParserNames[ptype] << " type to output." << endl);
|
---|
145 | (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
|
---|
146 | return true;
|
---|
147 | } else {
|
---|
148 | DoeLog(1) && (eLog() << Verbose(1) << "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?" << endl);
|
---|
149 | return false;
|
---|
150 | }
|
---|
151 | } else {
|
---|
152 | return false;
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | bool FormatParserStorage::add(std::string type)
|
---|
157 | {
|
---|
158 | return add(getTypeFromName(type));
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | /** Parses an istream depending on its suffix
|
---|
163 | * \param &input input stream
|
---|
164 | * \param suffix
|
---|
165 | * \return true - parsing ok, false - suffix unknown
|
---|
166 | */
|
---|
167 | bool FormatParserStorage::load(std::istream &input, std::string suffix)
|
---|
168 | {
|
---|
169 | enum ParserTypes type = getTypeFromSuffix(suffix);
|
---|
170 | if (type != ParserTypes_end)
|
---|
171 | get(type).load(&input);
|
---|
172 | else
|
---|
173 | return false;
|
---|
174 | return true;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /** Stores all selected atoms in an ostream depending on its suffix
|
---|
178 | * \param &output output stream
|
---|
179 | * \param suffix
|
---|
180 | * \return true - storing ok, false - suffix unknown
|
---|
181 | */
|
---|
182 | bool FormatParserStorage::saveSelectedAtoms(std::ostream &output, std::string suffix)
|
---|
183 | {
|
---|
184 | std::vector<atom *> atoms = World::getInstance().getSelectedAtoms();
|
---|
185 | return save(output, suffix, atoms);
|
---|
186 | }
|
---|
187 |
|
---|
188 | /** Stores all selected atoms in an ostream depending on its suffix
|
---|
189 | * We store in the order of the atomic ids, not in the order they appear in the molecules.
|
---|
190 | * Hence, we first create a vector from all selected molecules' atoms.
|
---|
191 | * \param &output output stream
|
---|
192 | * \param suffix
|
---|
193 | * \return true - storing ok, false - suffix unknown
|
---|
194 | */
|
---|
195 | bool FormatParserStorage::saveSelectedMolecules(std::ostream &output, std::string suffix)
|
---|
196 | {
|
---|
197 | std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
|
---|
198 | std::map<size_t, atom *> IdAtoms;
|
---|
199 | for (std::vector<molecule *>::const_iterator MolIter = molecules.begin();
|
---|
200 | MolIter != molecules.end();
|
---|
201 | ++MolIter) {
|
---|
202 | for(molecule::atomSet::const_iterator AtomIter = (*MolIter)->begin();
|
---|
203 | AtomIter != (*MolIter)->end();
|
---|
204 | ++AtomIter) {
|
---|
205 | IdAtoms.insert( make_pair((*AtomIter)->getId(), (*AtomIter)) );
|
---|
206 | }
|
---|
207 | }
|
---|
208 | std::vector<atom *> atoms;
|
---|
209 | atoms.reserve(IdAtoms.size());
|
---|
210 | for (std::map<size_t, atom *>::const_iterator iter = IdAtoms.begin();
|
---|
211 | iter != IdAtoms.end();
|
---|
212 | ++iter) {
|
---|
213 | atoms.push_back(iter->second);
|
---|
214 | }
|
---|
215 | return save(output, suffix, atoms);
|
---|
216 | }
|
---|
217 |
|
---|
218 | /** Stores world in an ostream depending on its suffix
|
---|
219 | * \param &output output stream
|
---|
220 | * \param suffix
|
---|
221 | * \return true - storing ok, false - suffix unknown
|
---|
222 | */
|
---|
223 | bool FormatParserStorage::saveWorld(std::ostream &output, std::string suffix)
|
---|
224 | {
|
---|
225 | std::vector<atom *> atoms = World::getInstance().getAllAtoms();
|
---|
226 | return save(output, suffix, atoms);
|
---|
227 | }
|
---|
228 |
|
---|
229 | /** Stores a given vector of \a atoms in an ostream depending on its suffix
|
---|
230 | * \param &output output stream
|
---|
231 | * \param suffix
|
---|
232 | * \return true - storing ok, false - suffix unknown
|
---|
233 | */
|
---|
234 | bool FormatParserStorage::save(std::ostream &output, std::string suffix, const std::vector<atom *> &atoms)
|
---|
235 | {
|
---|
236 | enum ParserTypes type = getTypeFromSuffix(suffix);
|
---|
237 | if (type != ParserTypes_end)
|
---|
238 | get(type).save(&output, atoms);
|
---|
239 | else
|
---|
240 | return false;
|
---|
241 | return true;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /** Returns reference to the desired output parser as FormatParser, adds if not present.
|
---|
245 | * \param _type type of desired parser
|
---|
246 | * \return reference to the output FormatParser with desired type
|
---|
247 | */
|
---|
248 | FormatParser &FormatParserStorage::get(ParserTypes _type)
|
---|
249 | {
|
---|
250 | if (!ParserPresent[_type]) {
|
---|
251 | add(_type);
|
---|
252 | }
|
---|
253 | return *ParserList[_type];
|
---|
254 | }
|
---|
255 |
|
---|
256 | CONSTRUCT_SINGLETON(FormatParserStorage)
|
---|