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");
1840 unsigned char** genes;
1841 Patch* current_patch;
1843 vector<double> Dis(num_comb, 0.0),
1845 Disp(num_comb, 0.0),
1846 Dstp(num_comb, 0.0);
1848 vector< vector<double> > rSquare = vector< vector<double> > (patchNbr, vector<double>(num_comb, 0.0));
1850 vector<bool> NA(num_comb,
false);
1853 int extant_patches = 0;
1855 vector<int> patchSizes(patchNbr, 0);
1858 for(
int patch = 0; patch < patchNbr; patch++) {
1860 current_patch = pop->
getPatch(patch);
1864 if(patchSizes[patch]) ++extant_patches;
1866 total_size += patchSizes[patch];
1870 message(
"TTNOhtaStats::FHwrite:: computing association stats of %i combinations\n", num_comb);
1875 unsigned int a1, a2;
1876 unsigned int twoLocHapMap[2][2] = {{0,1},{2,3}};
1877 unsigned int reverseHapMap[4][2] = {{0,2},{0,3},{1,2},{1,3}};
1879 vector< double > meanGenoFreq(4,0.0);
1880 vector< double > meanHapFreq(4,0.0);
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));
1886 for (
size_t pcomb = 0; pcomb < num_comb; pcomb++) {
1891 char refLoc1, refLoc2;
1900 refLoc1 = genes[0][loc1];
1901 refLoc2 = genes[0][loc2];
1903 meanGenoFreq.assign(4, 0.0);
1904 meanHapFreq.assign(4,0.0);
1906 for(
int patch = 0; patch < patchNbr; patch++) {
1908 current_patch = pop->
getPatch(patch);
1910 genoFreq[patch].
assign(4,0.0);
1911 hapFreq[patch].assign(4,0.0);
1913 if(patchSizes[patch] == 0)
continue;
1916 for(
unsigned int s =0; s < 2; ++s) {
1918 for(
unsigned int j = 0, size = current_patch->
size(
sex_t(s),
ADLTx); j < size; j++) {
1926 if (genes[0][loc1] == refLoc1) {
1927 ++genoFreq[patch][0];
1931 ++genoFreq[patch][1];
1936 if (genes[0][loc2] == refLoc2) {
1937 ++genoFreq[patch][2];
1941 ++genoFreq[patch][3];
1945 ++hapFreq[patch][twoLocHapMap[a1][a2]];
1949 if (genes[1][loc1] == refLoc1) {
1950 ++genoFreq[patch][0];
1954 ++genoFreq[patch][1];
1959 if (genes[1][loc2] == refLoc2) {
1960 ++genoFreq[patch][2];
1964 ++genoFreq[patch][3];
1968 ++hapFreq[patch][twoLocHapMap[a1][a2]];
1972 for (
size_t geno = 0; geno < 4; geno++) {
1973 genoFreq[patch][geno] /= patchSizes[patch];
1974 meanGenoFreq[geno] += genoFreq[patch][geno];
1977 for (
size_t hap = 0; hap < 4; hap++) {
1978 hapFreq[patch][hap] /= patchSizes[patch];
1979 meanHapFreq[hap] += hapFreq[patch][hap];
1983 for (
size_t geno = 0; geno < 4; geno++)
1984 meanGenoFreq[geno] /= extant_patches;
1986 for (
size_t hap = 0; hap < 4; hap++)
1987 meanHapFreq[hap] /= extant_patches;
1990 if ( !(meanGenoFreq[0]*meanGenoFreq[1]) && !(meanGenoFreq[2] * meanGenoFreq[3]))
1995 for(
int patch = 0; patch < patchNbr; patch++) {
1997 if(!patchSizes[patch])
continue;
1999 for (
size_t hap = 0; hap < 4; hap++) {
2001 Dis[pcomb] += pow(hapFreq[patch][hap] -
2002 (genoFreq[patch][reverseHapMap[hap][0]] * genoFreq[patch][reverseHapMap[hap][1]]), 2);
2005 Dst[pcomb] += pow((genoFreq[patch][reverseHapMap[hap][0]] * genoFreq[patch][reverseHapMap[hap][1]]) -
2006 (meanGenoFreq[reverseHapMap[hap][0]] * meanGenoFreq[reverseHapMap[hap][1]]), 2);
2009 Disp[pcomb] += pow(hapFreq[patch][hap] - meanHapFreq[hap], 2);
2012 Dstp[pcomb] += pow(meanHapFreq[hap] -
2013 (meanGenoFreq[reverseHapMap[hap][0]] * meanGenoFreq[reverseHapMap[hap][1]]), 2);
2016 double denom = genoFreq[patch][0] * genoFreq[patch][1] * genoFreq[patch][2] * genoFreq[patch][3];
2019 rSquare[patch][pcomb] = 0;
2021 rSquare[patch][pcomb] = pow(hapFreq[patch][0] - genoFreq[patch][0]*genoFreq[patch][2], 2)
2026 Dis[pcomb] /= extant_patches;
2027 Dst[pcomb] /= extant_patches;
2028 Disp[pcomb] /= extant_patches;
2029 Dstp[pcomb] /= extant_patches;
2059 FILE.open(filename.c_str(), ios::out);
2061 if(!FILE)
fatal(
"Trait neutral could not open output file: \"%s\"\n",filename.c_str());
2064 message(
"TTNOhtaStats::FHwrite (%s)\n",filename.c_str());
2068 FILE <<
"loc1\tloc2\tDst\tDis\tDstp\tDisp";
2069 for(
int patch = 0; patch < patchNbr; patch++)
2070 FILE <<
"\tr_" << patch+1;
2073 for (
size_t pcomb = 0; pcomb < num_comb; pcomb++) {
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];
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
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