| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * PdbParser.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Aug 17, 2010
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "Helpers/Assert.hpp"
 | 
|---|
| 23 | #include "Helpers/Log.hpp"
 | 
|---|
| 24 | #include "Helpers/toString.hpp"
 | 
|---|
| 25 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 26 | #include "World.hpp"
 | 
|---|
| 27 | #include "atom.hpp"
 | 
|---|
| 28 | #include "bond.hpp"
 | 
|---|
| 29 | #include "element.hpp"
 | 
|---|
| 30 | #include "molecule.hpp"
 | 
|---|
| 31 | #include "periodentafel.hpp"
 | 
|---|
| 32 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| 33 | #include "Parser/PdbParser.hpp"
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include <map>
 | 
|---|
| 36 | #include <vector>
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include <iostream>
 | 
|---|
| 39 | #include <iomanip>
 | 
|---|
| 40 | 
 | 
|---|
| 41 | using namespace std;
 | 
|---|
| 42 | 
 | 
|---|
| 43 | /**
 | 
|---|
| 44 |  * Constructor.
 | 
|---|
| 45 |  */
 | 
|---|
| 46 | PdbParser::PdbParser() {
 | 
|---|
| 47 |   knownTokens["ATOM"] = PdbKey::Atom;
 | 
|---|
| 48 |   knownTokens["HETATM"] = PdbKey::Atom;
 | 
|---|
| 49 |   knownTokens["TER"] = PdbKey::Filler;
 | 
|---|
| 50 |   knownTokens["END"] = PdbKey::EndOfFile;
 | 
|---|
| 51 |   knownTokens["CONECT"] = PdbKey::Connect;
 | 
|---|
| 52 |   knownTokens["REMARK"] = PdbKey::Remark;
 | 
|---|
| 53 |   knownTokens[""] = PdbKey::EndOfFile;
 | 
|---|
| 54 | 
 | 
|---|
| 55 |   // argh, why can't just PdbKey::X+(size_t)i
 | 
|---|
| 56 |   PositionEnumMap[0] = PdbKey::X;
 | 
|---|
| 57 |   PositionEnumMap[1] = PdbKey::Y;
 | 
|---|
| 58 |   PositionEnumMap[2] = PdbKey::Z;
 | 
|---|
| 59 | }
 | 
|---|
| 60 | 
 | 
|---|
| 61 | /**
 | 
|---|
| 62 |  * Destructor.
 | 
|---|
| 63 |  */
 | 
|---|
| 64 | PdbParser::~PdbParser() {
 | 
|---|
| 65 |   additionalAtomData.clear();
 | 
|---|
| 66 |   atomIdMap.clear();
 | 
|---|
| 67 | }
 | 
|---|
| 68 | 
 | 
|---|
| 69 | 
 | 
|---|
| 70 | /** Parses the initial word of the given \a line and returns the token type.
 | 
|---|
| 71 |  *
 | 
|---|
| 72 |  * @param line line to scan
 | 
|---|
| 73 |  * @return token type
 | 
|---|
| 74 |  */
 | 
|---|
| 75 | enum PdbKey::KnownTokens PdbParser::getToken(string &line)
 | 
|---|
| 76 | {
 | 
|---|
| 77 |   // look for first space
 | 
|---|
| 78 |   const size_t space_location = line.find(' ');
 | 
|---|
| 79 |   const size_t tab_location = line.find('\t');
 | 
|---|
| 80 |   size_t location = space_location < tab_location ? space_location : tab_location;
 | 
|---|
| 81 |   string token;
 | 
|---|
| 82 |   if (location != string::npos) {
 | 
|---|
| 83 |     //DoLog(1) && (Log() << Verbose(1) << "Found space at position " << space_location << std::endl);
 | 
|---|
| 84 |     token = line.substr(0,space_location);
 | 
|---|
| 85 |   } else {
 | 
|---|
| 86 |     token = line;
 | 
|---|
| 87 |   }
 | 
|---|
| 88 | 
 | 
|---|
| 89 |   //DoLog(1) && (Log() << Verbose(1) << "Token is " << token << std::endl);
 | 
|---|
| 90 |   if (knownTokens.count(token) == 0)
 | 
|---|
| 91 |     return PdbKey::NoToken;
 | 
|---|
| 92 |   else
 | 
|---|
| 93 |     return knownTokens[token];
 | 
|---|
| 94 | 
 | 
|---|
| 95 |   return PdbKey::NoToken;
 | 
|---|
| 96 | }
 | 
|---|
| 97 | 
 | 
|---|
| 98 | /**
 | 
|---|
| 99 |  * Loads atoms from a PDB-formatted file.
 | 
|---|
| 100 |  *
 | 
|---|
| 101 |  * \param PDB file
 | 
|---|
| 102 |  */
 | 
