Nemo  2.4.0b
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
832 {}

Member Function Documentation

◆ FHread()

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

Implements FileHandler.

834 {}

◆ FHwrite()

void TTQFreqExtractor::FHwrite ( )
virtual

Implements TraitFileHandler< TProtoQuanti >.

6568 {
6569  // make sure we don't use a sub-sampled pop
6570  Metapop* pop = get_service()->get_pop_ptr();
6571  int patchNbr = pop->getPatchNbr();
6572  Patch* current_patch;
6573  Individual* ind;
6574  TTQuanti* trait;
6575 
6576  const TMatrix& snp_allele = _FHLinkedTrait->get_diallele_values();
6577 
6578  bool _isAsymmetrical = _FHLinkedTrait->get_allele_model() == 2;
6579 
6580  unsigned int BIT = _isAsymmetrical; // read the derived allele value at position 1 in asymmetrical model
6581  unsigned int loc, rec;
6582  unsigned int nb_trait = _FHLinkedTrait->get_num_traits();
6583  unsigned int seqLength = _FHLinkedTrait->get_seq_length();
6584 
6585 
6586  ostringstream REC;
6587  string current;
6588 
6589  //first generation to record, build the record table;
6590 
6591  if(pop->getCurrentGeneration() == this->get_GenerationOccurrence() ||
6592  pop->getCurrentGeneration() == 1) {
6593 
6594  resetTable();
6595 
6596  //create header
6597  _records.push_back("pop trait locus allele g" + tstring::int2str(pop->getCurrentGeneration()));
6598 
6599  for(int i = 0; i < patchNbr; i++)
6600  for(unsigned int k = 0; k < nb_trait; k++) {
6601  for(unsigned int l = 0, LOCUS; l < _FHLinkedTrait->get_num_locus(k); l++) {
6602 
6603  LOCUS = _FHLinkedTrait->get_locus_ID(l,k);
6604 
6605  REC<<i+1<<" "<<k+1<<" "<<LOCUS+1<<" "<<snp_allele.get( LOCUS, BIT ); //only '+' or 'A' allele, depends on allele model
6606 
6607  _records.push_back(REC.str());
6608 
6609  REC.str("");
6610 
6611  }
6612  }
6613 
6614  } else { //add a 'column' for each generation recorded
6615 
6616  current = _records[0];
6617 
6618  _records[0] = current + " g" + tstring::int2str(pop->getCurrentGeneration());
6619  }
6620 
6621 
6622  //ONLY IMPLEMENTED FOR DIALLELIC LOCI!
6623 
6624 #ifdef _DEBUG_
6625  message("TTQFreqExtractor::compute allele frequencies\n");
6626 #endif
6627 
6628  // we are calculating allele frequencies of the whole set of alleles
6629  // thus also considers cases with pleiotropy
6630  vector<double> snp_tab(seqLength, 0.0);
6631 
6632  rec = 1;
6633  for(int i = 0; i < patchNbr; i++) {
6634 
6635  current_patch = pop->getPatch(i);
6636 
6637  snp_tab.assign(seqLength, 0.0);
6638 
6639  // cycle through whole population
6640  for(unsigned int s = 0; s < 2; ++s) {
6641 
6642  for(unsigned int j = 0, size = current_patch->size(sex_t(s), ADLTx); j < size; ++j) {
6643 
6644  ind = current_patch->get(sex_t(s), ADLTx, j);
6645 
6646  trait = dynamic_cast<TTQuanti*>(ind->getTrait(_FHLinkedTraitIndex));
6647 
6648  //compute allele freq at all positions, we count allele 1 for 'q' (freq 'p' is for trait-increasing allele 0)
6649  for(unsigned int i = 0; i < seqLength; ++i) {
6650  snp_tab[i] += trait->get_allele_bit(i, 0) + trait->get_allele_bit(i, 1);
6651  }
6652 
6653  }
6654  }
6655 
6656  double inv2N = 1.0 / double(2*current_patch->size(ADLTx));
6657 
6658  //calculate and record frequencies
6659  for(unsigned int k = 0; k < nb_trait; k++) {
6660  for(unsigned int l = 0; l < _FHLinkedTrait->get_num_locus(k); l++) {
6661 
6662  loc = _FHLinkedTrait->get_locus_seq_pos(l,k);
6663 
6664  assert(rec < _records.size());
6665 
6666  current = _records[ rec ];
6667 
6668  if(_isAsymmetrical)
6669  _records[ rec ] = _records[ rec ] + " " + tstring::dble2str( snp_tab[loc] * inv2N ); // we report the count of 1's (referred as 'q' above)
6670  else
6671  _records[ rec ] = _records[ rec ] + " " + tstring::dble2str(1.0 - (snp_tab[loc] * inv2N) ); // we report the count of 0's, or 'p' = (1-q)
6672 
6673  rec++;
6674  }
6675  }
6676  }
6677 
6678  if(pop->getCurrentGeneration() == pop->getGenerations() ) { //have we reached the last generation?
6679 
6680  std::string filename = get_path() + get_service()->getReplicateFileName() + get_extension();
6681  std::ofstream FILE ;
6682 
6683  //open for writing
6684  FILE.open(filename.c_str(), ios::out);
6685 
6686 #ifdef _DEBUG_
6687  message("TTQFreqExtractor::FHwrite (%s)\n",filename.c_str());
6688 #endif
6689 
6690  if(!FILE) fatal("Trait quanti could not open output file: \"%s\"\n",filename.c_str());
6691 
6692  for(unsigned int i = 0; i < _records.size(); ++i)
6693  FILE<<_records[i]<<endl;
6694 
6695  FILE.close();
6696  }
6697 }
std::string & get_path()
Definition: filehandler.h:139
FileServices * get_service()
Returns pointer to the FileServices.
Definition: filehandler.h:135
std::string & get_extension()
Definition: filehandler.h:143
string & getReplicateFileName()
Accessor to the current replicate file name.
Definition: fileservices.cc:429
virtual Metapop * get_pop_ptr()
Accessor to the pointer to the main population.
Definition: fileservices.h:111
This class contains traits along with other individual information (sex, pedigree,...
Definition: individual.h:49
TTrait * getTrait(IDX T)
Trait accessor.
Definition: individual.h:277
Top class of the metapopulation structure, contains the patches.
Definition: metapop.h:80
unsigned int getGenerations()
Definition: metapop.h:273
unsigned int getPatchNbr()
Definition: metapop.h:276
Patch * getPatch(unsigned int i)
Patch accessor, return the ith+1 patch in the metapop.
Definition: metapop.h:257
unsigned int getCurrentGeneration()
Definition: metapop.h:296
Second class in the metapopulation design structure, between the Metapop and Individual classes.
Definition: metapop.h:432
unsigned int size(age_t AGE)
Returns the size of the container of the appropriate age class(es) for both sexes.
Definition: metapop.h:498
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:534
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:561
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:50
double get(unsigned int i, unsigned int j) const
Accessor to element at row i and column j.
Definition: tmatrix.h:193
const TMatrix & get_diallele_values()
Definition: ttquanti.h:429
unsigned int get_locus_ID(unsigned int locus, unsigned int trait)
Definition: ttquanti.h:442
unsigned int get_allele_model()
Definition: ttquanti.h:428
unsigned int get_locus_seq_pos(unsigned int loc, unsigned int trait)
Definition: ttquanti.h:440
unsigned int get_num_locus()
Definition: ttquanti.h:418
unsigned int get_seq_length()
Definition: ttquanti.h:421
unsigned int get_num_traits()
Definition: ttquanti.h:417
vector< string > _records
Definition: ttquanti.h:825
void resetTable()
Definition: ttquanti.cc:6560
TTQuanti.
Definition: ttquanti.h:59
virtual bool get_allele_bit(unsigned int position, unsigned int allele) const =0
int _FHLinkedTraitIndex
Definition: filehandler.h:220
TProtoQuanti * _FHLinkedTrait
Definition: filehandler.h:219
static string int2str(const int i)
Writes an integer value into a string.
Definition: tstring.h:95
static string dble2str(const double d)
Writes a floating-point value into a string.
Definition: tstring.h:115
void fatal(const char *str,...)
Definition: output.cc:96
void message(const char *message,...)
Definition: output.cc:40
sex_t
Sex types, males are always 0 and females 1!!
Definition: types.h:36
@ ADLTx
Definition: types.h:42

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 ( )
6561 {
6562  if(_records.size() != 0) _records.clear();
6563 }

References _records.

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

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.0b by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR