| [3ae731] | 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"
 | 
|---|
| [4fbca9c] | 24 | #include "Helpers/toString.hpp"
 | 
|---|
| [3ae731] | 25 | #include "Helpers/Verbose.hpp"
 | 
|---|
 | 26 | #include "World.hpp"
 | 
|---|
 | 27 | #include "atom.hpp"
 | 
|---|
 | 28 | #include "bond.hpp"
 | 
|---|
| [bb6193] | 29 | #include "element.hpp"
 | 
|---|
 | 30 | #include "molecule.hpp"
 | 
|---|
| [3ae731] | 31 | #include "periodentafel.hpp"
 | 
|---|
 | 32 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| [4fbca9c] | 33 | #include "Parser/PdbParser.hpp"
 | 
|---|
| [bb6193] | 34 | 
 | 
|---|
| [3ae731] | 35 | #include <map>
 | 
|---|
 | 36 | #include <vector>
 | 
|---|
 | 37 | 
 | 
|---|
| [bb6193] | 38 | #include <iostream>
 | 
|---|
 | 39 | #include <iomanip>
 | 
|---|
| [3ae731] | 40 | 
 | 
|---|
 | 41 | using namespace std;
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 | /**
 | 
|---|
 | 44 |  * Constructor.
 | 
|---|
 | 45 |  */
 | 
|---|
 | 46 | PdbParser::PdbParser() {
 | 
|---|
| [4fbca9c] | 47 |   knownTokens["ATOM"] = PdbKey::Atom;
 | 
|---|
| [16462f] | 48 |   knownTokens["HETATM"] = PdbKey::Atom;
 | 
|---|
| [4fbca9c] | 49 |   knownTokens["TER"] = PdbKey::Filler;
 | 
|---|
 | 50 |   knownTokens["END"] = PdbKey::EndOfFile;
 | 
|---|
 | 51 |   knownTokens["CONECT"] = PdbKey::Connect;
 | 
|---|
 | 52 |   knownTokens["REMARK"] = PdbKey::Remark;
 | 
|---|
 | 53 |   knownTokens[""] = PdbKey::EndOfFile;
 | 
|---|
| [16462f] | 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;
 | 
|---|
| [3ae731] | 59 | }
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 | /**
 | 
|---|
 | 62 |  * Destructor.
 | 
|---|
 | 63 |  */
 | 
|---|
 | 64 | PdbParser::~PdbParser() {
 | 
|---|
 | 65 |   additionalAtomData.clear();
 | 
|---|
 | 66 |   atomIdMap.clear();
 | 
|---|
| [4fbca9c] | 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;
 | 
|---|
| [3ae731] | 96 | }
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 | /**
 | 
|---|
| [4fbca9c] | 99 |  * Loads atoms from a PDB-formatted file.
 | 
|---|
| [3ae731] | 100 |  *
 | 
|---|
| [4fbca9c] | 101 |  * \param PDB file
 | 
|---|
| [3ae731] | 102 |  */
 | 
|---|
 | 103 | void PdbParser::load(istream* file) {
 | 
|---|
| [4fbca9c] | 104 |   string line;
 | 
|---|
 | 105 |   size_t linecount  = 0;
 | 
|---|
 | 106 |   enum PdbKey::KnownTokens token;
 | 
|---|
 | 107 | 
 | 
|---|
| [16462f] | 108 |   // reset atomIdMap for this file (to correctly parse CONECT entries)
 | 
|---|
 | 109 |   atomIdMap.clear();
 | 
|---|
 | 110 | 
 | 
|---|
| [4fbca9c] | 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 |   }
 | 
|---|
| [3ae731] | 144 | }
 | 
|---|
 | 145 | 
 | 
|---|
 | 146 | /**
 | 
|---|
| [73916f] | 147 |  * Saves the \a atoms into as a PDB file.
 | 
|---|
| [3ae731] | 148 |  *
 | 
|---|
 | 149 |  * \param file where to save the state
 | 
|---|
| [73916f] | 150 |  * \param atoms atoms to store
 | 
|---|
| [3ae731] | 151 |  */
 | 