|---|
| 103 | void PdbParser::load(istream* file) {
 | 
|---|
| 104 |   string line;
 | 
|---|
| 105 |   size_t linecount  = 0;
 | 
|---|
| 106 |   enum PdbKey::KnownTokens token;
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   // reset atomIdMap for this file (to correctly parse CONECT entries)
 | 
|---|
| 109 |   atomIdMap.clear();
 | 
|---|
| 110 | 
 | 
|---|
| 111 |   molecule *newmol = World::getInstance().createMolecule();
 | 
|---|
| 112 |   newmol->ActiveFlag = true;
 | 
|---|
| 113 |   bool NotEndOfFile = true;
 | 
|---|
| 114 |   // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include
 | 
|---|
| 115 |   World::getInstance().getMolecules()->insert(newmol);
 | 
|---|
| 116 |   while (NotEndOfFile) {
 | 
|---|
| 117 |     std::getline(*file, line, '\n');
 | 
|---|
| 118 |     // extract first token
 | 
|---|
| 119 |     token = getToken(line);
 | 
|---|
| 120 |     //DoLog(1) && (Log() << Verbose(1) << " Recognized token of type : " << token << std::endl);
 | 
|---|
| 121 |     switch (token) {
 | 
|---|
| 122 |       case PdbKey::Atom:
 | 
|---|
| 123 |         readAtomDataLine(line, newmol);
 | 
|---|
| 124 |         break;
 | 
|---|
| 125 |       case PdbKey::Remark:
 | 
|---|
| 126 |         break;
 | 
|---|
| 127 |       case PdbKey::Connect:
 | 
|---|
| 128 |         readNeighbors(line);
 | 
|---|
| 129 |         break;
 | 
|---|
| 130 |       case PdbKey::Filler:
 | 
|---|
| 131 |         break;
 | 
|---|
| 132 |       case PdbKey::EndOfFile:
 | 
|---|
| 133 |         NotEndOfFile = false;
 | 
|---|
| 134 |         break;
 | 
|---|
| 135 |       default:
 | 
|---|
| 136 |         // TODO: put a throw here
 | 
|---|
| 137 |         DoeLog(2) && (eLog() << Verbose(2) << "Unknown token: '" << line << "'" << std::endl);
 | 
|---|
| 138 |         //ASSERT(0, "PdbParser::load() - Unknown token in line "+toString(linecount)+": "+line+".");
 | 
|---|
| 139 |         break;
 | 
|---|
| 140 |     }
 | 
|---|
| 141 |     NotEndOfFile = NotEndOfFile && (file->good());
 | 
|---|
| 142 |     linecount++;
 | 
|---|
| 143 |   }
 | 
|---|
| 144 | }
 | 
|---|
| 145 | 
 | 
|---|
| 146 | /**
 | 
|---|
| 147 |  * Saves the \a atoms into as a PDB file.
 | 
|---|
| 148 |  *
 | 
|---|
| 149 |  * \param file where to save the state
 | 
|---|
| 150 |  * \param atoms atoms to store
 | 
|---|
| 151 |  */
 | 
|---|
| 152 | void PdbParser::save(ostream* file, const std::vector<atom *> &AtomList)
 | 
