| 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["TER"] = PdbKey::Filler;
 | 
|---|
| 49 |   knownTokens["END"] = PdbKey::EndOfFile;
 | 
|---|
| 50 |   knownTokens["CONECT"] = PdbKey::Connect;
 | 
|---|
| 51 |   knownTokens["REMARK"] = PdbKey::Remark;
 | 
|---|
| 52 |   knownTokens[""] = PdbKey::EndOfFile;
 | 
|---|
| 53 | }
 | 
|---|
| 54 | 
 | 
|---|
| 55 | /**
 | 
|---|
| 56 |  * Destructor.
 | 
|---|
| 57 |  */
 | 
|---|
| 58 | PdbParser::~PdbParser() {
 | 
|---|
| 59 |   additionalAtomData.clear();
 | 
|---|
| 60 |   atomIdMap.clear();
 | 
|---|
| 61 | }
 | 
|---|
| 62 | 
 | 
|---|
| 63 | 
 | 
|---|
| 64 | /** Parses the initial word of the given \a line and returns the token type.
 | 
|---|
| 65 |  *
 | 
|---|
| 66 |  * @param line line to scan
 | 
|---|
| 67 |  * @return token type
 | 
|---|
| 68 |  */
 | 
|---|
| 69 | enum PdbKey::KnownTokens PdbParser::getToken(string &line)
 | 
