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

Member Function Documentation

◆ FHread()

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

Implements FileHandler.

864 {}

◆ FHwrite()

void TTQOhtaStats::FHwrite ( )
virtual

Implements TraitFileHandler< TProtoQuanti >.

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

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

Locations of visitors to this page
Catalogued on GSR