source: src/Parser/PdbParser.cpp@ 48801a

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 48801a was 48801a, checked in by Frederik Heber <heber@…>, 14 years ago

Decreased verbosity of PdbParser for multiple time steps.

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