|---|
| [73916f] | 152 | void PdbParser::save(ostream* file, const std::vector<atom *> &AtomList)
 | 
|---|
 | 153 | {
 | 
|---|
| [bb6193] | 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 |   }
 | 
|---|
| [3ae731] | 168 | 
 | 
|---|
| [16462f] | 169 |   // we distribute serials, hence clear map beforehand
 | 
|---|
| [4fbca9c] | 170 |   atomIdMap.clear();
 | 
|---|
| [bb6193] | 171 |   {
 | 
|---|
| [16462f] | 172 |     std::map<size_t,size_t> MolIdMap;
 | 
|---|
 | 173 |     size_t MolNo = 1;  // residue number starts at 1 in pdb
 | 
|---|
| [73916f] | 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 |       }
 | 
|---|
| [16462f] | 179 |     }
 | 
|---|
 | 180 |     const size_t MaxMol = MolNo;
 | 
|---|
| [bb6193] | 181 | 
 | 
|---|
| [16462f] | 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);
 | 
|---|
| [bb6193] | 186 |     char name[MAXSTRINGSIZE];
 | 
|---|
| [16462f] | 187 |     std::string ResidueName;
 | 
|---|
| [bb6193] | 188 | 
 | 
|---|
 | 189 |     // write ATOMs
 | 
|---|
 | 190 |     int AtomNo = 1; // serial number starts at 1 in pdb
 | 
|---|
| [73916f] | 191 |     for (vector<atom *>::const_iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) {
 | 
|---|
| [93fd43e] | 192 |       PdbAtomInfoContainer &atomInfo = getadditionalAtomData(*atomIt);
 | 
|---|
| [16462f] | 193 |       // gather info about residue
 | 
|---|
| [bb6193] | 194 |       const molecule *mol = (*atomIt)->getMolecule();
 | 
|---|
| [16462f] | 195 |       if (mol == NULL) {
 | 
|---|
 | 196 |         MolNo = 0;
 | 
|---|
 | 197 |         atomInfo.set(PdbKey::resSeq, "0");
 | 
|---|
| [bb6193] | 198 |       } else {
 | 
|---|
| [16462f] | 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);
 | 
|---|
| [bb6193] | 212 |       }
 | 
|---|
| [16462f] | 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);
 | 
|---|
| [bb6193] | 227 |       AtomNo++;
 | 
|---|
 | 228 |     }
 | 
|---|
| [16462f] | 229 |     for (size_t i = 0; i < MaxMol; ++i)
 | 
|---|
 | 230 |       delete elementNo[i];
 | 
|---|
 | 231 |     delete elementNo;
 | 
|---|
| [3ae731] | 232 | 
 | 
|---|
| [bb6193] | 233 |     // write CONECTs
 | 
|---|
| [73916f] | 234 |     for (vector<atom *>::const_iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) {
 | 
|---|
| [bb6193] | 235 |       writeNeighbors(file, 4, *atomIt);
 | 
|---|
 | 236 |     }
 | 
|---|
| [3ae731] | 237 |   }
 | 
|---|
 | 238 | 
 | 
|---|
| [bb6193] | 239 |   // END
 | 
|---|
 | 240 |   *file << "END" << endl;
 | 
|---|
| [3ae731] | 241 | }
 | 
|---|
 | 242 | 
 | 
|---|
| [93fd43e] | 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 | 
 | 
|---|
| [3ae731] | 264 | /**
 | 
|---|
| [4fbca9c] | 265 |  * Writes one line of PDB-formatted data to the provided stream.
 | 
|---|
| [3ae731] | 266 |  *
 | 
|---|
 | 267 |  * \param stream where to write the line to
 | 
|---|
| [bb6193] | 268 |  * \param *currentAtom the atom of which information should be written
 | 
|---|
 | 269 |  * \param AtomNo serial number of atom
 | 
|---|
| [16462f] | 270 |  * \param *name name of atom, i.e. H01
 | 
|---|
 | 271 |  * \param ResidueName Name of molecule
 | 
|---|
| [bb6193] | 272 |  * \param ResidueNo number of residue
 | 
|---|
| [3ae731] | 273 |  */
 | 
