Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 09:21:59

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 //
0027 /// \file SDDData.cc
0028 /// \brief Implementation of the SDDData class 
0029 
0030 #include "SDDData.hh"
0031 #include <iostream>
0032 #include <fstream>
0033 #include <sstream>
0034 #include <map>
0035 
0036 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
0037 
0038 SDDData::SDDData(std::string p_name):
0039 filename_(p_name)
0040 {
0041 }
0042 
0043 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0044 
0045 SDDData::SDDHeader SDDData::ReadHeader()
0046 {
0047 
0048     std::ifstream file(filename_.c_str());
0049     
0050     if(!file.is_open())
0051     {
0052         std::cout << "No file: " << filename_ << std::endl;
0053         return header_;
0054     }
0055     
0056     //1-SDD version
0057     ReadString(file,header_.sdd_version);
0058     //2-Software
0059     ReadString(file,header_.software);
0060     //3-Author
0061     ReadString(file,header_.author);
0062     //4-Simulation details
0063     ReadString(file,header_.sim_details);
0064 
0065     //5- Source
0066     ReadString(file,header_.src_details);
0067     //6-Source type
0068     ReadInt(file,header_.src_type);
0069     //7-Incident particles
0070     ReadInts(file,header_.src_pdg);
0071     //8-Mean Particle energy
0072     ReadDoubles(file,header_.src_energy);
0073     //9-Energy distribution
0074     ReadString(file,header_.energy_dist);
0075     //10-Particle fraction
0076     ReadDoubles(file,header_.part_fraction);
0077     //11-Dose or fluence
0078     ReadDoubles(file,header_.dose);
0079     //12-Dose rate
0080     ReadDouble(file,header_.dose_rate);
0081 
0082     //13-Irradiation target
0083     ReadString(file,header_.target);
0084     //14-Volumes
0085     ReadDoubles(file,header_.volumes);
0086     //15-Chromosome sizes
0087     ReadDoubles(file,header_.chromo_size);
0088     //16-DNA density
0089     ReadDouble(file,header_.dna_density);
0090 
0091     //17-Cell cycle phase
0092     ReadDoubles(file,header_.cell_cycle);
0093 
0094     //18-DNA strcuture
0095     ReadInts(file,header_.dna_struct);
0096 
0097     //19- in vitro/in vivo
0098     ReadInt(file,header_.vitro_vivo);
0099     
0100     //20-Proliferation status
0101     ReadString(file,header_.proliferation);
0102     
0103     //21-Microenvironment
0104     ReadDoubles(file,header_.microenv);
0105 
0106     //22-Damage definition
0107     ReadDoubles(file,header_.damage_def);
0108     
0109     //23-Time
0110     ReadDouble(file,header_.time);
0111     
0112     //24-Damage and primary count
0113     ReadInts(file,header_.damage_prim_count);
0114     
0115     //25-Data entries
0116     ReadBools(file,header_.entries);
0117 
0118     //26-Additional information
0119     ReadString(file,header_.info);
0120     
0121     file.close();
0122 
0123     return header_;
0124 }
0125 
0126 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0127 
0128 void SDDData::ParseData()
0129 {
0130     ReadHeader();
0131     
0132     data_.clear();
0133 
0134     std::ifstream file(filename_.c_str());
0135     
0136     std::string line;
0137 
0138     // Pass the header
0139     while(std::getline(file,line))
0140     {
0141         if(line.find("EndOfHeader")!=std::string::npos)
0142             break;
0143     }
0144 
0145     // Start to read the data
0146 
0147     while(std::getline(file,line))
0148     {
0149         if(!line.empty())
0150             ParseLineData(line);
0151     }
0152     
0153     file.close();
0154 }
0155 
0156 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0157 
0158 void SDDData::ParseLineData(std::string& line)
0159 {
0160 
0161     SDDDamage dmg;
0162 
0163     if(header_.entries[0])
0164         ExtractInts(line,2,dmg.classification);
0165     if(header_.entries[1])
0166         ExtractDoubles(line,3,dmg.coordinates);
0167     if(header_.entries[2])
0168         ExtractInts(line,4,dmg.chromo_ID);
0169     if(header_.entries[3])
0170         ExtractDoubles(line,1,dmg.chromo_position);
0171     if(header_.entries[4])
0172         ExtractInts(line,3,dmg.cause);
0173     if(header_.entries[5])
0174         ExtractInts(line,3,dmg.types);
0175     if(header_.entries[6])
0176         ExtractInts(line,3,dmg.break_spec);
0177     if(header_.entries[7])
0178         ExtractInts(line,3,dmg.dna_seq);
0179     if(header_.entries[8])
0180         ExtractDoubles(line,1,dmg.lesion_time);
0181     if(header_.entries[9])
0182         ExtractInts(line,1,dmg.particles);
0183     if(header_.entries[10])
0184         ExtractDoubles(line,1,dmg.energy);
0185     if(header_.entries[11])
0186         ExtractDoubles(line,3,dmg.translation);
0187     if(header_.entries[12])
0188         ExtractDoubles(line,1,dmg.direction);
0189     if(header_.entries[13])
0190         ExtractDoubles(line,1,dmg.particle_time);
0191 
0192     data_.push_back(dmg);
0193 }
0194 
0195 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0196 
0197 std::map<unsigned int,std::map<unsigned int,std::vector<Damage> > > SDDData::GetAllDamage()
0198 {
0199     if(data_.size()<=0)
0200     {
0201         ParseData();
0202     }
0203     
0204     std::map<unsigned int,std::map<unsigned int,std::vector<Damage> > > fmDamage;
0205     
0206     for(auto it=data_.begin();it!=data_.end();it++)
0207     {
0208 
0209         Damage::DamageType pType = Damage::DamageType::fOther;
0210         unsigned int pChromo = -1;
0211         unsigned int pEvt = -1;
0212         unsigned int pStrand = -1;
0213         unsigned long int pCopyNb = -1;
0214         Position pPos(0,0,0);
0215         Damage::DamageCause pCause = Damage::DamageCause::fUnknown;
0216         Damage::DamageChromatin pChromatin = Damage::DamageChromatin::fUnspecified;
0217 
0218         if(header_.entries[0])
0219         {
0220             pEvt = it->classification[1];
0221         }
0222         if(header_.entries[1])
0223         {
0224             pPos.setX(it->coordinates[0]);
0225             pPos.setY(it->coordinates[1]);
0226             pPos.setZ(it->coordinates[2]);
0227         }
0228         if(header_.entries[2])
0229         {
0230             switch(it->chromo_ID[0])
0231             {
0232                 case 0:
0233                 pChromatin = Damage::DamageChromatin::fUnspecified;
0234                 break;
0235                 case 1:
0236                 pChromatin = Damage::DamageChromatin::fHeterochromatin;
0237                 break;
0238                 case 2:
0239                 pChromatin = Damage::DamageChromatin::fEuchromatin;
0240                 break;
0241                 case 3:
0242                 pChromatin = Damage::DamageChromatin::fFreeDNA;
0243                 break;
0244                 case 4:
0245                 pChromatin = Damage::DamageChromatin::fOtherDNA;
0246                 break;
0247                 default:
0248                 pChromatin = Damage::DamageChromatin::fUnspecified;
0249             }
0250             pChromo = it->chromo_ID[1];
0251             pStrand = it->chromo_ID[3];
0252         }
0253         if(header_.entries[3])
0254         {
0255             pCopyNb = (unsigned long int)(it->chromo_position[0]);
0256         }
0257         if(header_.entries[4])
0258         {
0259             switch(it->cause[0])
0260             {
0261                 case 0:
0262                 pCause = Damage::DamageCause::fDirect;
0263                 break;
0264                 case 1:
0265                 pCause = Damage::DamageCause::fIndirect;
0266                 break;
0267                 default:
0268                 pCause = Damage::DamageCause::fUnknown;
0269 
0270             }
0271         }
0272         if(header_.entries[5])
0273         {
0274             if(it->types[0]>0)
0275                 pType = Damage::DamageType::fBase;
0276             if(it->types[1]>0)
0277                 pType = Damage::DamageType::fBackbone;
0278         }
0279         Damage aDamage(pType,pChromo,pEvt,pStrand,pCopyNb,pPos,pCause,pChromatin);
0280         auto chroPos = fmDamage.find(pChromo);
0281         if (chroPos == fmDamage.end()) {
0282             std::vector<Damage> dmv{aDamage};
0283             std::map<unsigned int,std::vector<Damage> > evtDamages{{pEvt,dmv}};
0284             fmDamage.insert({pChromo,evtDamages});
0285         } else {
0286             auto evtPos = chroPos->second.find(pEvt);
0287             if (evtPos == chroPos->second.end()) {
0288                 std::vector<Damage> dmv{aDamage};
0289                 chroPos->second.insert({pEvt,dmv});
0290             } else {
0291                 chroPos->second[pEvt].push_back(aDamage);
0292             }
0293         }
0294     }
0295     
0296     return fmDamage;
0297 }
0298 
0299 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0300 
0301 void SDDData::ExtractInts(std::string& strLine,int numInt,std::vector<int>& field)
0302 {
0303     std::string delimiter = ";";
0304     std::string token;
0305 
0306     size_t pos = strLine.find(delimiter);
0307 
0308     if(pos!=std::string::npos)
0309     {
0310         token = strLine.substr(0,pos);
0311         strLine.erase(0,pos+delimiter.length());
0312     }
0313     else
0314     {
0315         token = strLine;
0316         strLine = "";
0317     }
0318 
0319     std::string value;
0320     delimiter = ",";
0321 
0322     for(int i=1;i<numInt;i++)
0323     {
0324         pos = token.find(delimiter);
0325         value = token.substr(0,pos);
0326         field.push_back(std::atoi(value.c_str()));
0327         token.erase(0,pos+delimiter.length());
0328     }
0329 
0330     field.push_back(std::atoi(token.c_str()));
0331 }
0332 
0333 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0334 
0335 void SDDData::ExtractDoubles(std::string& strLine,int numInt,std::vector<double>& field)
0336 {
0337     std::string delimiter = ";";
0338     std::string token;
0339 
0340     size_t pos = strLine.find(delimiter);
0341 
0342     if(pos!=std::string::npos)
0343     {
0344         token = strLine.substr(0,pos);
0345         strLine.erase(0,pos+delimiter.length());
0346     }
0347     else
0348     {
0349         token = strLine;
0350         strLine = "";
0351     }
0352 
0353     std::string value;
0354     delimiter = ",";
0355 
0356     for(int i=1;i<numInt;i++)
0357     {
0358         pos = token.find(delimiter);
0359         value = token.substr(0,pos);
0360         field.push_back(std::atoi(value.c_str()));
0361         token.erase(0,pos+delimiter.length());
0362     }
0363 
0364     field.push_back(std::stod(token.c_str()));
0365 }
0366 
0367 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0368 
0369 void SDDData::ReadString(std::ifstream& file,std::string& field)
0370 {
0371     std::string line;
0372     std::string token;
0373 
0374     std::getline(file,line);
0375     std::istringstream ss(line);
0376 
0377     std::getline(ss,token,',');
0378     std::getline(ss,token,',');
0379 
0380     field=token;
0381 }
0382 
0383 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0384 
0385 void SDDData::ReadInt(std::ifstream& file,int& field)
0386 {
0387     std::string line;
0388     std::string token;
0389 
0390     std::getline(file,line);
0391     line = line.substr(0, line.size()-1);
0392 
0393     std::istringstream ss(line);
0394 
0395     std::getline(ss,token,',');
0396     std::getline(ss,token,',');
0397 
0398     field=std::atoi(token.c_str());
0399 }
0400 
0401 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0402 
0403 void SDDData::ReadInts(std::ifstream& file,std::vector<int>& field)
0404 {
0405     std::string line;
0406     std::string token;
0407 
0408     std::getline(file,line);
0409     line = line.substr(0, line.size()-1);
0410 
0411     std::istringstream ss(line);
0412 
0413     std::getline(ss,token,',');
0414 
0415     while(std::getline(ss,token,','))
0416         field.push_back(std::atoi(token.c_str()));
0417 }
0418 
0419 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0420 
0421 void SDDData::ReadDouble(std::ifstream& file,double& field)
0422 {
0423     std::string line;
0424     std::string token;
0425 
0426     std::getline(file,line);
0427     line = line.substr(0, line.size()-1);
0428 
0429     std::istringstream ss(line);
0430 
0431     std::getline(ss,token,',');
0432     std::getline(ss,token,',');
0433 
0434     field=std::stod(token.c_str());
0435 }
0436 
0437 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0438 
0439 void SDDData::ReadDoubles(std::ifstream& file,std::vector<double>& field)
0440 {
0441     std::string line;
0442     std::string token;
0443 
0444     std::getline(file,line);
0445     line = line.substr(0, line.size()-1);
0446 
0447     std::istringstream ss(line);
0448 
0449     std::getline(ss,token,',');
0450 
0451     while(std::getline(ss,token,','))
0452         field.push_back(std::stod(token.c_str()));
0453 }
0454 
0455 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0456 
0457 void SDDData::ReadBools(std::ifstream& file,std::vector<bool>& field)
0458 {
0459     std::string line;
0460     std::string token;
0461 
0462     std::getline(file,line);
0463     line = line.substr(0, line.size()-1);
0464 
0465     std::istringstream ss(line);
0466 
0467     std::getline(ss,token,',');
0468 
0469     while(std::getline(ss,token,','))
0470         field.push_back(std::stoi(token.c_str()));
0471 }
0472 
0473 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0474 
0475 double SDDData::GetDose()
0476 {
0477     return header_.dose[1];
0478 }
0479 
0480 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0481 
0482 std::map<int,unsigned long long int> SDDData::GetChromosomeBpSizesMap(double &sum)
0483 {
0484     sum=0;
0485     std::map<int,unsigned long long int> chromap;
0486     for (int i=1;i<header_.chromo_size.size(); i++) { // index 0 of header_.chromo_size is number of chromosomes
0487         double nbp = header_.chromo_size[i]; // in Mbp
0488         nbp *= 1e+6; // to bp
0489         sum += nbp;
0490         chromap.insert({(i-1),nbp}); 
0491     }
0492     return chromap;
0493 }
0494 
0495 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......