Nemo  2.4.0
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
TTQFreqExtractor Class Reference

TTQFreqExtractor. More...

#include <ttquanti.h>

+ Inheritance diagram for TTQFreqExtractor:
+ Collaboration diagram for TTQFreqExtractor:

Public Member Functions

 TTQFreqExtractor (TProtoQuanti *T)
 
void resetTable ()
 
virtual ~TTQFreqExtractor ()
 
virtual void FHwrite ()
 
virtual void FHread (string &filename)
 
- Public Member Functions inherited from TraitFileHandler< TProtoQuanti >
 TraitFileHandler (TProtoQuanti *trait_proto, const char *ext)
 
virtual ~TraitFileHandler ()
 
virtual void FHread (string &filename)=0
 
virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, TProtoQuanti *trait_proto)
 
virtual void set_multi (bool rpl_per, bool gen_per, int rpl_occ, TMatrix *Occ, string path, TProtoQuanti *trait_proto)
 
- Public Member Functions inherited from FileHandler
 FileHandler (const char *ext)
 
virtual ~FileHandler ()
 
virtual void init ()
 Called by notifier during simulation setup, performs file checking. More...
 
virtual vector< string > ifExist ()
 Checks if any file associated with the current file name already exists on disk. More...
 
virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path)
 Sets the hanlder parameters. More...
 
virtual void set_multi (bool rpl_per, bool gen_per, int rpl_occ, TMatrix *Occ, string path)
 
virtual void update ()
 Updates the inner replicate and generation counters and calls FHwrite if needed by the the periodicity of the file. More...
 
Metapopget_pop_ptr ()
 Returns the pointer to the current metapop through the FileServices interface. More...
 
void set_pop_ptr (Metapop *pop_ptr)
 
FileServicesget_service ()
 Returns pointer to the FileServices. More...
 
void set_service (FileServices *srv)
 
std::string & get_path ()
 
void set_path ()
 
std::string & get_extension ()
 
void set_extension (const char *ext)
 
std::string & get_filename ()
 Builds and returns the current file name depending on the periodicity of the file. More...
 
bool get_isInputHandler ()
 
void set_isInputHandler (bool val)
 
bool get_isReplicatePeriodic ()
 
void set_isReplicatePeriodic (bool val)
 
unsigned int get_ReplicateOccurrence ()
 
void set_ReplicateOccurrence (unsigned int val)
 
bool get_isGenerationPeriodic ()
 
void set_isGenerationPeriodic (bool val)
 
unsigned int get_GenerationOccurrence ()
 
void set_GenerationOccurrence (unsigned int val)
 
unsigned int get_ExecRank ()
 unused yet... More...
 
void set_ExecRank (int val)
 
TMatrixget_OccMatrix ()
 
void set_OccMatrix (TMatrix *occ)
 
bool get_isMasterExec ()
 
void set_isMasterExec (bool is)
 
- Public Member Functions inherited from Handler
virtual ~Handler ()
 

Private Attributes

vector< string > _records
 

Additional Inherited Members

- Protected Attributes inherited from TraitFileHandler< TProtoQuanti >
TProtoQuanti_FHLinkedTrait
 
int _FHLinkedTraitIndex
 
- Protected Attributes inherited from FileHandler
Metapop_pop
 Pointer to the current metapop, set during initialization within the init function. More...
 

Detailed Description

Constructor & Destructor Documentation

◆ TTQFreqExtractor()

TTQFreqExtractor::TTQFreqExtractor ( TProtoQuanti T)
inline

◆ ~TTQFreqExtractor()

virtual TTQFreqExtractor::~TTQFreqExtractor ( )
inlinevirtual
844 {}

Member Function Documentation

◆ FHread()

virtual void TTQFreqExtractor::FHread ( string &  filename)
inlinevirtual

Implements FileHandler.

846 {}

◆ FHwrite()

void TTQFreqExtractor::FHwrite ( )
virtual

Implements TraitFileHandler< TProtoQuanti >.