|---|
| [16462f] | 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();
 | 
|---|
| [3ae731] | 310 |   }
 | 
|---|
| [16462f] | 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 */
 | 
|---|
| [3ae731] | 320 | 
 | 
|---|
 | 321 |   *file << endl;
 | 
|---|
 | 322 | }
 | 
|---|
 | 323 | 
 | 
|---|
 | 324 | /**
 | 
|---|
 | 325 |  * Writes the neighbor information of one atom to the provided stream.
 | 
|---|
 | 326 |  *
 | 
|---|
| [bb6193] | 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
 | 
|---|
| [3ae731] | 330 |  */
 | 
|---|
| [bb6193] | 331 | void PdbParser::writeNeighbors(ostream* file, int MaxnumberOfNeighbors, atom* currentAtom) {
 | 
|---|
| [4c1230] | 332 |   int MaxNo = MaxnumberOfNeighbors;
 | 
|---|
| [bb6193] | 333 |   if (!currentAtom->ListOfBonds.empty()) {
 | 
|---|
 | 334 |     for(BondList::iterator currentBond = currentAtom->ListOfBonds.begin(); currentBond != currentAtom->ListOfBonds.end(); ++currentBond) {
 | 
|---|
| [4c1230] | 335 |       if (MaxNo >= MaxnumberOfNeighbors) {
 | 
|---|
 | 336 |         *file << "CONECT";
 | 
|---|
| [16462f] | 337 |         *file << setw(5) << getSerial(currentAtom->getId());
 | 
|---|
| [4c1230] | 338 |         MaxNo = 0;
 | 
|---|
| [bb6193] | 339 |       }
 | 
|---|
| [16462f] | 340 |       *file << setw(5) << getSerial((*currentBond)->GetOtherAtom(currentAtom)->getId());
 | 
|---|
| [bb6193] | 341 |       MaxNo++;
 | 
|---|
| [4c1230] | 342 |       if (MaxNo == MaxnumberOfNeighbors)
 | 
|---|
 | 343 |         *file << "\n";
 | 
|---|
| [3ae731] | 344 |     }
 | 
|---|
| [4c1230] | 345 |     if (MaxNo != MaxnumberOfNeighbors)
 | 
|---|
 | 346 |       *file << "\n";
 | 
|---|
| [3ae731] | 347 |   }
 | 
|---|
 | 348 | }
 | 
|---|
 | 349 | 
 | 
|---|
| [21585f] | 350 | /** Retrieves a value from PdbParser::atomIdMap.
 | 
|---|
 | 351 |  * \param atomid key
 | 
|---|
 | 352 |  * \return value
 | 
|---|
 | 353 |  */
 | 
|---|
| [4fbca9c] | 354 | size_t PdbParser::getSerial(const size_t atomid) const
 | 
|---|
| [21585f] | 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 |  */
 | 
|---|
| [16462f] | 365 | void PdbParser::setSerial(const size_t localatomid, const size_t atomid)
 | 
|---|
| [21585f] | 366 | {
 | 
|---|
| [4fbca9c] | 367 |   pair<std::map<size_t,size_t>::iterator, bool > inserter;
 | 
|---|
| [16462f] | 368 | //  DoLog(1) && (Log() << Verbose(1) << "PdbParser::setAtomId() - Inserting ("
 | 
|---|
 | 369 | //      << localatomid << " -> " << atomid << ")." << std::endl);
 | 
|---|
| [4fbca9c] | 370 |   inserter = atomIdMap.insert( make_pair(localatomid, atomid) );
 | 
|---|
| [21585f] | 371 |   ASSERT(inserter.second, "PdbParser::setAtomId: atomId already present in Map.");
 | 
|---|
 | 372 | }
 | 
|---|
 | 373 | 
 | 
|---|
| [4fbca9c] | 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.
 | 
|---|
| [3ae731] | 381 |  *
 | 