|---|
| 70 | {
 | 
|---|
| 71 |   // look for first space
 | 
|---|
| 72 |   const size_t space_location = line.find(' ');
 | 
|---|
| 73 |   const size_t tab_location = line.find('\t');
 | 
|---|
| 74 |   size_t location = space_location < tab_location ? space_location : tab_location;
 | 
|---|
| 75 |   string token;
 | 
|---|
| 76 |   if (location != string::npos) {
 | 
|---|
| 77 |     //DoLog(1) && (Log() << Verbose(1) << "Found space at position " << space_location << std::endl);
 | 
|---|
| 78 |     token = line.substr(0,space_location);
 | 
|---|
| 79 |   } else {
 | 
|---|
| 80 |     token = line;
 | 
|---|
| 81 |   }
 | 
|---|
| 82 | 
 | 
|---|
| 83 |   //DoLog(1) && (Log() << Verbose(1) << "Token is " << token << std::endl);
 | 
|---|
| 84 |   if (knownTokens.count(token) == 0)
 | 
|---|
| 85 |     return PdbKey::NoToken;
 | 
|---|
| 86 |   else
 | 
|---|
| 87 |     return knownTokens[token];
 | 
|---|
| 88 | 
 | 
|---|
| 89 |   return PdbKey::NoToken;
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | /**
 | 
|---|
| 93 |  * Loads atoms from a PDB-formatted file.
 | 
|---|
| 94 |  *
 | 
|---|
| 95 |  * \param PDB file
 | 
|---|
| 96 |  */
 | 
|---|
| 97 | void PdbParser::load(istream* file) {
 | 
|---|
| 98 |   string line;
 | 
|---|
| 99 |   size_t linecount  = 0;
 | 
|---|
| 100 |   enum PdbKey::KnownTokens token;
 | 
|---|
| 101 | 
 | 
|---|
| 102 |   molecule *newmol = World::getInstance().createMolecule();
 | 
|---|
| 103 |   newmol->ActiveFlag = true;
 | 
|---|
| 104 |   bool NotEndOfFile = true;
 | 
|---|
| 105 |   // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include
 | 
|---|
| 106 |   World::getInstance().getMolecules()->insert(newmol);
 | 
|---|
| 107 |   while (NotEndOfFile) {
 | 
|---|
| 108 |     std::getline(*file, line, '\n');
 | 
|---|
| 109 |     // extract first token
 | 
|---|
| 110 |     token = getToken(line);
 | 
|---|
| 111 |     //DoLog(1) && (Log() << Verbose(1) << " Recognized token of type : " << token << std::endl);
 | 
|---|
| 112 |     switch (token) {
 | 
|---|
| 113 |       case PdbKey::Atom:
 | 
|---|
| 114 |         readAtomDataLine(line, newmol);
 | 
|---|
| 115 |         break;
 | 
|---|
| 116 |       case PdbKey::Remark:
 | 
|---|
| 117 |         break;
 | 
|---|
| 118 |       case PdbKey::Connect:
 | 
|---|
| 119 |         readNeighbors(line);
 | 
|---|
| 120 |         break;
 | 
|---|
| 121 |       case PdbKey::Filler:
 | 
|---|
| 122 |         break;
 | 
|---|
| 123 |       case PdbKey::EndOfFile:
 | 
|---|
| 124 |         NotEndOfFile = false;
 | 
|---|
| 125 |         break;
 | 
|---|
| 126 |       default:
 | 
|---|
| 127 |         // TODO: put a throw here
 | 
|---|
| 128 |         DoeLog(2) && (eLog() << Verbose(2) << "Unknown token: '" << line << "'" << std::endl);
 | 
|---|
| 129 |         //ASSERT(0, "PdbParser::load() - Unknown token in line "+toString(linecount)+": "+line+".");
 | 
|---|
| 130 |         break;
 | 
|---|
| 131 |     }
 | 
|---|
| 132 |     NotEndOfFile = NotEndOfFile && (file->good());
 | 
|---|
| 133 |     linecount++;
 | 
|---|
| 134 |   }
 | 
|---|
| 135 | }
 | 
|---|
| 136 | 
 | 
|---|
| 137 | /**
 | 
|---|
| 138 |  * Saves the World's current state into as a PDB file.
 | 
|---|
| 139 |  *
 | 
|---|
| 140 |  * \param file where to save the state
 | 
|---|
| 141 |  */
 | 
|---|
| 142 | void PdbParser::save(ostream* file) {
 | 
|---|
| 143 |   DoLog(0) && (Log() << Verbose(0) << "Saving changes to pdb." << std::endl);
 | 
|---|
| 144 | 
 | 
|---|
| 145 |   {
 | 
|---|
| 146 |     // add initial remark
 | 
|---|
| 147 |     *file << "REMARK created by molecuilder on ";
 | 
|---|
| 148 |     time_t now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
 | 
|---|
| 149 |     // ctime ends in \n\0, we have to cut away the newline
 | 
|---|
| 150 |     std::string time(ctime(&now));
 | 
|---|
| 151 |     size_t pos = time.find('\n');
 | 
|---|
| 152 |     if (pos != 0)
 | 
|---|
| 153 |       *file << time.substr(0,pos);
 | 
|---|
| 154 |     else
 | 
|---|
| 155 |       *file << time;
 | 
|---|
| 156 |     *file << endl;
 | 
|---|
| 157 |   }
 | 
|---|
| 158 | 
 | 
|---|
| 159 |   // we distribute new atom numbers, hence clear map beforehand
 | 
|---|
| 160 |   atomIdMap.clear();
 | 
|---|
| 161 |   {
 | 
|---|
| 162 |     vector<atom *> AtomList = World::getInstance().getAllAtoms();
 | 
|---|
| 163 | 
 | 
|---|
| 164 |     std::vector<int> elementNo(MAX_ELEMENTS,1);
 | 
|---|
| 165 |     char name[MAXSTRINGSIZE];
 | 
|---|
| 166 | 
 | 
|---|
| 167 |     // write ATOMs
 | 
|---|
| 168 |     int AtomNo = 1; // serial number starts at 1 in pdb
 | 
|---|
| 169 |     int MolNo = 1;  // residue number starts at 1 in pdb
 | 
|---|
| 170 |     for (vector<atom *>::iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) {
 | 
|---|
| 171 |       const size_t  Z = (*atomIt)->getType()->getAtomicNumber();
 | 
|---|
| 172 |       sprintf(name, "%2s%02d",(*atomIt)->getType()->getSymbol().c_str(), elementNo[Z]);
 | 
|---|
| 173 |       elementNo[Z] = (elementNo[Z]+1) % 100;   // confine to two digits
 | 
|---|
| 174 |       const molecule *mol = (*atomIt)->getMolecule();
 | 
|---|
| 175 |       if (mol == NULL) {  // for homeless atoms, MolNo = -1 is reserved
 | 
|---|
| 176 |         MolNo = -1;
 | 
|---|
| 177 |       } else {
 | 
|---|
| 178 |         MolNo = mol->getId();
 | 
|---|
| 179 |       }
 | 
|---|
| 180 |       saveLine(file, *atomIt, name, AtomNo, MolNo);
 | 
|---|
| 181 |       setAtomId((*atomIt)->getId(), AtomNo);
 | 
|---|
| 182 |       AtomNo++;
 | 
|---|
| 183 |     }
 | 
|---|
| 184 | 
 | 
|---|
| 185 |     // write CONECTs
 | 
|---|
| 186 |     for (vector<atom *>::iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) {
 | 
|---|
| 187 |       writeNeighbors(file, 4, *atomIt);
 | 
|---|
| 188 |     }
 | 
|---|
| 189 |   }
 | 
|---|
| 190 | 
 | 
|---|
| 191 |   // END
 | 
|---|
| 192 |   *file << "END" << endl;
 | 
|---|
| 193 | }
 | 
|---|
| 194 | 
 | 
|---|
| 195 | /**
 | 
|---|
| 196 |  * Writes one line of PDB-formatted data to the provided stream.
 | 
|---|
| 197 |  *
 | 
|---|
| 198 |  * \param stream where to write the line to
 | 
|---|
| 199 |  * \param *currentAtom the atom of which information should be written
 | 
|---|
| 200 |  * \param *name name of atom, i.e. H01
 | 
|---|
| 201 |  * \param AtomNo serial number of atom
 | 
|---|
| 202 |  * \param ResidueNo number of residue
 | 
|---|
| 203 |  */
 | 
|---|
| 204 | void PdbParser::saveLine(ostream* file, const atom* currentAtom, const char *name, const int AtomNo, const int ResidueNo) {
 | 
|---|
| 205 |   *file << "ATOM ";
 | 
|---|
| 206 |   *file << setw(6) << AtomNo; /* atom serial number */
 | 
|---|
| 207 |   *file << setw(1) << " ";
 | 
|---|
| 208 |   *file << setfill(' ') << left << setw(4) << name << right;  /* atom name */
 | 
|---|
| 209 |   *file << setw(1) << " ";
 | 
|---|
| 210 |   *file << setfill(' ') << setw(3) << ((currentAtom->getMolecule() != NULL) ? currentAtom->getMolecule()->getName().substr(0,3) : "-");  /* residue name */
 | 
|---|
| 211 |   *file << setw(1) << " ";
 | 
|---|
| 212 |   *file << setfill(' ') << setw(1) << (char)('a'+(unsigned char)(AtomNo % 26)); /* letter for chain */
 | 
|---|
| 213 |   *file << setw(4) << ResidueNo; /* residue sequence number */
 | 
|---|
| 214 |   *file << setw(4) << "    ";
 | 
|---|
| 215 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
| 216 |     *file << setw(8) << setprecision(3) << showpoint << currentAtom->at(i); /* positional coordinate in Angstroem */
 | 
|---|
| 217 |   }
 | 
|---|
| 218 |   *file << setw(6) << setprecision(2) << showpoint << (double)currentAtom->getType()->getValence(); /* occupancy */
 | 
|---|
| 219 |   *file << setw(6) << setprecision(2) << showpoint << (double)currentAtom->getType()->getNoValenceOrbitals(); /* temperature factor */
 | 
|---|
| 220 |   *file << noshowpoint;
 | 
|---|
| 221 |   *file << setw(6) << "      ";
 | 
|---|
| 222 |   *file << setw(4) << "0";
 | 
|---|
| 223 |   *file << setfill(' ') << setw(2) << currentAtom->getType()->getSymbol();
 | 
|---|
| 224 |   *file << setw(2) << "0";
 | 
|---|
| 225 | 
 | 
|---|
| 226 |   *file << endl;
 | 
|---|
| 227 | }
 | 
