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

Member Function Documentation

◆ FHread()

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

Implements FileHandler.

309 {}

◆ FHwrite()

void TTNOhtaStats::FHwrite ( )
virtual

Implements TraitFileHandler< TProtoNeutralGenes >.

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