|---|
| 153 | {
 | 
|---|
| 154 |   DoLog(0) && (Log() << Verbose(0) << "Saving changes to pdb." << std::endl);
 | 
|---|
| 155 |   {
 | 
|---|
| 156 |     // add initial remark
 | 
|---|
| 157 |     *file << "REMARK created by molecuilder on ";
 | 
|---|
| 158 |     time_t now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
 | 
|---|
| 159 |     // ctime ends in \n\0, we have to cut away the newline
 | 
|---|
| 160 |     std::string time(ctime(&now));
 | 
|---|
| 161 |     size_t pos = time.find('\n');
 | 
|---|
| 162 |     if (pos != 0)
 | 
|---|
| 163 |       *file << time.substr(0,pos);
 | 
|---|
| 164 |     else
 | 
|---|
| 165 |       *file << time;
 | 
|---|
| 166 |     *file << endl;
 | 
|---|
| 167 |   }
 | 
|---|
| 168 | 
 | 
|---|
| 169 |   // we distribute serials, hence clear map beforehand
 | 
|---|
| 170 |   atomIdMap.clear();
 | 
|---|
| 171 |   {
 | 
|---|
| 172 |     std::map<size_t,size_t> MolIdMap;
 | 
|---|
| 173 |     size_t MolNo = 1;  // residue number starts at 1 in pdb
 | 
|---|
| 174 |     for (vector<atom *>::const_iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) {
 | 
|---|
| 175 |       const molecule *mol = (*atomIt)->getMolecule();
 | 
|---|
| 176 |       if ((mol != NULL) && (MolIdMap.find(mol->getId()) == MolIdMap.end())) {
 | 
|---|
| 177 |         MolIdMap[mol->getId()] = MolNo++;
 | 
|---|
| 178 |       }
 | 
|---|
| 179 |     }
 | 
|---|
| 180 |     const size_t MaxMol = MolNo;
 | 
|---|
| 181 | 
 | 
|---|
| 182 |     // have a count per element and per molecule (0 is for all homeless atoms)
 | 
|---|
| 183 |     std::vector<int> **elementNo = new std::vector<int>*[MaxMol];
 | 
|---|
| 184 |     for (size_t i = 0; i < MaxMol; ++i)
 | 
|---|
| 185 |       elementNo[i] = new std::vector<int>(MAX_ELEMENTS,1);
 | 
|---|
| 186 |     char name[MAXSTRINGSIZE];
 | 
|---|
| 187 |     std::string ResidueName;
 | 
|---|
| 188 | 
 | 
|---|
| 189 |     // write ATOMs
 | 
|---|
| 190 |     int AtomNo = 1; // serial number starts at 1 in pdb
 | 
|---|
| 191 |     for (vector<atom *>::const_iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) {
 | 
|---|
| 192 |       PdbAtomInfoContainer &atomInfo = getadditionalAtomData(*atomIt);
 | 
|---|
| 193 |       // gather info about residue
 | 
|---|
| 194 |       const molecule *mol = (*atomIt)->getMolecule();
 | 
|---|
| 195 |       if (mol == NULL) {
 | 
|---|
| 196 |         MolNo = 0;
 | 
|---|
| 197 |         atomInfo.set(PdbKey::resSeq, "0");
 | 
|---|
| 198 |       } else {
 | 
|---|
| 199 |         ASSERT(MolIdMap.find(mol->getId()) != MolIdMap.end(),
 | 
|---|
| 200 |             "PdbParser::save() - Mol id "+toString(mol->getId())+" not present despite we set it?!");
 | 
|---|
| 201 |         MolNo = MolIdMap[mol->getId()];
 | 
|---|
| 202 |         atomInfo.set(PdbKey::resSeq, toString(MolIdMap[mol->getId()]));
 | 
|---|
| 203 |         if (atomInfo.get<std::string>(PdbKey::resName) == "-")
 | 
|---|
| 204 |           atomInfo.set(PdbKey::resName, mol->getName().substr(0,3));
 | 
|---|
| 205 |       }
 | 
|---|
| 206 |       // get info about atom
 | 
|---|
| 207 |       const size_t  Z = (*atomIt)->getType()->getAtomicNumber();
 | 
|---|
| 208 |       if (atomInfo.get<std::string>(PdbKey::name) == "-") {  // if no name set, give it a new name
 | 
|---|
| 209 |         sprintf(name, "%2s%02d",(*atomIt)->getType()->getSymbol().c_str(), (*elementNo[MolNo])[Z]);
 | 
|---|
| 210 |         (*elementNo[MolNo])[Z] = ((*elementNo[MolNo])[Z]+1) % 100;   // confine to two digits
 | 
|---|
| 211 |         atomInfo.set(PdbKey::name, name);
 | 
|---|
| 212 |       }
 | 
|---|
| 213 |       // set position
 | 
|---|
| 214 |       for (size_t i=0; i<NDIM;++i) {
 | 
|---|
| 215 |         stringstream position;
 | 
|---|
| 216 |         position << setw(8) << fixed << setprecision(3) << (*atomIt)->getPosition().at(i);
 | 
|---|
| 217 |         atomInfo.set(PositionEnumMap[i], position.str());
 | 
|---|
| 218 |       }
 | 
|---|
| 219 |       // change element and charge if changed
 | 
|---|
| 220 |       if (atomInfo.get<std::string>(PdbKey::element) != (*atomIt)->getType()->getSymbol())
 | 
|---|
| 221 |         atomInfo.set(PdbKey::element, (*atomIt)->getType()->getSymbol());
 | 
|---|
| 222 |       setSerial((*atomIt)->getId(), AtomNo);
 | 
|---|
| 223 |       atomInfo.set(PdbKey::serial, toString(AtomNo));
 | 
|---|
| 224 | 
 | 
|---|
| 225 |       // finally save the line
 | 
|---|
| 226 |       saveLine(file, atomInfo);
 | 
|---|
| 227 |       AtomNo++;
 | 
|---|
| 228 |     }
 | 
|---|
| 229 |     for (size_t i = 0; i < MaxMol; ++i)
 | 
|---|
| 230 |       delete elementNo[i];
 | 
|---|
| 231 |     delete elementNo;
 | 
|---|
| 232 | 
 | 
|---|
| 233 |     // write CONECTs
 | 
|---|
| 234 |     for (vector<atom *>::const_iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) {
 | 
|---|
| 235 |       writeNeighbors(file, 4, *atomIt);
 | 
|---|
| 236 |     }
 | 
|---|
| 237 |   }
 | 
|---|
| 238 | 
 | 
|---|
| 239 |   // END
 | 
|---|
| 240 |   *file << "END" << endl;
 | 
|---|
| 241 | }
 | 
|---|
| 242 | 
 | 
|---|
| 243 | /** Either returns reference to present entry or creates new with default values.
 | 
|---|
| 244 |  *
 | 
|---|
| 245 |  * @param _atom atom whose entry we desire
 | 
|---|
| 246 |  * @return
 | 
|---|
| 247 |  */
 | 
|---|
| 248 | PdbAtomInfoContainer& PdbParser::getadditionalAtomData(atom *_atom)
 | 
|---|
| 249 | {
 | 
|---|
| 250 |   if (additionalAtomData.find(_atom->getId()) != additionalAtomData.end()) {
 | 
|---|
| 251 |   } else if (additionalAtomData.find(_atom->father->getId()) != additionalAtomData.end()) {
 | 
|---|
| 252 |     // use info from direct father
 | 
|---|
| 253 |     additionalAtomData[_atom->getId()] = additionalAtomData[_atom->father->getId()];
 | 
|---|
| 254 |   } else if (additionalAtomData.find(_atom->GetTrueFather()->getId()) != additionalAtomData.end()) {
 | 
|---|
| 255 |     // use info from topmost father
 | 
|---|
| 256 |     additionalAtomData[_atom->getId()] = additionalAtomData[_atom->GetTrueFather()->getId()];
 | 
|---|
| 257 |   } else {
 | 
|---|
| 258 |     // create new entry use default values if nothing else is known
 | 
|---|
| 259 |     additionalAtomData[_atom->getId()] = defaultAdditionalData;
 | 
|---|
| 260 |   }
 | 
|---|
| 261 |   return additionalAtomData[_atom->getId()];
 | 
|---|
| 262 | }
 | 
|---|
| 263 | 
 | 