|---|
| 228 | 
 | 
|---|
| 229 | /**
 | 
|---|
| 230 |  * Writes the neighbor information of one atom to the provided stream.
 | 
|---|
| 231 |  *
 | 
|---|
| 232 |  * \param *file  where to write neighbor information to
 | 
|---|
| 233 |  * \param MaxnumberOfNeighbors of neighbors
 | 
|---|
| 234 |  * \param *currentAtom to the atom of which to take the neighbor information
 | 
|---|
| 235 |  */
 | 
|---|
| 236 | void PdbParser::writeNeighbors(ostream* file, int MaxnumberOfNeighbors, atom* currentAtom) {
 | 
|---|
| 237 |   if (!currentAtom->ListOfBonds.empty()) {
 | 
|---|
| 238 |     *file << "CONECT";
 | 
|---|
| 239 |     *file << setw(5) << getSerial(currentAtom->getId());
 | 
|---|
| 240 |     int MaxNo = 0;
 | 
|---|
| 241 |     for(BondList::iterator currentBond = currentAtom->ListOfBonds.begin(); currentBond != currentAtom->ListOfBonds.end(); ++currentBond) {
 | 
|---|
| 242 |       if (MaxNo < MaxnumberOfNeighbors) {
 | 
|---|
| 243 |         *file << setw(5) << getSerial((*currentBond)->GetOtherAtom(currentAtom)->getId());
 | 
|---|
| 244 |       }
 | 
|---|
| 245 |       MaxNo++;
 | 
|---|
| 246 |     }
 | 
|---|
| 247 |     *file << endl;
 | 
|---|
| 248 |   }
 | 
|---|
| 249 | }
 | 
