[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 |
|
---|
[ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
[bbbad5] | 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 |
|
---|
[ad011c] | 34 | #include "CodePatterns/Log.hpp"
|
---|
| 35 | #include "CodePatterns/Verbose.hpp"
|
---|
[52baf9] | 36 |
|
---|
[ad011c] | 37 | #include "CodePatterns/Assert.hpp"
|
---|
[52baf9] | 38 |
|
---|
[73916f] | 39 | #include "molecule.hpp"
|
---|
| 40 |
|
---|
[ad011c] | 41 | #include "CodePatterns/Singleton_impl.hpp"
|
---|
[52baf9] | 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
|
---|
[78bb14] | 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);
|
---|
[52baf9] | 134 | }
|
---|
| 135 |
|
---|
[5c6946] | 136 |
|
---|
[52baf9] | 137 | /** Adds an PcpParser to the storage.
|
---|
| 138 | */
|
---|
[dc0d21] | 139 | void FormatParserStorage::addPcp()
|
---|
[52baf9] | 140 | {
|
---|
[dc0d21] | 141 | if (!ParserPresent[pcp]) {
|
---|
[52baf9] | 142 | ParserList[pcp] = new PcpParser();
|
---|
[dc0d21] | 143 | ParserPresent[pcp] = true;
|
---|
| 144 | } else
|
---|
[78bb14] | 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);
|
---|
[52baf9] | 147 | }
|
---|
| 148 |
|
---|
| 149 |
|
---|
[bb6193] | 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
|
---|
[78bb14] | 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);
|
---|
[bb6193] | 160 | }
|
---|
| 161 |
|
---|
| 162 |
|
---|
[52baf9] | 163 | /** Adds an TremoloParser to the storage.
|
---|
| 164 | */
|
---|
[dc0d21] | 165 | void FormatParserStorage::addTremolo()
|
---|
[52baf9] | 166 | {
|
---|
[dc0d21] | 167 | if (!ParserPresent[tremolo]) {
|
---|
[52baf9] | 168 | ParserList[tremolo] = new TremoloParser();
|
---|
[dc0d21] | 169 | ParserPresent[tremolo] = true;
|
---|
| 170 | } else
|
---|
[78bb14] | 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);
|
---|
[52baf9] | 173 | }
|
---|
| 174 |
|
---|
| 175 |
|
---|
| 176 | /** Adds an XyzParser to the storage.
|
---|
| 177 | */
|
---|
[dc0d21] | 178 | void FormatParserStorage::addXyz()
|
---|
[52baf9] | 179 | {
|
---|
[dc0d21] | 180 | if (!ParserPresent[xyz]) {
|
---|
[52baf9] | 181 | ParserList[xyz] = new XyzParser();
|
---|
[dc0d21] | 182 | ParserPresent[xyz] = true;
|
---|
| 183 | } else
|
---|
[78bb14] | 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);
|
---|
[52baf9] | 186 | }
|
---|
| 187 |
|
---|
[a42054] | 188 | ParserTypes FormatParserStorage::getTypeFromName(std::string type)
|
---|
[5c6946] | 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 |
|
---|
[a42054] | 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 |
|
---|
[5c6946] | 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 | {
|
---|
[a42054] | 224 | return add(getTypeFromName(type));
|
---|
[5c6946] | 225 | }
|
---|
| 226 |
|
---|
| 227 |
|
---|
[86cff86] | 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 | */
|
---|
[73916f] | 233 | bool FormatParserStorage::load(std::istream &input, std::string suffix)
|
---|
[86cff86] | 234 | {
|
---|
[a42054] | 235 | if (suffix == ParserSuffixes[mpqc]) {
|
---|
[86cff86] | 236 | getMpqc().load(&input);
|
---|
[a42054] | 237 | } else if (suffix == ParserSuffixes[pcp]) {
|
---|
[86cff86] | 238 | getPcp().load(&input);
|
---|
[a42054] | 239 | } else if (suffix == ParserSuffixes[pdb]) {
|
---|
[bb6193] | 240 | getPdb().load(&input);
|
---|
[a42054] | 241 | } else if (suffix == ParserSuffixes[tremolo]) {
|
---|
[86cff86] | 242 | getTremolo().load(&input);
|
---|
[a42054] | 243 | } else if (suffix == ParserSuffixes[xyz]) {
|
---|
[86cff86] | 244 | getXyz().load(&input);
|
---|
| 245 | } else {
|
---|
[5c6946] | 246 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::get()." << endl);
|
---|
[86cff86] | 247 | return false;
|
---|
| 248 | }
|
---|
| 249 | return true;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
[73916f] | 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 | }
|
---|
[cabb46] | 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 | */
|
---|
[73916f] | 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)
|
---|
[cabb46] | 310 | {
|
---|
| 311 | if (suffix == ParserSuffixes[mpqc]) {
|
---|
[73916f] | 312 | getMpqc().save(&output, atoms);
|
---|
[cabb46] | 313 | } else if (suffix == ParserSuffixes[pcp]) {
|
---|
[73916f] | 314 | getPcp().save(&output, atoms);
|
---|
[cabb46] | 315 | } else if (suffix == ParserSuffixes[pdb]) {
|
---|
[73916f] | 316 | getPdb().save(&output, atoms);
|
---|
[cabb46] | 317 | } else if (suffix == ParserSuffixes[tremolo]) {
|
---|
[73916f] | 318 | getTremolo().save(&output, atoms);
|
---|
[cabb46] | 319 | } else if (suffix == ParserSuffixes[xyz]) {
|
---|
[73916f] | 320 | getXyz().save(&output, atoms);
|
---|
[cabb46] | 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 |
|
---|
[dc0d21] | 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 |
|
---|
[bb6193] | 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 |
|
---|
[dc0d21] | 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 |
|
---|
[52baf9] | 379 |
|
---|
| 380 | CONSTRUCT_SINGLETON(FormatParserStorage)
|
---|