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");
6728 Patch* current_patch;
6730 vector<double> Dis(num_comb, 0.0),
6732 Disp(num_comb, 0.0),
6733 Dstp(num_comb, 0.0);
6735 vector< vector<double> > rSquare = vector< vector<double> > (patchNbr, vector<double>(num_comb, 0.0));
6737 vector<bool> NA(num_comb,
false);
6740 int extant_patches = 0;
6742 vector<int> patchSizes(patchNbr, 0);
6746 for(
int patch = 0; patch < patchNbr; patch++) {
6748 current_patch = pop->
getPatch(patch);
6752 if(patchSizes[patch]) ++extant_patches;
6754 total_size += patchSizes[patch];
6758 message(
"TTQOhtaStats::FHwrite:: computing association stats of %i combinations\n", num_comb);
6762 unsigned int a1, a2;
6763 unsigned int twoLocHapMap[2][2] = {{0,1},{2,3}};
6764 unsigned int reverseHapMap[4][2] = {{0,2},{0,3},{1,2},{1,3}};
6766 vector< double > meanAlleleFreq(4,0.0);
6767 vector< double > meanHapFreq(4,0.0);
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));
6773 for (
size_t pcomb = 0; pcomb < num_comb; pcomb++) {
6778 meanAlleleFreq.assign(4, 0.0);
6779 meanHapFreq.assign(4,0.0);
6781 for(
int patch = 0; patch < patchNbr; patch++) {
6783 current_patch = pop->
getPatch(patch);
6785 alleleFreq[patch].
assign(4,0.0);
6786 hapFreq[patch].assign(4,0.0);
6788 if(patchSizes[patch] == 0)
continue;
6792 for(
unsigned int s =0; s < 2; ++s) {
6794 for(
unsigned int j = 0, size = current_patch->
size(
sex_t(s),
ADLTx); j < size; j++) {
6803 ++alleleFreq[patch][0];
6807 ++alleleFreq[patch][1];
6813 ++alleleFreq[patch][2];
6817 ++alleleFreq[patch][3];
6821 ++hapFreq[patch][ twoLocHapMap[a1][a2] ];
6826 ++alleleFreq[patch][0];
6830 ++alleleFreq[patch][1];
6836 ++alleleFreq[patch][2];
6840 ++alleleFreq[patch][3];
6844 ++hapFreq[patch][ twoLocHapMap[a1][a2] ];
6849 for (
size_t geno = 0; geno < 4; geno++) {
6850 alleleFreq[patch][geno] /= patchSizes[patch];
6851 meanAlleleFreq[ geno ] += alleleFreq[patch][ geno ];
6854 for (
size_t hap = 0; hap < 4; hap++) {
6855 hapFreq[patch][hap] /= patchSizes[patch];
6856 meanHapFreq[hap] += hapFreq[patch][hap];
6860 for (
size_t geno = 0; geno < 4; geno++)
6861 meanAlleleFreq[geno] /= extant_patches;
6863 for (
size_t hap = 0; hap < 4; hap++)
6864 meanHapFreq[hap] /= extant_patches;
6867 if ( !(meanAlleleFreq[0]*meanAlleleFreq[1]) && !(meanAlleleFreq[2] * meanAlleleFreq[3]))
6872 for(
int patch = 0; patch < patchNbr; patch++) {
6874 if(!patchSizes[patch])
continue;
6876 for (
size_t hap = 0; hap < 4; hap++) {
6878 Dis[pcomb] += pow(hapFreq[patch][hap] -
6879 (alleleFreq[patch][ reverseHapMap[hap][0] ] * alleleFreq[patch][ reverseHapMap[hap][1]] ), 2);
6882 Dst[pcomb] += pow((alleleFreq[patch][reverseHapMap[hap][0]] * alleleFreq[patch][reverseHapMap[hap][1]]) -
6883 (meanAlleleFreq[reverseHapMap[hap][0]] * meanAlleleFreq[reverseHapMap[hap][1]]), 2);
6886 Disp[pcomb] += pow(hapFreq[patch][hap] - meanHapFreq[hap], 2);
6889 Dstp[pcomb] += pow(meanHapFreq[hap] -
6890 (meanAlleleFreq[reverseHapMap[hap][0]] * meanAlleleFreq[reverseHapMap[hap][1]]), 2);
6893 double denom = alleleFreq[patch][0] * alleleFreq[patch][1] * alleleFreq[patch][2] * alleleFreq[patch][3];
6896 rSquare[patch][pcomb] = 0;
6898 rSquare[patch][pcomb] = pow(hapFreq[patch][0] - alleleFreq[patch][0]*alleleFreq[patch][2], 2)
6903 Dis[pcomb] /= extant_patches;
6904 Dst[pcomb] /= extant_patches;
6905 Disp[pcomb] /= extant_patches;
6906 Dstp[pcomb] /= extant_patches;
6937 FILE.open(filename.c_str(), ios::out);
6939 if(!FILE)
fatal(
"Trait quanti could not open output file: \"%s\"\n",filename.c_str());
6942 message(
"TTQOhtaStats::FHwrite (%s)\n",filename.c_str());
6948 FILE <<
"loc1\tloc2\tDst\tDis\tDstp\tDisp";
6950 for(
int patch = 0; patch < patchNbr; patch++)
6951 FILE <<
"\tr_" << patch+1;
6954 for (
size_t pcomb = 0; pcomb < num_comb; pcomb++) {
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];
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_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