|---|
| 250 | 
 | 
|---|
| 251 | 
 | 
|---|
| 252 | /** Retrieves a value from PdbParser::atomIdMap.
 | 
|---|
| 253 |  * \param atomid key
 | 
|---|
| 254 |  * \return value
 | 
|---|
| 255 |  */
 | 
|---|
| 256 | size_t PdbParser::getSerial(const size_t atomid) const
 | 
|---|
| 257 | {
 | 
|---|
| 258 |   ConvertTo<size_t> toSize_t;
 | 
|---|
| 259 |   ASSERT(additionalAtomData.find(atomid) != additionalAtomData.end(),
 | 
|---|
| 260 |       "PdbParser::getSerial: atomid "+toString(atomid)+" not present in Map.");
 | 
|---|
| 261 |   const PdbAtomInfoContainer &atomInfo = additionalAtomData.at(atomid);
 | 
|---|
| 262 | 
 | 
|---|
| 263 |   return toSize_t(atomInfo.get(PdbKey::serial));
 | 
|---|
| 264 | }
 | 
|---|
| 265 | 
 | 
|---|
| 266 | /** Retrieves a value from PdbParser::atomIdMap.
 | 
|---|
| 267 |  * \param atomid key
 | 
|---|
| 268 |  * \return value
 | 
|---|
| 269 |  */
 | 
|---|
| 270 | size_t PdbParser::getAtomId(const size_t atomid) const
 | 
|---|
| 271 | {
 | 
|---|
| 272 |   ASSERT(atomIdMap.find(atomid) != atomIdMap.end(), "PdbParser::getAtomId: atomid not present in Map.");
 | 
|---|
| 273 |   return (atomIdMap.find(atomid)->second);
 | 
|---|
| 274 | }
 | 
|---|
| 275 | 
 | 
|---|
| 276 | /** Sets an entry in PdbParser::atomIdMap.
 | 
|---|
| 277 |  * \param localatomid key
 | 
|---|
| 278 |  * \param atomid value
 | 
|---|
| 279 |  * \return true - key not present, false - value present
 | 
|---|
| 280 |  */
 | 
|---|
| 281 | void PdbParser::setAtomId(const size_t localatomid, const size_t atomid)
 | 
|---|
| 282 | {
 | 
|---|
| 283 |   pair<std::map<size_t,size_t>::iterator, bool > inserter;
 | 
|---|
| 284 |   inserter = atomIdMap.insert( make_pair(localatomid, atomid) );
 | 
|---|
| 285 |   ASSERT(inserter.second, "PdbParser::setAtomId: atomId already present in Map.");
 | 
|---|
| 286 | }
 | 
|---|
| 287 | 
 | 
|---|
| 288 | /** Parse an ATOM line from a PDB file.
 | 
|---|
| 289 |  *
 | 
|---|
| 290 |  * Reads one data line of a pdstatus file and interprets it according to the
 | 
|---|
| 291 |  * specifications of the PDB 3.2 format: http://www.wwpdb.org/docs.html
 | 
|---|
| 292 |  *
 | 
|---|
| 293 |  *  A new atom is created and filled with available information, non-
 | 
|---|
| 294 |  *  standard information is placed in additionalAtomData at the atom's id.
 | 
|---|
| 295 |  *
 | 
|---|
| 296 |  * \param line to parse as an atom
 | 
|---|
| 297 |  * \param newmol molecule to add parsed atoms to
 | 
|---|
| 298 |  */
 | 