|---|
| 264 | /**
 | 
|---|
| 265 |  * Writes one line of PDB-formatted data to the provided stream.
 | 
|---|
| 266 |  *
 | 
|---|
| 267 |  * \param stream where to write the line to
 | 
|---|
| 268 |  * \param *currentAtom the atom of which information should be written
 | 
|---|
| 269 |  * \param AtomNo serial number of atom
 | 
|---|
| 270 |  * \param *name name of atom, i.e. H01
 | 
|---|
| 271 |  * \param ResidueName Name of molecule
 | 
|---|
| 272 |  * \param ResidueNo number of residue
 | 
|---|
| 273 |  */
 | 
|---|
| 274 | void PdbParser::saveLine(
 | 
|---|
| 275 |     ostream* file,
 | 
|---|
| 276 |     const PdbAtomInfoContainer &atomInfo)
 | 
|---|
| 277 | {
 | 
|---|
| 278 |   *file << setfill(' ') << left << setw(6)
 | 
|---|
| 279 |       << atomInfo.get<std::string>(PdbKey::token);
 | 
|---|
| 280 |   *file << setfill(' ') << right << setw(5)
 | 
|---|
| 281 |       << atomInfo.get<int>(PdbKey::serial); /* atom serial number */
 | 
|---|
| 282 |   *file << " "; /* char 12 is empty */
 | 
|---|
| 283 |   *file << setfill(' ') << left << setw(4)
 | 
|---|
| 284 |       << atomInfo.get<std::string>(PdbKey::name);  /* atom name */
 | 
|---|
| 285 |   *file << setfill(' ') << left << setw(1)
 | 
|---|
| 286 |       << atomInfo.get<std::string>(PdbKey::altLoc); /* alternate location/conformation */
 | 
|---|
| 287 |   *file << setfill(' ') << left << setw(3)
 | 
|---|
| 288 |       << atomInfo.get<std::string>(PdbKey::resName);  /* residue name */
 | 
|---|
| 289 |   *file << " "; /* char 21 is empty */
 | 
|---|
| 290 |   *file << setfill(' ') << left << setw(1)
 | 
|---|
| 291 |       << atomInfo.get<std::string>(PdbKey::chainID); /* chain identifier */
 | 
|---|
| 292 |   *file << setfill(' ') << left << setw(4)
 | 
|---|
| 293 |       << atomInfo.get<int>(PdbKey::resSeq); /* residue sequence number */
 | 
|---|
| 294 |   *file << setfill(' ') << left << setw(1)
 | 
|---|
| 295 |       << atomInfo.get<std::string>(PdbKey::iCode); /* iCode */
 | 
|---|
| 296 |   *file << "   "; /* char 28-30 are empty */
 | 
|---|
| 297 |   // have the following operate on stringstreams such that format specifiers
 | 
|---|
| 298 |   // only act on these
 | 
|---|
| 299 |   for (size_t i=0;i<NDIM;++i) {
 | 
|---|
| 300 |     stringstream position;
 | 
|---|
| 301 |     position << fixed << setprecision(3) << showpoint
 | 
|---|
| 302 |         << atomInfo.get<double>(PositionEnumMap[i]);
 | 
|---|
| 303 |     *file << setfill(' ') << right << setw(8) << position.str();
 | 
|---|
| 304 |   }
 | 
|---|
| 305 |   {
 | 
|---|
| 306 |     stringstream occupancy;
 | 
|---|
| 307 |     occupancy << fixed << setprecision(2) << showpoint
 | 
|---|
| 308 |         << atomInfo.get<double>(PdbKey::occupancy); /* occupancy */
 | 
|---|
| 309 |     *file << setfill(' ') << right << setw(6) << occupancy.str();
 | 
|---|
| 310 |   }
 | 
|---|
| 311 |   {
 | 
|---|
| 312 |     stringstream tempFactor;
 | 
|---|
| 313 |     tempFactor << fixed << setprecision(2) << showpoint
 | 
|---|
| 314 |         << atomInfo.get<double>(PdbKey::tempFactor); /* temperature factor */
 | 
|---|
| 315 |     *file << setfill(' ') << right << setw(6) << tempFactor.str();
 | 
|---|
| 316 |   }
 | 
|---|
| 317 |   *file << "          "; /* char 68-76 are empty */
 | 
|---|
| 318 |   *file << setfill(' ') << right << setw(2) << atomInfo.get<std::string>(PdbKey::element); /* element */
 | 
|---|
| 319 |   *file << setfill(' ') << right << setw(2) << atomInfo.get<int>(PdbKey::charge); /* charge */
 | 
|---|
| 320 | 
 | 
|---|
| 321 |   *file << endl;
 | 
|---|
| 322 | }
 | 
|---|
| 323 | 
 | 
|---|
| 324 | /**
 | 
|---|
| 325 |  * Writes the neighbor information of one atom to the provided stream.
 | 
|---|
| 326 |  *
 | 
|---|
| 327 |  * \param *file  where to write neighbor information to
 | 
|---|
| 328 |  * \param MaxnumberOfNeighbors of neighbors
 | 
|---|
| 329 |  * \param *currentAtom to the atom of which to take the neighbor information
 | 
|---|
| 330 |  */
 | 
