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