|---|
| 299 | void PdbParser::readAtomDataLine(std::string &line, molecule *newmol = NULL) {
 | 
|---|
| 300 |   vector<string>::iterator it;
 | 
|---|
| 301 |   stringstream lineStream;
 | 
|---|
| 302 |   atom* newAtom = World::getInstance().createAtom();
 | 
|---|
| 303 |   additionalAtomData[newAtom->getId()] = *(new PdbAtomInfoContainer);
 | 
|---|
| 304 |   PdbAtomInfoContainer &atomInfo = additionalAtomData[newAtom->getId()];
 | 
|---|
| 305 |   string word;
 | 
|---|
| 306 |   ConvertTo<size_t> toSize_t;
 | 
|---|
| 307 |   double tmp;
 | 
|---|
| 308 | 
 | 
|---|
| 309 |   lineStream << line;
 | 
|---|
| 310 |   atomInfo.set(PdbKey::serial, line.substr(6,5));
 | 
|---|
| 311 |   std::pair< std::set<size_t>::const_iterator, bool> Inserter =
 | 
|---|
| 312 |     SerialSet.insert(toSize_t(atomInfo.get(PdbKey::serial)));
 | 
|---|
| 313 |   ASSERT(Inserter.second,
 | 
|---|
| 314 |       "PdbParser::readAtomDataLine() - ATOM contains entry with serial "
 | 
|---|
| 315 |       +atomInfo.get(PdbKey::serial)+" already present!");
 | 
|---|
| 316 |   // assign hightest+1 instead, but then beware of CONECT entries! Another map needed!
 | 
|---|
| 317 | //  if (!Inserter.second) {
 | 
|---|
| 318 | //    const size_t id = (*SerialSet.rbegin())+1;
 | 
|---|
| 319 | //    SerialSet.insert(id);
 | 
|---|
| 320 | //    atomInfo.set(PdbKey::serial, toString(id));
 | 
|---|
| 321 | //    DoeLog(2) && (eLog() << Verbose(2)
 | 
|---|
| 322 | //        << "Serial " << atomInfo.get(PdbKey::serial) << " already present, "
 | 
|---|
| 323 | //        << "assigning " << toString(id) << " instead." << std::endl);
 | 
|---|
| 324 | //  }
 | 
|---|
| 325 | 
 | 
|---|
| 326 |   // check whether serial exists, if so, assign next available
 | 
|---|
| 327 | 
 | 
|---|
| 328 | //  DoLog(2) && (Log() << Verbose(2) << "Split line:"
 | 
|---|
| 329 | //      << line.substr(6,5) << "|"
 | 
|---|
| 330 | //      << line.substr(12,4) << "|"
 | 
|---|
| 331 | //      << line.substr(16,1) << "|"
 | 
|---|
| 332 | //      << line.substr(17,3) << "|"
 | 
|---|
| 333 | //      << line.substr(21,1) << "|"
 | 
|---|
| 334 | //      << line.substr(22,4) << "|"
 | 
|---|
| 335 | //      << line.substr(26,1) << "|"
 | 
|---|
| 336 | //      << line.substr(30,8) << "|"
 | 
|---|
| 337 | //      << line.substr(38,8) << "|"
 | 
|---|
| 338 | //      << line.substr(46,8) << "|"
 | 
|---|
| 339 | //      << line.substr(54,6) << "|"
 | 
|---|
| 340 | //      << line.substr(60,6) << "|"
 | 
|---|
| 341 | //      << line.substr(76,2) << "|"
 | 
|---|
| 342 | //      << line.substr(78,2) << std::endl);
 | 
|---|
| 343 | 
 | 
|---|
| 344 |   setAtomId(toSize_t(atomInfo.get(PdbKey::serial)), newAtom->getId());
 | 
|---|
| 345 |   atomInfo.set(PdbKey::name, line.substr(12,4));
 | 
|---|
| 346 |   atomInfo.set(PdbKey::altloc, line.substr(16,1));
 | 
|---|
| 347 |   atomInfo.set(PdbKey::resName, line.substr(17,3));
 | 
|---|
| 348 |   atomInfo.set(PdbKey::chainID, line.substr(21,1));
 | 
|---|
| 349 |   atomInfo.set(PdbKey::resSeq, line.substr(22,4));
 | 
|---|
| 350 |   atomInfo.set(PdbKey::iCode, line.substr(26,1));
 | 
|---|
| 351 |   PdbAtomInfoContainer::ScanKey(tmp, line.substr(30,8));
 | 
|---|
| 352 |   newAtom->set(0, tmp);
 | 
|---|
| 353 |   PdbAtomInfoContainer::ScanKey(tmp, line.substr(38,8));
 | 
|---|
| 354 |   newAtom->set(1, tmp);
 | 
|---|
| 355 |   PdbAtomInfoContainer::ScanKey(tmp, line.substr(46,8));
 | 
|---|
| 356 |   newAtom->set(2, tmp);
 | 
|---|
| 357 |   atomInfo.set(PdbKey::occupancy, line.substr(54,6));
 | 
|---|
| 358 |   atomInfo.set(PdbKey::tempFactor, line.substr(60,6));
 | 
|---|
| 359 |   atomInfo.set(PdbKey::charge, line.substr(78,2));
 | 
|---|
| 360 |   PdbAtomInfoContainer::ScanKey(word, line.substr(76,2));
 | 
|---|
| 361 |   newAtom->setType(World::getInstance().getPeriode()->FindElement(word));
 | 
|---|
| 362 | 
 | 
|---|
| 363 |   if (newmol != NULL)
 | 
|---|
| 364 |     newmol->AddAtom(newAtom);
 | 
|---|
| 365 | 
 | 
|---|
| 366 | //  printAtomInfo(newAtom);
 | 
|---|
| 367 | }
 | 
