| 1 | /*
|
|---|
| 2 | * TremoloParser.cpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: Mar 2, 2010
|
|---|
| 5 | * Author: metzler
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #include "TremoloParser.hpp"
|
|---|
| 9 | #include "World.hpp"
|
|---|
| 10 | #include "atom.hpp"
|
|---|
| 11 | #include "element.hpp"
|
|---|
| 12 | #include "periodentafel.hpp"
|
|---|
| 13 | #include <map>
|
|---|
| 14 | #include <vector>
|
|---|
| 15 |
|
|---|
| 16 | using namespace std;
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Constructor.
|
|---|
| 20 | */
|
|---|
| 21 | TremoloParser::TremoloParser() {
|
|---|
| 22 | knownKeys[" "] = noKey; // so we can detect invalid keys
|
|---|
| 23 | knownKeys["x"] = x;
|
|---|
| 24 | knownKeys["u"] = u;
|
|---|
| 25 | knownKeys["F"] = F;
|
|---|
| 26 | knownKeys["stress"] = stress;
|
|---|
| 27 | knownKeys["Id"] = Id;
|
|---|
| 28 | knownKeys["neighbors"] = neighbors;
|
|---|
| 29 | knownKeys["imprData"] = imprData;
|
|---|
| 30 | knownKeys["GroupMeasureTypeNo"] = GroupMeasureTypeNo;
|
|---|
| 31 | knownKeys["Type"] = Type;
|
|---|
| 32 | knownKeys["extType"] = extType;
|
|---|
| 33 | knownKeys["name"] = name;
|
|---|
| 34 | knownKeys["resName"] = resName;
|
|---|
| 35 | knownKeys["chainID"] = chainID;
|
|---|
| 36 | knownKeys["resSeq"] = resSeq;
|
|---|
| 37 | knownKeys["occupancy"] = occupancy;
|
|---|
| 38 | knownKeys["tempFactor"] = tempFactor;
|
|---|
| 39 | knownKeys["segID"] = segID;
|
|---|
| 40 | knownKeys["Charge"] = Charge;
|
|---|
| 41 | knownKeys["charge"] = charge;
|
|---|
| 42 | knownKeys["GrpTypeNo"] = GrpTypeNo;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * Destructor.
|
|---|
| 47 | */
|
|---|
| 48 | TremoloParser::~TremoloParser() {
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /**
|
|---|
| 52 | * Stores keys from the ATOMDATA line.
|
|---|
| 53 | *
|
|---|
| 54 | * \param line to parse the keys from
|
|---|
| 55 | * \param with which offset the keys begin within the line
|
|---|
| 56 | */
|
|---|
| 57 | void TremoloParser::parseAtomDataKeysLine(string line, int offset) {
|
|---|
| 58 | string keyword;
|
|---|
| 59 | stringstream lineStream;
|
|---|
| 60 |
|
|---|
| 61 | lineStream << line.substr(offset);
|
|---|
| 62 | while (lineStream.good()) {
|
|---|
| 63 | lineStream >> keyword;
|
|---|
| 64 | if (knownKeys[keyword.substr(0, keyword.find("="))] == noKey) {
|
|---|
| 65 | // throw exception about unknown key
|
|---|
| 66 | cout << "Unknown key: " << keyword << " is not part of the tremolo format specification." << endl;
|
|---|
| 67 | break;
|
|---|
| 68 | }
|
|---|
| 69 | usedFields.push_back(keyword);
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Reads one data line of a tremolo file and interprets it according to the keys
|
|---|
| 75 | * obtained from the ATOMDATA line.
|
|---|
| 76 | *
|
|---|
| 77 | * \param line to parse as an atom
|
|---|
| 78 | */
|
|---|
| 79 | void TremoloParser::readAtomDataLine(string line) {
|
|---|
| 80 | vector<string>::iterator it;
|
|---|
| 81 | stringstream lineStream;
|
|---|
| 82 | atom* newAtom = World::getInstance().createAtom();
|
|---|
| 83 | TremoloAtomInfoContainer atomInfo = *(new TremoloAtomInfoContainer());
|
|---|
| 84 | atomDataKey currentField;
|
|---|
| 85 | string word;
|
|---|
| 86 |
|
|---|
| 87 | lineStream << line;
|
|---|
| 88 | for (it=usedFields.begin(); it < usedFields.end(); it++) {
|
|---|
| 89 | cout << *it << " -- " << it->substr(0, it->find("=")) << " -- " << knownKeys[it->substr(0, it->find("="))] << endl;
|
|---|
| 90 | currentField = knownKeys[it->substr(0, it->find("="))];
|
|---|
| 91 | switch (currentField) {
|
|---|
| 92 | case x :
|
|---|
| 93 | // for the moment, assume there are always three dimensions
|
|---|
| 94 | lineStream >> newAtom->x.x[0];
|
|---|
| 95 | lineStream >> newAtom->x.x[1];
|
|---|
| 96 | lineStream >> newAtom->x.x[2];
|
|---|
| 97 | break;
|
|---|
| 98 | case u :
|
|---|
| 99 | // for the moment, assume there are always three dimensions
|
|---|
| 100 | lineStream >> newAtom->v.x[0];
|
|---|
| 101 | lineStream >> newAtom->v.x[1];
|
|---|
| 102 | lineStream >> newAtom->v.x[2];
|
|---|
| 103 | break;
|
|---|
| 104 | case F :
|
|---|
| 105 | lineStream >> word;
|
|---|
| 106 | atomInfo.F = word;
|
|---|
| 107 | break;
|
|---|
| 108 | case stress :
|
|---|
| 109 | lineStream >> word;
|
|---|
| 110 | atomInfo.F = word;
|
|---|
| 111 | break;
|
|---|
| 112 | case Id :
|
|---|
| 113 | // this ID is not used
|
|---|
| 114 | break;
|
|---|
| 115 | case neighbors :
|
|---|
| 116 | // TODO neighbor information
|
|---|
| 117 | lineStream >> word;
|
|---|
| 118 | break;
|
|---|
| 119 | case imprData :
|
|---|
| 120 | lineStream >> word;
|
|---|
| 121 | atomInfo.imprData = word;
|
|---|
| 122 | break;
|
|---|
| 123 | case GroupMeasureTypeNo :
|
|---|
| 124 | lineStream >> word;
|
|---|
| 125 | atomInfo.GroupMeasureTypeNo = word;
|
|---|
| 126 | break;
|
|---|
| 127 | case Type :
|
|---|
| 128 | char type[3];
|
|---|
| 129 | lineStream >> type;
|
|---|
| 130 | newAtom->setType(World::getInstance().getPeriode()->FindElement(type));
|
|---|
| 131 | break;
|
|---|
| 132 | case extType :
|
|---|
| 133 | lineStream >> word;
|
|---|
| 134 | atomInfo.extType = word;
|
|---|
| 135 | break;
|
|---|
| 136 | case name :
|
|---|
| 137 | lineStream >> word;
|
|---|
| 138 | atomInfo.name = word;
|
|---|
| 139 | break;
|
|---|
| 140 | case resName :
|
|---|
| 141 | lineStream >> word;
|
|---|
| 142 | atomInfo.resName = word;
|
|---|
| 143 | break;
|
|---|
| 144 | case chainID :
|
|---|
| 145 | lineStream >> word;
|
|---|
| 146 | atomInfo.chainID = word;
|
|---|
| 147 | break;
|
|---|
| 148 | case resSeq :
|
|---|
| 149 | lineStream >> word;
|
|---|
| 150 | atomInfo.resSeq = word;
|
|---|
| 151 | break;
|
|---|
| 152 | case occupancy :
|
|---|
| 153 | lineStream >> word;
|
|---|
| 154 | atomInfo.occupancy = word;
|
|---|
| 155 | break;
|
|---|
| 156 | case tempFactor :
|
|---|
| 157 | lineStream >> word;
|
|---|
| 158 | atomInfo.segID = word;
|
|---|
| 159 | break;
|
|---|
| 160 | case segID :
|
|---|
| 161 | lineStream >> word;
|
|---|
| 162 | atomInfo.F = word;
|
|---|
| 163 | break;
|
|---|
| 164 | case Charge :
|
|---|
| 165 | lineStream >> word;
|
|---|
| 166 | atomInfo.Charge = word;
|
|---|
| 167 | break;
|
|---|
| 168 | case charge :
|
|---|
| 169 | lineStream >> word;
|
|---|
| 170 | atomInfo.charge = word;
|
|---|
| 171 | break;
|
|---|
| 172 | case GrpTypeNo :
|
|---|
| 173 | lineStream >> word;
|
|---|
| 174 | atomInfo.GrpTypeNo = word;
|
|---|
| 175 | break;
|
|---|
| 176 | default :
|
|---|
| 177 | lineStream >> word;
|
|---|
| 178 | cout << "Unknown key: " << *it << ", word: " << word << endl;
|
|---|
| 179 | break;
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|
| 182 | moreData[newAtom->getId()] = atomInfo;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 | /**
|
|---|
| 187 | * Loads atoms from a tremolo-formatted file.
|
|---|
| 188 | *
|
|---|
| 189 | * \param tremolo file
|
|---|
| 190 | */
|
|---|
| 191 | void TremoloParser::load(istream* file) {
|
|---|
| 192 | string line;
|
|---|
| 193 | string::size_type location;
|
|---|
| 194 |
|
|---|
| 195 | usedFields.clear();
|
|---|
| 196 | while (file->good()) {
|
|---|
| 197 | std::getline(*file, line, '\n');
|
|---|
| 198 | if (usedFields.empty()) {
|
|---|
| 199 | location = line.find("ATOMDATA", 0);
|
|---|
| 200 | if (location != string::npos) {
|
|---|
| 201 | parseAtomDataKeysLine(line, location + 8);
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 | if (line.length() > 0 && line.at(0) != '#') {
|
|---|
| 205 | readAtomDataLine(line);
|
|---|
| 206 | }
|
|---|
| 207 | }
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /**
|
|---|
| 211 | * Saves the World's current state into as a tremolo file.
|
|---|
| 212 | *
|
|---|
| 213 | * \param file where to save the state
|
|---|
| 214 | */
|
|---|
| 215 | void TremoloParser::save(ostream* file) {
|
|---|
| 216 | /*
|
|---|
| 217 | write header
|
|---|
| 218 | for (each atom in world) {
|
|---|
| 219 | write coordinates and additional data
|
|---|
| 220 | }
|
|---|
| 221 | */
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | TremoloAtomInfoContainer::TremoloAtomInfoContainer() {
|
|---|
| 225 | name = "none";
|
|---|
| 226 | /* Add suitable default values.
|
|---|
| 227 | std::string F;
|
|---|
| 228 | std::string stress;
|
|---|
| 229 | std::string imprData;
|
|---|
| 230 | std::string GroupMeasureTypeNo;
|
|---|
| 231 | std::string extType;
|
|---|
| 232 | std::string name;
|
|---|
| 233 | std::string resName;
|
|---|
| 234 | std::string chainID;
|
|---|
| 235 | std::string resSeq;
|
|---|
| 236 | std::string occupancy;
|
|---|
| 237 | std::string tempFactor;
|
|---|
| 238 | std::string segID;
|
|---|
| 239 | std::string Charge;
|
|---|
| 240 | std::string charge;
|
|---|
| 241 | std::string GrpTypeNo;
|
|---|
| 242 | */
|
|---|
| 243 | };
|
|---|