|---|
| 331 | void PdbParser::writeNeighbors(ostream* file, int MaxnumberOfNeighbors, atom* currentAtom) {
 | 
|---|
| 332 |   int MaxNo = MaxnumberOfNeighbors;
 | 
|---|
| 333 |   if (!currentAtom->ListOfBonds.empty()) {
 | 
|---|
| 334 |     for(BondList::iterator currentBond = currentAtom->ListOfBonds.begin(); currentBond != currentAtom->ListOfBonds.end(); ++currentBond) {
 | 
|---|
| 335 |       if (MaxNo >= MaxnumberOfNeighbors) {
 | 
|---|
| 336 |         *file << "CONECT";
 | 
|---|
| 337 |         *file << setw(5) << getSerial(currentAtom->getId());
 | 
|---|
| 338 |         MaxNo = 0;
 | 
|---|
| 339 |       }
 | 
|---|
| 340 |       *file << setw(5) << getSerial((*currentBond)->GetOtherAtom(currentAtom)->getId());
 | 
|---|
| 341 |       MaxNo++;
 | 
|---|
| 342 |       if (MaxNo == MaxnumberOfNeighbors)
 | 
|---|
| 343 |         *file << "\n";
 | 
|---|
| 344 |     }
 | 
|---|
| 345 |     if (MaxNo != MaxnumberOfNeighbors)
 | 
|---|
| 346 |       *file << "\n";
 | 
|---|
| 347 |   }
 | 
|---|
| 348 | }
 | 
|---|
| 349 | 
 | 
|---|
| 350 | /** Retrieves a value from PdbParser::atomIdMap.
 | 
|---|
| 351 |  * \param atomid key
 | 
|---|
| 352 |  * \return value
 | 
|---|
| 353 |  */
 | 
|---|
| 354 | size_t PdbParser::getSerial(const size_t atomid) const
 | 
|---|
| 355 | {
 | 
|---|
| 356 |   ASSERT(atomIdMap.find(atomid) != atomIdMap.end(), "PdbParser::getAtomId: atomid not present in Map.");
 | 
|---|
| 357 |   return (atomIdMap.find(atomid)->second);
 | 
|---|
| 358 | }
 | 
|---|
| 359 | 
 | 
|---|
| 360 | /** Sets an entry in PdbParser::atomIdMap.
 | 
|---|
| 361 |  * \param localatomid key
 | 
|---|
| 362 |  * \param atomid value
 | 
|---|
| 363 |  * \return true - key not present, false - value present
 | 
|---|
| 364 |  */
 | 
|---|
| 365 | void PdbParser::setSerial(const size_t localatomid, const size_t atomid)
 | 
|---|
| 366 | {
 | 
|---|
| 367 |   pair<std::map<size_t,size_t>::iterator, bool > inserter;
 | 
|---|
| 368 | //  DoLog(1) && (Log() << Verbose(1) << "PdbParser::setAtomId() - Inserting ("
 | 
|---|
| 369 | //      << localatomid << " -> " << atomid << ")." << std::endl);
 | 
|---|
| 370 |   inserter = atomIdMap.insert( make_pair(localatomid, atomid) );
 | 
|---|
| 371 |   ASSERT(inserter.second, "PdbParser::setAtomId: atomId already present in Map.");
 | 
|---|
| 372 | }
 | 
|---|
| 373 | 
 | 
|---|
| 374 | /** Parse an ATOM line from a PDB file.
 | 
|---|
| 375 |  *
 | 
|---|
| 376 |  * Reads one data line of a pdstatus file and interprets it according to the
 | 
|---|
| 377 |  * specifications of the PDB 3.2 format: http://www.wwpdb.org/docs.html
 | 
|---|
| 378 |  *
 | 
|---|
| 379 |  *  A new atom is created and filled with available information, non-
 | 
|---|
| 380 |  *  standard information is placed in additionalAtomData at the atom's id.
 | 
|---|
| 381 |  *
 | 
|---|
| 382 |  * \param line to parse as an atom
 | 
|---|
| 383 |  * \param newmol molecule to add parsed atoms to
 | 
|---|
| 384 |  */
 | 
