[bcf653] | 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 |
|
---|
[52baf9] | 8 | /** \file FormatParserStorage.cpp
|
---|
| 9 | *
|
---|
| 10 | * date: Jun, 22 2010
|
---|
| 11 | * author: heber
|
---|
| 12 | *
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[bbbad5] | 20 | #include "Helpers/MemDebug.hpp"
|
---|
| 21 |
|
---|
[52baf9] | 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"
|
---|
[bb6193] | 30 | #include "Parser/PdbParser.hpp"
|
---|
[52baf9] | 31 | #include "Parser/TremoloParser.hpp"
|
---|
| 32 | #include "Parser/XyzParser.hpp"
|
---|
| 33 |
|
---|
[952f38] | 34 | #include "Helpers/Log.hpp"
|
---|
| 35 | #include "Helpers/Verbose.hpp"
|
---|
[52baf9] | 36 |
|
---|
| 37 | #include "Helpers/Assert.hpp"
|
---|
| 38 |
|
---|
[73916f] | 39 | #include "molecule.hpp"
|
---|
| 40 |
|
---|
[52baf9] | 41 | #include "Patterns/Singleton_impl.hpp"
|
---|
| 42 |
|
---|
[dc0d21] | 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 |
|
---|
[52baf9] | 52 | /** Constructor of class FormatParserStorage.
|
---|
| 53 | */
|
---|
| 54 | FormatParserStorage::FormatParserStorage()
|
---|
| 55 | {
|
---|
| 56 | ParserList.resize(ParserTypes_end, NULL);
|
---|
[60239f] | 57 | ParserStream.resize(ParserTypes_end, NULL);
|
---|
[52baf9] | 58 | ParserPresent.resize(ParserTypes_end, false);
|
---|
| 59 |
|
---|
[5c6946] | 60 | ParserNames[mpqc] = "mpqc";
|
---|
| 61 | ParserNames[pcp] = "pcp";
|
---|
[bb6193] | 62 | ParserNames[pdb] = "pdb";
|
---|
[5c6946] | 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 |
|
---|
[a42054] | 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) );
|
---|
[5c6946] | 77 |
|
---|
| 78 | ParserAddFunction[mpqc] = &FormatParserStorage::addMpqc;
|
---|
| 79 | ParserAddFunction[pcp] = &FormatParserStorage::addPcp;
|
---|
[bb6193] | 80 | ParserAddFunction[pdb] = &FormatParserStorage::addPdb;
|
---|
[5c6946] | 81 | ParserAddFunction[tremolo] = &FormatParserStorage::addTremolo;
|
---|
| 82 | ParserAddFunction[xyz] = &FormatParserStorage::addXyz;
|
---|
[52baf9] | 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)
|
---|
[60239f] | 92 | if (ParserPresent[iter]) {
|
---|
| 93 | if (ParserStream[iter]->is_open())
|
---|
| 94 | ParserStream[iter]->close();
|
---|
| 95 | delete ParserStream[iter];
|
---|
[52baf9] | 96 | delete ParserList[iter];
|
---|
[60239f] | 97 | }
|
---|
[52baf9] | 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 += ".";
|
---|
[a42054] | 116 | filename += ParserSuffixes[iter];
|
---|
[60239f] | 117 | ParserStream[iter] = new std::ofstream(filename.c_str());
|
---|
| 118 | ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
|
---|
[52baf9] | 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | /** Adds an MpqcParser to the storage.
|
---|
| 124 | */
|
---|
[dc0d21] | 125 | void FormatParserStorage::addMpqc()
|
---|
[52baf9] | 126 | {
|
---|
[dc0d21] | 127 | if (!ParserPresent[mpqc]) {
|
---|
[52baf9] | 128 | ParserList[mpqc] = dynamic_cast<FormatParser *>(new MpqcParser);
|
---|
[dc0d21] | 129 | ParserPresent[mpqc] = true;
|
---|
| 130 | }
|
---|
[52baf9] | 131 | else
|
---|
| 132 | DoeLog(1) && (eLog() << Verbose(1) << "Parser mpqc is already present." << endl);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[5c6946] | 135 |
|
---|
[52baf9] | 136 | /** Adds an PcpParser to the storage.
|
---|
| 137 | */
|
---|
[dc0d21] | 138 | void FormatParserStorage::addPcp()
|
---|
[52baf9] | 139 | {
|
---|
[dc0d21] | 140 | if (!ParserPresent[pcp]) {
|
---|
[52baf9] | 141 | ParserList[pcp] = new PcpParser();
|
---|
[dc0d21] | 142 | ParserPresent[pcp] = true;
|
---|
| 143 | } else
|
---|
[52baf9] | 144 | DoeLog(1) && (eLog() << Verbose(1) << "Parser pcp is already present." << endl);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
[bb6193] | 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 |
|
---|
[52baf9] | 160 | /** Adds an TremoloParser to the storage.
|
---|
| 161 | */
|
---|
[dc0d21] | 162 | void FormatParserStorage::addTremolo()
|
---|
[52baf9] | 163 | {
|
---|
[dc0d21] | 164 | if (!ParserPresent[tremolo]) {
|
---|
[52baf9] | 165 | ParserList[tremolo] = new TremoloParser();
|
---|
[dc0d21] | 166 | ParserPresent[tremolo] = true;
|
---|
| 167 | } else
|
---|
[52baf9] | 168 | DoeLog(1) && (eLog() << Verbose(1) << "Parser tremolo is already present." << endl);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 |
|
---|
| 172 | /** Adds an XyzParser to the storage.
|
---|
| 173 | */
|
---|
[dc0d21] | 174 | void FormatParserStorage::addXyz()
|
---|
[52baf9] | 175 | {
|
---|
[dc0d21] | 176 | if (!ParserPresent[xyz]) {
|
---|
[52baf9] | 177 | ParserList[xyz] = new XyzParser();
|
---|
[dc0d21] | 178 | ParserPresent[xyz] = true;
|
---|
| 179 | } else
|
---|
[52baf9] | 180 | DoeLog(1) && (eLog() << Verbose(1) << "Parser xyz is already present." << endl);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[a42054] | 183 | ParserTypes FormatParserStorage::getTypeFromName(std::string type)
|
---|
[5c6946] | 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 |
|
---|
[a42054] | 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 |
|
---|
[5c6946] | 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 | {
|
---|
[a42054] | 219 | return add(getTypeFromName(type));
|
---|
[5c6946] | 220 | }
|
---|
| 221 |
|
---|
| 222 |
|
---|
[86cff86] | 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 | */
|
---|
[73916f] | 228 | bool FormatParserStorage::load(std::istream &input, std::string suffix)
|
---|
[86cff86] | 229 | {
|
---|
[a42054] | 230 | if (suffix == ParserSuffixes[mpqc]) {
|
---|
[86cff86] | 231 | getMpqc().load(&input);
|
---|
[a42054] | 232 | } else if (suffix == ParserSuffixes[pcp]) {
|
---|
[86cff86] | 233 | getPcp().load(&input);
|
---|
[a42054] | 234 | } else if (suffix == ParserSuffixes[pdb]) {
|
---|
[bb6193] | 235 | getPdb().load(&input);
|
---|
[a42054] | 236 | } else if (suffix == ParserSuffixes[tremolo]) {
|
---|
[86cff86] | 237 | getTremolo().load(&input);
|
---|
[a42054] | 238 | } else if (suffix == ParserSuffixes[xyz]) {
|
---|
[86cff86] | 239 | getXyz().load(&input);
|
---|
| 240 | } else {
|
---|
[5c6946] | 241 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::get()." << endl);
|
---|
[86cff86] | 242 | return false;
|
---|
| 243 | }
|
---|
| 244 | return true;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[73916f] | 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 | }
|
---|
[cabb46] | 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 | */
|
---|
[73916f] | 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)
|
---|
[cabb46] | 305 | {
|
---|
| 306 | if (suffix == ParserSuffixes[mpqc]) {
|
---|
[73916f] | 307 | getMpqc().save(&output, atoms);
|
---|
[cabb46] | 308 | } else if (suffix == ParserSuffixes[pcp]) {
|
---|
[73916f] | 309 | getPcp().save(&output, atoms);
|
---|
[cabb46] | 310 | } else if (suffix == ParserSuffixes[pdb]) {
|
---|
[73916f] | 311 | getPdb().save(&output, atoms);
|
---|
[cabb46] | 312 | } else if (suffix == ParserSuffixes[tremolo]) {
|
---|
[73916f] | 313 | getTremolo().save(&output, atoms);
|
---|
[cabb46] | 314 | } else if (suffix == ParserSuffixes[xyz]) {
|
---|
[73916f] | 315 | getXyz().save(&output, atoms);
|
---|
[cabb46] | 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 |
|
---|
[dc0d21] | 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 |
|
---|
[bb6193] | 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 |
|
---|
[dc0d21] | 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 |
|
---|
[52baf9] | 374 |
|
---|
| 375 | CONSTRUCT_SINGLETON(FormatParserStorage)
|
---|