|---|
| 368 | 
 | 
|---|
| 369 | /** Prints all PDB-specific information known about an atom.
 | 
|---|
| 370 |  *
 | 
|---|
| 371 |  */
 | 
|---|
| 372 | void PdbParser::printAtomInfo(const atom * const newAtom) const
 | 
|---|
| 373 | {
 | 
|---|
| 374 |   const PdbAtomInfoContainer &atomInfo = additionalAtomData.at(newAtom->getId()); // operator[] const does not exist
 | 
|---|
| 375 | 
 | 
|---|
| 376 |   DoLog(1) && (Log() << Verbose(1) << "We know about atom " << newAtom->getId() << ":" << std::endl);
 | 
|---|
| 377 |   DoLog(1) && (Log() << Verbose(1) << "\tserial is " << atomInfo.get(PdbKey::serial) << std::endl);
 | 
|---|
| 378 |   DoLog(1) && (Log() << Verbose(1) << "\tname is " << atomInfo.get(PdbKey::name) << std::endl);
 | 
|---|
| 379 |   DoLog(1) && (Log() << Verbose(1) << "\taltloc is " << atomInfo.get(PdbKey::altloc) << std::endl);
 | 
|---|
| 380 |   DoLog(1) && (Log() << Verbose(1) << "\tresName is " << atomInfo.get(PdbKey::resName) << std::endl);
 | 
|---|
| 381 |   DoLog(1) && (Log() << Verbose(1) << "\tchainID is " << atomInfo.get(PdbKey::chainID) << std::endl);
 | 
|---|
| 382 |   DoLog(1) && (Log() << Verbose(1) << "\tresSeq is " << atomInfo.get(PdbKey::resSeq) << std::endl);
 | 
|---|
| 383 |   DoLog(1) && (Log() << Verbose(1) << "\tiCode is " << atomInfo.get(PdbKey::iCode) << std::endl);
 | 
|---|
| 384 |   DoLog(1) && (Log() << Verbose(1) << "\tx is " << newAtom->getPosition() << std::endl);
 | 
|---|
| 385 |   DoLog(1) && (Log() << Verbose(1) << "\toccupancy is " << atomInfo.get(PdbKey::occupancy) << std::endl);
 | 
|---|
| 386 |   DoLog(1) && (Log() << Verbose(1) << "\ttempFactor is " << atomInfo.get(PdbKey::tempFactor) << std::endl);
 | 
|---|
| 387 |   DoLog(1) && (Log() << Verbose(1) << "\telement is '" << *(newAtom->getType()) << "'" << std::endl);
 | 
|---|
| 388 |   DoLog(1) && (Log() << Verbose(1) << "\tcharge is " << atomInfo.get(PdbKey::charge) << std::endl);
 | 
|---|
| 389 | }
 | 
