Changeset c0e28c for src/Parser


Ignore:
Timestamp:
Jan 6, 2012, 2:02:08 PM (13 years ago)
Author:
Frederik Heber <heber@…>
Branches:
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
Children:
d08ab3
Parents:
fbbcde
git-author:
Frederik Heber <heber@…> (01/04/12 18:34:22)
git-committer:
Frederik Heber <heber@…> (01/06/12 14:02:08)
Message:

FIX: One could not load another pdb file due to local ids messing up.

  • We enhanced FormatParser_common now contains two maps to go from local to global and back again. Thi is necessary, as ids in a file only make sense within that file. We added the following functions: resetIdAssociations(), associateLocaltoGlobalId(), getGlobalId(), getLocalId().
  • adapted TremoloParser and PdbParser because they are the only formats that also contain bond information and where the associations are needed to translate the local connections into global ones.
  • removed SerialSet entirely from PdbParser, is replaced by new construct, in similar manner AtomIdMap for TremoloParser.
  • TEST: Added regression tests for all Parser to check for loading twice the same file.
Location:
src/Parser
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/FormatParser_common.cpp

    rfbbcde rc0e28c  
    5252  if (parameters != NULL)
    5353    delete parameters;
     54  // clear id translation maps
     55  LocaltoGobalIdMap.clear();
     56  GlobaltoLocalIdMap.clear();
    5457}
    5558
     
    9598 * \param ostream where to save the World's state
    9699 */
    97 void FormatParser_common::setOstream(ostream* output) {
     100void FormatParser_common::setOstream(ostream* output)
     101{
    98102  saveStream = output;
    99103}
     104
     105/** Function to be called when beginning to parse a new file.
     106 *
     107 * Resets internal translation maps.
     108 *
     109 */
     110void FormatParser_common::resetIdAssociations()
     111{
     112  LocaltoGobalIdMap.clear();
     113  GlobaltoLocalIdMap.clear();
     114}
     115
     116/** Installs an association between a local id from a parsed file and the
     117 * global, unique one.
     118 *
     119 * @param local local atom id
     120 * @param global global atom id
     121 */
     122void FormatParser_common::associateLocaltoGlobalId(const int local, const int global)
     123{
     124  ASSERT(LocaltoGobalIdMap.count(local) == 0,
     125      "FormatParser_common::associateLocaltoGlobalId() - local id "
     126      +toString(local)+" is already contained.");
     127  ASSERT(GlobaltoLocalIdMap.count(global) == 0,
     128      "FormatParser_common::associateLocaltoGlobalId() - global id "
     129      +toString(global)+" is already contained.");
     130  LocaltoGobalIdMap[local] = global;
     131  GlobaltoLocalIdMap[global] = local;
     132}
     133
     134/** Getter for the global id to a given \a local one.
     135 *
     136 * @param local local atom id
     137 * @return global atom id, -1 if unknown
     138 */
     139int FormatParser_common::getGlobalId(const int local) const
     140{
     141  IdtoIdMap::const_iterator iter = LocaltoGobalIdMap.find(local);
     142  if(iter == LocaltoGobalIdMap.end())
     143    return -1;
     144  return iter->second;
     145}
     146
     147/** Getter for the local id to a given \a global one.
     148 *
     149 * @param global global atom id
     150 * @return local atom id, -1 if unknown
     151 */
     152int FormatParser_common::getLocalId(const int global) const
     153{
     154  IdtoIdMap::const_iterator iter = GlobaltoLocalIdMap.find(global);
     155  if(iter == GlobaltoLocalIdMap.end())
     156    return -1;
     157  return iter->second;
     158}
  • src/Parser/FormatParser_common.hpp

    rfbbcde rc0e28c  
    1515
    1616#include <iosfwd>
     17#include <map>
    1718
    1819#include "CodePatterns/Observer/Observer.hpp"
     
    4950  virtual void AtomRemoved(atomId_t) {};
    5051
     52  // functions to add atoms and bonds with ids relative to the parsed file
     53  void resetIdAssociations();
     54  void associateLocaltoGlobalId(const int local, const int global);
     55  int getGlobalId(const int local) const;
     56  int getLocalId(const int global) const;
     57
    5158private:
    5259  //!> Output stream to write to when save() is called.
    5360  std::ostream* saveStream;
     61
     62  //!> typedef for atomic id-to-id map
     63  typedef std::map<int, int> IdtoIdMap;
     64
     65  //!> Maps original atom IDs received from the parsed file to atom IDs in the world.
     66  IdtoIdMap LocaltoGobalIdMap;
     67
     68  //!> Maps atom IDs in the World to those received from the parsed file.
     69  IdtoIdMap GlobaltoLocalIdMap;
    5470};
    5571
  • src/Parser/PdbParser.cpp

    rfbbcde rc0e28c  
    7676  PdbAtomInfoContainer::clearknownDataKeys();
    7777  additionalAtomData.clear();
    78   atomIdMap.clear();
    7978}
    8079
     
    118117  enum PdbKey::KnownTokens token;
    119118
    120   // reset atomIdMap for this file (to correctly parse CONECT entries)
    121   atomIdMap.clear();
     119  // reset id maps for this file (to correctly parse CONECT entries)
     120  resetIdAssociations();
    122121
    123122  bool NotEndOfFile = true;
     
    192191
    193192  // re-distribute serials
     193  resetIdAssociations();
    194194  // (new atoms might have been added)
    195   // (serials must be consistent over time steps)
    196   atomIdMap.clear();
    197195  int AtomNo = 1; // serial number starts at 1 in pdb
    198196  for (vector<atom *>::const_iterator atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) {
    199197    PdbAtomInfoContainer &atomInfo = getadditionalAtomData(*atomIt);
    200     setSerial((*atomIt)->getId(), AtomNo);
     198    associateLocaltoGlobalId(AtomNo, (*atomIt)->getId());
    201199    atomInfo.set(PdbKey::serial, toString(AtomNo));
    202     AtomNo++;
     200    ++AtomNo;
    203201  }
    204202
     
    305303  // don't insert here as this is our check whether we are in the first time step
    306304  //additionalAtomData.insert( std::make_pair(id, defaultAdditionalData) );
    307   //SerialSet.insert(id);
    308305}
    309306
     
    321318//      +toString(id)+" to remove.");
    322319  if (iter != additionalAtomData.end()) {
    323     ConvertTo<size_t> toSize_t;
    324     SerialSet.erase(toSize_t((iter->second).get<std::string>(PdbKey::serial)));
    325320    additionalAtomData.erase(iter);
    326321  }
     
    439434      if (MaxNo >= MaxnumberOfNeighbors) {
    440435        *file << "CONECT";
    441         *file << setw(5) << getSerial(currentAtom->getId());
     436        *file << setw(5) << getLocalId(currentAtom->getId());
    442437        charsleft = 80-6-5;
    443438        MaxNo = 0;
    444439      }
    445       *file << setw(5) << getSerial((*currentBond)->GetOtherAtom(currentAtom)->getId());
     440      *file << setw(5) << getLocalId((*currentBond)->GetOtherAtom(currentAtom)->getId());
    446441      charsleft -= 5;
    447442      MaxNo++;
     
    460455}
    461456
    462 /** Retrieves a value from  FormatParser< pdb >::atomIdMap.
    463  * \param atomid key
    464  * \return value
    465  */
    466 size_t FormatParser< pdb >::getSerial(const size_t atomid) const
    467 {
    468   ASSERT(atomIdMap.find(atomid) != atomIdMap.end(),
    469       "FormatParser< pdb >::getAtomId: atomid "+toString(atomid)+" not present in Map.");
    470   return (atomIdMap.find(atomid)->second);
    471 }
    472 
    473 /** Sets an entry in  FormatParser< pdb >::atomIdMap.
    474  * \param localatomid key
    475  * \param atomid value
    476  * \return true - key not present, false - value present
    477  */
    478 void FormatParser< pdb >::setSerial(const size_t localatomid, const size_t atomid)
    479 {
    480   pair<std::map<size_t,size_t>::iterator, bool > inserter;
    481 //  LOG(1, "FormatParser< pdb >::setAtomId() - Inserting (" << localatomid << " -> " << atomid << ").");
    482   inserter = atomIdMap.insert( make_pair(localatomid, atomid) );
    483   ASSERT(inserter.second, "FormatParser< pdb >::setAtomId: atomId already present in Map.");
    484 }
    485 
    486457/** Either returns present atom with given id or a newly created one.
    487458 *
     
    489460 * @return
    490461 */
    491 atom* FormatParser< pdb >::getAtomToParse(std::string id_string) const
     462atom* FormatParser< pdb >::getAtomToParse(std::string id_string)
    492463{
    493464  // get the local ID
    494465  ConvertTo<int> toInt;
    495   unsigned int AtomID = toInt(id_string);
    496   LOG(4, "INFO: Local id is "+toString(AtomID)+".");
     466  const unsigned int AtomID_local = toInt(id_string);
     467  LOG(4, "INFO: Local id is "+toString(AtomID_local)+".");
    497468  // get the atomic ID if present
    498469  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 << ".");
     470  if (getGlobalId(AtomID_local) != -1) {
     471    const unsigned int AtomID_global = getGlobalId(AtomID_local);
     472    LOG(4, "INFO: Global id present as " << AtomID_global << ".");
    503473    // check if atom exists
    504     newAtom = World::getInstance().getAtom(AtomById(AtomID));
     474    newAtom = World::getInstance().getAtom(AtomById(AtomID_global));
    505475    LOG(5, "INFO: Listing all present atoms with id.");
    506476    BOOST_FOREACH(atom *_atom, World::getInstance().getAllAtoms())
     
    510480  if (newAtom == NULL) {
    511481    newAtom = World::getInstance().createAtom();
     482    const unsigned int AtomID_global = newAtom->getId();
    512483    LOG(4, "INFO: No association to global id present, creating atom.");
    513484  } else {
     
    607578      "FormatParser< pdb >::readAtomDataLine() - additionalAtomData present though atom is newly parsed.");
    608579  if (FirstTimestep) {
    609     LOG(3,"INFO: Parsing new atom.");
     580    LOG(3,"INFO: Parsing new atom "+toString(*newAtom)+" "+toString(newAtom->getId())+".");
    610581  } else {
    611582    LOG(3,"INFO: Parsing present atom "+toString(*newAtom)+".");
     
    616587  string word;
    617588  ConvertTo<size_t> toSize_t;
    618 
    619   // assign highest+1 instead, but then beware of CONECT entries! Another map needed!
    620 //  if (!Inserter.second) {
    621 //    const size_t id = (*SerialSet.rbegin())+1;
    622 //    SerialSet.insert(id);
    623 //    atomInfo.set(PdbKey::serial, toString(id));
    624 //    ELOG(2, "Serial " << atomInfo.get<std::string>(PdbKey::serial) << " already present, "
    625 //        << "assigning " << toString(id) << " instead.");
    626 //  }
    627589
    628590  // check whether serial exists, if so, assign next available
     
    648610    // then fill info container
    649611    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,
    654         "FormatParser< pdb >::readAtomDataLine() - ATOM contains entry with serial "
    655         +atomInfo.get<std::string>(PdbKey::serial)+" already present!");
    656     setSerial(toSize_t(atomInfo.get<std::string>(PdbKey::serial)), newAtom->getId());
     612    // associate local with global id
     613    associateLocaltoGlobalId(toSize_t(atomInfo.get<std::string>(PdbKey::serial)), newAtom->getId());
    657614    // set position
    658615    Vector tempVector;
     
    791748        ListOfNeighbors.push_back(otherid);
    792749      else
    793         ELOG(2, "FormatParser< pdb >::readNeighbors() - discarding conect entry with id 0.");
     750        ELOG(2, "FormatParser< pdb >::readNeighbors() - discarding CONECT entry with id 0.");
    794751    } else  {
    795752      break;
     
    799756
    800757  // add neighbours
    801   atom *_atom = World::getInstance().getAtom(AtomById(getSerial(id)));
     758  atom *_atom = World::getInstance().getAtom(AtomById(getGlobalId(id)));
    802759  LOG(2, "STATUS: Atom " << _atom->getId() << " gets " << ListOfNeighbors.size() << " more neighbours.");
    803760  for (std::list<size_t>::const_iterator iter = ListOfNeighbors.begin();
    804761      iter != ListOfNeighbors.end();
    805762      ++iter) {
    806     atom * const _Otheratom = World::getInstance().getAtom(AtomById(getSerial(*iter)));
     763    atom * const _Otheratom = World::getInstance().getAtom(AtomById(getGlobalId(*iter)));
    807764    LOG(3, "INFO: Adding Bond (" << *_atom << "," << *_Otheratom << ")");
    808765    _atom->addBond(_step, _Otheratom);
    809766  }
    810767}
    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  */
    821 //string  FormatParser< pdb >::adaptIdDependentDataString(string data) {
    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();
    840 //  return "";
    841 //}
    842 
    843768
    844769bool FormatParser< pdb >::operator==(const FormatParser< pdb >& b) const
  • src/Parser/PdbParser.hpp

    rfbbcde rc0e28c  
    7373  bool isPresentadditionalAtomData(unsigned int _id);
    7474  PdbAtomInfoContainer& getadditionalAtomData(atom *_atom);
    75   size_t getSerial(const size_t atomid) const;
    76   void setSerial(const size_t localatomid, const size_t atomid);
    7775
    7876  // internal helper functions
    79   atom* getAtomToParse(std::string id_string) const;
     77  atom* getAtomToParse(std::string id_string);
    8078  void readPdbAtomInfoContainer(PdbAtomInfoContainer &atomInfo, std::string &line) const;
    8179
     
    10098   */
    10199  PdbAtomInfoContainer defaultAdditionalData;
    102 
    103   /**
    104    * Maps original atom IDs received from the parsed file to atom IDs in the
    105    * world.
    106    */
    107   std::map<size_t, size_t> atomIdMap;
    108 
    109   /**
    110    * Ascertaining uniqueness of Ids.
    111    */
    112   std::set<size_t> SerialSet;
    113100};
    114101
  • src/Parser/TremoloParser.cpp

    rfbbcde rc0e28c  
    103103  usedFields.clear();
    104104  additionalAtomData.clear();
    105   atomIdMap.clear();
    106105  knownKeys.clear();
    107106}
     
    116115  string::size_type location;
    117116
    118   // reset atomIdMap, for we now get new serials
    119   atomIdMap.clear();
     117  // reset the id maps
     118  resetIdAssociations();
     119
    120120  LOG(1, "INFO: Clearing usedFields.");
    121121  usedFields.clear();
     
    444444        ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!");
    445445        LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter);
    446         atomIdMap[toInt(*tok_iter)] = atomid;
     446        associateLocaltoGlobalId(toInt(*tok_iter), atomid);
    447447        tok_iter++;
    448448        break;
     
    525525        neighbor != currentInfo->second.neighbors.end(); neighbor++
    526526      ) {
    527 //        LOG(1, "INFO: Creating bond between ("
    528 //            << currentInfo->first
    529 //            << ") and ("
    530 //            << atomIdMap[*neighbor] << "|" << *neighbor << ")");
     527        LOG(3, "INFO: Creating bond between ("
     528            << currentInfo->first
     529            << ") and ("
     530            << getGlobalId(*neighbor) << "|" << *neighbor << ")");
     531        ASSERT(getGlobalId(*neighbor) != -1,
     532            "FormatParser< tremolo >::processNeighborInformation() - global id to local id "
     533            +toString(*neighbor)+" is unknown.");
    531534        World::getInstance().getAtom(AtomById(currentInfo->first))
    532             ->addBond(WorldTime::getTime(), World::getInstance().getAtom(AtomById(atomIdMap[*neighbor])));
     535            ->addBond(WorldTime::getTime(), World::getInstance().getAtom(AtomById(getGlobalId(*neighbor))));
    533536      }
    534537      currentInfo->second.neighbors_processed = true;
     
    558561
    559562  line >> id;
    560   result << atomIdMap[id];
     563  result << getGlobalId(id);
    561564  while (line.good()) {
    562565    line >> separator >> id;
    563     result << separator << atomIdMap[id];
     566    result << separator << getGlobalId(id);
    564567  }
    565568
  • src/Parser/TremoloParser.hpp

    rfbbcde rc0e28c  
    103103   */
    104104  TremoloAtomInfoContainer defaultAdditionalData;
    105 
    106   /**
    107    * Maps original atom IDs received from the parsed file to atom IDs in the
    108    * world.
    109    */
    110   std::map<int, int> atomIdMap;
    111105};
    112106
  • src/Parser/unittests/ParserPdbUnitTest.cpp

    rfbbcde rc0e28c  
    6666  parser = new FormatParser<pdb>();
    6767
    68   setVerbosity(2);
     68  setVerbosity(3);
    6969
    7070  // we need hydrogens and oxygens in the following tests
     
    9494  parser->save(&output, atoms);
    9595
    96 //  std::cout << "Save PDB is:" << std::endl;
     96  //  std::cout << "Save PDB is:" << std::endl;
    9797//  std::cout << output.str() << std::endl;
    9898
Note: See TracChangeset for help on using the changeset viewer.