|---|
| 385 | void PdbParser::readAtomDataLine(std::string &line, molecule *newmol = NULL) {
 | 
|---|
| 386 |   vector<string>::iterator it;
 | 
|---|
| 387 |   stringstream lineStream;
 | 
|---|
| 388 |   atom* newAtom = World::getInstance().createAtom();
 | 
|---|
| 389 |   PdbAtomInfoContainer &atomInfo = getadditionalAtomData(newAtom);
 | 
|---|
| 390 |   string word;
 | 
|---|
| 391 |   ConvertTo<size_t> toSize_t;
 | 
|---|
| 392 |   double tmp;
 | 
|---|
| 393 | 
 | 
|---|
| 394 |   lineStream << line;
 | 
|---|
| 395 |   atomInfo.set(PdbKey::token, line.substr(0,6));
 | 
|---|
| 396 |   atomInfo.set(PdbKey::serial, line.substr(6,5));
 | 
|---|
| 397 |   std::pair< std::set<size_t>::const_iterator, bool> Inserter =
 | 
|---|
| 398 |     SerialSet.insert(toSize_t(atomInfo.get<std::string>(PdbKey::serial)));
 | 
|---|
| 399 |   ASSERT(Inserter.second,
 | 
|---|
| 400 |       "PdbParser::readAtomDataLine() - ATOM contains entry with serial "
 | 
|---|
| 401 |       +atomInfo.get<std::string>(PdbKey::serial)+" already present!");
 | 
|---|
| 402 |   // assign hightest+1 instead, but then beware of CONECT entries! Another map needed!
 | 
|---|
| 403 | //  if (!Inserter.second) {
 | 
|---|
| 404 | //    const size_t id = (*SerialSet.rbegin())+1;
 | 
|---|
| 405 | //    SerialSet.insert(id);
 | 
|---|
| 406 | //    atomInfo.set(PdbKey::serial, toString(id));
 | 
|---|
| 407 | //    DoeLog(2) && (eLog() << Verbose(2)
 | 
|---|
| 408 | //        << "Serial " << atomInfo.get<std::string>(PdbKey::serial) << " already present, "
 | 
|---|
| 409 | //        << "assigning " << toString(id) << " instead." << std::endl);
 | 
|---|
| 410 | //  }
 | 
|---|
| 411 | 
 | 
|---|
| 412 |   // check whether serial exists, if so, assign next available
 | 
|---|
| 413 | 
 | 
|---|
| 414 | //  DoLog(2) && (Log() << Verbose(2) << "Split line:"
 | 
|---|
| 415 | //      << line.substr(6,5) << "|"
 | 
|---|
| 416 | //      << line.substr(12,4) << "|"
 | 
|---|
| 417 | //      << line.substr(16,1) << "|"
 | 
|---|
| 418 | //      << line.substr(17,3) << "|"
 | 
|---|
| 419 | //      << line.substr(21,1) << "|"
 | 
|---|
| 420 | //      << line.substr(22,4) << "|"
 | 
|---|
| 421 | //      << line.substr(26,1) << "|"
 | 
|---|
| 422 | //      << line.substr(30,8) << "|"
 | 
|---|
| 423 | //      << line.substr(38,8) << "|"
 | 
|---|
| 424 | //      << line.substr(46,8) << "|"
 | 
|---|
| 425 | //      << line.substr(54,6) << "|"
 | 
|---|
| 426 | //      << line.substr(60,6) << "|"
 | 
|---|
| 427 | //      << line.substr(76,2) << "|"
 | 
|---|
| 428 | //      << line.substr(78,2) << std::endl);
 | 
|---|
| 429 | 
 | 
|---|
| 430 |   setSerial(toSize_t(atomInfo.get<std::string>(PdbKey::serial)), newAtom->getId());
 | 
|---|
| 431 |   atomInfo.set(PdbKey::name, line.substr(12,4));
 | 
|---|
| 432 |   atomInfo.set(PdbKey::altLoc, line.substr(16,1));
 | 
|---|
| 433 |   atomInfo.set(PdbKey::resName, line.substr(17,3));
 | 
|---|
| 434 |   atomInfo.set(PdbKey::chainID, line.substr(21,1));
 | 
|---|
| 435 |   atomInfo.set(PdbKey::resSeq, line.substr(22,4));
 | 
|---|
| 436 |   atomInfo.set(PdbKey::iCode, line.substr(26,1));
 | 
|---|
| 437 |   PdbAtomInfoContainer::ScanKey(tmp, line.substr(30,8));
 | 
|---|
| 438 |   newAtom->set(0, tmp);
 | 
|---|
| 439 |   PdbAtomInfoContainer::ScanKey(tmp, line.substr(38,8));
 | 
|---|
| 440 |   newAtom->set(1, tmp);
 | 
|---|
| 441 |   PdbAtomInfoContainer::ScanKey(tmp, line.substr(46,8));
 | 
|---|
| 442 |   newAtom->set(2, tmp);
 | 
|---|
| 443 |   atomInfo.set(PdbKey::occupancy, line.substr(54,6));
 | 
|---|
| 444 |   atomInfo.set(PdbKey::tempFactor, line.substr(60,6));
 | 
|---|
| 445 |   atomInfo.set(PdbKey::charge, line.substr(78,2));
 | 
|---|
| 446 |   atomInfo.set(PdbKey::element, line.substr(76,2));
 | 
|---|
| 447 |   const element *elem = World::getInstance().getPeriode()
 | 
|---|
| 448 |       ->FindElement(atomInfo.get<std::string>(PdbKey::element));
 | 
|---|
| 449 |   ASSERT(elem != NULL,
 | 
|---|
| 450 |       "PdbParser::readAtomDataLine() - element "+atomInfo.get<std::string>(PdbKey::element)+" is unknown!");
 | 
|---|
| 451 |   newAtom->setType(elem);
 | 
|---|
| 452 | 
 | 
|---|
| 453 |   if (newmol != NULL)
 | 
|---|
| 454 |     newmol->AddAtom(newAtom);
 | 
|---|
| 455 | 
 | 
|---|
| 456 | //  printAtomInfo(newAtom);
 | 
|---|
| 457 | }
 | 
|---|
| 458 | 
 | 
|---|
| 459 | /** Prints all PDB-specific information known about an atom.
 | 
|---|
| 460 |  *
 | 
|---|
| 461 |  */
 | 
|---|
| 462 | void PdbParser::printAtomInfo(const atom * const newAtom) const
 | 