|---|
 | 382 |  * \param line to parse as an atom
 | 
|---|
| [4fbca9c] | 383 |  * \param newmol molecule to add parsed atoms to
 | 
|---|
| [3ae731] | 384 |  */
 | 
|---|
| [4fbca9c] | 385 | void PdbParser::readAtomDataLine(std::string &line, molecule *newmol = NULL) {
 | 
|---|
 | 386 |   vector<string>::iterator it;
 | 
|---|
 | 387 |   stringstream lineStream;
 | 
|---|
 | 388 |   atom* newAtom = World::getInstance().createAtom();
 | 
|---|
| [93fd43e] | 389 |   PdbAtomInfoContainer &atomInfo = getadditionalAtomData(newAtom);
 | 
|---|
| [4fbca9c] | 390 |   string word;
 | 
|---|
 | 391 |   ConvertTo<size_t> toSize_t;
 | 
|---|
 | 392 |   double tmp;
 | 
|---|
 | 393 | 
 | 
|---|
 | 394 |   lineStream << line;
 | 
|---|
| [16462f] | 395 |   atomInfo.set(PdbKey::token, line.substr(0,6));
 | 
|---|
| [4fbca9c] | 396 |   atomInfo.set(PdbKey::serial, line.substr(6,5));
 | 
|---|
 | 397 |   std::pair< std::set<size_t>::const_iterator, bool> Inserter =
 | 
|---|
| [16462f] | 398 |     SerialSet.insert(toSize_t(atomInfo.get<std::string>(PdbKey::serial)));
 | 
|---|
| [4fbca9c] | 399 |   ASSERT(Inserter.second,
 | 
|---|
 | 400 |       "PdbParser::readAtomDataLine() - ATOM contains entry with serial "
 | 
|---|
| [16462f] | 401 |       +atomInfo.get<std::string>(PdbKey::serial)+" already present!");
 | 
|---|
| [4fbca9c] | 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)
 | 
|---|
| [16462f] | 408 | //        << "Serial " << atomInfo.get<std::string>(PdbKey::serial) << " already present, "
 | 
|---|
| [4fbca9c] | 409 | //        << "assigning " << toString(id) << " instead." << std::endl);
 | 
|---|
| [bb6193] | 410 | //  }
 | 
|---|
| [4fbca9c] | 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 | 
 | 
|---|
| [16462f] | 430 |   setSerial(toSize_t(atomInfo.get<std::string>(PdbKey::serial)), newAtom->getId());
 | 
|---|
| [4fbca9c] | 431 |   atomInfo.set(PdbKey::name, line.substr(12,4));
 | 
|---|
| [16462f] | 432 |   atomInfo.set(PdbKey::altLoc, line.substr(16,1));
 | 
|---|
| [4fbca9c] | 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));
 | 
|---|
| [16462f] | 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);
 | 
|---|
| [4fbca9c] | 452 | 
 | 
|---|
 | 453 |   if (newmol != NULL)
 | 
|---|
 | 454 |     newmol->AddAtom(newAtom);
 | 
|---|
 | 455 | 
 | 
|---|
 | 456 | //  printAtomInfo(newAtom);
 | 
|---|
| [3ae731] | 457 | }
 | 
|---|
 | 458 | 
 | 
|---|
| [4fbca9c] | 459 | /** Prints all PDB-specific information known about an atom.
 | 
|---|
| [3ae731] | 460 |  *
 | 
|---|
 | 461 |  */
 | 
|---|
| [4fbca9c] | 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);
 | 
|---|
| [16462f] | 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);
 | 
|---|
| [4fbca9c] | 480 |   DoLog(1) && (Log() << Verbose(1) << "\telement is '" << *(newAtom->getType()) << "'" << std::endl);
 | 
|---|
| [16462f] | 481 |   DoLog(1) && (Log() << Verbose(1) << "\tcharge is " << atomInfo.get<int>(PdbKey::charge) << std::endl);
 | 
|---|
| [3ae731] | 482 | }
 | 
|---|
 | 483 | 
 | 
