source: src/Parser/PdbParser.cpp@ 47d041

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 47d041 was 47d041, checked in by Frederik Heber <heber@…>, 14 years ago

HUGE: Removed all calls to Log(), eLog(), replaced by LOG() and ELOG().

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