|---|
| 463 | {
 | 
|---|
| 464 |   const PdbAtomInfoContainer &atomInfo = additionalAtomData.at(newAtom->getId()); // operator[] const does not exist
 | 
|---|
| 465 | 
 | 
|---|
| 466 |   DoLog(1) && (Log() << Verbose(1) << "We know about atom " << newAtom->getId() << ":" << std::endl);
 | 
|---|
| 467 |   DoLog(1) && (Log() << Verbose(1) << "\ttoken is " << atomInfo.get<std::string>(PdbKey::token) << std::endl);
 | 
|---|
| 468 |   DoLog(1) && (Log() << Verbose(1) << "\tserial is " << atomInfo.get<int>(PdbKey::serial) << std::endl);
 | 
|---|
| 469 |   DoLog(1) && (Log() << Verbose(1) << "\tname is " << atomInfo.get<std::string>(PdbKey::name) << std::endl);
 | 
|---|
| 470 |   DoLog(1) && (Log() << Verbose(1) << "\taltLoc is " << atomInfo.get<std::string>(PdbKey::altLoc) << std::endl);
 | 
|---|
| 471 |   DoLog(1) && (Log() << Verbose(1) << "\tresName is " << atomInfo.get<std::string>(PdbKey::resName) << std::endl);
 | 
|---|
| 472 |   DoLog(1) && (Log() << Verbose(1) << "\tchainID is " << atomInfo.get<std::string>(PdbKey::chainID) << std::endl);
 | 
|---|
| 473 |   DoLog(1) && (Log() << Verbose(1) << "\tresSeq is " << atomInfo.get<int>(PdbKey::resSeq) << std::endl);
 | 
|---|
| 474 |   DoLog(1) && (Log() << Verbose(1) << "\tiCode is " << atomInfo.get<std::string>(PdbKey::iCode) << std::endl);
 | 
|---|
| 475 |   DoLog(1) && (Log() << Verbose(1) << "\tX is " << atomInfo.get<double>(PdbKey::X) << std::endl);
 | 
|---|
| 476 |   DoLog(1) && (Log() << Verbose(1) << "\tY is " << atomInfo.get<double>(PdbKey::Y) << std::endl);
 | 
|---|
| 477 |   DoLog(1) && (Log() << Verbose(1) << "\tZ is " << atomInfo.get<double>(PdbKey::Z) << std::endl);
 | 
|---|
| 478 |   DoLog(1) && (Log() << Verbose(1) << "\toccupancy is " << atomInfo.get<double>(PdbKey::occupancy) << std::endl);
 | 
|---|
| 479 |   DoLog(1) && (Log() << Verbose(1) << "\ttempFactor is " << atomInfo.get<double>(PdbKey::tempFactor) << std::endl);
 | 
|---|
| 480 |   DoLog(1) && (Log() << Verbose(1) << "\telement is '" << *(newAtom->getType()) << "'" << std::endl);
 | 
|---|
| 481 |   DoLog(1) && (Log() << Verbose(1) << "\tcharge is " << atomInfo.get<int>(PdbKey::charge) << std::endl);
 | 
|---|
| 482 | }
 | 
|---|
| 483 | 
 | 
|---|
| 484 | /**
 | 
|---|
| 485 |  * Reads neighbor information for one atom from the input.
 | 
|---|
| 486 |  *
 | 
|---|
| 487 |  * \param line to parse as an atom
 | 
|---|
| 488 |  */
 | 
|---|
| 489 | void PdbParser::readNeighbors(std::string &line)
 | 
|---|
| 490 | {
 | 
|---|
| 491 |   const size_t length = line.length();
 | 
|---|
| 492 |   std::list<size_t> ListOfNeighbors;
 | 
|---|
| 493 |   ConvertTo<size_t> toSize_t;
 | 
|---|
| 494 | 
 | 
|---|
| 495 |   // obtain neighbours
 | 
|---|
| 496 |   // show split line for debugging
 | 
|---|
| 497 |   string output;
 | 
|---|
| 498 |   ASSERT(length >=16,
 | 
|---|
| 499 |       "PdbParser::readNeighbors() - CONECT entry has not enough entries: "+line+"!");
 | 
|---|
| 500 | //  output = "Split line:|";
 | 
|---|
| 501 | //  output += line.substr(6,5) + "|";
 | 
|---|
| 502 |   const size_t id = toSize_t(line.substr(6,5));
 | 
|---|
| 503 |   for (size_t index = 11; index <= 26; index+=5) {
 | 
|---|
| 504 |     if (index+5 <= length) {
 | 
|---|
| 505 | //      output += line.substr(index,5) + "|";
 | 
|---|
| 506 |       const size_t otherid = toSize_t(line.substr(index,5));
 | 
|---|
| 507 |       ListOfNeighbors.push_back(otherid);
 | 
|---|
| 508 |     } else  {
 | 
|---|
| 509 |       break;
 | 
|---|
| 510 |     }
 | 
|---|
| 511 |   }
 | 
|---|
| 512 | //  DoLog(2) && (Log() << Verbose(2) << output << std::endl);
 | 
|---|
| 513 | 
 | 
|---|
| 514 |   // add neighbours
 | 
|---|
| 515 |   atom *_atom = World::getInstance().getAtom(AtomById(getSerial(id)));
 | 
|---|
| 516 |   for (std::list<size_t>::const_iterator iter = ListOfNeighbors.begin();
 | 
|---|
| 517 |       iter != ListOfNeighbors.end();
 | 
|---|
| 518 |       ++iter) {
 | 
|---|
| 519 | //    DoLog(1) && (Log() << Verbose(1) << "Adding Bond (" << getAtomId(id) << "," << getAtomId(*iter) << ")" << std::endl);
 | 
|---|
| 520 |     atom * const _Otheratom = World::getInstance().getAtom(AtomById(getSerial(*iter)));
 | 
|---|
| 521 |     _atom->addBond(_Otheratom);
 | 
|---|
| 522 |   }
 | 
|---|
| 523 | }
 | 