|---|
 | 484 | /**
 | 
|---|
| [4fbca9c] | 485 |  * Reads neighbor information for one atom from the input.
 | 
|---|
 | 486 |  *
 | 
|---|
 | 487 |  * \param line to parse as an atom
 | 
|---|
| [3ae731] | 488 |  */
 | 
|---|
| [4fbca9c] | 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
 | 
|---|
| [16462f] | 515 |   atom *_atom = World::getInstance().getAtom(AtomById(getSerial(id)));
 | 
|---|
| [4fbca9c] | 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);
 | 
|---|
| [16462f] | 520 |     atom * const _Otheratom = World::getInstance().getAtom(AtomById(getSerial(*iter)));
 | 
|---|
| [4fbca9c] | 521 |     _atom->addBond(_Otheratom);
 | 
|---|
 | 522 |   }
 | 
|---|
| [3ae731] | 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 |  */
 | 
|---|
| [4fbca9c] | 534 | //string PdbParser::adaptIdDependentDataString(string data) {
 | 
|---|
| [bb6193] | 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();
 | 
|---|
| [4fbca9c] | 553 | //  return "";
 | 
|---|
 | 554 | //}
 | 
|---|
| [3ae731] | 555 | 
 | 
|---|
 | 556 | 
 | 
|---|
| [4fbca9c] | 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 | 
 | 
|---|
| [16462f] | 567 |       status = status && (atomInfo.get<std::string>(PdbKey::serial) == OtheratomInfo.get<std::string>(PdbKey::serial));
 | 
|---|
| [4fbca9c] | 568 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in serials!" << std::endl);
 | 
|---|
| [16462f] | 569 |       status = status && (atomInfo.get<std::string>(PdbKey::name) == OtheratomInfo.get<std::string>(PdbKey::name));
 | 
|---|
| [4fbca9c] | 570 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in names!" << std::endl);
 | 
|---|
| [16462f] | 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));
 | 
|---|
| [4fbca9c] | 574 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in resNames!" << std::endl);
 | 
|---|
| [16462f] | 575 |       status = status && (atomInfo.get<std::string>(PdbKey::chainID) == OtheratomInfo.get<std::string>(PdbKey::chainID));
 | 
|---|
| [4fbca9c] | 576 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in chainIDs!" << std::endl);
 | 
|---|
| [16462f] | 577 |       status = status && (atomInfo.get<std::string>(PdbKey::resSeq) == OtheratomInfo.get<std::string>(PdbKey::resSeq));
 | 
|---|
| [4fbca9c] | 578 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in resSeqs!" << std::endl);
 | 
|---|
| [16462f] | 579 |       status = status && (atomInfo.get<std::string>(PdbKey::iCode) == OtheratomInfo.get<std::string>(PdbKey::iCode));
 | 
|---|
| [4fbca9c] | 580 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in iCodes!" << std::endl);
 | 
|---|
| [16462f] | 581 |       status = status && (atomInfo.get<std::string>(PdbKey::occupancy) == OtheratomInfo.get<std::string>(PdbKey::occupancy));
 | 
|---|
| [4fbca9c] | 582 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in occupancies!" << std::endl);
 | 
|---|
| [16462f] | 583 |       status = status && (atomInfo.get<std::string>(PdbKey::tempFactor) == OtheratomInfo.get<std::string>(PdbKey::tempFactor));
 | 
|---|
| [4fbca9c] | 584 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in tempFactors!" << std::endl);
 | 
|---|
| [16462f] | 585 |       status = status && (atomInfo.get<std::string>(PdbKey::charge) == OtheratomInfo.get<std::string>(PdbKey::charge));
 | 
|---|
| [4fbca9c] | 586 |       if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in charges!" << std::endl);
 | 
|---|
 | 587 |     }
 | 
|---|
| [3ae731] | 588 |   }
 | 
|---|
 | 589 | 
 | 
|---|
| [4fbca9c] | 590 |   return status;
 | 
|---|
| [3ae731] | 591 | }
 | 
|---|
 | 592 | 
 | 
|---|