Nemo  2.4.1
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 >.

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

Catalogued on GSR