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