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
849 {}

Member Function Documentation

◆ FHread()

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

Implements FileHandler.

851 {}

◆ FHwrite()

void TTQOhtaStats::FHwrite ( )
virtual

Implements TraitFileHandler< TProtoQuanti >.

6704 {
6705  Metapop* pop = get_pop_ptr();
6706  int patchNbr = pop->getPatchNbr();
6707  unsigned int nb_trait = _FHLinkedTrait->get_num_traits();
6708  unsigned int nb_locus = _FHLinkedTrait->get_num_locus();
6709  TTQuanti_diallelic* trait;
6710 
6711  // Output Ohta stats :
6712  // Only if pop alive
6713  // Only for diallelic mutation models
6714  // Only for a single trait
6715  // Only for multi-population scenarios
6716  if ((!pop->isAlive()) || (_FHLinkedTrait->get_allele_model() > 2)
6717  || (nb_trait > 1) || (patchNbr < 2)) {
6718  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");
6719  return ;
6720  }
6721 
6722  _pairwiseCombs.copy(nChooseKVec(nb_locus, 2));
6723 
6724  unsigned int num_comb = _pairwiseCombs.nrows();
6725 
6726  ostringstream REC;
6727  Individual* ind;
6728  Patch* current_patch;
6729 
6730  vector<double> Dis(num_comb, 0.0),
6731  Dst(num_comb, 0.0),
6732  Disp(num_comb, 0.0),
6733  Dstp(num_comb, 0.0);
6734 
6735  vector< vector<double> > rSquare = vector< vector<double> > (patchNbr, vector<double>(num_comb, 0.0));
6736 
6737  vector<bool> NA(num_comb, false);
6738 
6739  int total_size = 0;
6740  int extant_patches = 0;
6741 
6742  vector<int> patchSizes(patchNbr, 0);
6743 
6744 
6745  // pre-record patch sizes and number of extant patches
6746  for(int patch = 0; patch < patchNbr; patch++) {
6747 
6748  current_patch = pop->getPatch(patch);
6749 
6750  patchSizes[patch] = 2 * (current_patch->size(FEM, ADLTx) + current_patch->size(MAL, ADLTx));
6751 
6752  if(patchSizes[patch]) ++extant_patches; // count patches with individuals
6753 
6754  total_size += patchSizes[patch];
6755  }
6756 
6757 #ifdef _DEBUG_
6758  message("TTQOhtaStats::FHwrite:: computing association stats of %i combinations\n", num_comb);
6759  fflush(stdout);
6760 #endif
6761 
6762  unsigned int a1, a2;
6763  unsigned int twoLocHapMap[2][2] = {{0,1},{2,3}}; // genot: AB, Ab, aB, ab; A = 0, a = 1, B = 2, b = 3
6764  unsigned int reverseHapMap[4][2] = {{0,2},{0,3},{1,2},{1,3}}; // AB -> {0,2} ...
6765 
6766  vector< double > meanAlleleFreq(4,0.0); // allele counter at the two loci,"A" = [0], "a" = [1], "B" = [2], "b" = [3]
6767  vector< double > meanHapFreq(4,0.0); // genotype counter
6768 
6769  vector< vector< double > > alleleFreq = vector< vector< double > > (patchNbr, vector< double >(4,0.0));
6770  vector< vector< double > > hapFreq = vector< vector< double > > (patchNbr, vector< double >(4,0.0));
6771 
6772  // cycling through all pairwise locus combinations
6773  for (size_t pcomb = 0; pcomb < num_comb; pcomb++) {
6774 
6775  size_t loc1 = _pairwiseCombs.get(pcomb,0),
6776  loc2 = _pairwiseCombs.get(pcomb,1);
6777 
6778  meanAlleleFreq.assign(4, 0.0);
6779  meanHapFreq.assign(4,0.0);
6780 
6781  for(int patch = 0; patch < patchNbr; patch++) {
6782 
6783  current_patch = pop->getPatch(patch);
6784 
6785  alleleFreq[patch].assign(4,0.0);
6786  hapFreq[patch].assign(4,0.0);
6787 
6788  if(patchSizes[patch] == 0) continue;
6789 
6790  // @TODO adapt Ohta stat output for any age class not only adults
6791  // All adults in a patch
6792  for(unsigned int s =0; s < 2; ++s) { // two sexes
6793 
6794  for(unsigned int j = 0, size = current_patch->size(sex_t(s), ADLTx); j < size; j++) {
6795 
6796  ind = current_patch->get(sex_t(s), ADLTx, j);
6797 
6798  trait = dynamic_cast<TTQuanti_diallelic*>( ind->getTrait(_FHLinkedTraitIndex));
6799 
6800  // Homologous copy 1
6801  // locus 1; "A" = 0, "a" = 1
6802  if ( !trait->get_allele_bit(loc1, 0) ) { //reference allele is allele '0'
6803  ++alleleFreq[patch][0];
6804  a1 = 0;
6805  }
6806  else {
6807  ++alleleFreq[patch][1];
6808  a1 = 1;
6809  }
6810 
6811  // locus 2
6812  if (!trait->get_allele_bit(loc2, 0)) {
6813  ++alleleFreq[patch][2];
6814  a2 = 0;
6815  }
6816  else {
6817  ++alleleFreq[patch][3];
6818  a2 = 1;
6819  }
6820 
6821  ++hapFreq[patch][ twoLocHapMap[a1][a2] ];
6822 
6823  // Homologous copy 2
6824  // locus 1
6825  if (!trait->get_allele_bit(loc1, 1)) {
6826  ++alleleFreq[patch][0];
6827  a1 = 0;
6828  }
6829  else {
6830  ++alleleFreq[patch][1];
6831  a1 = 1;
6832  }
6833 
6834  // locus 2
6835  if (!trait->get_allele_bit(loc2, 1)) {
6836  ++alleleFreq[patch][2];
6837  a2 = 0;
6838  }
6839  else {
6840  ++alleleFreq[patch][3];
6841  a2 = 1;
6842  }
6843 
6844  ++hapFreq[patch][ twoLocHapMap[a1][a2] ];
6845  } // all individuals
6846  } // two sexes
6847 
6848 
6849  for (size_t geno = 0; geno < 4; geno++) {
6850  alleleFreq[patch][geno] /= patchSizes[patch];
6851  meanAlleleFreq[ geno ] += alleleFreq[patch][ geno ];
6852  }
6853 
6854  for (size_t hap = 0; hap < 4; hap++) {
6855  hapFreq[patch][hap] /= patchSizes[patch];
6856  meanHapFreq[hap] += hapFreq[patch][hap];
6857  }
6858  } // All patches
6859 
6860  for (size_t geno = 0; geno < 4; geno++)
6861  meanAlleleFreq[geno] /= extant_patches;
6862 
6863  for (size_t hap = 0; hap < 4; hap++)
6864  meanHapFreq[hap] /= extant_patches;
6865 
6866  // eliminate pairs with fixed alleles
6867  if ( !(meanAlleleFreq[0]*meanAlleleFreq[1]) && !(meanAlleleFreq[2] * meanAlleleFreq[3]))
6868  NA[pcomb] = true;
6869 
6870  if (!NA[pcomb]) {
6871 
6872  for(int patch = 0; patch < patchNbr; patch++) {
6873 
6874  if(!patchSizes[patch]) continue;
6875 
6876  for (size_t hap = 0; hap < 4; hap++) {
6877  // Ohta (1982), Eq. 10
6878  Dis[pcomb] += pow(hapFreq[patch][hap] -
6879  (alleleFreq[patch][ reverseHapMap[hap][0] ] * alleleFreq[patch][ reverseHapMap[hap][1]] ), 2);
6880 
6881  // Ohta (1982), Eq. 11
6882  Dst[pcomb] += pow((alleleFreq[patch][reverseHapMap[hap][0]] * alleleFreq[patch][reverseHapMap[hap][1]]) -
6883  (meanAlleleFreq[reverseHapMap[hap][0]] * meanAlleleFreq[reverseHapMap[hap][1]]), 2);
6884 
6885  // Ohta (1982), Eq. 12
6886  Disp[pcomb] += pow(hapFreq[patch][hap] - meanHapFreq[hap], 2);
6887 
6888  // Ohta (1982), Eq. 13
6889  Dstp[pcomb] += pow(meanHapFreq[hap] -
6890  (meanAlleleFreq[reverseHapMap[hap][0]] * meanAlleleFreq[reverseHapMap[hap][1]]), 2);
6891  }
6892 
6893  double denom = alleleFreq[patch][0] * alleleFreq[patch][1] * alleleFreq[patch][2] * alleleFreq[patch][3];
6894 
6895  if (denom == 0.0)
6896  rSquare[patch][pcomb] = 0;
6897  else
6898  rSquare[patch][pcomb] = pow(hapFreq[patch][0] - alleleFreq[patch][0]*alleleFreq[patch][2], 2) // p(AB) - p(A)*p(B)
6899  / denom;
6900  }
6901 
6902  // Ohta (1982), Eq. 10-13
6903  Dis[pcomb] /= extant_patches;
6904  Dst[pcomb] /= extant_patches;
6905  Disp[pcomb] /= extant_patches;
6906  Dstp[pcomb] /= extant_patches;
6907  }
6908 
6909 // cout << "\nLocus " << loc1+1 << " and locus " << loc2+1 << endl;
6910 // cout << "\"A allele\" : " << refLoc1 << "\n\"B allele\" : " << refLoc2 << endl;
6911 // for(int patch = 0; patch < patchNbr; patch++) {
6912 // cout << "Patch " << patch+1 << endl;
6913 // for (size_t geno = 0; geno < 4; geno++)
6914 // cout << geno << " : " << alleleFreq[patch][geno] << endl;
6915 // for (size_t hap = 0; hap < 4; hap++)
6916 // cout << hap << " : " << hapFreq[patch][hap] << endl;
6917 // for (size_t hap = 0; hap < 4; hap++)
6918 // cout << "Exp_" << reverseHapMap[hap][0] << reverseHapMap[hap][1] << " : "
6919 // << (alleleFreq[patch][reverseHapMap[hap][0]] * alleleFreq[patch][reverseHapMap[hap][1]]) << endl;
6920 // }
6921 // cout << "*****************" << endl;
6922 // for (size_t geno = 0; geno < 4; geno++)
6923 // cout << geno << " : " << meanAlleleFreq[geno] << endl;
6924 // for (size_t hap = 0; hap < 4; hap++)
6925 // cout << hap << " : " << meanHapFreq[hap] << endl;
6926 // for (size_t hap = 0; hap < 4; hap++)
6927 // cout << "Exp_" << reverseHapMap[hap][0] << reverseHapMap[hap][1] << " : "
6928 // << (meanAlleleFreq[reverseHapMap[hap][0]] * meanAlleleFreq[reverseHapMap[hap][1]]) << endl;
6929 // cout << "*****************" << endl;
6930 
6931  } // All pairwise locus combinations
6932 
6933  string filename = get_filename();
6934  ofstream FILE;
6935 
6936  //open file for writing
6937  FILE.open(filename.c_str(), ios::out);
6938 
6939  if(!FILE) fatal("Trait quanti could not open output file: \"%s\"\n",filename.c_str());
6940 
6941  #ifdef _DEBUG_
6942  message("TTQOhtaStats::FHwrite (%s)\n",filename.c_str());
6943  fflush(stdout);
6944 #endif
6945 
6946  // FG v2.4.0 removed the epistatic effects from the Ohta file, should be saved in the logfile
6947 
6948  FILE << "loc1\tloc2\tDst\tDis\tDstp\tDisp";
6949 
6950  for(int patch = 0; patch < patchNbr; patch++)
6951  FILE << "\tr_" << patch+1;
6952  FILE << endl;
6953 
6954  for (size_t pcomb = 0; pcomb < num_comb; pcomb++) {
6955  if (!NA[pcomb]) {
6956  FILE << _pairwiseCombs.get(pcomb,0)+1 << "\t" << _pairwiseCombs.get(pcomb,1)+1 << "\t"
6957  << Dst[pcomb] << "\t" << Dis[pcomb] << "\t" << Dstp[pcomb] << "\t" << Disp[pcomb];
6958  for(int patch = 0; patch < patchNbr; patch++)
6959  FILE << "\t" << rSquare[patch][pcomb];
6960  FILE << endl;
6961  }
6962  }
6963 
6964  FILE.close();
6965 }
std::string & get_filename()
Builds and returns the current file name depending on the periodicity of the file.
Definition: filehandler.cc:151
Metapop * get_pop_ptr()
Returns the pointer to the current metapop through the FileServices interface.
Definition: filehandler.h:131
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:428
unsigned int get_num_locus()
Definition: ttquanti.h:418
unsigned int get_num_traits()
Definition: ttquanti.h:417
TMatrix _pairwiseCombs
Definition: ttquanti.h:844
TTQuanti_diallelic.
Definition: ttquanti.h:276
virtual bool get_allele_bit(unsigned int position, unsigned int allele) const
Definition: ttquanti.cc:3889
int _FHLinkedTraitIndex
Definition: filehandler.h:220
TProtoQuanti * _FHLinkedTrait
Definition: filehandler.h:219
void fatal(const char *str,...)
Definition: output.cc:96
int error(const char *str,...)
Definition: output.cc:77
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, 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_pop_ptr(), Metapop::getPatch(), Metapop::getPatchNbr(), 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