| [3ae731] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [5aaa43] | 5 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| [94d5ac6] | 6 | * | 
|---|
|  | 7 | * | 
|---|
|  | 8 | *   This file is part of MoleCuilder. | 
|---|
|  | 9 | * | 
|---|
|  | 10 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
|  | 11 | *    it under the terms of the GNU General Public License as published by | 
|---|
|  | 12 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
|  | 13 | *    (at your option) any later version. | 
|---|
|  | 14 | * | 
|---|
|  | 15 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
|  | 16 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 17 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 18 | *    GNU General Public License for more details. | 
|---|
|  | 19 | * | 
|---|
|  | 20 | *    You should have received a copy of the GNU General Public License | 
|---|
|  | 21 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| [3ae731] | 22 | */ | 
|---|
|  | 23 |  | 
|---|
|  | 24 | /* | 
|---|
|  | 25 | * PdbParser.cpp | 
|---|
|  | 26 | * | 
|---|
|  | 27 | *  Created on: Aug 17, 2010 | 
|---|
|  | 28 | *      Author: heber | 
|---|
|  | 29 | */ | 
|---|
|  | 30 |  | 
|---|
|  | 31 | // include config.h | 
|---|
|  | 32 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 33 | #include <config.h> | 
|---|
|  | 34 | #endif | 
|---|
|  | 35 |  | 
|---|
| [ad011c] | 36 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [3ae731] | 37 |  | 
|---|
| [ad011c] | 38 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 39 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 40 | #include "CodePatterns/toString.hpp" | 
|---|
|  | 41 | #include "CodePatterns/Verbose.hpp" | 
|---|
| [42127c] | 42 |  | 
|---|
| [6f0841] | 43 | #include "Atom/atom.hpp" | 
|---|
| [129204] | 44 | #include "Bond/bond.hpp" | 
|---|
| [42127c] | 45 | #include "Descriptors/AtomIdDescriptor.hpp" | 
|---|
| [3bdb6d] | 46 | #include "Element/element.hpp" | 
|---|
|  | 47 | #include "Element/periodentafel.hpp" | 
|---|
| [42127c] | 48 | #include "molecule.hpp" | 
|---|
|  | 49 | #include "MoleculeListClass.hpp" | 
|---|
| [4fbca9c] | 50 | #include "Parser/PdbParser.hpp" | 
|---|
| [073a9e4] | 51 | #include "World.hpp" | 
|---|
|  | 52 | #include "WorldTime.hpp" | 
|---|
| [bb6193] | 53 |  | 
|---|
| [105b72] | 54 | #include <algorithm> | 
|---|
|  | 55 | #include <cmath> | 
|---|
| [3ae731] | 56 | #include <map> | 
|---|
|  | 57 | #include <vector> | 
|---|
|  | 58 |  | 
|---|
| [bb6193] | 59 | #include <iostream> | 
|---|
|  | 60 | #include <iomanip> | 
|---|
| [3ae731] | 61 |  | 
|---|
|  | 62 | using namespace std; | 
|---|
|  | 63 |  | 
|---|
| [765f16] | 64 | // declare specialized static variables | 
|---|
|  | 65 | const std::string FormatParserTrait<pdb>::name = "pdb"; | 
|---|
|  | 66 | const std::string FormatParserTrait<pdb>::suffix = "pdb"; | 
|---|
|  | 67 | const ParserTypes FormatParserTrait<pdb>::type = pdb; | 
|---|
|  | 68 |  | 
|---|
| [3ae731] | 69 | /** | 
|---|
|  | 70 | * Constructor. | 
|---|
|  | 71 | */ | 
|---|
| [765f16] | 72 | FormatParser< pdb >::FormatParser() : | 
|---|
|  | 73 | FormatParser_common(NULL) | 
|---|
|  | 74 | { | 
|---|
| [4fbca9c] | 75 | knownTokens["ATOM"] = PdbKey::Atom; | 
|---|
| [16462f] | 76 | knownTokens["HETATM"] = PdbKey::Atom; | 
|---|
| [4fbca9c] | 77 | knownTokens["TER"] = PdbKey::Filler; | 
|---|
| [9dba5f] | 78 | knownTokens["END"] = PdbKey::EndOfTimestep; | 
|---|
| [4fbca9c] | 79 | knownTokens["CONECT"] = PdbKey::Connect; | 
|---|
|  | 80 | knownTokens["REMARK"] = PdbKey::Remark; | 
|---|
| [9dba5f] | 81 | knownTokens[""] = PdbKey::EndOfTimestep; | 
|---|
| [16462f] | 82 |  | 
|---|
|  | 83 | // argh, why can't just PdbKey::X+(size_t)i | 
|---|
|  | 84 | PositionEnumMap[0] = PdbKey::X; | 
|---|
|  | 85 | PositionEnumMap[1] = PdbKey::Y; | 
|---|
|  | 86 | PositionEnumMap[2] = PdbKey::Z; | 
|---|
| [3ae731] | 87 | } | 
|---|
|  | 88 |  | 
|---|
|  | 89 | /** | 
|---|
|  | 90 | * Destructor. | 
|---|
|  | 91 | */ | 
|---|
| [765f16] | 92 | FormatParser< pdb >::~FormatParser() | 
|---|
|  | 93 | { | 
|---|
| [873037] | 94 | PdbAtomInfoContainer::clearknownDataKeys(); | 
|---|
| [3ae731] | 95 | additionalAtomData.clear(); | 
|---|
| [4fbca9c] | 96 | } | 
|---|
|  | 97 |  | 
|---|
|  | 98 |  | 
|---|
|  | 99 | /** Parses the initial word of the given \a line and returns the token type. | 
|---|
|  | 100 | * | 
|---|
|  | 101 | * @param line line to scan | 
|---|
|  | 102 | * @return token type | 
|---|
|  | 103 | */ | 
|---|
| [955b91] | 104 | enum PdbKey::KnownTokens FormatParser< pdb >::getToken(std::string &line) | 
|---|
| [4fbca9c] | 105 | { | 
|---|
|  | 106 | // look for first space | 
|---|
| [3dfd9c] | 107 | std::string token = line.substr(0,6); | 
|---|
|  | 108 | const size_t space_location = token.find(' '); | 
|---|
|  | 109 | const size_t tab_location = token.find('\t'); | 
|---|
| [4fbca9c] | 110 | size_t location = space_location < tab_location ? space_location : tab_location; | 
|---|
|  | 111 | if (location != string::npos) { | 
|---|
| [47d041] | 112 | //LOG(1, "Found space at position " << space_location); | 
|---|
| [3dfd9c] | 113 | token = token.substr(0,space_location); | 
|---|
| [4fbca9c] | 114 | } | 
|---|
|  | 115 |  | 
|---|
| [47d041] | 116 | //LOG(1, "Token is " << token); | 
|---|
| [4fbca9c] | 117 | if (knownTokens.count(token) == 0) | 
|---|
|  | 118 | return PdbKey::NoToken; | 
|---|
|  | 119 | else | 
|---|
|  | 120 | return knownTokens[token]; | 
|---|
|  | 121 |  | 
|---|
|  | 122 | return PdbKey::NoToken; | 
|---|
| [3ae731] | 123 | } | 
|---|
|  | 124 |  | 
|---|
|  | 125 | /** | 
|---|
| [4fbca9c] | 126 | * Loads atoms from a PDB-formatted file. | 
|---|
| [3ae731] | 127 | * | 
|---|
| [4fbca9c] | 128 | * \param PDB file | 
|---|
| [3ae731] | 129 | */ | 
|---|
| [765f16] | 130 | void FormatParser< pdb >::load(istream* file) { | 
|---|
| [4fbca9c] | 131 | string line; | 
|---|
|  | 132 | size_t linecount  = 0; | 
|---|
|  | 133 | enum PdbKey::KnownTokens token; | 
|---|
|  | 134 |  | 
|---|
| [c0e28c] | 135 | // reset id maps for this file (to correctly parse CONECT entries) | 
|---|
|  | 136 | resetIdAssociations(); | 
|---|
| [16462f] | 137 |  | 
|---|
| [9dba5f] | 138 | bool NotEndOfFile = true; | 
|---|
| [4fbca9c] | 139 | molecule *newmol = World::getInstance().createMolecule(); | 
|---|
|  | 140 | newmol->ActiveFlag = true; | 
|---|
| [b0a2e3] | 141 | unsigned int step = 0; | 
|---|
| [4fbca9c] | 142 | // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include | 
|---|
|  | 143 | World::getInstance().getMolecules()->insert(newmol); | 
|---|
|  | 144 | while (NotEndOfFile) { | 
|---|
| [9dba5f] | 145 | bool NotEndOfTimestep = true; | 
|---|
| [b0a2e3] | 146 | while (NotEndOfTimestep && NotEndOfFile) { | 
|---|
| [9dba5f] | 147 | std::getline(*file, line, '\n'); | 
|---|
| [b0a2e3] | 148 | if (!line.empty()) { | 
|---|
|  | 149 | // extract first token | 
|---|
|  | 150 | token = getToken(line); | 
|---|
|  | 151 | switch (token) { | 
|---|
|  | 152 | case PdbKey::Atom: | 
|---|
|  | 153 | LOG(3,"INFO: Parsing ATOM entry for time step " << step << "."); | 
|---|
|  | 154 | readAtomDataLine(step, line, newmol); | 
|---|
|  | 155 | break; | 
|---|
|  | 156 | case PdbKey::Remark: | 
|---|
|  | 157 | LOG(3,"INFO: Parsing REM entry for time step " << step << "."); | 
|---|
|  | 158 | break; | 
|---|
|  | 159 | case PdbKey::Connect: | 
|---|
|  | 160 | LOG(3,"INFO: Parsing CONECT entry for time step " << step << "."); | 
|---|
|  | 161 | readNeighbors(step, line); | 
|---|
|  | 162 | break; | 
|---|
|  | 163 | case PdbKey::Filler: | 
|---|
|  | 164 | LOG(3,"INFO: Stumbled upon Filler entry for time step " << step << "."); | 
|---|
|  | 165 | break; | 
|---|
|  | 166 | case PdbKey::EndOfTimestep: | 
|---|
| [44f53e] | 167 | LOG(1,"INFO: Parsing END entry or empty line for time step " << step << "."); | 
|---|
| [b0a2e3] | 168 | NotEndOfTimestep = false; | 
|---|
|  | 169 | break; | 
|---|
|  | 170 | default: | 
|---|
|  | 171 | // TODO: put a throw here | 
|---|
| [47d041] | 172 | ELOG(2, "Unknown token: '" << line << "' for time step " << step << "."); | 
|---|
| [765f16] | 173 | //ASSERT(0, "FormatParser< pdb >::load() - Unknown token in line "+toString(linecount)+": "+line+"."); | 
|---|
| [b0a2e3] | 174 | break; | 
|---|
|  | 175 | } | 
|---|
| [9dba5f] | 176 | } | 
|---|
|  | 177 | NotEndOfFile = NotEndOfFile && (file->good()); | 
|---|
|  | 178 | linecount++; | 
|---|
| [4fbca9c] | 179 | } | 
|---|
| [b0a2e3] | 180 | ++step; | 
|---|
|  | 181 | } | 
|---|
| [3dfd9c] | 182 | LOG(4, "INFO: Listing all newly parsed atoms."); | 
|---|
|  | 183 | BOOST_FOREACH(atom *_atom, *newmol) | 
|---|
| [48801a] | 184 | LOG(4, "INFO: Atom " << _atom->getName() << " " << *dynamic_cast<AtomInfo *>(_atom) << "."); | 
|---|
| [4afa46] | 185 |  | 
|---|
|  | 186 | // refresh atom::nr and atom::name | 
|---|
|  | 187 | newmol->getAtomCount(); | 
|---|
| [3ae731] | 188 | } | 
|---|
|  | 189 |  | 
|---|
|  | 190 | /** | 
|---|
| [73916f] | 191 | * Saves the \a atoms into as a PDB file. | 
|---|
| [3ae731] | 192 | * | 
|---|
|  | 193 | * \param file where to save the state | 
|---|
| [73916f] | 194 | * \param atoms atoms to store | 
|---|
| [3ae731] | 195 | */ | 
|---|
| [765f16] | 196 | void FormatParser< pdb >::save(ostream* file, const std::vector<atom *> &AtomList) | 
|---|
| [73916f] | 197 | { | 
|---|
| [830b3e] | 198 | LOG(2, "DEBUG: Saving changes to pdb."); | 
|---|
| [9dba5f] | 199 |  | 
|---|
|  | 200 | // check for maximum number of time steps | 
|---|
|  | 201 | size_t max_timesteps = 0; | 
|---|
|  | 202 | BOOST_FOREACH(atom *_atom, World::getInstance().getAllAtoms()) { | 
|---|
| [48801a] | 203 | LOG(4, "INFO: Atom " << _atom->getName() << " " << *dynamic_cast<AtomInfo *>(_atom) << "."); | 
|---|
| [9dba5f] | 204 | if (_atom->getTrajectorySize() > max_timesteps) | 
|---|
|  | 205 | max_timesteps = _atom->getTrajectorySize(); | 
|---|
| [bb6193] | 206 | } | 
|---|
| [9dba5f] | 207 | LOG(2,"INFO: Found a maximum of " << max_timesteps << " time steps to store."); | 
|---|
| [3ae731] | 208 |  | 
|---|
| [9dba5f] | 209 | // re-distribute serials | 
|---|
| [c0e28c] | 210 | resetIdAssociations(); | 
|---|
| [9dba5f] | 211 | // (new atoms might have been added) | 
|---|
|  | 212 | int AtomNo = 1; // serial number starts at 1 in pdb | 
|---|
|  | 213 | for (vector<atom *>::const_iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) { | 
|---|
|  | 214 | PdbAtomInfoContainer &atomInfo = getadditionalAtomData(*atomIt); | 
|---|
| [c0e28c] | 215 | associateLocaltoGlobalId(AtomNo, (*atomIt)->getId()); | 
|---|
| [9dba5f] | 216 | atomInfo.set(PdbKey::serial, toString(AtomNo)); | 
|---|
| [c0e28c] | 217 | ++AtomNo; | 
|---|
| [9dba5f] | 218 | } | 
|---|
|  | 219 |  | 
|---|
| [5c5472] | 220 | // store all time steps (always do first step) | 
|---|
|  | 221 | for (size_t step = 0; (step == 0) || (step < max_timesteps); ++step) { | 
|---|
| [9dba5f] | 222 | { | 
|---|
|  | 223 | // add initial remark | 
|---|
|  | 224 | *file << "REMARK created by molecuilder on "; | 
|---|
|  | 225 | time_t now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time' | 
|---|
|  | 226 | // ctime ends in \n\0, we have to cut away the newline | 
|---|
|  | 227 | std::string time(ctime(&now)); | 
|---|
|  | 228 | size_t pos = time.find('\n'); | 
|---|
|  | 229 | if (pos != 0) | 
|---|
|  | 230 | *file << time.substr(0,pos); | 
|---|
|  | 231 | else | 
|---|
|  | 232 | *file << time; | 
|---|
|  | 233 | *file << ", time step " << step; | 
|---|
|  | 234 | *file << endl; | 
|---|
| [16462f] | 235 | } | 
|---|
| [9dba5f] | 236 |  | 
|---|
|  | 237 | { | 
|---|
|  | 238 | std::map<size_t,size_t> MolIdMap; | 
|---|
|  | 239 | size_t MolNo = 1;  // residue number starts at 1 in pdb | 
|---|
|  | 240 | for (vector<atom *>::const_iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) { | 
|---|
|  | 241 | const molecule *mol = (*atomIt)->getMolecule(); | 
|---|
|  | 242 | if ((mol != NULL) && (MolIdMap.find(mol->getId()) == MolIdMap.end())) { | 
|---|
|  | 243 | MolIdMap[mol->getId()] = MolNo++; | 
|---|
|  | 244 | } | 
|---|
| [bb6193] | 245 | } | 
|---|
| [9dba5f] | 246 | const size_t MaxMol = MolNo; | 
|---|
|  | 247 |  | 
|---|
|  | 248 | // have a count per element and per molecule (0 is for all homeless atoms) | 
|---|
|  | 249 | std::vector<int> **elementNo = new std::vector<int>*[MaxMol]; | 
|---|
|  | 250 | for (size_t i = 0; i < MaxMol; ++i) | 
|---|
|  | 251 | elementNo[i] = new std::vector<int>(MAX_ELEMENTS,1); | 
|---|
|  | 252 | char name[MAXSTRINGSIZE]; | 
|---|
|  | 253 | std::string ResidueName; | 
|---|
|  | 254 |  | 
|---|
|  | 255 | // write ATOMs | 
|---|
|  | 256 | for (vector<atom *>::const_iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) { | 
|---|
|  | 257 | PdbAtomInfoContainer &atomInfo = getadditionalAtomData(*atomIt); | 
|---|
|  | 258 | // gather info about residue | 
|---|
|  | 259 | const molecule *mol = (*atomIt)->getMolecule(); | 
|---|
|  | 260 | if (mol == NULL) { | 
|---|
|  | 261 | MolNo = 0; | 
|---|
|  | 262 | atomInfo.set(PdbKey::resSeq, "0"); | 
|---|
|  | 263 | } else { | 
|---|
|  | 264 | ASSERT(MolIdMap.find(mol->getId()) != MolIdMap.end(), | 
|---|
| [765f16] | 265 | "FormatParser< pdb >::save() - Mol id "+toString(mol->getId())+" not present despite we set it?!"); | 
|---|
| [9dba5f] | 266 | MolNo = MolIdMap[mol->getId()]; | 
|---|
|  | 267 | atomInfo.set(PdbKey::resSeq, toString(MolIdMap[mol->getId()])); | 
|---|
|  | 268 | if (atomInfo.get<std::string>(PdbKey::resName) == "-") | 
|---|
|  | 269 | atomInfo.set(PdbKey::resName, mol->getName().substr(0,3)); | 
|---|
|  | 270 | } | 
|---|
|  | 271 | // get info about atom | 
|---|
|  | 272 | const size_t  Z = (*atomIt)->getType()->getAtomicNumber(); | 
|---|
|  | 273 | if (atomInfo.get<std::string>(PdbKey::name) == "-") {  // if no name set, give it a new name | 
|---|
|  | 274 | sprintf(name, "%2s%02d",(*atomIt)->getType()->getSymbol().c_str(), (*elementNo[MolNo])[Z]); | 
|---|
|  | 275 | (*elementNo[MolNo])[Z] = ((*elementNo[MolNo])[Z]+1) % 100;   // confine to two digits | 
|---|
|  | 276 | atomInfo.set(PdbKey::name, name); | 
|---|
|  | 277 | } | 
|---|
|  | 278 | // set position | 
|---|
|  | 279 | for (size_t i=0; i<NDIM;++i) { | 
|---|
|  | 280 | stringstream position; | 
|---|
| [105b72] | 281 | position << setprecision(7) << (*atomIt)->getPositionAtStep(step).at(i); | 
|---|
| [9dba5f] | 282 | atomInfo.set(PositionEnumMap[i], position.str()); | 
|---|
|  | 283 | } | 
|---|
|  | 284 | // change element and charge if changed | 
|---|
| [8990879] | 285 | if (atomInfo.get<std::string>(PdbKey::element) != (*atomIt)->getType()->getSymbol()) { | 
|---|
|  | 286 | std::string symbol = (*atomIt)->getType()->getSymbol(); | 
|---|
|  | 287 | if ((symbol[1] >= 'a') && (symbol[1] <= 'z')) | 
|---|
|  | 288 | symbol[1] = (symbol[1] - 'a') + 'A'; | 
|---|
|  | 289 | atomInfo.set(PdbKey::element, symbol); | 
|---|
|  | 290 | } | 
|---|
| [9dba5f] | 291 |  | 
|---|
|  | 292 | // finally save the line | 
|---|
|  | 293 | saveLine(file, atomInfo); | 
|---|
| [16462f] | 294 | } | 
|---|
| [9dba5f] | 295 | for (size_t i = 0; i < MaxMol; ++i) | 
|---|
|  | 296 | delete elementNo[i]; | 
|---|
|  | 297 | delete elementNo; | 
|---|
| [3ae731] | 298 |  | 
|---|
| [9dba5f] | 299 | // write CONECTs | 
|---|
|  | 300 | for (vector<atom *>::const_iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) { | 
|---|
|  | 301 | writeNeighbors(file, 4, *atomIt); | 
|---|
|  | 302 | } | 
|---|
| [bb6193] | 303 | } | 
|---|
| [9dba5f] | 304 | // END | 
|---|
|  | 305 | *file << "END" << endl; | 
|---|
| [3ae731] | 306 | } | 
|---|
|  | 307 |  | 
|---|
|  | 308 | } | 
|---|
|  | 309 |  | 
|---|
| [6bc86c] | 310 | /** Add default info, when new atom is added to World. | 
|---|
|  | 311 | * | 
|---|
|  | 312 | * @param id of atom | 
|---|
|  | 313 | */ | 
|---|
| [765f16] | 314 | void FormatParser< pdb >::AtomInserted(atomId_t id) | 
|---|
| [6bc86c] | 315 | { | 
|---|
| [765f16] | 316 | //LOG(3, "FormatParser< pdb >::AtomInserted() - notified of atom " << id << "'s insertion."); | 
|---|
| [6bc86c] | 317 | ASSERT(!isPresentadditionalAtomData(id), | 
|---|
| [765f16] | 318 | "FormatParser< pdb >::AtomInserted() - additionalAtomData already present for newly added atom " | 
|---|
| [6bc86c] | 319 | +toString(id)+"."); | 
|---|
|  | 320 | // don't insert here as this is our check whether we are in the first time step | 
|---|
|  | 321 | //additionalAtomData.insert( std::make_pair(id, defaultAdditionalData) ); | 
|---|
|  | 322 | } | 
|---|
|  | 323 |  | 
|---|
|  | 324 | /** Remove additional AtomData info, when atom has been removed from World. | 
|---|
|  | 325 | * | 
|---|
|  | 326 | * @param id of atom | 
|---|
|  | 327 | */ | 
|---|
| [765f16] | 328 | void FormatParser< pdb >::AtomRemoved(atomId_t id) | 
|---|
| [6bc86c] | 329 | { | 
|---|
| [765f16] | 330 | //LOG(3, "FormatParser< pdb >::AtomRemoved() - notified of atom " << id << "'s removal."); | 
|---|
| [8bf9c6] | 331 | std::map<const atomId_t, PdbAtomInfoContainer>::iterator iter = additionalAtomData.find(id); | 
|---|
| [6bc86c] | 332 | // as we do not insert AtomData on AtomInserted, we cannot be assured of its presence | 
|---|
|  | 333 | //  ASSERT(iter != additionalAtomData.end(), | 
|---|
| [765f16] | 334 | //      "FormatParser< pdb >::AtomRemoved() - additionalAtomData is not present for atom " | 
|---|
| [6bc86c] | 335 | //      +toString(id)+" to remove."); | 
|---|
|  | 336 | if (iter != additionalAtomData.end()) { | 
|---|
|  | 337 | additionalAtomData.erase(iter); | 
|---|
|  | 338 | } | 
|---|
|  | 339 | } | 
|---|
|  | 340 |  | 
|---|
|  | 341 |  | 
|---|
| [9dba5f] | 342 | /** Checks whether there is an entry for the given atom's \a _id. | 
|---|
|  | 343 | * | 
|---|
|  | 344 | * @param _id atom's id we wish to check on | 
|---|
|  | 345 | * @return true - entry present, false - only for atom's father or no entry | 
|---|
|  | 346 | */ | 
|---|
| [8bf9c6] | 347 | bool FormatParser< pdb >::isPresentadditionalAtomData(const atomId_t _id) const | 
|---|
| [9dba5f] | 348 | { | 
|---|
| [8bf9c6] | 349 | std::map<const atomId_t, PdbAtomInfoContainer>::const_iterator iter = additionalAtomData.find(_id); | 
|---|
|  | 350 | return (iter != additionalAtomData.end()); | 
|---|
| [9dba5f] | 351 | } | 
|---|
|  | 352 |  | 
|---|
|  | 353 |  | 
|---|
| [93fd43e] | 354 | /** Either returns reference to present entry or creates new with default values. | 
|---|
|  | 355 | * | 
|---|
|  | 356 | * @param _atom atom whose entry we desire | 
|---|
|  | 357 | * @return | 
|---|
|  | 358 | */ | 
|---|
| [765f16] | 359 | PdbAtomInfoContainer& FormatParser< pdb >::getadditionalAtomData(atom *_atom) | 
|---|
| [93fd43e] | 360 | { | 
|---|
|  | 361 | if (additionalAtomData.find(_atom->getId()) != additionalAtomData.end()) { | 
|---|
|  | 362 | } else if (additionalAtomData.find(_atom->father->getId()) != additionalAtomData.end()) { | 
|---|
|  | 363 | // use info from direct father | 
|---|
|  | 364 | additionalAtomData[_atom->getId()] = additionalAtomData[_atom->father->getId()]; | 
|---|
|  | 365 | } else if (additionalAtomData.find(_atom->GetTrueFather()->getId()) != additionalAtomData.end()) { | 
|---|
|  | 366 | // use info from topmost father | 
|---|
|  | 367 | additionalAtomData[_atom->getId()] = additionalAtomData[_atom->GetTrueFather()->getId()]; | 
|---|
|  | 368 | } else { | 
|---|
|  | 369 | // create new entry use default values if nothing else is known | 
|---|
|  | 370 | additionalAtomData[_atom->getId()] = defaultAdditionalData; | 
|---|
|  | 371 | } | 
|---|
|  | 372 | return additionalAtomData[_atom->getId()]; | 
|---|
|  | 373 | } | 
|---|
|  | 374 |  | 
|---|
| [105b72] | 375 | /** Tiny helper function to print a float with a most 8 digits. | 
|---|
|  | 376 | * | 
|---|
|  | 377 | * A few examples best give the picture: | 
|---|
|  | 378 | * 1234.678 | 
|---|
|  | 379 | *    1.234 | 
|---|
|  | 380 | *    0.001 | 
|---|
|  | 381 | *    0.100 | 
|---|
|  | 382 | * 1234567. | 
|---|
|  | 383 | * 123456.7 | 
|---|
|  | 384 | * -1234.56 | 
|---|
|  | 385 | * | 
|---|
|  | 386 | * \param value | 
|---|
|  | 387 | * \return string representation | 
|---|
|  | 388 | */ | 
|---|
|  | 389 | const std::string FormatParser< pdb >::printCoordinate( | 
|---|
|  | 390 | const double value) | 
|---|
|  | 391 | { | 
|---|
|  | 392 | size_t meaningful_bits=7; // one for decimal dot | 
|---|
|  | 393 | if (value < 0) //one for the minus sign | 
|---|
|  | 394 | --meaningful_bits; | 
|---|
|  | 395 | // count digits before dot (without minus and round towards zero!) | 
|---|
|  | 396 | const int full = floor(fabs(value)); | 
|---|
|  | 397 | size_t bits_before_dot = 1; | 
|---|
|  | 398 | { | 
|---|
|  | 399 | int tmp = full; | 
|---|
|  | 400 | for (;bits_before_dot < meaningful_bits;++bits_before_dot) { | 
|---|
|  | 401 | // even if value is 0...somethingish, we still must start at one digit | 
|---|
|  | 402 | tmp = tmp/10; | 
|---|
|  | 403 | if (tmp == 0) | 
|---|
|  | 404 | break; | 
|---|
|  | 405 | } | 
|---|
|  | 406 | } | 
|---|
|  | 407 | // this fixes bits available after dot | 
|---|
|  | 408 | const size_t bits_after_dot = std::min((int)meaningful_bits - (int)bits_before_dot, 3); | 
|---|
|  | 409 | stringstream position; | 
|---|
|  | 410 | if (bits_after_dot > 0) { | 
|---|
|  | 411 | if (value < 0) | 
|---|
|  | 412 | position << "-"; | 
|---|
|  | 413 | const int remainder = round((abs(value)-full)*pow(10,bits_after_dot)); | 
|---|
|  | 414 | position << full << "." << setfill('0') << setw(bits_after_dot) << remainder; | 
|---|
|  | 415 | if (bits_after_dot == 2) | 
|---|
|  | 416 | ELOG(2, "PdbParser is writing coordinates with just a two decimal places."); | 
|---|
|  | 417 | if (bits_after_dot == 1) | 
|---|
|  | 418 | ELOG(1, "PdbParser is writing coordinates with just a single decimal places."); | 
|---|
|  | 419 | } else { | 
|---|
|  | 420 | ELOG(0, "PdbParser is writing coordinates without any decimal places."); | 
|---|
|  | 421 | position << full << "."; | 
|---|
|  | 422 | } | 
|---|
|  | 423 | return position.str(); | 
|---|
|  | 424 | } | 
|---|
|  | 425 |  | 
|---|
| [3ae731] | 426 | /** | 
|---|
| [4fbca9c] | 427 | * Writes one line of PDB-formatted data to the provided stream. | 
|---|
| [3ae731] | 428 | * | 
|---|
|  | 429 | * \param stream where to write the line to | 
|---|
| [bb6193] | 430 | * \param *currentAtom the atom of which information should be written | 
|---|
|  | 431 | * \param AtomNo serial number of atom | 
|---|
| [16462f] | 432 | * \param *name name of atom, i.e. H01 | 
|---|
|  | 433 | * \param ResidueName Name of molecule | 
|---|
| [bb6193] | 434 | * \param ResidueNo number of residue | 
|---|
| [3ae731] | 435 | */ | 
|---|
| [765f16] | 436 | void FormatParser< pdb >::saveLine( | 
|---|
| [16462f] | 437 | ostream* file, | 
|---|
|  | 438 | const PdbAtomInfoContainer &atomInfo) | 
|---|
|  | 439 | { | 
|---|
|  | 440 | *file << setfill(' ') << left << setw(6) | 
|---|
|  | 441 | << atomInfo.get<std::string>(PdbKey::token); | 
|---|
|  | 442 | *file << setfill(' ') << right << setw(5) | 
|---|
| [1f5e97] | 443 | << (atomInfo.get<int>(PdbKey::serial) % 100000); /* atom serial number */ | 
|---|
| [16462f] | 444 | *file << " "; /* char 12 is empty */ | 
|---|
|  | 445 | *file << setfill(' ') << left << setw(4) | 
|---|
|  | 446 | << atomInfo.get<std::string>(PdbKey::name);  /* atom name */ | 
|---|
|  | 447 | *file << setfill(' ') << left << setw(1) | 
|---|
|  | 448 | << atomInfo.get<std::string>(PdbKey::altLoc); /* alternate location/conformation */ | 
|---|
|  | 449 | *file << setfill(' ') << left << setw(3) | 
|---|
|  | 450 | << atomInfo.get<std::string>(PdbKey::resName);  /* residue name */ | 
|---|
|  | 451 | *file << " "; /* char 21 is empty */ | 
|---|
|  | 452 | *file << setfill(' ') << left << setw(1) | 
|---|
|  | 453 | << atomInfo.get<std::string>(PdbKey::chainID); /* chain identifier */ | 
|---|
|  | 454 | *file << setfill(' ') << left << setw(4) | 
|---|
| [1f5e97] | 455 | << (atomInfo.get<int>(PdbKey::resSeq) % 10000); /* residue sequence number */ | 
|---|
| [16462f] | 456 | *file << setfill(' ') << left << setw(1) | 
|---|
|  | 457 | << atomInfo.get<std::string>(PdbKey::iCode); /* iCode */ | 
|---|
|  | 458 | *file << "   "; /* char 28-30 are empty */ | 
|---|
|  | 459 | // have the following operate on stringstreams such that format specifiers | 
|---|
|  | 460 | // only act on these | 
|---|
|  | 461 | for (size_t i=0;i<NDIM;++i) { | 
|---|
| [105b72] | 462 | *file << setfill(' ') << right << setw(8) | 
|---|
|  | 463 | << printCoordinate(atomInfo.get<double>(PositionEnumMap[i])); | 
|---|
| [16462f] | 464 | } | 
|---|
|  | 465 | { | 
|---|
|  | 466 | stringstream occupancy; | 
|---|
|  | 467 | occupancy << fixed << setprecision(2) << showpoint | 
|---|
|  | 468 | << atomInfo.get<double>(PdbKey::occupancy); /* occupancy */ | 
|---|
|  | 469 | *file << setfill(' ') << right << setw(6) << occupancy.str(); | 
|---|
| [3ae731] | 470 | } | 
|---|
| [16462f] | 471 | { | 
|---|
|  | 472 | stringstream tempFactor; | 
|---|
|  | 473 | tempFactor << fixed << setprecision(2) << showpoint | 
|---|
|  | 474 | << atomInfo.get<double>(PdbKey::tempFactor); /* temperature factor */ | 
|---|
|  | 475 | *file << setfill(' ') << right << setw(6) << tempFactor.str(); | 
|---|
|  | 476 | } | 
|---|
|  | 477 | *file << "          "; /* char 68-76 are empty */ | 
|---|
|  | 478 | *file << setfill(' ') << right << setw(2) << atomInfo.get<std::string>(PdbKey::element); /* element */ | 
|---|
|  | 479 | *file << setfill(' ') << right << setw(2) << atomInfo.get<int>(PdbKey::charge); /* charge */ | 
|---|
| [3ae731] | 480 |  | 
|---|
|  | 481 | *file << endl; | 
|---|
|  | 482 | } | 
|---|
|  | 483 |  | 
|---|
|  | 484 | /** | 
|---|
|  | 485 | * Writes the neighbor information of one atom to the provided stream. | 
|---|
|  | 486 | * | 
|---|
| [9d83b6] | 487 | * Note that ListOfBonds of WorldTime::CurrentTime is used. | 
|---|
|  | 488 | * | 
|---|
| [473237] | 489 | * Also, we fill up the CONECT line to extend over 80 chars. | 
|---|
|  | 490 | * | 
|---|
| [bb6193] | 491 | * \param *file  where to write neighbor information to | 
|---|
|  | 492 | * \param MaxnumberOfNeighbors of neighbors | 
|---|
|  | 493 | * \param *currentAtom to the atom of which to take the neighbor information | 
|---|
| [3ae731] | 494 | */ | 
|---|
| [765f16] | 495 | void FormatParser< pdb >::writeNeighbors(ostream* file, int MaxnumberOfNeighbors, atom* currentAtom) { | 
|---|
| [4c1230] | 496 | int MaxNo = MaxnumberOfNeighbors; | 
|---|
| [473237] | 497 | int charsleft = 80; | 
|---|
| [9d83b6] | 498 | const BondList & ListOfBonds = currentAtom->getListOfBonds(); | 
|---|
|  | 499 | if (!ListOfBonds.empty()) { | 
|---|
|  | 500 | for(BondList::const_iterator currentBond = ListOfBonds.begin(); currentBond != ListOfBonds.end(); ++currentBond) { | 
|---|
| [4c1230] | 501 | if (MaxNo >= MaxnumberOfNeighbors) { | 
|---|
|  | 502 | *file << "CONECT"; | 
|---|
| [c0e28c] | 503 | *file << setw(5) << getLocalId(currentAtom->getId()); | 
|---|
| [473237] | 504 | charsleft = 80-6-5; | 
|---|
| [4c1230] | 505 | MaxNo = 0; | 
|---|
| [bb6193] | 506 | } | 
|---|
| [c0e28c] | 507 | *file << setw(5) << getLocalId((*currentBond)->GetOtherAtom(currentAtom)->getId()); | 
|---|
| [473237] | 508 | charsleft -= 5; | 
|---|
| [bb6193] | 509 | MaxNo++; | 
|---|
| [473237] | 510 | if (MaxNo == MaxnumberOfNeighbors) { | 
|---|
|  | 511 | for (;charsleft > 0; charsleft--) | 
|---|
|  | 512 | *file << ' '; | 
|---|
| [4c1230] | 513 | *file << "\n"; | 
|---|
| [473237] | 514 | } | 
|---|
| [3ae731] | 515 | } | 
|---|
| [473237] | 516 | if (MaxNo != MaxnumberOfNeighbors) { | 
|---|
|  | 517 | for (;charsleft > 0; charsleft--) | 
|---|
|  | 518 | *file << ' '; | 
|---|
| [4c1230] | 519 | *file << "\n"; | 
|---|
| [473237] | 520 | } | 
|---|
| [3ae731] | 521 | } | 
|---|
|  | 522 | } | 
|---|
|  | 523 |  | 
|---|
| [9dba5f] | 524 | /** Either returns present atom with given id or a newly created one. | 
|---|
|  | 525 | * | 
|---|
|  | 526 | * @param id_string | 
|---|
|  | 527 | * @return | 
|---|
|  | 528 | */ | 
|---|
| [c0e28c] | 529 | atom* FormatParser< pdb >::getAtomToParse(std::string id_string) | 
|---|
| [9dba5f] | 530 | { | 
|---|
|  | 531 | // get the local ID | 
|---|
|  | 532 | ConvertTo<int> toInt; | 
|---|
| [c0e28c] | 533 | const unsigned int AtomID_local = toInt(id_string); | 
|---|
|  | 534 | LOG(4, "INFO: Local id is "+toString(AtomID_local)+"."); | 
|---|
| [9dba5f] | 535 | // get the atomic ID if present | 
|---|
|  | 536 | atom* newAtom = NULL; | 
|---|
| [c0e28c] | 537 | if (getGlobalId(AtomID_local) != -1) { | 
|---|
|  | 538 | const unsigned int AtomID_global = getGlobalId(AtomID_local); | 
|---|
|  | 539 | LOG(4, "INFO: Global id present as " << AtomID_global << "."); | 
|---|
| [9dba5f] | 540 | // check if atom exists | 
|---|
| [c0e28c] | 541 | newAtom = World::getInstance().getAtom(AtomById(AtomID_global)); | 
|---|
| [9dba5f] | 542 | LOG(5, "INFO: Listing all present atoms with id."); | 
|---|
|  | 543 | BOOST_FOREACH(atom *_atom, World::getInstance().getAllAtoms()) | 
|---|
|  | 544 | LOG(5, "INFO: " << *_atom << " with id " << _atom->getId()); | 
|---|
|  | 545 | } | 
|---|
|  | 546 | // if not exists, create | 
|---|
|  | 547 | if (newAtom == NULL) { | 
|---|
|  | 548 | newAtom = World::getInstance().createAtom(); | 
|---|
| [8bf9c6] | 549 | //const unsigned int AtomID_global = newAtom->getId(); | 
|---|
| [9dba5f] | 550 | LOG(4, "INFO: No association to global id present, creating atom."); | 
|---|
|  | 551 | } else { | 
|---|
|  | 552 | LOG(4, "INFO: Existing atom found: " << *newAtom << "."); | 
|---|
|  | 553 | } | 
|---|
|  | 554 | return newAtom; | 
|---|
|  | 555 | } | 
|---|
|  | 556 |  | 
|---|
| [5fa2ba] | 557 | /** read a line starting with key ATOM. | 
|---|
|  | 558 | * | 
|---|
|  | 559 | * We check for line's length and parse only up to this value. | 
|---|
|  | 560 | * | 
|---|
|  | 561 | * @param atomInfo container to put information in | 
|---|
|  | 562 | * @param line line containing key ATOM | 
|---|
|  | 563 | */ | 
|---|
| [765f16] | 564 | void FormatParser< pdb >::readPdbAtomInfoContainer(PdbAtomInfoContainer &atomInfo, std::string &line) const | 
|---|
| [9dba5f] | 565 | { | 
|---|
| [5fa2ba] | 566 | const size_t length = line.length(); | 
|---|
|  | 567 | if (length < 80) | 
|---|
| [765f16] | 568 | ELOG(2, "FormatParser< pdb >::readPdbAtomInfoContainer() - pdb is mal-formed, containing less than 80 chars!"); | 
|---|
| [5fa2ba] | 569 | if (length >= 6) { | 
|---|
|  | 570 | LOG(4,"INFO: Parsing token from "+line.substr(0,6)+"."); | 
|---|
|  | 571 | atomInfo.set(PdbKey::token, line.substr(0,6)); | 
|---|
|  | 572 | } | 
|---|
|  | 573 | if (length >= 11) { | 
|---|
|  | 574 | LOG(4,"INFO: Parsing serial from "+line.substr(6,5)+"."); | 
|---|
|  | 575 | atomInfo.set(PdbKey::serial, line.substr(6,5)); | 
|---|
|  | 576 | ASSERT(atomInfo.get<int>(PdbKey::serial) != 0, | 
|---|
| [765f16] | 577 | "FormatParser< pdb >::readPdbAtomInfoContainer() - serial 0 is invalid (filler id for conect entries)."); | 
|---|
| [5fa2ba] | 578 | } | 
|---|
|  | 579 |  | 
|---|
|  | 580 | if (length >= 16) { | 
|---|
|  | 581 | LOG(4,"INFO: Parsing name from "+line.substr(12,4)+"."); | 
|---|
|  | 582 | atomInfo.set(PdbKey::name, line.substr(12,4)); | 
|---|
|  | 583 | } | 
|---|
|  | 584 | if (length >= 17) { | 
|---|
|  | 585 | LOG(4,"INFO: Parsing altLoc from "+line.substr(16,1)+"."); | 
|---|
|  | 586 | atomInfo.set(PdbKey::altLoc, line.substr(16,1)); | 
|---|
|  | 587 | } | 
|---|
|  | 588 | if (length >= 20) { | 
|---|
|  | 589 | LOG(4,"INFO: Parsing resName from "+line.substr(17,3)+"."); | 
|---|
|  | 590 | atomInfo.set(PdbKey::resName, line.substr(17,3)); | 
|---|
|  | 591 | } | 
|---|
|  | 592 | if (length >= 22) { | 
|---|
|  | 593 | LOG(4,"INFO: Parsing chainID from "+line.substr(21,1)+"."); | 
|---|
|  | 594 | atomInfo.set(PdbKey::chainID, line.substr(21,1)); | 
|---|
|  | 595 | } | 
|---|
|  | 596 | if (length >= 26) { | 
|---|
|  | 597 | LOG(4,"INFO: Parsing resSeq from "+line.substr(22,4)+"."); | 
|---|
|  | 598 | atomInfo.set(PdbKey::resSeq, line.substr(22,4)); | 
|---|
|  | 599 | } | 
|---|
|  | 600 | if (length >= 27) { | 
|---|
|  | 601 | LOG(4,"INFO: Parsing iCode from "+line.substr(26,1)+"."); | 
|---|
|  | 602 | atomInfo.set(PdbKey::iCode, line.substr(26,1)); | 
|---|
|  | 603 | } | 
|---|
|  | 604 |  | 
|---|
|  | 605 | if (length >= 60) { | 
|---|
|  | 606 | LOG(4,"INFO: Parsing occupancy from "+line.substr(54,6)+"."); | 
|---|
|  | 607 | atomInfo.set(PdbKey::occupancy, line.substr(54,6)); | 
|---|
|  | 608 | } | 
|---|
|  | 609 | if (length >= 66) { | 
|---|
|  | 610 | LOG(4,"INFO: Parsing tempFactor from "+line.substr(60,6)+"."); | 
|---|
|  | 611 | atomInfo.set(PdbKey::tempFactor, line.substr(60,6)); | 
|---|
|  | 612 | } | 
|---|
|  | 613 | if (length >= 80) { | 
|---|
|  | 614 | LOG(4,"INFO: Parsing charge from "+line.substr(78,2)+"."); | 
|---|
|  | 615 | atomInfo.set(PdbKey::charge, line.substr(78,2)); | 
|---|
|  | 616 | } | 
|---|
|  | 617 | if (length >= 78) { | 
|---|
|  | 618 | LOG(4,"INFO: Parsing element from "+line.substr(76,2)+"."); | 
|---|
|  | 619 | atomInfo.set(PdbKey::element, line.substr(76,2)); | 
|---|
|  | 620 | } else { | 
|---|
|  | 621 | LOG(4,"INFO: Trying to parse alternative element from name "+line.substr(12,4)+"."); | 
|---|
|  | 622 | atomInfo.set(PdbKey::element, line.substr(12,4)); | 
|---|
|  | 623 | } | 
|---|
| [9dba5f] | 624 | } | 
|---|
|  | 625 |  | 
|---|
| [4fbca9c] | 626 | /** Parse an ATOM line from a PDB file. | 
|---|
|  | 627 | * | 
|---|
|  | 628 | * Reads one data line of a pdstatus file and interprets it according to the | 
|---|
|  | 629 | * specifications of the PDB 3.2 format: http://www.wwpdb.org/docs.html | 
|---|
|  | 630 | * | 
|---|
|  | 631 | *  A new atom is created and filled with available information, non- | 
|---|
|  | 632 | *  standard information is placed in additionalAtomData at the atom's id. | 
|---|
| [3ae731] | 633 | * | 
|---|
| [b0a2e3] | 634 | * \param _step time step to use | 
|---|
| [3ae731] | 635 | * \param line to parse as an atom | 
|---|
| [4fbca9c] | 636 | * \param newmol molecule to add parsed atoms to | 
|---|
| [3ae731] | 637 | */ | 
|---|
| [765f16] | 638 | void FormatParser< pdb >::readAtomDataLine(const unsigned int _step, std::string &line, molecule *newmol = NULL) { | 
|---|
| [4fbca9c] | 639 | vector<string>::iterator it; | 
|---|
| [9dba5f] | 640 |  | 
|---|
|  | 641 | atom* newAtom = getAtomToParse(line.substr(6,5)); | 
|---|
|  | 642 | LOG(3,"INFO: Parsing END entry or empty line."); | 
|---|
|  | 643 | bool FirstTimestep = isPresentadditionalAtomData(newAtom->getId()) ? false : true; | 
|---|
| [b0a2e3] | 644 | ASSERT((FirstTimestep && (_step == 0)) || (!FirstTimestep && (_step !=0)), | 
|---|
| [765f16] | 645 | "FormatParser< pdb >::readAtomDataLine() - additionalAtomData present though atom is newly parsed."); | 
|---|
| [9dba5f] | 646 | if (FirstTimestep) { | 
|---|
| [c0e28c] | 647 | LOG(3,"INFO: Parsing new atom "+toString(*newAtom)+" "+toString(newAtom->getId())+"."); | 
|---|
| [9dba5f] | 648 | } else { | 
|---|
|  | 649 | LOG(3,"INFO: Parsing present atom "+toString(*newAtom)+"."); | 
|---|
|  | 650 | } | 
|---|
| [93fd43e] | 651 | PdbAtomInfoContainer &atomInfo = getadditionalAtomData(newAtom); | 
|---|
| [9dba5f] | 652 | LOG(4,"INFO: Information in container is "+toString(atomInfo)+"."); | 
|---|
|  | 653 |  | 
|---|
| [4fbca9c] | 654 | string word; | 
|---|
|  | 655 | ConvertTo<size_t> toSize_t; | 
|---|
|  | 656 |  | 
|---|
|  | 657 | // check whether serial exists, if so, assign next available | 
|---|
|  | 658 |  | 
|---|
| [47d041] | 659 | //  LOG(2, "Split line:" | 
|---|
| [4fbca9c] | 660 | //      << line.substr(6,5) << "|" | 
|---|
|  | 661 | //      << line.substr(12,4) << "|" | 
|---|
|  | 662 | //      << line.substr(16,1) << "|" | 
|---|
|  | 663 | //      << line.substr(17,3) << "|" | 
|---|
|  | 664 | //      << line.substr(21,1) << "|" | 
|---|
|  | 665 | //      << line.substr(22,4) << "|" | 
|---|
|  | 666 | //      << line.substr(26,1) << "|" | 
|---|
|  | 667 | //      << line.substr(30,8) << "|" | 
|---|
|  | 668 | //      << line.substr(38,8) << "|" | 
|---|
|  | 669 | //      << line.substr(46,8) << "|" | 
|---|
|  | 670 | //      << line.substr(54,6) << "|" | 
|---|
|  | 671 | //      << line.substr(60,6) << "|" | 
|---|
|  | 672 | //      << line.substr(76,2) << "|" | 
|---|
| [47d041] | 673 | //      << line.substr(78,2)); | 
|---|
| [4fbca9c] | 674 |  | 
|---|
| [9dba5f] | 675 | if (FirstTimestep) { | 
|---|
|  | 676 | // first time step | 
|---|
|  | 677 | // then fill info container | 
|---|
|  | 678 | readPdbAtomInfoContainer(atomInfo, line); | 
|---|
| [c0e28c] | 679 | // associate local with global id | 
|---|
|  | 680 | associateLocaltoGlobalId(toSize_t(atomInfo.get<std::string>(PdbKey::serial)), newAtom->getId()); | 
|---|
| [9dba5f] | 681 | // set position | 
|---|
|  | 682 | Vector tempVector; | 
|---|
|  | 683 | LOG(4,"INFO: Parsing position from (" | 
|---|
|  | 684 | +line.substr(30,8)+"," | 
|---|
|  | 685 | +line.substr(38,8)+"," | 
|---|
|  | 686 | +line.substr(46,8)+")."); | 
|---|
|  | 687 | PdbAtomInfoContainer::ScanKey(tempVector[0], line.substr(30,8)); | 
|---|
|  | 688 | PdbAtomInfoContainer::ScanKey(tempVector[1], line.substr(38,8)); | 
|---|
|  | 689 | PdbAtomInfoContainer::ScanKey(tempVector[2], line.substr(46,8)); | 
|---|
|  | 690 | newAtom->setPosition(tempVector); | 
|---|
|  | 691 | // set element | 
|---|
| [8990879] | 692 | std::string value = atomInfo.get<std::string>(PdbKey::element); | 
|---|
|  | 693 | // make second character lower case if not | 
|---|
|  | 694 | if ((value[1] >= 'A') && (value[1] <= 'Z')) | 
|---|
|  | 695 | value[1] = (value[1] - 'A') + 'a'; | 
|---|
| [9dba5f] | 696 | const element *elem = World::getInstance().getPeriode() | 
|---|
| [8990879] | 697 | ->FindElement(value); | 
|---|
| [9dba5f] | 698 | ASSERT(elem != NULL, | 
|---|
| [765f16] | 699 | "FormatParser< pdb >::readAtomDataLine() - element "+atomInfo.get<std::string>(PdbKey::element)+" is unknown!"); | 
|---|
| [9dba5f] | 700 | newAtom->setType(elem); | 
|---|
|  | 701 |  | 
|---|
|  | 702 | if (newmol != NULL) | 
|---|
|  | 703 | newmol->AddAtom(newAtom); | 
|---|
|  | 704 | } else { | 
|---|
|  | 705 | // not first time step | 
|---|
|  | 706 | // then parse into different container | 
|---|
|  | 707 | PdbAtomInfoContainer consistencyInfo; | 
|---|
|  | 708 | readPdbAtomInfoContainer(consistencyInfo, line); | 
|---|
|  | 709 | // then check additional info for consistency | 
|---|
|  | 710 | ASSERT(atomInfo.get<std::string>(PdbKey::token) == consistencyInfo.get<std::string>(PdbKey::token), | 
|---|
| [765f16] | 711 | "FormatParser< pdb >::readAtomDataLine() - difference in token on multiple time step for atom with id " | 
|---|
| [9dba5f] | 712 | +atomInfo.get<std::string>(PdbKey::serial)+"!"); | 
|---|
|  | 713 | ASSERT(atomInfo.get<std::string>(PdbKey::name) == consistencyInfo.get<std::string>(PdbKey::name), | 
|---|
| [765f16] | 714 | "FormatParser< pdb >::readAtomDataLine() - difference in name on multiple time step for atom with id " | 
|---|
| [9dba5f] | 715 | +atomInfo.get<std::string>(PdbKey::serial)+":" | 
|---|
|  | 716 | +atomInfo.get<std::string>(PdbKey::name)+"!=" | 
|---|
|  | 717 | +consistencyInfo.get<std::string>(PdbKey::name)+"."); | 
|---|
|  | 718 | ASSERT(atomInfo.get<std::string>(PdbKey::altLoc) == consistencyInfo.get<std::string>(PdbKey::altLoc), | 
|---|
| [765f16] | 719 | "FormatParser< pdb >::readAtomDataLine() - difference in altLoc on multiple time step for atom with id " | 
|---|
| [9dba5f] | 720 | +atomInfo.get<std::string>(PdbKey::serial)+"!"); | 
|---|
|  | 721 | ASSERT(atomInfo.get<std::string>(PdbKey::resName) == consistencyInfo.get<std::string>(PdbKey::resName), | 
|---|
| [765f16] | 722 | "FormatParser< pdb >::readAtomDataLine() - difference in resName on multiple time step for atom with id " | 
|---|
| [9dba5f] | 723 | +atomInfo.get<std::string>(PdbKey::serial)+"!"); | 
|---|
|  | 724 | ASSERT(atomInfo.get<std::string>(PdbKey::chainID) == consistencyInfo.get<std::string>(PdbKey::chainID), | 
|---|
| [765f16] | 725 | "FormatParser< pdb >::readAtomDataLine() - difference in chainID on multiple time step for atom with id " | 
|---|
| [9dba5f] | 726 | +atomInfo.get<std::string>(PdbKey::serial)+"!"); | 
|---|
|  | 727 | ASSERT(atomInfo.get<std::string>(PdbKey::resSeq) == consistencyInfo.get<std::string>(PdbKey::resSeq), | 
|---|
| [765f16] | 728 | "FormatParser< pdb >::readAtomDataLine() - difference in resSeq on multiple time step for atom with id " | 
|---|
| [9dba5f] | 729 | +atomInfo.get<std::string>(PdbKey::serial)+"!"); | 
|---|
|  | 730 | ASSERT(atomInfo.get<std::string>(PdbKey::iCode) == consistencyInfo.get<std::string>(PdbKey::iCode), | 
|---|
| [765f16] | 731 | "FormatParser< pdb >::readAtomDataLine() - difference in iCode on multiple time step for atom with id " | 
|---|
| [9dba5f] | 732 | +atomInfo.get<std::string>(PdbKey::serial)+"!"); | 
|---|
|  | 733 | ASSERT(atomInfo.get<std::string>(PdbKey::occupancy) == consistencyInfo.get<std::string>(PdbKey::occupancy), | 
|---|
| [765f16] | 734 | "FormatParser< pdb >::readAtomDataLine() - difference in occupancy on multiple time step for atom with id " | 
|---|
| [9dba5f] | 735 | +atomInfo.get<std::string>(PdbKey::serial)+"!"); | 
|---|
|  | 736 | ASSERT(atomInfo.get<std::string>(PdbKey::tempFactor) == consistencyInfo.get<std::string>(PdbKey::tempFactor), | 
|---|
| [765f16] | 737 | "FormatParser< pdb >::readAtomDataLine() - difference in tempFactor on multiple time step for atom with id " | 
|---|
| [9dba5f] | 738 | +atomInfo.get<std::string>(PdbKey::serial)+"!"); | 
|---|
|  | 739 | ASSERT(atomInfo.get<std::string>(PdbKey::charge) == consistencyInfo.get<std::string>(PdbKey::charge), | 
|---|
| [765f16] | 740 | "FormatParser< pdb >::readAtomDataLine() - difference in charge on multiple time step for atom with id " | 
|---|
| [9dba5f] | 741 | +atomInfo.get<std::string>(PdbKey::serial)+"!"); | 
|---|
|  | 742 | ASSERT(atomInfo.get<std::string>(PdbKey::element) == consistencyInfo.get<std::string>(PdbKey::element), | 
|---|
| [765f16] | 743 | "FormatParser< pdb >::readAtomDataLine() - difference in element on multiple time step for atom with id " | 
|---|
| [9dba5f] | 744 | +atomInfo.get<std::string>(PdbKey::serial)+"!"); | 
|---|
|  | 745 | // and parse in trajectory | 
|---|
|  | 746 | Vector tempVector; | 
|---|
|  | 747 | LOG(4,"INFO: Parsing trajectory position from (" | 
|---|
|  | 748 | +line.substr(30,8)+"," | 
|---|
|  | 749 | +line.substr(38,8)+"," | 
|---|
|  | 750 | +line.substr(46,8)+")."); | 
|---|
|  | 751 | PdbAtomInfoContainer::ScanKey(tempVector[0], line.substr(30,8)); | 
|---|
|  | 752 | PdbAtomInfoContainer::ScanKey(tempVector[1], line.substr(38,8)); | 
|---|
|  | 753 | PdbAtomInfoContainer::ScanKey(tempVector[2], line.substr(46,8)); | 
|---|
| [b0a2e3] | 754 | LOG(4,"INFO: Adding trajectory point to time step "+toString(_step)+"."); | 
|---|
| [9dba5f] | 755 | // and set position at new time step | 
|---|
| [b0a2e3] | 756 | newAtom->setPositionAtStep(_step, tempVector); | 
|---|
| [9dba5f] | 757 | } | 
|---|
|  | 758 |  | 
|---|
| [4fbca9c] | 759 |  | 
|---|
|  | 760 | //  printAtomInfo(newAtom); | 
|---|
| [3ae731] | 761 | } | 
|---|
|  | 762 |  | 
|---|
| [4fbca9c] | 763 | /** Prints all PDB-specific information known about an atom. | 
|---|
| [3ae731] | 764 | * | 
|---|
|  | 765 | */ | 
|---|
| [765f16] | 766 | void FormatParser< pdb >::printAtomInfo(const atom * const newAtom) const | 
|---|
| [4fbca9c] | 767 | { | 
|---|
|  | 768 | const PdbAtomInfoContainer &atomInfo = additionalAtomData.at(newAtom->getId()); // operator[] const does not exist | 
|---|
|  | 769 |  | 
|---|
| [47d041] | 770 | LOG(1, "We know about atom " << newAtom->getId() << ":"); | 
|---|
|  | 771 | LOG(1, "\ttoken is " << atomInfo.get<std::string>(PdbKey::token)); | 
|---|
|  | 772 | LOG(1, "\tserial is " << atomInfo.get<int>(PdbKey::serial)); | 
|---|
|  | 773 | LOG(1, "\tname is " << atomInfo.get<std::string>(PdbKey::name)); | 
|---|
|  | 774 | LOG(1, "\taltLoc is " << atomInfo.get<std::string>(PdbKey::altLoc)); | 
|---|
|  | 775 | LOG(1, "\tresName is " << atomInfo.get<std::string>(PdbKey::resName)); | 
|---|
|  | 776 | LOG(1, "\tchainID is " << atomInfo.get<std::string>(PdbKey::chainID)); | 
|---|
|  | 777 | LOG(1, "\tresSeq is " << atomInfo.get<int>(PdbKey::resSeq)); | 
|---|
|  | 778 | LOG(1, "\tiCode is " << atomInfo.get<std::string>(PdbKey::iCode)); | 
|---|
|  | 779 | LOG(1, "\tX is " << atomInfo.get<double>(PdbKey::X)); | 
|---|
|  | 780 | LOG(1, "\tY is " << atomInfo.get<double>(PdbKey::Y)); | 
|---|
|  | 781 | LOG(1, "\tZ is " << atomInfo.get<double>(PdbKey::Z)); | 
|---|
|  | 782 | LOG(1, "\toccupancy is " << atomInfo.get<double>(PdbKey::occupancy)); | 
|---|
|  | 783 | LOG(1, "\ttempFactor is " << atomInfo.get<double>(PdbKey::tempFactor)); | 
|---|
|  | 784 | LOG(1, "\telement is '" << *(newAtom->getType()) << "'"); | 
|---|
|  | 785 | LOG(1, "\tcharge is " << atomInfo.get<int>(PdbKey::charge)); | 
|---|
| [3ae731] | 786 | } | 
|---|
|  | 787 |  | 
|---|
|  | 788 | /** | 
|---|
| [4fbca9c] | 789 | * Reads neighbor information for one atom from the input. | 
|---|
|  | 790 | * | 
|---|
| [b0a2e3] | 791 | * \param _step time step to use | 
|---|
| [4fbca9c] | 792 | * \param line to parse as an atom | 
|---|
| [3ae731] | 793 | */ | 
|---|
| [765f16] | 794 | void FormatParser< pdb >::readNeighbors(const unsigned int _step, std::string &line) | 
|---|
| [4fbca9c] | 795 | { | 
|---|
|  | 796 | const size_t length = line.length(); | 
|---|
|  | 797 | std::list<size_t> ListOfNeighbors; | 
|---|
|  | 798 | ConvertTo<size_t> toSize_t; | 
|---|
|  | 799 |  | 
|---|
|  | 800 | // obtain neighbours | 
|---|
|  | 801 | // show split line for debugging | 
|---|
|  | 802 | string output; | 
|---|
|  | 803 | ASSERT(length >=16, | 
|---|
| [765f16] | 804 | "FormatParser< pdb >::readNeighbors() - CONECT entry has not enough entries: "+line+"!"); | 
|---|
|  | 805 | //  output = "Split line:|"; | 
|---|
|  | 806 | //  output += line.substr(6,5) + "|"; | 
|---|
| [4fbca9c] | 807 | const size_t id = toSize_t(line.substr(6,5)); | 
|---|
|  | 808 | for (size_t index = 11; index <= 26; index+=5) { | 
|---|
|  | 809 | if (index+5 <= length) { | 
|---|
| [473237] | 810 | output += line.substr(index,5) + "|"; | 
|---|
|  | 811 | // search for digits | 
|---|
|  | 812 | int otherid = -1; | 
|---|
|  | 813 | PdbAtomInfoContainer::ScanKey(otherid, line.substr(index,5)); | 
|---|
| [5fa2ba] | 814 | if (otherid > 0) | 
|---|
|  | 815 | ListOfNeighbors.push_back(otherid); | 
|---|
|  | 816 | else | 
|---|
| [44f53e] | 817 | ELOG(3, "FormatParser< pdb >::readNeighbors() - discarding CONECT entry with id 0."); | 
|---|
| [4fbca9c] | 818 | } else  { | 
|---|
|  | 819 | break; | 
|---|
|  | 820 | } | 
|---|
|  | 821 | } | 
|---|
| [473237] | 822 | LOG(4, output); | 
|---|
| [4fbca9c] | 823 |  | 
|---|
|  | 824 | // add neighbours | 
|---|
| [c0e28c] | 825 | atom *_atom = World::getInstance().getAtom(AtomById(getGlobalId(id))); | 
|---|
| [473237] | 826 | LOG(2, "STATUS: Atom " << _atom->getId() << " gets " << ListOfNeighbors.size() << " more neighbours."); | 
|---|
| [4fbca9c] | 827 | for (std::list<size_t>::const_iterator iter = ListOfNeighbors.begin(); | 
|---|
|  | 828 | iter != ListOfNeighbors.end(); | 
|---|
|  | 829 | ++iter) { | 
|---|
| [c0e28c] | 830 | atom * const _Otheratom = World::getInstance().getAtom(AtomById(getGlobalId(*iter))); | 
|---|
| [473237] | 831 | LOG(3, "INFO: Adding Bond (" << *_atom << "," << *_Otheratom << ")"); | 
|---|
| [b0a2e3] | 832 | _atom->addBond(_step, _Otheratom); | 
|---|
| [4fbca9c] | 833 | } | 
|---|
| [3ae731] | 834 | } | 
|---|
|  | 835 |  | 
|---|
| [765f16] | 836 | bool FormatParser< pdb >::operator==(const FormatParser< pdb >& b) const | 
|---|
| [4fbca9c] | 837 | { | 
|---|
|  | 838 | bool status = true; | 
|---|
|  | 839 | World::AtomComposite atoms = World::getInstance().getAllAtoms(); | 
|---|
|  | 840 | for (World::AtomComposite::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) { | 
|---|
|  | 841 | if ((additionalAtomData.find((*iter)->getId()) != additionalAtomData.end()) | 
|---|
|  | 842 | && (b.additionalAtomData.find((*iter)->getId()) != b.additionalAtomData.end())) { | 
|---|
|  | 843 | const PdbAtomInfoContainer &atomInfo = additionalAtomData.at((*iter)->getId()); | 
|---|
|  | 844 | const PdbAtomInfoContainer &OtheratomInfo = b.additionalAtomData.at((*iter)->getId()); | 
|---|
|  | 845 |  | 
|---|
| [16462f] | 846 | status = status && (atomInfo.get<std::string>(PdbKey::serial) == OtheratomInfo.get<std::string>(PdbKey::serial)); | 
|---|
| [47d041] | 847 | if (!status) ELOG(1, "Mismatch in serials!"); | 
|---|
| [16462f] | 848 | status = status && (atomInfo.get<std::string>(PdbKey::name) == OtheratomInfo.get<std::string>(PdbKey::name)); | 
|---|
| [47d041] | 849 | if (!status) ELOG(1, "Mismatch in names!"); | 
|---|
| [16462f] | 850 | status = status && (atomInfo.get<std::string>(PdbKey::altLoc) == OtheratomInfo.get<std::string>(PdbKey::altLoc)); | 
|---|
| [47d041] | 851 | if (!status) ELOG(1, "Mismatch in altLocs!"); | 
|---|
| [16462f] | 852 | status = status && (atomInfo.get<std::string>(PdbKey::resName) == OtheratomInfo.get<std::string>(PdbKey::resName)); | 
|---|
| [47d041] | 853 | if (!status) ELOG(1, "Mismatch in resNames!"); | 
|---|
| [16462f] | 854 | status = status && (atomInfo.get<std::string>(PdbKey::chainID) == OtheratomInfo.get<std::string>(PdbKey::chainID)); | 
|---|
| [47d041] | 855 | if (!status) ELOG(1, "Mismatch in chainIDs!"); | 
|---|
| [16462f] | 856 | status = status && (atomInfo.get<std::string>(PdbKey::resSeq) == OtheratomInfo.get<std::string>(PdbKey::resSeq)); | 
|---|
| [47d041] | 857 | if (!status) ELOG(1, "Mismatch in resSeqs!"); | 
|---|
| [16462f] | 858 | status = status && (atomInfo.get<std::string>(PdbKey::iCode) == OtheratomInfo.get<std::string>(PdbKey::iCode)); | 
|---|
| [47d041] | 859 | if (!status) ELOG(1, "Mismatch in iCodes!"); | 
|---|
| [16462f] | 860 | status = status && (atomInfo.get<std::string>(PdbKey::occupancy) == OtheratomInfo.get<std::string>(PdbKey::occupancy)); | 
|---|
| [47d041] | 861 | if (!status) ELOG(1, "Mismatch in occupancies!"); | 
|---|
| [16462f] | 862 | status = status && (atomInfo.get<std::string>(PdbKey::tempFactor) == OtheratomInfo.get<std::string>(PdbKey::tempFactor)); | 
|---|
| [47d041] | 863 | if (!status) ELOG(1, "Mismatch in tempFactors!"); | 
|---|
| [16462f] | 864 | status = status && (atomInfo.get<std::string>(PdbKey::charge) == OtheratomInfo.get<std::string>(PdbKey::charge)); | 
|---|
| [47d041] | 865 | if (!status) ELOG(1, "Mismatch in charges!"); | 
|---|
| [4fbca9c] | 866 | } | 
|---|
| [3ae731] | 867 | } | 
|---|
|  | 868 |  | 
|---|
| [4fbca9c] | 869 | return status; | 
|---|
| [3ae731] | 870 | } | 
|---|
|  | 871 |  | 
|---|