|---|
| 390 | 
 | 
|---|
| 391 | /**
 | 
|---|
| 392 |  * Reads neighbor information for one atom from the input.
 | 
|---|
| 393 |  *
 | 
|---|
| 394 |  * \param line to parse as an atom
 | 
|---|
| 395 |  */
 | 
|---|
| 396 | void PdbParser::readNeighbors(std::string &line)
 | 
|---|
| 397 | {
 | 
|---|
| 398 |   const size_t length = line.length();
 | 
|---|
| 399 |   std::list<size_t> ListOfNeighbors;
 | 
|---|
| 400 |   ConvertTo<size_t> toSize_t;
 | 
|---|
| 401 | 
 | 
|---|
| 402 |   // obtain neighbours
 | 
|---|
| 403 |   // show split line for debugging
 | 
|---|
| 404 |   string output;
 | 
|---|
| 405 |   ASSERT(length >=16,
 | 
|---|
| 406 |       "PdbParser::readNeighbors() - CONECT entry has not enough entries: "+line+"!");
 | 
|---|
| 407 | //  output = "Split line:|";
 | 
|---|
| 408 | //  output += line.substr(6,5) + "|";
 | 
|---|
| 409 |   const size_t id = toSize_t(line.substr(6,5));
 | 
|---|
| 410 |   for (size_t index = 11; index <= 26; index+=5) {
 | 
|---|
| 411 |     if (index+5 <= length) {
 | 
|---|
| 412 | //      output += line.substr(index,5) + "|";
 | 
|---|
| 413 |       const size_t otherid = toSize_t(line.substr(index,5));
 | 
|---|
| 414 |       ListOfNeighbors.push_back(otherid);
 | 
|---|
| 415 |     } else  {
 | 
|---|
| 416 |       break;
 | 
|---|
| 417 |     }
 | 
|---|
| 418 |   }
 | 
|---|
| 419 | //  DoLog(2) && (Log() << Verbose(2) << output << std::endl);
 | 
|---|
| 420 | 
 | 
|---|
| 421 |   // add neighbours
 | 
|---|
| 422 |   atom *_atom = World::getInstance().getAtom(AtomById(getAtomId(id)));
 | 
|---|
| 423 |   for (std::list<size_t>::const_iterator iter = ListOfNeighbors.begin();
 | 
|---|
| 424 |       iter != ListOfNeighbors.end();
 | 
|---|
| 425 |       ++iter) {
 | 
|---|
| 426 | //    DoLog(1) && (Log() << Verbose(1) << "Adding Bond (" << getAtomId(id) << "," << getAtomId(*iter) << ")" << std::endl);
 | 
|---|
| 427 |     atom * const _Otheratom = World::getInstance().getAtom(AtomById(getAtomId(*iter)));
 | 
|---|
| 428 |     _atom->addBond(_Otheratom);
 | 
|---|
| 429 |   }
 | 
|---|
| 430 | }
 | 
|---|
| 431 | 
 | 
|---|
| 432 | /**
 | 
|---|
| 433 |  * Replaces atom IDs read from the file by the corresponding world IDs. All IDs
 | 
|---|
| 434 |  * IDs of the input string will be replaced; expected separating characters are
 | 
|---|
| 435 |  * "-" and ",".
 | 
|---|
| 436 |  *
 | 
|---|
| 437 |  * \param string in which atom IDs should be adapted
 | 
|---|
| 438 |  *
 | 
|---|
| 439 |  * \return input string with modified atom IDs
 | 
|---|
| 440 |  */
 | 
|---|
| 441 | //string PdbParser::adaptIdDependentDataString(string data) {
 | 
|---|
| 442 | //  // there might be no IDs
 | 
|---|
| 443 | //  if (data == "-") {
 | 
|---|
| 444 | //    return "-";
 | 
|---|
| 445 | //  }
 | 
