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

TTQOhtaStats. More...

#include <ttquanti.h>

+ Inheritance diagram for TTQOhtaStats:
+ Collaboration diagram for TTQOhtaStats:

Public Member Functions

 TTQOhtaStats (TProtoQuanti *T)
 
virtual ~TTQOhtaStats ()
 
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

TMatrix _pairwiseCombs
 

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

◆ TTQOhtaStats()

TTQOhtaStats::TTQOhtaStats ( TProtoQuanti T)
inline

◆ ~TTQOhtaStats()

virtual TTQOhtaStats::~TTQOhtaStats ( )
inlinevirtual
861 {}

Member Function Documentation

◆ FHread()

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

Implements FileHandler.

863 {}

◆ FHwrite()

void TTQOhtaStats::FHwrite ( )
virtual

Implements TraitFileHandler< TProtoQuanti >.

6466 {
6467 
6468  // Output Ohta stats :
6469  // Only if pop alive
6470  // Only for diallelic mutation models
6471  // Only for a single trait
6472  // Only for multi-population scenarios
6473  if ((!_pop->isAlive()) || (_FHLinkedTrait->get_allele_model() > 2)
6474  || (_FHLinkedTrait->get_num_traits() > 1) || (_pop->getPatchNbr() < 2)) {
6475  error("TTQOhtaStats::quanti trait and population are not compatible with Ohta stats (min. 2 patches, one di-allelic trait), file will not be written.\n");
6476  return ;
6477  }
6478 
6479  // sets the pop ptr to a sub sampled pop, if sub sampling happened
6480  if(!get_service()) fatal("TTQuantiFH: link to file services not set!!\n");
6481  Metapop* pop = get_service()->getSampledPop();
6482  int patchNbr = pop->getPatchNbr();
6483  unsigned int nb_trait = _FHLinkedTrait->get_num_traits();
6484  unsigned int nb_locus = _FHLinkedTrait->get_num_locus();
6485  TTQuanti_diallelic* trait;
6486 
6487 
6488  _pairwiseCombs.copy(nChooseKVec(nb_locus, 2));
6489 
6490  unsigned int num_comb = _pairwiseCombs.nrows();
6491 
6492  ostringstream REC;
6493  Individual* ind;
6494  Patch* current_patch;
6495 
6496  vector<double> Dis(num_comb, 0.0),
6497  Dst(num_comb, 0.0),
6498  Disp(num_comb, 0.0),
6499  Dstp(num_comb, 0.0);
6500 
6501  vector< vector<double> > rSquare = vector< vector<double> > (patchNbr, vector<double>(num_comb, 0.0));
6502 
6503  vector<bool> NA(num_comb, false);
6504 
6505  int total_size = 0;
6506  int extant_patches = 0;
6507 
6508  vector<int> patchSizes(patchNbr, 0);
6509 
6510 
6511  // pre-record patch sizes and number of extant patches
6512  for(int patch = 0; patch < patchNbr; patch++) {
6513 
6514  current_patch = pop->getPatch(patch);
6515 
6516  patchSizes[patch] = 2 * (current_patch->size(FEM, ADLTx) + current_patch->size(MAL, ADLTx));
6517 
6518  if(patchSizes[patch]) ++extant_patches; // count patches with individuals
6519 
6520  total_size += patchSizes[patch];
6521  }
6522 
6523 #ifdef _DEBUG_
6524  message("TTQOhtaStats::FHwrite:: computing association stats of %i combinations\n", num_comb);
6525  fflush(stdout);
6526 #endif
6527 
6528  unsigned int a1, a2;
6529  unsigned int twoLocHapMap[2][2] = {{0,1},{2,3}}; // genot: AB, Ab, aB, ab; A = 0, a = 1, B = 2, b = 3
6530  unsigned int reverseHapMap[4][2] = {{0,2},{0,3},{1,2},{1,3}}; // AB -> {0,2} ...
6531 
6532  vector< double > meanAlleleFreq(4,0.0); // allele counter at the two loci,"A" = [0], "a" = [1], "B" = [2], "b" = [3]
6533  vector< double > meanHapFreq(4,0.0); // genotype counter
6534 
6535  vector< vector< double > > alleleFreq = vector< vector< double > > (patchNbr, vector< double >(4,0.0));
6536  vector< vector< double > > hapFreq = vector< vector< double > > (patchNbr, vector< double >(4,0.0));
6537 
6538  // cycling through all pairwise locus combinations
6539  for (size_t pcomb = 0; pcomb < num_comb; pcomb++) {
6540 
6541  size_t loc1 = _pairwiseCombs.get(pcomb,0),
6542  loc2 = _pairwiseCombs.get(pcomb,1);
6543 
6544  meanAlleleFreq.assign(4, 0.0);
6545  meanHapFreq.assign(4,0.0);
6546 
6547  for(int patch = 0; patch < patchNbr; patch++) {
6548 
6549  current_patch = pop->getPatch(patch);
6550 
6551  alleleFreq[patch].assign(4,0.0);
6552  hapFreq[patch].assign(4,0.0);
6553 
6554  if(patchSizes[patch] == 0) continue;
6555 
6556  // @TODO adapt Ohta stat output for any age class not only adults
6557  // All adults in a patch
6558  for(unsigned int s =0; s < 2; ++s) { // two sexes
6559 
6560  for(unsigned int j = 0, size = current_patch->size(sex_t(s), ADLTx); j < size; j++) {
6561 
6562  ind = current_patch->get(sex_t(s), ADLTx, j);
6563 
6564  trait = dynamic_cast<TTQuanti_diallelic*>( ind->getTrait(_FHLinkedTraitIndex));
6565 
6566  // Homologous copy 1
6567  // locus 1; "A" = 0, "a" = 1
6568  if ( !trait->get_allele_bit(loc1, 0) ) { //reference allele is allele '0'
6569  ++alleleFreq[patch][0];
6570  a1 = 0;
6571  }
6572  else {
6573  ++alleleFreq[patch][1];
6574  a1 = 1;
6575  }
6576 
6577  // locus 2
6578  if (!trait->get_allele_bit(loc2, 0)) {
6579  ++alleleFreq[patch][2];
6580  a2 = 0;
6581  }
6582  else {
6583  ++alleleFreq[patch][3];
6584  a2 = 1;
6585  }
6586 
6587  ++hapFreq[patch][ twoLocHapMap[a1][a2] ];
6588 
6589  // Homologous copy 2
6590  // locus 1
6591  if (!trait->get_allele_bit(loc1, 1)) {
6592  ++alleleFreq[patch][0];
6593  a1 = 0;
6594  }
6595  else {
6596  ++alleleFreq[patch][1];
6597  a1 = 1;
6598  }
6599 
6600  // locus 2
6601  if (!trait->get_allele_bit(loc2, 1)) {
6602  ++alleleFreq[patch][2];
6603  a2 = 0;
6604  }
6605  else {
6606  ++alleleFreq[patch][3];
6607  a2 = 1;
6608  }
6609 
6610  ++hapFreq[patch][ twoLocHapMap[a1][a2] ];
6611  } // all individuals
6612  } // two sexes
6613 
6614 
6615  for (size_t geno = 0; geno < 4; geno++) {
6616  alleleFreq[patch][geno] /= patchSizes[patch];
6617  meanAlleleFreq[ geno ] += alleleFreq[patch][ geno ];
6618  }
6619 
6620  for (size_t hap = 0; hap < 4; hap++) {
6621  hapFreq[patch][hap] /= patchSizes[patch];
6622  meanHapFreq[hap] += hapFreq[patch][hap];
6623  }
6624  } // All patches
6625 
6626  for (size_t geno = 0; geno < 4; geno++)
6627  meanAlleleFreq[geno] /= extant_patches;
6628 
6629  for (size_t hap = 0; hap < 4; hap++)
6630  meanHapFreq[hap] /= extant_patches;
6631 
6632  // eliminate pairs with fixed alleles
6633  if ( !(meanAlleleFreq[0]*meanAlleleFreq[1]) && !(meanAlleleFreq[2] * meanAlleleFreq[3]))
6634  NA[pcomb] = true;
6635 
6636  if (!NA[pcomb]) {
6637 
6638  for(int patch = 0; patch < patchNbr; patch++) {
6639 
6640  if(!patchSizes[patch]) continue;
6641 
6642  for (size_t hap = 0; hap < 4; hap++) {
6643  // Ohta (1982), Eq. 10
6644  Dis[pcomb] += pow(hapFreq[patch][hap] -
6645  (alleleFreq[patch][ reverseHapMap[hap][0] ] * alleleFreq[patch][ reverseHapMap[hap][1]] ), 2);
6646 
6647  // Ohta (1982), Eq. 11
6648  Dst[pcomb] += pow((alleleFreq[patch][reverseHapMap[hap][0]] * alleleFreq[patch][reverseHapMap[hap][1]]) -
6649  (meanAlleleFreq[reverseHapMap[hap][0]] * meanAlleleFreq[reverseHapMap[hap][1]]), 2);
6650 
6651  // Ohta (1982), Eq. 12
6652  Disp[pcomb] += pow(hapFreq[patch][hap] - meanHapFreq[hap], 2);
6653 
6654  // Ohta (1982), Eq. 13
6655  Dstp[pcomb] += pow(meanHapFreq[hap] -
6656  (meanAlleleFreq[reverseHapMap[hap][0]] * meanAlleleFreq[reverseHapMap[hap][1]]), 2);
6657  }
6658 
6659  double denom = alleleFreq[patch][0] * alleleFreq[patch][1] * alleleFreq[patch][2] * alleleFreq[patch][3];
6660 
6661  if (denom == 0.0)
6662  rSquare[patch][pcomb] = 0;
6663  else
6664  rSquare[patch][pcomb] = pow(hapFreq[patch][0] - alleleFreq[patch][0]*alleleFreq[patch][2], 2) // p(AB) - p(A)*p(B)
6665  / denom;
6666  }
6667 
6668  // Ohta (1982), Eq. 10-13
6669  Dis[pcomb] /= extant_patches;
6670  Dst[pcomb] /= extant_patches;
6671  Disp[pcomb] /= extant_patches;
6672  Dstp[pcomb] /= extant_patches;
6673  }
6674 
6675 // cout << "\nLocus " << loc1+1 << " and locus " << loc2+1 << endl;
6676 // cout << "\"A allele\" : " << refLoc1 << "\n\"B allele\" : " << refLoc2 << endl;
6677 // for(int patch = 0; patch < patchNbr; patch++) {
6678 // cout << "Patch " << patch+1 << endl;
6679 // for (size_t geno = 0; geno < 4; geno++)
6680 // cout << geno << " : " << alleleFreq[patch][geno] << endl;
6681 // for (size_t hap = 0; hap < 4; hap++)
6682 // cout << hap << " : " << hapFreq[patch][hap] << endl;
6683 // for (size_t hap = 0; hap < 4; hap++)
6684 // cout << "Exp_" << reverseHapMap[hap][0] << reverseHapMap[hap][1] << " : "
6685 // << (alleleFreq[patch][reverseHapMap[hap][0]] * alleleFreq[patch][reverseHapMap[hap][1]]) << endl;
6686 // }
6687 // cout << "*****************" << endl;
6688 // for (size_t geno = 0; geno < 4; geno++)
6689 // cout << geno << " : " << meanAlleleFreq[geno] << endl;
6690 // for (size_t hap = 0; hap < 4; hap++)
6691 // cout << hap << " : " << meanHapFreq[hap] << endl;
6692 // for (size_t hap = 0; hap < 4; hap++)
6693 // cout << "Exp_" << reverseHapMap[hap][0] << reverseHapMap[hap][1] << " : "
6694 // << (meanAlleleFreq[reverseHapMap[hap][0]] * meanAlleleFreq[reverseHapMap[hap][1]]) << endl;
6695 // cout << "*****************" << endl;
6696 
6697  } // All pairwise locus combinations
6698 
6699  string filename = get_filename();
6700  ofstream FILE;
6701 
6702  //open file for writing
6703  FILE.open(filename.c_str(), ios::out);
6704  std::ios_base::sync_with_stdio(false); // better for writing performances
6705 
6706  if(!FILE) fatal("Trait quanti could not open output file: \"%s\"\n",filename.c_str());
6707 
6708  #ifdef _DEBUG_
6709  message("TTQOhtaStats::FHwrite (%s)\n",filename.c_str());
6710  fflush(stdout);
6711 #endif
6712 
6713 
6714  FILE << "loc1\tloc2\tDst\tDis\tDstp\tDisp";
6715 
6716  for(int patch = 0; patch < patchNbr; patch++)
6717  FILE << "\tr_" << patch+1;
6718  FILE << endl;
6719 
6720  for (size_t pcomb = 0; pcomb < num_comb; pcomb++) {
6721  if (!NA[pcomb]) {
6722  FILE << _pairwiseCombs.get(pcomb,0)+1 << "\t" << _pairwiseCombs.get(pcomb,1)+1 << "\t"
6723  << Dst[pcomb] << "\t" << Dis[pcomb] << "\t" << Dstp[pcomb] << "\t" << Disp[pcomb];
6724  for(int patch = 0; patch < patchNbr; patch++)
6725  FILE << "\t" << rSquare[patch][pcomb];
6726  FILE << endl;
6727  }
6728  }
6729 
6730  FILE.close();
6731  std::ios_base::sync_with_stdio(true); // reset
6732 }
std::string & get_filename()
Builds and returns the current file name depending on the periodicity of the file.
Definition: filehandler.cc:150
Metapop * _pop
Pointer to the current metapop, set during initialization within the init function.
Definition: filehandler.h:102
FileServices * get_service()
Returns pointer to the FileServices.
Definition: filehandler.h:138
Metapop * getSampledPop()
Sets the down-sampled population and provides accessor to file handlers.
Definition: fileservices.cc:197
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 getPatchNbr()
Definition: metapop.h:275
bool isAlive()
Checks if the population still contains at least one individual in any sex or age class.
Definition: metapop.h:308
Patch * getPatch(unsigned int i)
Patch accessor, return the ith+1 patch in the metapop.
Definition: metapop.h:256
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
void copy(const TMatrix &mat)
Copy a matrix.
Definition: tmatrix.h:77
double get(unsigned int i, unsigned int j) const
Accessor to element at row i and column j.
Definition: tmatrix.h:192
unsigned int nrows() const
Definition: tmatrix.h:212
unsigned int get_allele_model()
Definition: ttquanti.h:433
unsigned int get_num_locus()
Definition: ttquanti.h:423
unsigned int get_num_traits()
Definition: ttquanti.h:422
TMatrix _pairwiseCombs
Definition: ttquanti.h:856
TTQuanti_diallelic.
Definition: ttquanti.h:280
virtual bool get_allele_bit(unsigned int position, unsigned int allele) const
Definition: ttquanti.cc:3969
int _FHLinkedTraitIndex
Definition: filehandler.h:223
TProtoQuanti * _FHLinkedTrait
Definition: filehandler.h:222
void fatal(const char *str,...)
Definition: output.cc:99
int error(const char *str,...)
Definition: output.cc:78
void message(const char *message,...)
Definition: output.cc:39
sex_t
Sex types, males are always 0 and females 1!!
Definition: types.h:35
@ FEM
Definition: types.h:36
@ MAL
Definition: types.h:36
@ ADLTx
Definition: types.h:41
TMatrix nChooseKVec(int n, int k)
Definition: utils.cc:197

References TraitFileHandler< TProtoQuanti >::_FHLinkedTrait, TraitFileHandler< TProtoQuanti >::_FHLinkedTraitIndex, _pairwiseCombs, FileHandler::_pop, ADLTx, Patch::assign(), TMatrix::copy(), error(), fatal(), FEM, Patch::get(), TMatrix::get(), TTQuanti_diallelic::get_allele_bit(), TProtoQuanti::get_allele_model(), FileHandler::get_filename(), TProtoQuanti::get_num_locus(), TProtoQuanti::get_num_traits(), FileHandler::get_service(), Metapop::getPatch(), Metapop::getPatchNbr(), FileServices::getSampledPop(), Individual::getTrait(), Metapop::isAlive(), MAL, message(), nChooseKVec(), TMatrix::nrows(), and Patch::size().

Member Data Documentation

◆ _pairwiseCombs

TMatrix TTQOhtaStats::_pairwiseCombs
private

Referenced by FHwrite().


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