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