|---|
| 446 | //
 | 
|---|
| 447 | //  char separator;
 | 
|---|
| 448 | //  int id;
 | 
|---|
| 449 | //  stringstream line, result;
 | 
|---|
| 450 | //  line << data;
 | 
|---|
| 451 | //
 | 
|---|
| 452 | //  line >> id;
 | 
|---|
| 453 | //  result << atomIdMap[id];
 | 
|---|
| 454 | //  while (line.good()) {
 | 
|---|
| 455 | //    line >> separator >> id;
 | 
|---|
| 456 | //    result << separator << atomIdMap[id];
 | 
|---|
| 457 | //  }
 | 
|---|
| 458 | //
 | 
|---|
| 459 | //  return result.str();
 | 
|---|
| 460 | //  return "";
 | 
|---|
| 461 | //}
 | 
|---|
| 462 | 
 | 
|---|
| 463 | 
 | 
|---|
| 464 | bool PdbParser::operator==(const PdbParser& b) const
 | 
|---|
| 465 | {
 | 
|---|
| 466 |   bool status = true;
 | 
|---|
| 467 |   World::AtomComposite atoms = World::getInstance().getAllAtoms();
 | 
|---|
| 468 |   for (World::AtomComposite::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
| 469 |     if ((additionalAtomData.find((*iter)->getId()) != additionalAtomData.end())
 | 
|---|
| 470 |         && (b.additionalAtomData.find((*iter)->getId()) != b.additionalAtomData.end())) {
 | 
|---|
| 471 |       const PdbAtomInfoContainer &atomInfo = additionalAtomData.at((*iter)->getId());
 | 
|---|
| 472 |       const PdbAtomInfoContainer &OtheratomInfo = b.additionalAtomData.at((*iter)->getId());
 | 
|---|
| 473 | 
 | 
|---|
| 474 |       status = status && (atomInfo.get(PdbKey::serial) == OtheratomInfo.get(PdbKey::serial));
 | 
|---|
| 475 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in serials!" << std::endl);
 | 
|---|
| 476 |       status = status && (atomInfo.get(PdbKey::name) == OtheratomInfo.get(PdbKey::name));
 | 
|---|
| 477 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in names!" << std::endl);
 | 
|---|
| 478 |       status = status && (atomInfo.get(PdbKey::altloc) == OtheratomInfo.get(PdbKey::altloc));
 | 
|---|
| 479 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in altlocs!" << std::endl);
 | 
|---|
| 480 |       status = status && (atomInfo.get(PdbKey::resName) == OtheratomInfo.get(PdbKey::resName));
 | 
|---|
| 481 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in resNames!" << std::endl);
 | 
|---|
| 482 |       status = status && (atomInfo.get(PdbKey::chainID) == OtheratomInfo.get(PdbKey::chainID));
 | 
|---|
| 483 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in chainIDs!" << std::endl);
 | 
|---|
| 484 |       status = status && (atomInfo.get(PdbKey::resSeq) == OtheratomInfo.get(PdbKey::resSeq));
 | 
|---|
| 485 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in resSeqs!" << std::endl);
 | 
|---|
| 486 |       status = status && (atomInfo.get(PdbKey::iCode) == OtheratomInfo.get(PdbKey::iCode));
 | 
|---|
| 487 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in iCodes!" << std::endl);
 | 
|---|
| 488 |       status = status && (atomInfo.get(PdbKey::occupancy) == OtheratomInfo.get(PdbKey::occupancy));
 | 
|---|
| 489 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in occupancies!" << std::endl);
 | 
|---|
| 490 |       status = status && (atomInfo.get(PdbKey::tempFactor) == OtheratomInfo.get(PdbKey::tempFactor));
 | 
|---|
| 491 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in tempFactors!" << std::endl);
 | 
|---|
| 492 |       status = status && (atomInfo.get(PdbKey::charge) == OtheratomInfo.get(PdbKey::charge));
 | 
|---|
| 493 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in charges!" << std::endl);
 | 
|---|
| 494 |     }
 | 
|---|
| 495 |   }
 | 
|---|
| 496 | 
 | 
|---|
| 497 |   return status;
 | 
|---|
| 498 | }
 | 
|---|
| 499 | 
 | 
|---|