|---|
| 524 | 
 | 
|---|
| 525 | /**
 | 
|---|
| 526 |  * Replaces atom IDs read from the file by the corresponding world IDs. All IDs
 | 
|---|
| 527 |  * IDs of the input string will be replaced; expected separating characters are
 | 
|---|
| 528 |  * "-" and ",".
 | 
|---|
| 529 |  *
 | 
|---|
| 530 |  * \param string in which atom IDs should be adapted
 | 
|---|
| 531 |  *
 | 
|---|
| 532 |  * \return input string with modified atom IDs
 | 
|---|
| 533 |  */
 | 
|---|
| 534 | //string PdbParser::adaptIdDependentDataString(string data) {
 | 
|---|
| 535 | //  // there might be no IDs
 | 
|---|
| 536 | //  if (data == "-") {
 | 
|---|
| 537 | //    return "-";
 | 
|---|
| 538 | //  }
 | 
|---|
| 539 | //
 | 
|---|
| 540 | //  char separator;
 | 
|---|
| 541 | //  int id;
 | 
|---|
| 542 | //  stringstream line, result;
 | 
|---|
| 543 | //  line << data;
 | 
|---|
| 544 | //
 | 
|---|
| 545 | //  line >> id;
 | 
|---|
| 546 | //  result << atomIdMap[id];
 | 
|---|
| 547 | //  while (line.good()) {
 | 
|---|
| 548 | //    line >> separator >> id;
 | 
|---|
| 549 | //    result << separator << atomIdMap[id];
 | 
|---|
| 550 | //  }
 | 
|---|
| 551 | //
 | 
|---|
| 552 | //  return result.str();
 | 
|---|
| 553 | //  return "";
 | 
|---|
| 554 | //}
 | 
|---|
| 555 | 
 | 
|---|
| 556 | 
 | 
|---|
| 557 | bool PdbParser::operator==(const PdbParser& b) const
 | 
|---|
| 558 | {
 | 
|---|
| 559 |   bool status = true;
 | 
|---|
| 560 |   World::AtomComposite atoms = World::getInstance().getAllAtoms();
 | 
|---|
| 561 |   for (World::AtomComposite::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
| 562 |     if ((additionalAtomData.find((*iter)->getId()) != additionalAtomData.end())
 | 
|---|
| 563 |         && (b.additionalAtomData.find((*iter)->getId()) != b.additionalAtomData.end())) {
 | 
|---|
| 564 |       const PdbAtomInfoContainer &atomInfo = additionalAtomData.at((*iter)->getId());
 | 
|---|
| 565 |       const PdbAtomInfoContainer &OtheratomInfo = b.additionalAtomData.at((*iter)->getId());
 | 
|---|
| 566 | 
 | 
|---|
| 567 |       status = status && (atomInfo.get<std::string>(PdbKey::serial) == OtheratomInfo.get<std::string>(PdbKey::serial));
 | 
|---|
| 568 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in serials!" << std::endl);
 | 
|---|
| 569 |       status = status && (atomInfo.get<std::string>(PdbKey::name) == OtheratomInfo.get<std::string>(PdbKey::name));
 | 
|---|
| 570 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in names!" << std::endl);
 | 
|---|
| 571 |       status = status && (atomInfo.get<std::string>(PdbKey::altLoc) == OtheratomInfo.get<std::string>(PdbKey::altLoc));
 | 
|---|
| 572 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in altLocs!" << std::endl);
 | 
|---|
| 573 |       status = status && (atomInfo.get<std::string>(PdbKey::resName) == OtheratomInfo.get<std::string>(PdbKey::resName));
 | 
|---|
| 574 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in resNames!" << std::endl);
 | 
|---|
| 575 |       status = status && (atomInfo.get<std::string>(PdbKey::chainID) == OtheratomInfo.get<std::string>(PdbKey::chainID));
 | 
|---|
| 576 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in chainIDs!" << std::endl);
 | 
|---|
| 577 |       status = status && (atomInfo.get<std::string>(PdbKey::resSeq) == OtheratomInfo.get<std::string>(PdbKey::resSeq));
 | 
|---|
| 578 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in resSeqs!" << std::endl);
 | 
|---|
| 579 |       status = status && (atomInfo.get<std::string>(PdbKey::iCode) == OtheratomInfo.get<std::string>(PdbKey::iCode));
 | 
|---|
| 580 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in iCodes!" << std::endl);
 | 
|---|
| 581 |       status = status && (atomInfo.get<std::string>(PdbKey::occupancy) == OtheratomInfo.get<std::string>(PdbKey::occupancy));
 | 
|---|
| 582 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in occupancies!" << std::endl);
 | 
|---|
| 583 |       status = status && (atomInfo.get<std::string>(PdbKey::tempFactor) == OtheratomInfo.get<std::string>(PdbKey::tempFactor));
 | 
|---|
| 584 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in tempFactors!" << std::endl);
 | 
|---|
| 585 |       status = status && (atomInfo.get<std::string>(PdbKey::charge) == OtheratomInfo.get<std::string>(PdbKey::charge));
 | 
|---|
| 586 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in charges!" << std::endl);
 | 
|---|
| 587 |     }
 | 
|---|
| 588 |   }
 | 
|---|
| 589 | 
 | 
|---|
| 590 |   return status;
 | 
|---|
| 591 | }
 | 
|---|
| 592 | 
 | 
|---|