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 "CodePatterns/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include "CodePatterns/Assert.hpp"
|
---|
23 | #include "CodePatterns/Log.hpp"
|
---|
24 | #include "CodePatterns/toString.hpp"
|
---|
25 | #include "CodePatterns/Verbose.hpp"
|
---|
26 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
27 | #include "World.hpp"
|
---|
28 | #include "atom.hpp"
|
---|
29 | #include "bond.hpp"
|
---|
30 | #include "element.hpp"
|
---|
31 | #include "molecule.hpp"
|
---|
32 | #include "periodentafel.hpp"
|
---|
33 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
34 | #include "Parser/PdbParser.hpp"
|
---|
35 | #include "World.hpp"
|
---|
36 | #include "WorldTime.hpp"
|
---|
37 |
|
---|
38 | #include <map>
|
---|
39 | #include <vector>
|
---|
40 |
|
---|
41 | #include <iostream>
|
---|
42 | #include <iomanip>
|
---|
43 |
|
---|
44 | using namespace std;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Constructor.
|
---|
48 | */
|
---|
49 | PdbParser::PdbParser() {
|
---|
50 | knownTokens["ATOM"] = PdbKey::Atom;
|
---|
51 | knownTokens["HETATM"] = PdbKey::Atom;
|
---|
52 | knownTokens["TER"] = PdbKey::Filler;
|
---|
53 | knownTokens["END"] = PdbKey::EndOfTimestep;
|
---|
54 | knownTokens["CONECT"] = PdbKey::Connect;
|
---|
55 | knownTokens["REMARK"] = PdbKey::Remark;
|
---|
56 | knownTokens[""] = PdbKey::EndOfTimestep;
|
---|
57 |
|
---|
58 | // argh, why can't just PdbKey::X+(size_t)i
|
---|
59 | PositionEnumMap[0] = PdbKey::X;
|
---|
60 | PositionEnumMap[1] = PdbKey::Y;
|
---|
61 | PositionEnumMap[2] = PdbKey::Z;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Destructor.
|
---|
66 | */
|
---|
67 | PdbParser::~PdbParser() {
|
---|
68 | PdbAtomInfoContainer::clearknownDataKeys();
|
---|
69 | additionalAtomData.clear();
|
---|
70 | atomIdMap.clear();
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | /** Parses the initial word of the given \a line and returns the token type.
|
---|
75 | *
|
---|
76 | * @param line line to scan
|
---|
77 | * @return token type
|
---|
78 | */
|
---|
79 | enum PdbKey::KnownTokens PdbParser::getToken(string &line)
|
---|
80 | {
|
---|
81 | // look for first space
|
---|
82 | const size_t space_location = line.find(' ');
|
---|
83 | const size_t tab_location = line.find('\t');
|
---|
84 | size_t location = space_location < tab_location ? space_location : tab_location;
|
---|
85 | string token;
|
---|
86 | if (location != string::npos) {
|
---|
87 | //DoLog(1) && (Log() << Verbose(1) << "Found space at position " << space_location << std::endl);
|
---|
88 | token = line.substr(0,space_location);
|
---|
89 | } else {
|
---|
90 | token = line;
|
---|
91 | }
|
---|
92 |
|
---|
93 | //DoLog(1) && (Log() << Verbose(1) << "Token is " << token << std::endl);
|
---|
94 | if (knownTokens.count(token) == 0)
|
---|
95 | return PdbKey::NoToken;
|
---|
96 | else
|
---|
97 | return knownTokens[token];
|
---|
98 |
|
---|
99 | return PdbKey::NoToken;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Loads atoms from a PDB-formatted file.
|
---|
104 | *
|
---|
105 | * \param PDB file
|
---|
106 | */
|
---|
107 | void PdbParser::load(istream* file) {
|
---|
108 | string line;
|
---|
109 | size_t linecount = 0;
|
---|
110 | enum PdbKey::KnownTokens token;
|
---|
111 |
|
---|
112 | // reset atomIdMap for this file (to correctly parse CONECT entries)
|
---|
113 | atomIdMap.clear();
|
---|
114 |
|
---|
115 | bool NotEndOfFile = true;
|
---|
116 | molecule *newmol = World::getInstance().createMolecule();
|
---|
117 | newmol->ActiveFlag = true;
|
---|
118 | // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include
|
---|
119 | World::getInstance().getMolecules()->insert(newmol);
|
---|
120 | while (NotEndOfFile) {
|
---|
121 | bool NotEndOfTimestep = true;
|
---|
122 | while (NotEndOfTimestep) {
|
---|
123 | std::getline(*file, line, '\n');
|
---|
124 | // extract first token
|
---|
125 | token = getToken(line);
|
---|
126 | switch (token) {
|
---|
127 | case PdbKey::Atom:
|
---|
128 | LOG(3,"INFO: Parsing ATOM entry.");
|
---|
129 | readAtomDataLine(line, newmol);
|
---|
130 | break;
|
---|
131 | case PdbKey::Remark:
|
---|
132 | LOG(3,"INFO: Parsing REM entry.");
|
---|
133 | break;
|
---|
134 | case PdbKey::Connect:
|
---|
135 | LOG(3,"INFO: Parsing CONECT entry.");
|
---|
136 | readNeighbors(line);
|
---|
137 | break;
|
---|
138 | case PdbKey::Filler:
|
---|
139 | LOG(3,"INFO: Stumbled upon Filler entry.");
|
---|
140 | break;
|
---|
141 | case PdbKey::EndOfTimestep:
|
---|
142 | LOG(3,"INFO: Parsing END entry or empty line.");
|
---|
143 | NotEndOfTimestep = false;
|
---|
144 | break;
|
---|
145 | default:
|
---|
146 | // TODO: put a throw here
|
---|
147 | DoeLog(2) && (eLog() << Verbose(2) << "Unknown token: '" << line << "'" << std::endl);
|
---|
148 | //ASSERT(0, "PdbParser::load() - Unknown token in line "+toString(linecount)+": "+line+".");
|
---|
149 | break;
|
---|
150 | }
|
---|
151 | NotEndOfFile = NotEndOfFile && (file->good());
|
---|
152 | linecount++;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Saves the \a atoms into as a PDB file.
|
---|
159 | *
|
---|
160 | * \param file where to save the state
|
---|
161 | * \param atoms atoms to store
|
---|
162 | */
|
---|
163 | void PdbParser::save(ostream* file, const std::vector<atom *> &AtomList)
|
---|
164 | {
|
---|
165 | DoLog(0) && (Log() << Verbose(0) << "Saving changes to pdb." << std::endl);
|
---|
166 |
|
---|
167 | // check for maximum number of time steps
|
---|
168 | size_t max_timesteps = 0;
|
---|
169 | BOOST_FOREACH(atom *_atom, World::getInstance().getAllAtoms()) {
|
---|
170 | if (_atom->getTrajectorySize() > max_timesteps)
|
---|
171 | max_timesteps = _atom->getTrajectorySize();
|
---|
172 | }
|
---|
173 | LOG(2,"INFO: Found a maximum of " << max_timesteps << " time steps to store.");
|
---|
174 |
|
---|
175 | // re-distribute serials
|
---|
176 | // (new atoms might have been added)
|
---|
177 | // (serials must be consistent over time steps)
|
---|
178 | atomIdMap.clear();
|
---|
179 | int AtomNo = 1; // serial number starts at 1 in pdb
|
---|
180 | for (vector<atom *>::const_iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) {
|
---|
181 | PdbAtomInfoContainer &atomInfo = getadditionalAtomData(*atomIt);
|
---|
182 | setSerial((*atomIt)->getId(), AtomNo);
|
---|
183 | atomInfo.set(PdbKey::serial, toString(AtomNo));
|
---|
184 | AtomNo++;
|
---|
185 | }
|
---|
186 |
|
---|
187 | // store all time steps
|
---|
188 | for (size_t step = 0; step < max_timesteps; ++step) {
|
---|
189 | {
|
---|
190 | // add initial remark
|
---|
191 | *file << "REMARK created by molecuilder on ";
|
---|
192 | time_t now = time((time_t *)NULL); // Get the system time and put it into 'now' as 'calender time'
|
---|
193 | // ctime ends in \n\0, we have to cut away the newline
|
---|
194 | std::string time(ctime(&now));
|
---|
195 | size_t pos = time.find('\n');
|
---|
196 | if (pos != 0)
|
---|
197 | *file << time.substr(0,pos);
|
---|
198 | else
|
---|
199 | *file << time;
|
---|
200 | *file << ", time step " << step;
|
---|
201 | *file << endl;
|
---|
202 | }
|
---|
203 |
|
---|
204 | {
|
---|
205 | std::map<size_t,size_t> MolIdMap;
|
---|
206 | size_t MolNo = 1; // residue number starts at 1 in pdb
|
---|
207 | for (vector<atom *>::const_iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) {
|
---|
208 | const molecule *mol = (*atomIt)->getMolecule();
|
---|
209 | if ((mol != NULL) && (MolIdMap.find(mol->getId()) == MolIdMap.end())) {
|
---|
210 | MolIdMap[mol->getId()] = MolNo++;
|
---|
211 | }
|
---|
212 | }
|
---|
213 | const size_t MaxMol = MolNo;
|
---|
214 |
|
---|
215 | // have a count per element and per molecule (0 is for all homeless atoms)
|
---|
216 | std::vector<int> **elementNo = new std::vector<int>*[MaxMol];
|
---|
217 | for (size_t i = 0; i < MaxMol; ++i)
|
---|
218 | elementNo[i] = new std::vector<int>(MAX_ELEMENTS,1);
|
---|
219 | char name[MAXSTRINGSIZE];
|
---|
220 | std::string ResidueName;
|
---|
221 |
|
---|
222 | // write ATOMs
|
---|
223 | for (vector<atom *>::const_iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) {
|
---|
224 | PdbAtomInfoContainer &atomInfo = getadditionalAtomData(*atomIt);
|
---|
225 | // gather info about residue
|
---|
226 | const molecule *mol = (*atomIt)->getMolecule();
|
---|
227 | if (mol == NULL) {
|
---|
228 | MolNo = 0;
|
---|
229 | atomInfo.set(PdbKey::resSeq, "0");
|
---|
230 | } else {
|
---|
231 | ASSERT(MolIdMap.find(mol->getId()) != MolIdMap.end(),
|
---|
232 | "PdbParser::save() - Mol id "+toString(mol->getId())+" not present despite we set it?!");
|
---|
233 | MolNo = MolIdMap[mol->getId()];
|
---|
234 | atomInfo.set(PdbKey::resSeq, toString(MolIdMap[mol->getId()]));
|
---|
235 | if (atomInfo.get<std::string>(PdbKey::resName) == "-")
|
---|
236 | atomInfo.set(PdbKey::resName, mol->getName().substr(0,3));
|
---|
237 | }
|
---|
238 | // get info about atom
|
---|
239 | const size_t Z = (*atomIt)->getType()->getAtomicNumber();
|
---|
240 | if (atomInfo.get<std::string>(PdbKey::name) == "-") { // if no name set, give it a new name
|
---|
241 | sprintf(name, "%2s%02d",(*atomIt)->getType()->getSymbol().c_str(), (*elementNo[MolNo])[Z]);
|
---|
242 | (*elementNo[MolNo])[Z] = ((*elementNo[MolNo])[Z]+1) % 100; // confine to two digits
|
---|
243 | atomInfo.set(PdbKey::name, name);
|
---|
244 | }
|
---|
245 | // set position
|
---|
246 | for (size_t i=0; i<NDIM;++i) {
|
---|
247 | stringstream position;
|
---|
248 | position << setw(8) << fixed << setprecision(3) << (*atomIt)->getPositionAtStep(step).at(i);
|
---|
249 | atomInfo.set(PositionEnumMap[i], position.str());
|
---|
250 | }
|
---|
251 | // change element and charge if changed
|
---|
252 | if (atomInfo.get<std::string>(PdbKey::element) != (*atomIt)->getType()->getSymbol())
|
---|
253 | atomInfo.set(PdbKey::element, (*atomIt)->getType()->getSymbol());
|
---|
254 |
|
---|
255 | // finally save the line
|
---|
256 | saveLine(file, atomInfo);
|
---|
257 | }
|
---|
258 | for (size_t i = 0; i < MaxMol; ++i)
|
---|
259 | delete elementNo[i];
|
---|
260 | delete elementNo;
|
---|
261 |
|
---|
262 | // write CONECTs
|
---|
263 | for (vector<atom *>::const_iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) {
|
---|
264 | writeNeighbors(file, 4, *atomIt);
|
---|
265 | }
|
---|
266 | }
|
---|
267 | // END
|
---|
268 | *file << "END" << endl;
|
---|
269 | }
|
---|
270 |
|
---|
271 | }
|
---|
272 |
|
---|
273 | /** Checks whether there is an entry for the given atom's \a _id.
|
---|
274 | *
|
---|
275 | * @param _id atom's id we wish to check on
|
---|
276 | * @return true - entry present, false - only for atom's father or no entry
|
---|
277 | */
|
---|
278 | bool PdbParser::isPresentadditionalAtomData(unsigned int _id)
|
---|
279 | {
|
---|
280 | return (additionalAtomData.find(_id) != additionalAtomData.end());
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | /** Either returns reference to present entry or creates new with default values.
|
---|
285 | *
|
---|
286 | * @param _atom atom whose entry we desire
|
---|
287 | * @return
|
---|
288 | */
|
---|
289 | PdbAtomInfoContainer& PdbParser::getadditionalAtomData(atom *_atom)
|
---|
290 | {
|
---|
291 | if (additionalAtomData.find(_atom->getId()) != additionalAtomData.end()) {
|
---|
292 | } else if (additionalAtomData.find(_atom->father->getId()) != additionalAtomData.end()) {
|
---|
293 | // use info from direct father
|
---|
294 | additionalAtomData[_atom->getId()] = additionalAtomData[_atom->father->getId()];
|
---|
295 | } else if (additionalAtomData.find(_atom->GetTrueFather()->getId()) != additionalAtomData.end()) {
|
---|
296 | // use info from topmost father
|
---|
297 | additionalAtomData[_atom->getId()] = additionalAtomData[_atom->GetTrueFather()->getId()];
|
---|
298 | } else {
|
---|
299 | // create new entry use default values if nothing else is known
|
---|
300 | additionalAtomData[_atom->getId()] = defaultAdditionalData;
|
---|
301 | }
|
---|
302 | return additionalAtomData[_atom->getId()];
|
---|
303 | }
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Writes one line of PDB-formatted data to the provided stream.
|
---|
307 | *
|
---|
308 | * \param stream where to write the line to
|
---|
309 | * \param *currentAtom the atom of which information should be written
|
---|
310 | * \param AtomNo serial number of atom
|
---|
311 | * \param *name name of atom, i.e. H01
|
---|
312 | * \param ResidueName Name of molecule
|
---|
313 | * \param ResidueNo number of residue
|
---|
314 | */
|
---|
315 | void PdbParser::saveLine(
|
---|
316 | ostream* file,
|
---|
317 | const PdbAtomInfoContainer &atomInfo)
|
---|
318 | {
|
---|
319 | *file << setfill(' ') << left << setw(6)
|
---|
320 | << atomInfo.get<std::string>(PdbKey::token);
|
---|
321 | *file << setfill(' ') << right << setw(5)
|
---|
322 | << atomInfo.get<int>(PdbKey::serial); /* atom serial number */
|
---|
323 | *file << " "; /* char 12 is empty */
|
---|
324 | *file << setfill(' ') << left << setw(4)
|
---|
325 | << atomInfo.get<std::string>(PdbKey::name); /* atom name */
|
---|
326 | *file << setfill(' ') << left << setw(1)
|
---|
327 | << atomInfo.get<std::string>(PdbKey::altLoc); /* alternate location/conformation */
|
---|
328 | *file << setfill(' ') << left << setw(3)
|
---|
329 | << atomInfo.get<std::string>(PdbKey::resName); /* residue name */
|
---|
330 | *file << " "; /* char 21 is empty */
|
---|
331 | *file << setfill(' ') << left << setw(1)
|
---|
332 | << atomInfo.get<std::string>(PdbKey::chainID); /* chain identifier */
|
---|
333 | *file << setfill(' ') << left << setw(4)
|
---|
334 | << atomInfo.get<int>(PdbKey::resSeq); /* residue sequence number */
|
---|
335 | *file << setfill(' ') << left << setw(1)
|
---|
336 | << atomInfo.get<std::string>(PdbKey::iCode); /* iCode */
|
---|
337 | *file << " "; /* char 28-30 are empty */
|
---|
338 | // have the following operate on stringstreams such that format specifiers
|
---|
339 | // only act on these
|
---|
340 | for (size_t i=0;i<NDIM;++i) {
|
---|
341 | stringstream position;
|
---|
342 | position << fixed << setprecision(3) << showpoint
|
---|
343 | << atomInfo.get<double>(PositionEnumMap[i]);
|
---|
344 | *file << setfill(' ') << right << setw(8) << position.str();
|
---|
345 | }
|
---|
346 | {
|
---|
347 | stringstream occupancy;
|
---|
348 | occupancy << fixed << setprecision(2) << showpoint
|
---|
349 | << atomInfo.get<double>(PdbKey::occupancy); /* occupancy */
|
---|
350 | *file << setfill(' ') << right << setw(6) << occupancy.str();
|
---|
351 | }
|
---|
352 | {
|
---|
353 | stringstream tempFactor;
|
---|
354 | tempFactor << fixed << setprecision(2) << showpoint
|
---|
355 | << atomInfo.get<double>(PdbKey::tempFactor); /* temperature factor */
|
---|
356 | *file << setfill(' ') << right << setw(6) << tempFactor.str();
|
---|
357 | }
|
---|
358 | *file << " "; /* char 68-76 are empty */
|
---|
359 | *file << setfill(' ') << right << setw(2) << atomInfo.get<std::string>(PdbKey::element); /* element */
|
---|
360 | *file << setfill(' ') << right << setw(2) << atomInfo.get<int>(PdbKey::charge); /* charge */
|
---|
361 |
|
---|
362 | *file << endl;
|
---|
363 | }
|
---|
364 |
|
---|
365 | /**
|
---|
366 | * Writes the neighbor information of one atom to the provided stream.
|
---|
367 | *
|
---|
368 | * Note that ListOfBonds of WorldTime::CurrentTime is used.
|
---|
369 | *
|
---|
370 | * \param *file where to write neighbor information to
|
---|
371 | * \param MaxnumberOfNeighbors of neighbors
|
---|
372 | * \param *currentAtom to the atom of which to take the neighbor information
|
---|
373 | */
|
---|
374 | void PdbParser::writeNeighbors(ostream* file, int MaxnumberOfNeighbors, atom* currentAtom) {
|
---|
375 | int MaxNo = MaxnumberOfNeighbors;
|
---|
376 | const BondList & ListOfBonds = currentAtom->getListOfBonds();
|
---|
377 | if (!ListOfBonds.empty()) {
|
---|
378 | for(BondList::const_iterator currentBond = ListOfBonds.begin(); currentBond != ListOfBonds.end(); ++currentBond) {
|
---|
379 | if (MaxNo >= MaxnumberOfNeighbors) {
|
---|
380 | *file << "CONECT";
|
---|
381 | *file << setw(5) << getSerial(currentAtom->getId());
|
---|
382 | MaxNo = 0;
|
---|
383 | }
|
---|
384 | *file << setw(5) << getSerial((*currentBond)->GetOtherAtom(currentAtom)->getId());
|
---|
385 | MaxNo++;
|
---|
386 | if (MaxNo == MaxnumberOfNeighbors)
|
---|
387 | *file << "\n";
|
---|
388 | }
|
---|
389 | if (MaxNo != MaxnumberOfNeighbors)
|
---|
390 | *file << "\n";
|
---|
391 | }
|
---|
392 | }
|
---|
393 |
|
---|
394 | /** Retrieves a value from PdbParser::atomIdMap.
|
---|
395 | * \param atomid key
|
---|
396 | * \return value
|
---|
397 | */
|
---|
398 | size_t PdbParser::getSerial(const size_t atomid) const
|
---|
399 | {
|
---|
400 | ASSERT(atomIdMap.find(atomid) != atomIdMap.end(), "PdbParser::getAtomId: atomid not present in Map.");
|
---|
401 | return (atomIdMap.find(atomid)->second);
|
---|
402 | }
|
---|
403 |
|
---|
404 | /** Sets an entry in PdbParser::atomIdMap.
|
---|
405 | * \param localatomid key
|
---|
406 | * \param atomid value
|
---|
407 | * \return true - key not present, false - value present
|
---|
408 | */
|
---|
409 | void PdbParser::setSerial(const size_t localatomid, const size_t atomid)
|
---|
410 | {
|
---|
411 | pair<std::map<size_t,size_t>::iterator, bool > inserter;
|
---|
412 | // DoLog(1) && (Log() << Verbose(1) << "PdbParser::setAtomId() - Inserting ("
|
---|
413 | // << localatomid << " -> " << atomid << ")." << std::endl);
|
---|
414 | inserter = atomIdMap.insert( make_pair(localatomid, atomid) );
|
---|
415 | ASSERT(inserter.second, "PdbParser::setAtomId: atomId already present in Map.");
|
---|
416 | }
|
---|
417 |
|
---|
418 | /** Either returns present atom with given id or a newly created one.
|
---|
419 | *
|
---|
420 | * @param id_string
|
---|
421 | * @return
|
---|
422 | */
|
---|
423 | atom* PdbParser::getAtomToParse(std::string id_string) const
|
---|
424 | {
|
---|
425 | // get the local ID
|
---|
426 | ConvertTo<int> toInt;
|
---|
427 | unsigned int AtomID = toInt(id_string);
|
---|
428 | LOG(4, "INFO: Local id is "+toString(AtomID)+".");
|
---|
429 | // get the atomic ID if present
|
---|
430 | atom* newAtom = NULL;
|
---|
431 | if (atomIdMap.count((size_t)AtomID)) {
|
---|
432 | std::map<size_t, size_t>::const_iterator iter = atomIdMap.find(AtomID);
|
---|
433 | AtomID = iter->second;
|
---|
434 | LOG(4, "INFO: Global id present as " << AtomID << ".");
|
---|
435 | // check if atom exists
|
---|
436 | newAtom = World::getInstance().getAtom(AtomById(AtomID));
|
---|
437 | LOG(5, "INFO: Listing all present atoms with id.");
|
---|
438 | BOOST_FOREACH(atom *_atom, World::getInstance().getAllAtoms())
|
---|
439 | LOG(5, "INFO: " << *_atom << " with id " << _atom->getId());
|
---|
440 | }
|
---|
441 | // if not exists, create
|
---|
442 | if (newAtom == NULL) {
|
---|
443 | newAtom = World::getInstance().createAtom();
|
---|
444 | LOG(4, "INFO: No association to global id present, creating atom.");
|
---|
445 | } else {
|
---|
446 | LOG(4, "INFO: Existing atom found: " << *newAtom << ".");
|
---|
447 | }
|
---|
448 | return newAtom;
|
---|
449 | }
|
---|
450 |
|
---|
451 | void PdbParser::readPdbAtomInfoContainer(PdbAtomInfoContainer &atomInfo, std::string &line) const
|
---|
452 | {
|
---|
453 | LOG(4,"INFO: Parsing token from "+line.substr(0,6)+".");
|
---|
454 | atomInfo.set(PdbKey::token, line.substr(0,6));
|
---|
455 | LOG(4,"INFO: Parsing serial from "+line.substr(6,5)+".");
|
---|
456 | atomInfo.set(PdbKey::serial, line.substr(6,5));
|
---|
457 |
|
---|
458 | LOG(4,"INFO: Parsing name from "+line.substr(12,4)+".");
|
---|
459 | atomInfo.set(PdbKey::name, line.substr(12,4));
|
---|
460 | LOG(4,"INFO: Parsing altLoc from "+line.substr(16,1)+".");
|
---|
461 | atomInfo.set(PdbKey::altLoc, line.substr(16,1));
|
---|
462 | LOG(4,"INFO: Parsing resName from "+line.substr(17,3)+".");
|
---|
463 | atomInfo.set(PdbKey::resName, line.substr(17,3));
|
---|
464 | LOG(4,"INFO: Parsing chainID from "+line.substr(21,1)+".");
|
---|
465 | atomInfo.set(PdbKey::chainID, line.substr(21,1));
|
---|
466 | LOG(4,"INFO: Parsing resSeq from "+line.substr(22,4)+".");
|
---|
467 | atomInfo.set(PdbKey::resSeq, line.substr(22,4));
|
---|
468 | LOG(4,"INFO: Parsing iCode from "+line.substr(26,1)+".");
|
---|
469 | atomInfo.set(PdbKey::iCode, line.substr(26,1));
|
---|
470 |
|
---|
471 | LOG(4,"INFO: Parsing occupancy from "+line.substr(54,6)+".");
|
---|
472 | atomInfo.set(PdbKey::occupancy, line.substr(54,6));
|
---|
473 | LOG(4,"INFO: Parsing tempFactor from "+line.substr(60,6)+".");
|
---|
474 | atomInfo.set(PdbKey::tempFactor, line.substr(60,6));
|
---|
475 | LOG(4,"INFO: Parsing charge from "+line.substr(78,2)+".");
|
---|
476 | atomInfo.set(PdbKey::charge, line.substr(78,2));
|
---|
477 | LOG(4,"INFO: Parsing element from "+line.substr(76,2)+".");
|
---|
478 | atomInfo.set(PdbKey::element, line.substr(76,2));
|
---|
479 | }
|
---|
480 |
|
---|
481 | /** Parse an ATOM line from a PDB file.
|
---|
482 | *
|
---|
483 | * Reads one data line of a pdstatus file and interprets it according to the
|
---|
484 | * specifications of the PDB 3.2 format: http://www.wwpdb.org/docs.html
|
---|
485 | *
|
---|
486 | * A new atom is created and filled with available information, non-
|
---|
487 | * standard information is placed in additionalAtomData at the atom's id.
|
---|
488 | *
|
---|
489 | * \param line to parse as an atom
|
---|
490 | * \param newmol molecule to add parsed atoms to
|
---|
491 | */
|
---|
492 | void PdbParser::readAtomDataLine(std::string &line, molecule *newmol = NULL) {
|
---|
493 | vector<string>::iterator it;
|
---|
494 |
|
---|
495 | atom* newAtom = getAtomToParse(line.substr(6,5));
|
---|
496 | LOG(3,"INFO: Parsing END entry or empty line.");
|
---|
497 | bool FirstTimestep = isPresentadditionalAtomData(newAtom->getId()) ? false : true;
|
---|
498 | if (FirstTimestep) {
|
---|
499 | LOG(3,"INFO: Parsing new atom.");
|
---|
500 | } else {
|
---|
501 | LOG(3,"INFO: Parsing present atom "+toString(*newAtom)+".");
|
---|
502 | }
|
---|
503 | PdbAtomInfoContainer &atomInfo = getadditionalAtomData(newAtom);
|
---|
504 | LOG(4,"INFO: Information in container is "+toString(atomInfo)+".");
|
---|
505 |
|
---|
506 | string word;
|
---|
507 | ConvertTo<size_t> toSize_t;
|
---|
508 |
|
---|
509 | // assign highest+1 instead, but then beware of CONECT entries! Another map needed!
|
---|
510 | // if (!Inserter.second) {
|
---|
511 | // const size_t id = (*SerialSet.rbegin())+1;
|
---|
512 | // SerialSet.insert(id);
|
---|
513 | // atomInfo.set(PdbKey::serial, toString(id));
|
---|
514 | // DoeLog(2) && (eLog() << Verbose(2)
|
---|
515 | // << "Serial " << atomInfo.get<std::string>(PdbKey::serial) << " already present, "
|
---|
516 | // << "assigning " << toString(id) << " instead." << std::endl);
|
---|
517 | // }
|
---|
518 |
|
---|
519 | // check whether serial exists, if so, assign next available
|
---|
520 |
|
---|
521 | // DoLog(2) && (Log() << Verbose(2) << "Split line:"
|
---|
522 | // << line.substr(6,5) << "|"
|
---|
523 | // << line.substr(12,4) << "|"
|
---|
524 | // << line.substr(16,1) << "|"
|
---|
525 | // << line.substr(17,3) << "|"
|
---|
526 | // << line.substr(21,1) << "|"
|
---|
527 | // << line.substr(22,4) << "|"
|
---|
528 | // << line.substr(26,1) << "|"
|
---|
529 | // << line.substr(30,8) << "|"
|
---|
530 | // << line.substr(38,8) << "|"
|
---|
531 | // << line.substr(46,8) << "|"
|
---|
532 | // << line.substr(54,6) << "|"
|
---|
533 | // << line.substr(60,6) << "|"
|
---|
534 | // << line.substr(76,2) << "|"
|
---|
535 | // << line.substr(78,2) << std::endl);
|
---|
536 |
|
---|
537 | if (FirstTimestep) {
|
---|
538 | // first time step
|
---|
539 | // then fill info container
|
---|
540 | readPdbAtomInfoContainer(atomInfo, line);
|
---|
541 | // set the serial
|
---|
542 | std::pair< std::set<size_t>::const_iterator, bool> Inserter =
|
---|
543 | SerialSet.insert(toSize_t(atomInfo.get<std::string>(PdbKey::serial)));
|
---|
544 | ASSERT(Inserter.second,
|
---|
545 | "PdbParser::readAtomDataLine() - ATOM contains entry with serial "
|
---|
546 | +atomInfo.get<std::string>(PdbKey::serial)+" already present!");
|
---|
547 | setSerial(toSize_t(atomInfo.get<std::string>(PdbKey::serial)), newAtom->getId());
|
---|
548 | // set position
|
---|
549 | Vector tempVector;
|
---|
550 | LOG(4,"INFO: Parsing position from ("
|
---|
551 | +line.substr(30,8)+","
|
---|
552 | +line.substr(38,8)+","
|
---|
553 | +line.substr(46,8)+").");
|
---|
554 | PdbAtomInfoContainer::ScanKey(tempVector[0], line.substr(30,8));
|
---|
555 | PdbAtomInfoContainer::ScanKey(tempVector[1], line.substr(38,8));
|
---|
556 | PdbAtomInfoContainer::ScanKey(tempVector[2], line.substr(46,8));
|
---|
557 | newAtom->setPosition(tempVector);
|
---|
558 | // set element
|
---|
559 | const element *elem = World::getInstance().getPeriode()
|
---|
560 | ->FindElement(atomInfo.get<std::string>(PdbKey::element));
|
---|
561 | ASSERT(elem != NULL,
|
---|
562 | "PdbParser::readAtomDataLine() - element "+atomInfo.get<std::string>(PdbKey::element)+" is unknown!");
|
---|
563 | newAtom->setType(elem);
|
---|
564 |
|
---|
565 | if (newmol != NULL)
|
---|
566 | newmol->AddAtom(newAtom);
|
---|
567 | } else {
|
---|
568 | // not first time step
|
---|
569 | // then parse into different container
|
---|
570 | PdbAtomInfoContainer consistencyInfo;
|
---|
571 | readPdbAtomInfoContainer(consistencyInfo, line);
|
---|
572 | // then check additional info for consistency
|
---|
573 | ASSERT(atomInfo.get<std::string>(PdbKey::token) == consistencyInfo.get<std::string>(PdbKey::token),
|
---|
574 | "PdbParser::readAtomDataLine() - difference in token on multiple time step for atom with id "
|
---|
575 | +atomInfo.get<std::string>(PdbKey::serial)+"!");
|
---|
576 | ASSERT(atomInfo.get<std::string>(PdbKey::name) == consistencyInfo.get<std::string>(PdbKey::name),
|
---|
577 | "PdbParser::readAtomDataLine() - difference in name on multiple time step for atom with id "
|
---|
578 | +atomInfo.get<std::string>(PdbKey::serial)+":"
|
---|
579 | +atomInfo.get<std::string>(PdbKey::name)+"!="
|
---|
580 | +consistencyInfo.get<std::string>(PdbKey::name)+".");
|
---|
581 | ASSERT(atomInfo.get<std::string>(PdbKey::altLoc) == consistencyInfo.get<std::string>(PdbKey::altLoc),
|
---|
582 | "PdbParser::readAtomDataLine() - difference in altLoc on multiple time step for atom with id "
|
---|
583 | +atomInfo.get<std::string>(PdbKey::serial)+"!");
|
---|
584 | ASSERT(atomInfo.get<std::string>(PdbKey::resName) == consistencyInfo.get<std::string>(PdbKey::resName),
|
---|
585 | "PdbParser::readAtomDataLine() - difference in resName on multiple time step for atom with id "
|
---|
586 | +atomInfo.get<std::string>(PdbKey::serial)+"!");
|
---|
587 | ASSERT(atomInfo.get<std::string>(PdbKey::chainID) == consistencyInfo.get<std::string>(PdbKey::chainID),
|
---|
588 | "PdbParser::readAtomDataLine() - difference in chainID on multiple time step for atom with id "
|
---|
589 | +atomInfo.get<std::string>(PdbKey::serial)+"!");
|
---|
590 | ASSERT(atomInfo.get<std::string>(PdbKey::resSeq) == consistencyInfo.get<std::string>(PdbKey::resSeq),
|
---|
591 | "PdbParser::readAtomDataLine() - difference in resSeq on multiple time step for atom with id "
|
---|
592 | +atomInfo.get<std::string>(PdbKey::serial)+"!");
|
---|
593 | ASSERT(atomInfo.get<std::string>(PdbKey::iCode) == consistencyInfo.get<std::string>(PdbKey::iCode),
|
---|
594 | "PdbParser::readAtomDataLine() - difference in iCode on multiple time step for atom with id "
|
---|
595 | +atomInfo.get<std::string>(PdbKey::serial)+"!");
|
---|
596 | ASSERT(atomInfo.get<std::string>(PdbKey::occupancy) == consistencyInfo.get<std::string>(PdbKey::occupancy),
|
---|
597 | "PdbParser::readAtomDataLine() - difference in occupancy on multiple time step for atom with id "
|
---|
598 | +atomInfo.get<std::string>(PdbKey::serial)+"!");
|
---|
599 | ASSERT(atomInfo.get<std::string>(PdbKey::tempFactor) == consistencyInfo.get<std::string>(PdbKey::tempFactor),
|
---|
600 | "PdbParser::readAtomDataLine() - difference in tempFactor on multiple time step for atom with id "
|
---|
601 | +atomInfo.get<std::string>(PdbKey::serial)+"!");
|
---|
602 | ASSERT(atomInfo.get<std::string>(PdbKey::charge) == consistencyInfo.get<std::string>(PdbKey::charge),
|
---|
603 | "PdbParser::readAtomDataLine() - difference in charge on multiple time step for atom with id "
|
---|
604 | +atomInfo.get<std::string>(PdbKey::serial)+"!");
|
---|
605 | ASSERT(atomInfo.get<std::string>(PdbKey::element) == consistencyInfo.get<std::string>(PdbKey::element),
|
---|
606 | "PdbParser::readAtomDataLine() - difference in element on multiple time step for atom with id "
|
---|
607 | +atomInfo.get<std::string>(PdbKey::serial)+"!");
|
---|
608 | // and parse in trajectory
|
---|
609 | Vector tempVector;
|
---|
610 | LOG(4,"INFO: Parsing trajectory position from ("
|
---|
611 | +line.substr(30,8)+","
|
---|
612 | +line.substr(38,8)+","
|
---|
613 | +line.substr(46,8)+").");
|
---|
614 | PdbAtomInfoContainer::ScanKey(tempVector[0], line.substr(30,8));
|
---|
615 | PdbAtomInfoContainer::ScanKey(tempVector[1], line.substr(38,8));
|
---|
616 | PdbAtomInfoContainer::ScanKey(tempVector[2], line.substr(46,8));
|
---|
617 | // increase time step
|
---|
618 | size_t timestep = atomInfo.get<size_t>(PdbKey::timestep)+1;
|
---|
619 | atomInfo.set(PdbKey::timestep, toString(timestep));
|
---|
620 | LOG(4,"INFO: Adding time step "+atomInfo.get<std::string>(PdbKey::timestep)+".");
|
---|
621 | // and set position at new time step
|
---|
622 | newAtom->setPositionAtStep(timestep, tempVector);
|
---|
623 | }
|
---|
624 |
|
---|
625 |
|
---|
626 | // printAtomInfo(newAtom);
|
---|
627 | }
|
---|
628 |
|
---|
629 | /** Prints all PDB-specific information known about an atom.
|
---|
630 | *
|
---|
631 | */
|
---|
632 | void PdbParser::printAtomInfo(const atom * const newAtom) const
|
---|
633 | {
|
---|
634 | const PdbAtomInfoContainer &atomInfo = additionalAtomData.at(newAtom->getId()); // operator[] const does not exist
|
---|
635 |
|
---|
636 | DoLog(1) && (Log() << Verbose(1) << "We know about atom " << newAtom->getId() << ":" << std::endl);
|
---|
637 | DoLog(1) && (Log() << Verbose(1) << "\ttoken is " << atomInfo.get<std::string>(PdbKey::token) << std::endl);
|
---|
638 | DoLog(1) && (Log() << Verbose(1) << "\tserial is " << atomInfo.get<int>(PdbKey::serial) << std::endl);
|
---|
639 | DoLog(1) && (Log() << Verbose(1) << "\tname is " << atomInfo.get<std::string>(PdbKey::name) << std::endl);
|
---|
640 | DoLog(1) && (Log() << Verbose(1) << "\taltLoc is " << atomInfo.get<std::string>(PdbKey::altLoc) << std::endl);
|
---|
641 | DoLog(1) && (Log() << Verbose(1) << "\tresName is " << atomInfo.get<std::string>(PdbKey::resName) << std::endl);
|
---|
642 | DoLog(1) && (Log() << Verbose(1) << "\tchainID is " << atomInfo.get<std::string>(PdbKey::chainID) << std::endl);
|
---|
643 | DoLog(1) && (Log() << Verbose(1) << "\tresSeq is " << atomInfo.get<int>(PdbKey::resSeq) << std::endl);
|
---|
644 | DoLog(1) && (Log() << Verbose(1) << "\tiCode is " << atomInfo.get<std::string>(PdbKey::iCode) << std::endl);
|
---|
645 | DoLog(1) && (Log() << Verbose(1) << "\tX is " << atomInfo.get<double>(PdbKey::X) << std::endl);
|
---|
646 | DoLog(1) && (Log() << Verbose(1) << "\tY is " << atomInfo.get<double>(PdbKey::Y) << std::endl);
|
---|
647 | DoLog(1) && (Log() << Verbose(1) << "\tZ is " << atomInfo.get<double>(PdbKey::Z) << std::endl);
|
---|
648 | DoLog(1) && (Log() << Verbose(1) << "\toccupancy is " << atomInfo.get<double>(PdbKey::occupancy) << std::endl);
|
---|
649 | DoLog(1) && (Log() << Verbose(1) << "\ttempFactor is " << atomInfo.get<double>(PdbKey::tempFactor) << std::endl);
|
---|
650 | DoLog(1) && (Log() << Verbose(1) << "\telement is '" << *(newAtom->getType()) << "'" << std::endl);
|
---|
651 | DoLog(1) && (Log() << Verbose(1) << "\tcharge is " << atomInfo.get<int>(PdbKey::charge) << std::endl);
|
---|
652 | }
|
---|
653 |
|
---|
654 | /**
|
---|
655 | * Reads neighbor information for one atom from the input.
|
---|
656 | *
|
---|
657 | * \param line to parse as an atom
|
---|
658 | */
|
---|
659 | void PdbParser::readNeighbors(std::string &line)
|
---|
660 | {
|
---|
661 | const size_t length = line.length();
|
---|
662 | std::list<size_t> ListOfNeighbors;
|
---|
663 | ConvertTo<size_t> toSize_t;
|
---|
664 |
|
---|
665 | // obtain neighbours
|
---|
666 | // show split line for debugging
|
---|
667 | string output;
|
---|
668 | ASSERT(length >=16,
|
---|
669 | "PdbParser::readNeighbors() - CONECT entry has not enough entries: "+line+"!");
|
---|
670 | // output = "Split line:|";
|
---|
671 | // output += line.substr(6,5) + "|";
|
---|
672 | const size_t id = toSize_t(line.substr(6,5));
|
---|
673 | for (size_t index = 11; index <= 26; index+=5) {
|
---|
674 | if (index+5 <= length) {
|
---|
675 | // output += line.substr(index,5) + "|";
|
---|
676 | const size_t otherid = toSize_t(line.substr(index,5));
|
---|
677 | ListOfNeighbors.push_back(otherid);
|
---|
678 | } else {
|
---|
679 | break;
|
---|
680 | }
|
---|
681 | }
|
---|
682 | // DoLog(2) && (Log() << Verbose(2) << output << std::endl);
|
---|
683 |
|
---|
684 | // add neighbours
|
---|
685 | atom *_atom = World::getInstance().getAtom(AtomById(getSerial(id)));
|
---|
686 | for (std::list<size_t>::const_iterator iter = ListOfNeighbors.begin();
|
---|
687 | iter != ListOfNeighbors.end();
|
---|
688 | ++iter) {
|
---|
689 | // DoLog(1) && (Log() << Verbose(1) << "Adding Bond (" << getAtomId(id) << "," << getAtomId(*iter) << ")" << std::endl);
|
---|
690 | atom * const _Otheratom = World::getInstance().getAtom(AtomById(getSerial(*iter)));
|
---|
691 | _atom->addBond(WorldTime::getTime(), _Otheratom);
|
---|
692 | }
|
---|
693 | }
|
---|
694 |
|
---|
695 | /**
|
---|
696 | * Replaces atom IDs read from the file by the corresponding world IDs. All IDs
|
---|
697 | * IDs of the input string will be replaced; expected separating characters are
|
---|
698 | * "-" and ",".
|
---|
699 | *
|
---|
700 | * \param string in which atom IDs should be adapted
|
---|
701 | *
|
---|
702 | * \return input string with modified atom IDs
|
---|
703 | */
|
---|
704 | //string PdbParser::adaptIdDependentDataString(string data) {
|
---|
705 | // // there might be no IDs
|
---|
706 | // if (data == "-") {
|
---|
707 | // return "-";
|
---|
708 | // }
|
---|
709 | //
|
---|
710 | // char separator;
|
---|
711 | // int id;
|
---|
712 | // stringstream line, result;
|
---|
713 | // line << data;
|
---|
714 | //
|
---|
715 | // line >> id;
|
---|
716 | // result << atomIdMap[id];
|
---|
717 | // while (line.good()) {
|
---|
718 | // line >> separator >> id;
|
---|
719 | // result << separator << atomIdMap[id];
|
---|
720 | // }
|
---|
721 | //
|
---|
722 | // return result.str();
|
---|
723 | // return "";
|
---|
724 | //}
|
---|
725 |
|
---|
726 |
|
---|
727 | bool PdbParser::operator==(const PdbParser& b) const
|
---|
728 | {
|
---|
729 | bool status = true;
|
---|
730 | World::AtomComposite atoms = World::getInstance().getAllAtoms();
|
---|
731 | for (World::AtomComposite::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
|
---|
732 | if ((additionalAtomData.find((*iter)->getId()) != additionalAtomData.end())
|
---|
733 | && (b.additionalAtomData.find((*iter)->getId()) != b.additionalAtomData.end())) {
|
---|
734 | const PdbAtomInfoContainer &atomInfo = additionalAtomData.at((*iter)->getId());
|
---|
735 | const PdbAtomInfoContainer &OtheratomInfo = b.additionalAtomData.at((*iter)->getId());
|
---|
736 |
|
---|
737 | status = status && (atomInfo.get<std::string>(PdbKey::serial) == OtheratomInfo.get<std::string>(PdbKey::serial));
|
---|
738 | if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in serials!" << std::endl);
|
---|
739 | status = status && (atomInfo.get<std::string>(PdbKey::name) == OtheratomInfo.get<std::string>(PdbKey::name));
|
---|
740 | if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in names!" << std::endl);
|
---|
741 | status = status && (atomInfo.get<std::string>(PdbKey::altLoc) == OtheratomInfo.get<std::string>(PdbKey::altLoc));
|
---|
742 | if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in altLocs!" << std::endl);
|
---|
743 | status = status && (atomInfo.get<std::string>(PdbKey::resName) == OtheratomInfo.get<std::string>(PdbKey::resName));
|
---|
744 | if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in resNames!" << std::endl);
|
---|
745 | status = status && (atomInfo.get<std::string>(PdbKey::chainID) == OtheratomInfo.get<std::string>(PdbKey::chainID));
|
---|
746 | if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in chainIDs!" << std::endl);
|
---|
747 | status = status && (atomInfo.get<std::string>(PdbKey::resSeq) == OtheratomInfo.get<std::string>(PdbKey::resSeq));
|
---|
748 | if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in resSeqs!" << std::endl);
|
---|
749 | status = status && (atomInfo.get<std::string>(PdbKey::iCode) == OtheratomInfo.get<std::string>(PdbKey::iCode));
|
---|
750 | if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in iCodes!" << std::endl);
|
---|
751 | status = status && (atomInfo.get<std::string>(PdbKey::occupancy) == OtheratomInfo.get<std::string>(PdbKey::occupancy));
|
---|
752 | if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in occupancies!" << std::endl);
|
---|
753 | status = status && (atomInfo.get<std::string>(PdbKey::tempFactor) == OtheratomInfo.get<std::string>(PdbKey::tempFactor));
|
---|
754 | if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in tempFactors!" << std::endl);
|
---|
755 | status = status && (atomInfo.get<std::string>(PdbKey::charge) == OtheratomInfo.get<std::string>(PdbKey::charge));
|
---|
756 | if (!status) DoeLog(1) && (eLog() << Verbose(1) << "Mismatch in charges!" << std::endl);
|
---|
757 | }
|
---|
758 | }
|
---|
759 |
|
---|
760 | return status;
|
---|
761 | }
|
---|
762 |
|
---|