1796 if ((!pop->
isAlive()) || (nb_allele > 2) || (patchNbr < 2)) {
1797 error(
"TTNOhtaStats::neutral trait and population are not compatible with Ohta stats (min. 2 patches, di-allelic loci), file will not be written.\n");
1808 Patch* current_patch;
1810 vector<double> Dis(num_comb, 0.0),
1812 Disp(num_comb, 0.0),
1813 Dstp(num_comb, 0.0);
1815 vector< vector<double> > rSquare = vector< vector<double> > (patchNbr, vector<double>(num_comb, 0.0));
1817 vector<bool> NA(num_comb,
false);
1820 int extant_patches = 0;
1822 vector<int> patchSizes(patchNbr, 0);
1825 for(
int patch = 0; patch < patchNbr; patch++) {
1827 current_patch = pop->
getPatch(patch);
1831 if(patchSizes[patch]) ++extant_patches;
1833 total_size += patchSizes[patch];
1837 message(
"TTNOhtaStats::FHwrite:: computing association stats of %i combinations\n", num_comb);
1842 unsigned int a1, a2;
1843 unsigned int twoLocHapMap[2][2] = {{0,1},{2,3}};
1844 unsigned int reverseHapMap[4][2] = {{0,2},{0,3},{1,2},{1,3}};
1846 vector< double > meanGenoFreq(4,0.0);
1847 vector< double > meanHapFreq(4,0.0);
1849 vector< vector< double > > genoFreq = vector< vector< double > > (patchNbr, vector< double >(4,0.0));
1850 vector< vector< double > > hapFreq = vector< vector< double > > (patchNbr, vector< double >(4,0.0));
1853 for (
size_t pcomb = 0; pcomb < num_comb; pcomb++) {
1858 unsigned int refLoc1, refLoc2;
1870 meanGenoFreq.assign(4, 0.0);
1871 meanHapFreq.assign(4,0.0);
1873 for(
int patch = 0; patch < patchNbr; patch++) {
1875 current_patch = pop->
getPatch(patch);
1877 genoFreq[patch].
assign(4,0.0);
1878 hapFreq[patch].assign(4,0.0);
1880 if(patchSizes[patch] == 0)
continue;
1883 for(
unsigned int s =0; s < 2; ++s) {
1885 for(
unsigned int j = 0, size = current_patch->
size(
sex_t(s),
ADLTx); j < size; j++) {
1894 ++genoFreq[patch][0];
1898 ++genoFreq[patch][1];
1904 ++genoFreq[patch][2];
1908 ++genoFreq[patch][3];
1912 ++hapFreq[patch][twoLocHapMap[a1][a2]];
1917 ++genoFreq[patch][0];
1921 ++genoFreq[patch][1];
1927 ++genoFreq[patch][2];
1931 ++genoFreq[patch][3];
1935 ++hapFreq[patch][twoLocHapMap[a1][a2]];
1939 for (
size_t geno = 0; geno < 4; geno++) {
1940 genoFreq[patch][geno] /= patchSizes[patch];
1941 meanGenoFreq[geno] += genoFreq[patch][geno];
1944 for (
size_t hap = 0; hap < 4; hap++) {
1945 hapFreq[patch][hap] /= patchSizes[patch];
1946 meanHapFreq[hap] += hapFreq[patch][hap];
1950 for (
size_t geno = 0; geno < 4; geno++)
1951 meanGenoFreq[geno] /= extant_patches;
1953 for (
size_t hap = 0; hap < 4; hap++)
1954 meanHapFreq[hap] /= extant_patches;
1957 if ( !(meanGenoFreq[0]*meanGenoFreq[1]) && !(meanGenoFreq[2] * meanGenoFreq[3]))
1962 for(
int patch = 0; patch < patchNbr; patch++) {
1964 if(!patchSizes[patch])
continue;
1966 for (
size_t hap = 0; hap < 4; hap++) {
1968 Dis[pcomb] += pow(hapFreq[patch][hap] -
1969 (genoFreq[patch][reverseHapMap[hap][0]] * genoFreq[patch][reverseHapMap[hap][1]]), 2);
1972 Dst[pcomb] += pow((genoFreq[patch][reverseHapMap[hap][0]] * genoFreq[patch][reverseHapMap[hap][1]]) -
1973 (meanGenoFreq[reverseHapMap[hap][0]] * meanGenoFreq[reverseHapMap[hap][1]]), 2);
1976 Disp[pcomb] += pow(hapFreq[patch][hap] - meanHapFreq[hap], 2);
1979 Dstp[pcomb] += pow(meanHapFreq[hap] -
1980 (meanGenoFreq[reverseHapMap[hap][0]] * meanGenoFreq[reverseHapMap[hap][1]]), 2);
1983 double denom = genoFreq[patch][0] * genoFreq[patch][1] * genoFreq[patch][2] * genoFreq[patch][3];
1986 rSquare[patch][pcomb] = 0;
1988 rSquare[patch][pcomb] = pow(hapFreq[patch][0] - genoFreq[patch][0]*genoFreq[patch][2], 2)
1993 Dis[pcomb] /= extant_patches;
1994 Dst[pcomb] /= extant_patches;
1995 Disp[pcomb] /= extant_patches;
1996 Dstp[pcomb] /= extant_patches;
2026 FILE.open(filename.c_str(), ios::out);
2027 std::ios_base::sync_with_stdio(
false);
2029 if(!FILE)
fatal(
"Trait neutral could not open output file: \"%s\"\n",filename.c_str());
2032 message(
"TTNOhtaStats::FHwrite (%s)\n",filename.c_str());
2036 FILE <<
"loc1\tloc2\tDst\tDis\tDstp\tDisp";
2037 for(
int patch = 0; patch < patchNbr; patch++)
2038 FILE <<
"\tr_" << patch+1;
2041 for (
size_t pcomb = 0; pcomb < num_comb; pcomb++) {
2044 << Dst[pcomb] <<
"\t" << Dis[pcomb] <<
"\t" << Dstp[pcomb] <<
"\t" << Disp[pcomb];
2045 for(
int patch = 0; patch < patchNbr; patch++)
2046 FILE <<
"\t" << rSquare[patch][pcomb];
2052 std::ios_base::sync_with_stdio(
true);
std::string & get_filename()
Builds and returns the current file name depending on the periodicity of the file.
Definition: filehandler.cc:149
Metapop * get_pop_ptr()
Returns the pointer to the current metapop through the FileServices interface.
Definition: filehandler.h:133
This class contains traits along with other individual information (sex, pedigree,...
Definition: individual.h:47
TTrait * getTrait(IDX T)
Trait accessor.
Definition: individual.h:275
Second class in the metapopulation design structure, between the Metapop and Individual classes.
Definition: metapop.h:430
unsigned int size(age_t AGE)
Returns the size of the container of the appropriate age class(es) for both sexes.
Definition: metapop.h:496
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:532
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:559
void copy(const TMatrix &mat)
Copy a matrix.
Definition: tmatrix.h:76
double get(unsigned int i, unsigned int j) const
Accessor to element at row i and column j.
Definition: tmatrix.h:191
unsigned int nrows() const
Definition: tmatrix.h:211
unsigned int get_locus_num()
Definition: ttneutralgenes.h:198
unsigned int get_allele_num()
Definition: ttneutralgenes.h:199
TMatrix _pairwiseCombs
Definition: ttneutralgenes.h:301
Interface for all trait types, declares all basic trait operations.
Definition: ttrait.h:44
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:222
TProtoNeutralGenes * _FHLinkedTrait
Definition: filehandler.h:221
void fatal(const char *str,...)
Definition: output.cc:98
int error(const char *str,...)
Definition: output.cc:77
void message(const char *message,...)
Definition: output.cc:38
sex_t
Sex types, males are always 0 and females 1!!
Definition: types.h:34
@ FEM
Definition: types.h:35
@ MAL
Definition: types.h:35
@ ADLTx
Definition: types.h:40
TMatrix nChooseKVec(int n, int k)
Definition: utils.cc:196