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

TTNOhtaStats. More...

#include <ttneutralgenes.h>

+ Inheritance diagram for TTNOhtaStats:
+ Collaboration diagram for TTNOhtaStats:

Public Member Functions

 TTNOhtaStats (TProtoNeutralGenes *T)
 
virtual ~TTNOhtaStats ()
 
virtual void FHwrite ()
 
virtual void FHread (string &filename)
 
- Public Member Functions inherited from TraitFileHandler< TProtoNeutralGenes >
 TraitFileHandler (TProtoNeutralGenes *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, TProtoNeutralGenes *trait_proto)
 
virtual void set_multi (bool rpl_per, bool gen_per, int rpl_occ, TMatrix *Occ, string path, TProtoNeutralGenes *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< TProtoNeutralGenes >
TProtoNeutralGenes_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

◆ TTNOhtaStats()

TTNOhtaStats::TTNOhtaStats ( TProtoNeutralGenes T)
inline

◆ ~TTNOhtaStats()

virtual TTNOhtaStats::~TTNOhtaStats ( )
inlinevirtual
254 {}

Member Function Documentation

◆ FHread()

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

Implements FileHandler.

256 {}

◆ FHwrite()

void TTNOhtaStats::FHwrite ( )
virtual

Implements TraitFileHandler< TProtoNeutralGenes >.

1819 {
1820  Metapop* pop = get_pop_ptr();
1821  int patchNbr = pop->getPatchNbr();
1822  unsigned int nb_allele = _FHLinkedTrait->get_allele_num();
1823  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
1824 
1825  // Output Ohta stats :
1826  // Only if pop alive
1827  // Only for diallelic mutation models
1828  // Only for multi-population scenarios
1829  if ((!pop->isAlive()) || (nb_allele > 2) || (patchNbr < 2)) {
1830  error("TTNOhtaStats::neutral trait and population are not compatible with Ohta stats (min. 2 patches, di-allelic loci), file will not be written.\n");
1831  return;
1832  }
1833 
1834  _pairwiseCombs.copy(nChooseKVec(nb_locus, 2));
1835 
1836  unsigned int num_comb = _pairwiseCombs.nrows();
1837 
1838  ostringstream REC;
1839  Individual* ind;
1840  unsigned char** genes;
1841  Patch* current_patch;
1842 
1843  vector<double> Dis(num_comb, 0.0),
1844  Dst(num_comb, 0.0),
1845  Disp(num_comb, 0.0),
1846  Dstp(num_comb, 0.0);
1847 
1848  vector< vector<double> > rSquare = vector< vector<double> > (patchNbr, vector<double>(num_comb, 0.0));
1849 
1850  vector<bool> NA(num_comb, false);
1851 
1852  int total_size = 0;
1853  int extant_patches = 0;
1854 
1855  vector<int> patchSizes(patchNbr, 0);
1856 
1857  // pre-record patch sizes and number of extant patches
1858  for(int patch = 0; patch < patchNbr; patch++) {
1859 
1860  current_patch = pop->getPatch(patch);
1861 
1862  patchSizes[patch] = 2 * (current_patch->size(FEM, ADLTx) + current_patch->size(MAL, ADLTx));
1863 
1864  if(patchSizes[patch]) ++extant_patches; // count patches with individuals
1865 
1866  total_size += patchSizes[patch];
1867  }
1868 
1869 #ifdef _DEBUG_
1870  message("TTNOhtaStats::FHwrite:: computing association stats of %i combinations\n", num_comb);
1871  fflush(stdout);
1872 #endif
1873 
1874 
1875  unsigned int a1, a2;
1876  unsigned int twoLocHapMap[2][2] = {{0,1},{2,3}}; // genot: AB, Ab, aB, ab; A = 0, a = 1, B = 2, b = 3
1877  unsigned int reverseHapMap[4][2] = {{0,2},{0,3},{1,2},{1,3}}; // AB -> {0,2} ...
1878 
1879  vector< double > meanGenoFreq(4,0.0); // allele counter at the two loci,0 = [0], 1 = [1], 0 = [2], 1 = [3]
1880  vector< double > meanHapFreq(4,0.0); // genotype counter
1881 
1882  vector< vector< double > > genoFreq = vector< vector< double > > (patchNbr, vector< double >(4,0.0));
1883  vector< vector< double > > hapFreq = vector< vector< double > > (patchNbr, vector< double >(4,0.0));
1884 
1885  // cycling through all pairwise locus combinations
1886  for (size_t pcomb = 0; pcomb < num_comb; pcomb++) {
1887 
1888  size_t loc1 = _pairwiseCombs.get(pcomb,0),
1889  loc2 = _pairwiseCombs.get(pcomb,1);
1890 
1891  char refLoc1, refLoc2;
1892 
1893  current_patch = pop->getPatch(0);
1894 
1895  ind = current_patch->get(FEM, ADLTx, 0);
1896 
1897  genes = (unsigned char**)ind->getTrait(_FHLinkedTraitIndex)->get_sequence();
1898 
1899  // reference alleles from the first female adult of the first patch
1900  refLoc1 = genes[0][loc1];
1901  refLoc2 = genes[0][loc2];
1902 
1903  meanGenoFreq.assign(4, 0.0);
1904  meanHapFreq.assign(4,0.0);
1905 
1906  for(int patch = 0; patch < patchNbr; patch++) {
1907 
1908  current_patch = pop->getPatch(patch);
1909 
1910  genoFreq[patch].assign(4,0.0);
1911  hapFreq[patch].assign(4,0.0);
1912 
1913  if(patchSizes[patch] == 0) continue;
1914 
1915  // All adults in a patch
1916  for(unsigned int s =0; s < 2; ++s) { // two sexes
1917 
1918  for(unsigned int j = 0, size = current_patch->size(sex_t(s), ADLTx); j < size; j++) {
1919 
1920  ind = current_patch->get(sex_t(s), ADLTx, j);
1921 
1922  genes = (unsigned char**)ind->getTrait(_FHLinkedTraitIndex)->get_sequence();
1923 
1924  // Homologous copy 1
1925  // locus 1
1926  if (genes[0][loc1] == refLoc1) {
1927  ++genoFreq[patch][0];
1928  a1 = 0;
1929  }
1930  else {
1931  ++genoFreq[patch][1];
1932  a1 = 1;
1933  }
1934 
1935  // locus 2
1936  if (genes[0][loc2] == refLoc2) {
1937  ++genoFreq[patch][2];
1938  a2 = 0;
1939  }
1940  else {
1941  ++genoFreq[patch][3];
1942  a2 = 1;
1943  }
1944 
1945  ++hapFreq[patch][twoLocHapMap[a1][a2]];
1946 
1947  // Homologous copy 2
1948  // locus 1
1949  if (genes[1][loc1] == refLoc1) {
1950  ++genoFreq[patch][0];
1951  a1 = 0;
1952  }
1953  else {
1954  ++genoFreq[patch][1];
1955  a1 = 1;
1956  }
1957 
1958  // locus 2
1959  if (genes[1][loc2] == refLoc2) {
1960  ++genoFreq[patch][2];
1961  a2 = 0;
1962  }
1963  else {
1964  ++genoFreq[patch][3];
1965  a2 = 1;
1966  }
1967 
1968  ++hapFreq[patch][twoLocHapMap[a1][a2]];
1969  } // All individuals
1970  } // two sexes
1971 
1972  for (size_t geno = 0; geno < 4; geno++) {
1973  genoFreq[patch][geno] /= patchSizes[patch];
1974  meanGenoFreq[geno] += genoFreq[patch][geno];
1975  }
1976 
1977  for (size_t hap = 0; hap < 4; hap++) {
1978  hapFreq[patch][hap] /= patchSizes[patch];
1979  meanHapFreq[hap] += hapFreq[patch][hap];
1980  }
1981  } // All patches
1982 
1983  for (size_t geno = 0; geno < 4; geno++)
1984  meanGenoFreq[geno] /= extant_patches;
1985 
1986  for (size_t hap = 0; hap < 4; hap++)
1987  meanHapFreq[hap] /= extant_patches;
1988 
1989  // eliminate pairs with fixed alleles
1990  if ( !(meanGenoFreq[0]*meanGenoFreq[1]) && !(meanGenoFreq[2] * meanGenoFreq[3]))
1991  NA[pcomb] = true;
1992 
1993  if (!NA[pcomb]) {
1994 
1995  for(int patch = 0; patch < patchNbr; patch++) {
1996 
1997  if(!patchSizes[patch]) continue;
1998 
1999  for (size_t hap = 0; hap < 4; hap++) {
2000  // Ohta (1982), Eq. 10
2001  Dis[pcomb] += pow(hapFreq[patch][hap] -
2002  (genoFreq[patch][reverseHapMap[hap][0]] * genoFreq[patch][reverseHapMap[hap][1]]), 2);
2003 
2004  // Ohta (1982), Eq. 11
2005  Dst[pcomb] += pow((genoFreq[patch][reverseHapMap[hap][0]] * genoFreq[patch][reverseHapMap[hap][1]]) -
2006  (meanGenoFreq[reverseHapMap[hap][0]] * meanGenoFreq[reverseHapMap[hap][1]]), 2);
2007 
2008  // Ohta (1982), Eq. 12
2009  Disp[pcomb] += pow(hapFreq[patch][hap] - meanHapFreq[hap], 2);
2010 
2011  // Ohta (1982), Eq. 13
2012  Dstp[pcomb] += pow(meanHapFreq[hap] -
2013  (meanGenoFreq[reverseHapMap[hap][0]] * meanGenoFreq[reverseHapMap[hap][1]]), 2);
2014  }
2015 
2016  double denom = genoFreq[patch][0] * genoFreq[patch][1] * genoFreq[patch][2] * genoFreq[patch][3];
2017 
2018  if (denom == 0.0)
2019  rSquare[patch][pcomb] = 0;
2020  else
2021  rSquare[patch][pcomb] = pow(hapFreq[patch][0] - genoFreq[patch][0]*genoFreq[patch][2], 2)
2022  / denom;
2023  }
2024 
2025  // Ohta (1982), Eq. 10-13
2026  Dis[pcomb] /= extant_patches;
2027  Dst[pcomb] /= extant_patches;
2028  Disp[pcomb] /= extant_patches;
2029  Dstp[pcomb] /= extant_patches;
2030 
2031  }
2032 
2033 // cout << "\nLocus " << loc1+1 << " and locus " << loc2+1 << endl;
2034 // cout << "\"A allele\" : " << refLoc1 << "\n\"B allele\" : " << refLoc2 << endl;
2035 // for(int patch = 0; patch < patchNbr; patch++) {
2036 // cout << "Patch " << patch+1 << endl;
2037 // for (size_t geno = 0; geno < Gtypes.size(); geno++)
2038 // cout << geno << " : " << genoFreq[patch][geno] << endl;
2039 // for (size_t hap = 0; hap < Htypes.size(); hap++)
2040 // cout << hap << " : " << hapFreq[patch][hap] << endl;
2041 // for (size_t hap = 0; hap < Htypes.size(); hap++)
2042 // cout << "Exp_" << reverseHapMap[hap][0] << reverseHapMap[hap][1] << " : " << (genoFreq[patch][reverseHapMap[hap][0]] * genoFreq[patch][reverseHapMap[hap][1]]) << endl;
2043 // }
2044 // cout << "*****************" << endl;
2045 // for (size_t geno = 0; geno < Gtypes.size(); geno++)
2046 // cout << geno << " : " << meanGenoFreq[geno] << endl;
2047 // for (size_t hap = 0; hap < Htypes.size(); hap++)
2048 // cout << hap << " : " << meanHapFreq[hap] << endl;
2049 // for (size_t hap = 0; hap < Htypes.size(); hap++)
2050 // cout << "Exp_" << reverseHapMap[hap][0] << reverseHapMap[hap][1] << " : " << (meanGenoFreq[reverseHapMap[hap][0]] * meanGenoFreq[reverseHapMap[hap][1]]) << endl;
2051 // cout << "*****************" << endl;
2052 
2053  } // All pairwise locus combinations
2054 
2055  string filename = get_filename();
2056  ofstream FILE;
2057 
2058  //open file for writing
2059  FILE.open(filename.c_str(), ios::out);
2060 
2061  if(!FILE) fatal("Trait neutral could not open output file: \"%s\"\n",filename.c_str());
2062 
2063 #ifdef _DEBUG_
2064  message("TTNOhtaStats::FHwrite (%s)\n",filename.c_str());
2065  fflush(stdout);
2066 #endif
2067 
2068  FILE << "loc1\tloc2\tDst\tDis\tDstp\tDisp";
2069  for(int patch = 0; patch < patchNbr; patch++)
2070  FILE << "\tr_" << patch+1;
2071  FILE << endl;
2072 
2073  for (size_t pcomb = 0; pcomb < num_comb; pcomb++) {
2074  if (!NA[pcomb]) {
2075  FILE << _pairwiseCombs.get(pcomb,0)+1 << "\t" << _pairwiseCombs.get(pcomb,1)+1 << "\t"
2076  << Dst[pcomb] << "\t" << Dis[pcomb] << "\t" << Dstp[pcomb] << "\t" << Disp[pcomb];
2077  for(int patch = 0; patch < patchNbr; patch++)
2078  FILE << "\t" << rSquare[patch][pcomb];
2079  FILE << endl;
2080  }
2081  }
2082 
2083  FILE.close();
2084 }
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_locus_num()
Definition: ttneutralgenes.h:159
unsigned int get_allele_num()
Definition: ttneutralgenes.h:160
TMatrix _pairwiseCombs
Definition: ttneutralgenes.h:249
virtual void ** get_sequence() const =0
sequence accessor.
int _FHLinkedTraitIndex
Definition: filehandler.h:220
TProtoNeutralGenes * _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< TProtoNeutralGenes >::_FHLinkedTrait, TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTraitIndex, _pairwiseCombs, ADLTx, Patch::assign(), TMatrix::copy(), error(), fatal(), FEM, Patch::get(), TMatrix::get(), TProtoNeutralGenes::get_allele_num(), FileHandler::get_filename(), TProtoNeutralGenes::get_locus_num(), FileHandler::get_pop_ptr(), TTrait::get_sequence(), Metapop::getPatch(), Metapop::getPatchNbr(), Individual::getTrait(), Metapop::isAlive(), MAL, message(), nChooseKVec(), TMatrix::nrows(), and Patch::size().

Member Data Documentation

◆ _pairwiseCombs

TMatrix TTNOhtaStats::_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