6327 {
6328  // make sure we don't use a sub-sampled pop
6329  Metapop* pop = get_service()->get_pop_ptr();
6330  int patchNbr = pop->getPatchNbr();
6331  Patch* current_patch;
6332  Individual* ind;
6333  TTQuanti* trait;
6334 
6335  const TMatrix& snp_allele = _FHLinkedTrait->get_diallele_values();
6336 
6337  bool _isAsymmetrical = _FHLinkedTrait->get_allele_model() == 2;
6338 
6339  unsigned int BIT = _isAsymmetrical; // read the derived allele value at position 1 in asymmetrical model
6340  unsigned int loc, rec;
6341  unsigned int nb_trait = _FHLinkedTrait->get_num_traits();
6342  unsigned int seqLength = _FHLinkedTrait->get_seq_length();
6343 
6344 
6345  ostringstream REC;
6346  string current;
6347 
6348  //first generation to record, build the record table;
6349 
6350  if(pop->getCurrentGeneration() == this->get_GenerationOccurrence() ||
6351  pop->getCurrentGeneration() == 1) {
6352 
6353  resetTable();
6354 
6355  //create header
6356  _records.push_back("pop trait locus allele g" + tstring::int2str(pop->getCurrentGeneration()));
6357 
6358  for(int i = 0; i < patchNbr; i++)
6359  for(unsigned int k = 0; k < nb_trait; k++) {
6360  for(unsigned int l = 0, LOCUS; l < _FHLinkedTrait->get_num_locus(k); l++) {
6361 
6362  LOCUS = _FHLinkedTrait->get_locus_ID(l,k);
6363 
6364  REC<<i+1<<" "<<k+1<<" "<<LOCUS+1<<" "<<snp_allele.get( LOCUS, BIT ); //only '+' or 'A' allele, depends on allele model
6365 
6366  _records.push_back(REC.str());
6367 
6368  REC.str("");
6369 
6370  }
6371  }
6372 
6373  } else { //add a 'column' for each generation recorded
6374 
6375  current = _records[0];
6376 
6377  _records[0] = current + " g" + tstring::int2str(pop->getCurrentGeneration());
6378  }
6379 
6380 
6381  //ONLY IMPLEMENTED FOR DIALLELIC LOCI!
6382 
6383 #ifdef _DEBUG_
6384  message("TTQFreqExtractor::compute allele frequencies\n");
6385 #endif
6386 
6387  // we are calculating allele frequencies of the whole set of alleles
6388  // thus also considers cases with pleiotropy
6389  vector<double> snp_tab(seqLength, 0.0);
6390 
6391  rec = 1;
6392  for(int i = 0; i < patchNbr; i++) {
6393 
6394  current_patch = pop->getPatch(i);
6395 
6396  snp_tab.assign(seqLength, 0.0);
6397 
6398  // cycle through whole population
6399  for(unsigned int s = 0; s < 2; ++s) {
6400 
6401  for(unsigned int j = 0, size = current_patch->size(sex_t(s), ADLTx); j < size; ++j) {
6402 
6403  ind = current_patch->get(sex_t(s), ADLTx, j);
6404 
6405  trait = dynamic_cast<TTQuanti*>(ind->getTrait(_FHLinkedTraitIndex));
6406 
6407  //compute allele freq at all positions, we count allele 1 for 'q' (freq 'p' is for trait-increasing allele 0)
6408  for(unsigned int i = 0; i < seqLength; ++i) {
6409  snp_tab[i] += trait->get_allele_bit(i, 0) + trait->get_allele_bit(i, 1);
6410  }
6411 
6412  }
6413  }
6414 
6415  double inv2N = 1.0 / double(2*current_patch->size(ADLTx));
6416 
6417  //calculate and record frequencies
6418  for(unsigned int k = 0; k < nb_trait; k++) {
6419  for(unsigned int l = 0; l < _FHLinkedTrait->get_num_locus(k); l++) {
6420 
6421  loc = _FHLinkedTrait->get_locus_seq_pos(l,k);
6422 
6423  assert(rec < _records.size());
6424 
6425  current = _records[ rec ];
6426 
6427  if(_isAsymmetrical)
6428  _records[ rec ] = _records[ rec ] + " " + tstring::dble2str( snp_tab[loc] * inv2N ); // we report the count of 1's (referred as 'q' above)
6429  else
6430  _records[ rec ] = _records[ rec ] + " " + tstring::dble2str(1.0 - (snp_tab[loc] * inv2N) ); // we report the count of 0's, or 'p' = (1-q)
6431 
6432  rec++;
6433  }
6434  }
6435  }
6436 
6437  if(pop->getCurrentGeneration() == pop->getGenerations() ) { //have we reached the last generation?
6438 
6439  std::string filename = get_path() + get_service()->getReplicateFileName() + get_extension();
6440  std::ofstream FILE ;
6441 
6442  //open for writing
6443  FILE.open(filename.c_str(), ios::out);
6444  std::ios_base::sync_with_stdio(false); // better for writing performances
6445 
6446 #ifdef _DEBUG_
6447  message("TTQFreqExtractor::FHwrite (%s)\n",filename.c_str());
6448 #endif
6449 
6450  if(!FILE) fatal("Trait quanti could not open output file: \"%s\"\n",filename.c_str());
6451 
6452  for(unsigned int i = 0; i < _records.size(); ++i)
6453  FILE<<_records[i]<<endl;
6454 
6455  FILE.close();
6456 
6457  std::ios_base::sync_with_stdio(true); // reset
6458  }
6459 }
std::string & get_path()
Definition: filehandler.h:142
FileServices * get_service()
Returns pointer to the FileServices.
Definition: filehandler.h:138
std::string & get_extension()
Definition: filehandler.h:146
string & getReplicateFileName()
Accessor to the current replicate file name.
Definition: fileservices.cc:457
virtual Metapop * get_pop_ptr()
Accessor to the pointer to the main population.
Definition: fileservices.h:112
This class contains traits along with other individual information (sex, pedigree,...
Definition: individual.h:48
TTrait * getTrait(IDX T)
Trait accessor.
Definition: individual.h:276
Top class of the metapopulation structure, contains the patches.
Definition: metapop.h:79
unsigned int getGenerations()
Definition: metapop.h:272
unsigned int getPatchNbr()
Definition: metapop.h:275
Patch * getPatch(unsigned int i)
Patch accessor, return the ith+1 patch in the metapop.
Definition: metapop.h:256
unsigned int getCurrentGeneration()
Definition: metapop.h:295
Second class in the metapopulation design structure, between the Metapop and Individual classes.
Definition: metapop.h:431
unsigned int size(age_t AGE)
Returns the size of the container of the appropriate age class(es) for both sexes.
Definition: metapop.h:497
Individual * get(sex_t SEX, age_idx AGE, unsigned int at)
Returns a pointer to the individual sitting at the index passed.
Definition: metapop.h:533
void assign(sex_t SEX, age_idx AGE, size_t n)
Assigns a new container of given size for the sex and age class passed, sets all values to NULL.
Definition: metapop.h:560
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
double get(unsigned int i, unsigned int j) const
Accessor to element at row i and column j.
Definition: tmatrix.h:192
const TMatrix & get_diallele_values()
Definition: ttquanti.h:434
unsigned int get_locus_ID(unsigned int locus, unsigned int trait)
Definition: ttquanti.h:454
unsigned int get_allele_model()
Definition: ttquanti.h:433
unsigned int get_locus_seq_pos(unsigned int loc, unsigned int trait)
Definition: ttquanti.h:452
unsigned int get_num_locus()
Definition: ttquanti.h:423
unsigned int get_seq_length()
Definition: ttquanti.h:426
unsigned int get_num_traits()
Definition: ttquanti.h:422
vector< string > _records
Definition: ttquanti.h:837
void resetTable()
Definition: ttquanti.cc:6319
TTQuanti.
Definition: ttquanti.h:60
virtual bool get_allele_bit(unsigned int position, unsigned int allele) const =0
int _FHLinkedTraitIndex
Definition: filehandler.h:223
TProtoQuanti * _FHLinkedTrait
Definition: filehandler.h:222
static string int2str(const int i)
Writes an integer value into a string.
Definition: tstring.h:94
static string dble2str(const double d)
Writes a floating-point value into a string.
Definition: tstring.h:114
void fatal(const char *str,...)
Definition: output.cc:99
void message(const char *message,...)
Definition: output.cc:39
sex_t
Sex types, males are always 0 and females 1!!
Definition: types.h:35
@ ADLTx
Definition: types.h:41

References TraitFileHandler< TProtoQuanti >::_FHLinkedTrait, TraitFileHandler< TProtoQuanti >::_FHLinkedTraitIndex, _records, ADLTx, Patch::assign(), tstring::dble2str(), fatal(), Patch::get(), TMatrix::get(), TTQuanti::get_allele_bit(), TProtoQuanti::get_allele_model(), TProtoQuanti::get_diallele_values(), FileHandler::get_extension(), TProtoQuanti::get_locus_ID(), TProtoQuanti::get_locus_seq_pos(), TProtoQuanti::get_num_locus(), TProtoQuanti::get_num_traits(), FileHandler::get_path(), FileServices::get_pop_ptr(), TProtoQuanti::get_seq_length(), FileHandler::get_service(), Metapop::getCurrentGeneration(), Metapop::getGenerations(), Metapop::getPatch(), Metapop::getPatchNbr(), FileServices::getReplicateFileName(), Individual::getTrait(), tstring::int2str(), message(), resetTable(), and Patch::size().

◆ resetTable()

void TTQFreqExtractor::resetTable ( )
6320 {
6321  if(_records.size() != 0) _records.clear();
6322 }

References _records.

Referenced by FHwrite(), and TProtoQuanti::loadFileServices().

+ Here is the caller graph for this function:

Member Data Documentation

◆ _records

vector< string > TTQFreqExtractor::_records
private

Referenced by FHwrite(), and resetTable().


The documentation for this class was generated from the following files:

Generated for Nemo v2.4.0 by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR