[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[bcf653] | 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 |
|
---|
[765f16] | 25 | #include <boost/preprocessor/iteration/local.hpp>
|
---|
[52baf9] | 26 |
|
---|
[ad011c] | 27 | #include "CodePatterns/Assert.hpp"
|
---|
[765f16] | 28 | #include "CodePatterns/Log.hpp"
|
---|
[52baf9] | 29 |
|
---|
[73916f] | 30 | #include "molecule.hpp"
|
---|
[765f16] | 31 | #include "FormatParserStorage.hpp"
|
---|
| 32 | #include "ParserTypes.hpp"
|
---|
| 33 |
|
---|
| 34 | #include "MpqcParser.hpp"
|
---|
| 35 | #include "PcpParser.hpp"
|
---|
| 36 | #include "PdbParser.hpp"
|
---|
[1bef07] | 37 | #include "Psi3Parser.hpp"
|
---|
[765f16] | 38 | #include "TremoloParser.hpp"
|
---|
[3d0892] | 39 | #include "XmlParser.hpp"
|
---|
[765f16] | 40 | #include "XyzParser.hpp"
|
---|
[73916f] | 41 |
|
---|
[ad011c] | 42 | #include "CodePatterns/Singleton_impl.hpp"
|
---|
[52baf9] | 43 |
|
---|
[babcc1] | 44 | const std::string FormatParserStorage::unknownTypeString("unknown");
|
---|
[dc0d21] | 45 |
|
---|
[52baf9] | 46 | /** Constructor of class FormatParserStorage.
|
---|
| 47 | */
|
---|
| 48 | FormatParserStorage::FormatParserStorage()
|
---|
| 49 | {
|
---|
| 50 | ParserList.resize(ParserTypes_end, NULL);
|
---|
[60239f] | 51 | ParserStream.resize(ParserTypes_end, NULL);
|
---|
[52baf9] | 52 | ParserPresent.resize(ParserTypes_end, false);
|
---|
[091838] | 53 | ParserDesiredOutputFormat.resize(ParserTypes_end, false);
|
---|
[52baf9] | 54 |
|
---|
[765f16] | 55 | #include "ParserTypes.def"
|
---|
| 56 |
|
---|
| 57 | #define insert_print(z,n,seq,map, before, after) \
|
---|
| 58 | map .insert( std::make_pair( \
|
---|
| 59 | BOOST_PP_SEQ_ELEM(n, seq) \
|
---|
| 60 | , before < \
|
---|
| 61 | BOOST_PP_SEQ_ELEM(n, seq) \
|
---|
| 62 | > after \
|
---|
| 63 | ) );
|
---|
[db67ea] | 64 |
|
---|
[765f16] | 65 | #define insert_invert_print(z,n,seq,map, before, after) \
|
---|
| 66 | map .insert( std::make_pair( before < \
|
---|
| 67 | BOOST_PP_SEQ_ELEM(n, seq) \
|
---|
| 68 | > after, \
|
---|
| 69 | BOOST_PP_SEQ_ELEM(n, seq) \
|
---|
| 70 | ) );
|
---|
| 71 |
|
---|
| 72 | // fill ParserNames
|
---|
| 73 | #if defined ParserTypes_END // do we have parameters at all?
|
---|
| 74 | #define BOOST_PP_LOCAL_MACRO(n) insert_print(~, n, PARSERSEQUENCE, ParserNames, FormatParserTrait, ::name)
|
---|
| 75 | #define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
|
---|
| 76 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 77 | #endif
|
---|
| 78 |
|
---|
| 79 | // fill ParserLookupNames
|
---|
| 80 | #if defined ParserTypes_END // do we have parameters at all?
|
---|
| 81 | #define BOOST_PP_LOCAL_MACRO(n) insert_invert_print(~, n, PARSERSEQUENCE, ParserLookupNames, FormatParserTrait, ::name)
|
---|
| 82 | #define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
|
---|
| 83 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 84 | #endif
|
---|
| 85 |
|
---|
| 86 | // fill ParserSuffixes
|
---|
| 87 | #if defined ParserTypes_END // do we have parameters at all?
|
---|
| 88 | #define BOOST_PP_LOCAL_MACRO(n) insert_print(~, n, PARSERSEQUENCE, ParserSuffixes, FormatParserTrait, ::suffix)
|
---|
| 89 | #define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
|
---|
| 90 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 91 | #endif
|
---|
| 92 |
|
---|
| 93 | // fill ParserLookupSuffixes
|
---|
| 94 | #if defined ParserTypes_END // do we have parameters at all?
|
---|
| 95 | #define BOOST_PP_LOCAL_MACRO(n) insert_invert_print(~, n, PARSERSEQUENCE, ParserLookupSuffixes, FormatParserTrait, ::suffix)
|
---|
| 96 | #define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
|
---|
| 97 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 98 | #endif
|
---|
| 99 |
|
---|
| 100 | // fill ParserAddFunction
|
---|
| 101 | #if defined ParserTypes_END // do we have parameters at all?
|
---|
| 102 | #define BOOST_PP_LOCAL_MACRO(n) insert_print(~, n, PARSERSEQUENCE, ParserAddFunction, &FormatParserStorage::addParser, )
|
---|
| 103 | #define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
|
---|
| 104 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 105 | #endif
|
---|
| 106 |
|
---|
| 107 | #undef insert_print
|
---|
| 108 | #undef insert_invert_print
|
---|
| 109 | #include "ParserTypes.undef"
|
---|
| 110 |
|
---|
| 111 | //std::cout << "ParserNames:" << std::endl << ParserNames << std::endl;
|
---|
| 112 | //std::cout << "ParserSuffixes:" << std::endl << ParserSuffixes << std::endl;
|
---|
| 113 | //std::cout << "ParserLookupNames:" << std::endl << ParserLookupNames << std::endl;
|
---|
| 114 | //std::cout << "ParserLookupSuffixes:" << std::endl << ParserLookupSuffixes << std::endl;
|
---|
| 115 | //std::cout << "ParserAddFunction:" << std::endl << ParserAddFunction << std::endl;
|
---|
| 116 |
|
---|
[52baf9] | 117 | }
|
---|
| 118 |
|
---|
| 119 | /** Destructor of class FormatParserStorage.
|
---|
| 120 | * Free all stored FormatParsers.
|
---|
| 121 | * Save on Exit.
|
---|
| 122 | */
|
---|
| 123 | FormatParserStorage::~FormatParserStorage()
|
---|
| 124 | {
|
---|
| 125 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
[60239f] | 126 | if (ParserPresent[iter]) {
|
---|
[091838] | 127 | if (ParserStream[iter] != NULL) {
|
---|
| 128 | if (ParserStream[iter]->is_open())
|
---|
| 129 | ParserStream[iter]->close();
|
---|
| 130 | delete ParserStream[iter];
|
---|
| 131 | }
|
---|
[52baf9] | 132 | delete ParserList[iter];
|
---|
[60239f] | 133 | }
|
---|
[52baf9] | 134 | }
|
---|
| 135 |
|
---|
| 136 | /** Sets the filename of all current parsers in storage to prefix.suffix.
|
---|
| 137 | * \param &prefix prefix to use.
|
---|
| 138 | */
|
---|
| 139 | void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
|
---|
| 140 | {
|
---|
| 141 | prefix=_prefix;
|
---|
| 142 | };
|
---|
| 143 |
|
---|
[091838] | 144 | /** Sets \a type as a format to be stored on call of SaveAll.
|
---|
| 145 | *
|
---|
| 146 | * @param type type to add to desired output formats
|
---|
| 147 | */
|
---|
| 148 | void FormatParserStorage::setOutputFormat(ParserTypes type)
|
---|
| 149 | {
|
---|
| 150 | LOG(0, "STATUS: Adding " << ParserNames[type] << " type to output.");
|
---|
| 151 | ParserDesiredOutputFormat[type] = true;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /** Sets \a type as a format to be stored on call of SaveAll.
|
---|
| 155 | *
|
---|
| 156 | * @param type type to add to desired output formats
|
---|
| 157 | */
|
---|
| 158 | void FormatParserStorage::setOutputFormat(std::string type)
|
---|
| 159 | {
|
---|
| 160 | std::map<std::string, ParserTypes>::const_iterator iter = ParserLookupNames.find(type);
|
---|
| 161 | ASSERT(iter != ParserLookupNames.end(),
|
---|
| 162 | "FormatParserStorage::setOutputFormat() - output format "+type+" is unknown.");
|
---|
| 163 | setOutputFormat(iter->second);
|
---|
| 164 | }
|
---|
[52baf9] | 165 |
|
---|
[091838] | 166 | /** Saves the world in the desired output formats.
|
---|
| 167 | *
|
---|
| 168 | */
|
---|
[52baf9] | 169 | void FormatParserStorage::SaveAll()
|
---|
| 170 | {
|
---|
| 171 | std::string filename;
|
---|
| 172 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
[091838] | 173 | if (ParserPresent[iter] && ParserDesiredOutputFormat[iter]) {
|
---|
[52baf9] | 174 | filename = prefix;
|
---|
| 175 | filename += ".";
|
---|
[a42054] | 176 | filename += ParserSuffixes[iter];
|
---|
[60239f] | 177 | ParserStream[iter] = new std::ofstream(filename.c_str());
|
---|
| 178 | ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
|
---|
[52baf9] | 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 |
|
---|
[a42054] | 183 | ParserTypes FormatParserStorage::getTypeFromName(std::string type)
|
---|
[5c6946] | 184 | {
|
---|
| 185 | if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
|
---|
[47d041] | 186 | ELOG(1, "Unknown type " << type << ".");
|
---|
[5c6946] | 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()) {
|
---|
[47d041] | 195 | ELOG(1, "Unknown type " << type << ".");
|
---|
[a42054] | 196 | return ParserTypes_end;
|
---|
| 197 | } else
|
---|
| 198 | return ParserLookupSuffixes[type];
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[babcc1] | 201 | const std::string &FormatParserStorage::getNameFromType(ParserTypes type)
|
---|
| 202 | {
|
---|
| 203 | if (ParserNames.find(type) == ParserNames.end()) {
|
---|
[47d041] | 204 | ELOG(1, "Unknown type " << type << ".");
|
---|
[babcc1] | 205 | return unknownTypeString;
|
---|
| 206 | } else
|
---|
| 207 | return ParserNames[type];
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | const std::string &FormatParserStorage::getSuffixFromType(ParserTypes type)
|
---|
| 211 | {
|
---|
| 212 | if (ParserSuffixes.find(type) == ParserSuffixes.end()) {
|
---|
[47d041] | 213 | ELOG(1, "Unknown type " << type << ".");
|
---|
[babcc1] | 214 | return unknownTypeString;
|
---|
| 215 | } else
|
---|
| 216 | return ParserSuffixes[type];
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[5c6946] | 219 | bool FormatParserStorage::add(ParserTypes ptype)
|
---|
| 220 | {
|
---|
| 221 | if (ptype != ParserTypes_end) {
|
---|
| 222 | if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
|
---|
| 223 | (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
|
---|
| 224 | return true;
|
---|
| 225 | } else {
|
---|
[765f16] | 226 | ELOG(1, "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?");
|
---|
[5c6946] | 227 | return false;
|
---|
| 228 | }
|
---|
| 229 | } else {
|
---|
| 230 | return false;
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | bool FormatParserStorage::add(std::string type)
|
---|
| 235 | {
|
---|
[765f16] | 236 | enum ParserTypes Ptype = getTypeFromName(type);
|
---|
| 237 | return add(Ptype);
|
---|
[5c6946] | 238 | }
|
---|
| 239 |
|
---|
[98c428] | 240 | /** Recognizes type of file and parse via FormatParserStorage::load().
|
---|
| 241 | * \param filename path and filename
|
---|
| 242 | * \return true - parsing ok, false - suffix unknown
|
---|
| 243 | */
|
---|
| 244 | bool FormatParserStorage::load(boost::filesystem::path filename)
|
---|
| 245 | {
|
---|
| 246 | return load(filename.string());
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | /** Recognizes type of file and parse via FormatParserStorage::load().
|
---|
| 250 | * \param filename path and filename
|
---|
| 251 | * \return true - parsing ok, false - suffix unknown
|
---|
| 252 | */
|
---|
| 253 | bool FormatParserStorage::load(std::string &filename)
|
---|
| 254 | {
|
---|
| 255 | std::string FilenameSuffix = filename.substr(filename.find_last_of('.')+1, filename.length());
|
---|
| 256 | ifstream input;
|
---|
| 257 | LOG(0, "STATUS: Loading filler molecule " << filename
|
---|
| 258 | << " of suffix " << FilenameSuffix << ".");
|
---|
| 259 | input.open(filename.c_str());
|
---|
| 260 | const bool status = load(input, FilenameSuffix);
|
---|
| 261 | input.close();
|
---|
| 262 |
|
---|
| 263 | return status;
|
---|
| 264 | }
|
---|
[5c6946] | 265 |
|
---|
[86cff86] | 266 | /** Parses an istream depending on its suffix
|
---|
| 267 | * \param &input input stream
|
---|
| 268 | * \param suffix
|
---|
| 269 | * \return true - parsing ok, false - suffix unknown
|
---|
| 270 | */
|
---|
[98c428] | 271 | bool FormatParserStorage::load(std::istream &input, std::string &suffix)
|
---|
[86cff86] | 272 | {
|
---|
[6d0f058] | 273 | enum ParserTypes type = getTypeFromSuffix(suffix);
|
---|
| 274 | if (type != ParserTypes_end)
|
---|
| 275 | get(type).load(&input);
|
---|
| 276 | else
|
---|
[86cff86] | 277 | return false;
|
---|
| 278 | return true;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[73916f] | 281 | /** Stores all selected atoms in an ostream depending on its suffix
|
---|
| 282 | * \param &output output stream
|
---|
| 283 | * \param suffix
|
---|
| 284 | * \return true - storing ok, false - suffix unknown
|
---|
| 285 | */
|
---|
| 286 | bool FormatParserStorage::saveSelectedAtoms(std::ostream &output, std::string suffix)
|
---|
| 287 | {
|
---|
| 288 | std::vector<atom *> atoms = World::getInstance().getSelectedAtoms();
|
---|
| 289 | return save(output, suffix, atoms);
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | /** Stores all selected atoms in an ostream depending on its suffix
|
---|
| 293 | * We store in the order of the atomic ids, not in the order they appear in the molecules.
|
---|
| 294 | * Hence, we first create a vector from all selected molecules' atoms.
|
---|
[59fff1] | 295 | *
|
---|
| 296 | * TODO: Change here atom * to const atom * when FormatParserStorage::save() uses vector<const atom *>
|
---|
| 297 | *
|
---|
[73916f] | 298 | * \param &output output stream
|
---|
| 299 | * \param suffix
|
---|
| 300 | * \return true - storing ok, false - suffix unknown
|
---|
| 301 | */
|
---|
| 302 | bool FormatParserStorage::saveSelectedMolecules(std::ostream &output, std::string suffix)
|
---|
| 303 | {
|
---|
| 304 | std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
|
---|
| 305 | std::map<size_t, atom *> IdAtoms;
|
---|
| 306 | for (std::vector<molecule *>::const_iterator MolIter = molecules.begin();
|
---|
| 307 | MolIter != molecules.end();
|
---|
| 308 | ++MolIter) {
|
---|
[59fff1] | 309 | for(molecule::iterator AtomIter = (*MolIter)->begin();
|
---|
[73916f] | 310 | AtomIter != (*MolIter)->end();
|
---|
| 311 | ++AtomIter) {
|
---|
| 312 | IdAtoms.insert( make_pair((*AtomIter)->getId(), (*AtomIter)) );
|
---|
| 313 | }
|
---|
| 314 | }
|
---|
| 315 | std::vector<atom *> atoms;
|
---|
| 316 | atoms.reserve(IdAtoms.size());
|
---|
| 317 | for (std::map<size_t, atom *>::const_iterator iter = IdAtoms.begin();
|
---|
| 318 | iter != IdAtoms.end();
|
---|
| 319 | ++iter) {
|
---|
| 320 | atoms.push_back(iter->second);
|
---|
| 321 | }
|
---|
| 322 | return save(output, suffix, atoms);
|
---|
| 323 | }
|
---|
[cabb46] | 324 |
|
---|
| 325 | /** Stores world in an ostream depending on its suffix
|
---|
| 326 | * \param &output output stream
|
---|
| 327 | * \param suffix
|
---|
| 328 | * \return true - storing ok, false - suffix unknown
|
---|
| 329 | */
|
---|
[73916f] | 330 | bool FormatParserStorage::saveWorld(std::ostream &output, std::string suffix)
|
---|
| 331 | {
|
---|
| 332 | std::vector<atom *> atoms = World::getInstance().getAllAtoms();
|
---|
| 333 | return save(output, suffix, atoms);
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | /** Stores a given vector of \a atoms in an ostream depending on its suffix
|
---|
| 337 | * \param &output output stream
|
---|
| 338 | * \param suffix
|
---|
| 339 | * \return true - storing ok, false - suffix unknown
|
---|
| 340 | */
|
---|
| 341 | bool FormatParserStorage::save(std::ostream &output, std::string suffix, const std::vector<atom *> &atoms)
|
---|
[cabb46] | 342 | {
|
---|
[6d0f058] | 343 | enum ParserTypes type = getTypeFromSuffix(suffix);
|
---|
| 344 | if (type != ParserTypes_end)
|
---|
| 345 | get(type).save(&output, atoms);
|
---|
| 346 | else
|
---|
[cabb46] | 347 | return false;
|
---|
| 348 | return true;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
[7a51be] | 351 | /** Returns reference to the desired output parser as FormatParser, adds if not present.
|
---|
| 352 | * \param _type type of desired parser
|
---|
| 353 | * \return reference to the output FormatParser with desired type
|
---|
| 354 | */
|
---|
[765f16] | 355 | FormatParserInterface &FormatParserStorage::get(ParserTypes _type)
|
---|
[7a51be] | 356 | {
|
---|
| 357 | if (!ParserPresent[_type]) {
|
---|
| 358 | add(_type);
|
---|
| 359 | }
|
---|
| 360 | return *ParserList[_type];
|
---|
| 361 | }
|
---|
[52baf9] | 362 |
|
---|
| 363 | CONSTRUCT_SINGLETON(FormatParserStorage)
|
---|