6476 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");
6495 Patch* current_patch;
6497 vector<double> Dis(num_comb, 0.0),
6499 Disp(num_comb, 0.0),
6500 Dstp(num_comb, 0.0);
6502 vector< vector<double> > rSquare = vector< vector<double> > (patchNbr, vector<double>(num_comb, 0.0));
6504 vector<bool> NA(num_comb,
false);
6507 int extant_patches = 0;
6509 vector<int> patchSizes(patchNbr, 0);
6513 for(
int patch = 0; patch < patchNbr; patch++) {
6515 current_patch = pop->
getPatch(patch);
6519 if(patchSizes[patch]) ++extant_patches;
6521 total_size += patchSizes[patch];
6525 message(
"TTQOhtaStats::FHwrite:: computing association stats of %i combinations\n", num_comb);
6529 unsigned int a1, a2;
6530 unsigned int twoLocHapMap[2][2] = {{0,1},{2,3}};
6531 unsigned int reverseHapMap[4][2] = {{0,2},{0,3},{1,2},{1,3}};
6533 vector< double > meanAlleleFreq(4,0.0);
6534 vector< double > meanHapFreq(4,0.0);
6536 vector< vector< double > > alleleFreq = vector< vector< double > > (patchNbr, vector< double >(4,0.0));
6537 vector< vector< double > > hapFreq = vector< vector< double > > (patchNbr, vector< double >(4,0.0));
6540 for (
size_t pcomb = 0; pcomb < num_comb; pcomb++) {
6545 meanAlleleFreq.assign(4, 0.0);
6546 meanHapFreq.assign(4,0.0);
6548 for(
int patch = 0; patch < patchNbr; patch++) {
6550 current_patch = pop->
getPatch(patch);
6552 alleleFreq[patch].
assign(4,0.0);
6553 hapFreq[patch].assign(4,0.0);
6555 if(patchSizes[patch] == 0)
continue;
6559 for(
unsigned int s =0; s < 2; ++s) {
6561 for(
unsigned int j = 0, size = current_patch->
size(
sex_t(s),
ADLTx); j < size; j++) {
6570 ++alleleFreq[patch][0];
6574 ++alleleFreq[patch][1];
6580 ++alleleFreq[patch][2];
6584 ++alleleFreq[patch][3];
6588 ++hapFreq[patch][ twoLocHapMap[a1][a2] ];
6593 ++alleleFreq[patch][0];
6597 ++alleleFreq[patch][1];
6603 ++alleleFreq[patch][2];
6607 ++alleleFreq[patch][3];
6611 ++hapFreq[patch][ twoLocHapMap[a1][a2] ];
6616 for (
size_t geno = 0; geno < 4; geno++) {
6617 alleleFreq[patch][geno] /= patchSizes[patch];
6618 meanAlleleFreq[ geno ] += alleleFreq[patch][ geno ];
6621 for (
size_t hap = 0; hap < 4; hap++) {
6622 hapFreq[patch][hap] /= patchSizes[patch];
6623 meanHapFreq[hap] += hapFreq[patch][hap];
6627 for (
size_t geno = 0; geno < 4; geno++)
6628 meanAlleleFreq[geno] /= extant_patches;
6630 for (
size_t hap = 0; hap < 4; hap++)
6631 meanHapFreq[hap] /= extant_patches;
6634 if ( !(meanAlleleFreq[0]*meanAlleleFreq[1]) && !(meanAlleleFreq[2] * meanAlleleFreq[3]))
6639 for(
int patch = 0; patch < patchNbr; patch++) {
6641 if(!patchSizes[patch])
continue;
6643 for (
size_t hap = 0; hap < 4; hap++) {
6645 Dis[pcomb] += pow(hapFreq[patch][hap] -
6646 (alleleFreq[patch][ reverseHapMap[hap][0] ] * alleleFreq[patch][ reverseHapMap[hap][1]] ), 2);
6649 Dst[pcomb] += pow((alleleFreq[patch][reverseHapMap[hap][0]] * alleleFreq[patch][reverseHapMap[hap][1]]) -
6650 (meanAlleleFreq[reverseHapMap[hap][0]] * meanAlleleFreq[reverseHapMap[hap][1]]), 2);
6653 Disp[pcomb] += pow(hapFreq[patch][hap] - meanHapFreq[hap], 2);
6656 Dstp[pcomb] += pow(meanHapFreq[hap] -
6657 (meanAlleleFreq[reverseHapMap[hap][0]] * meanAlleleFreq[reverseHapMap[hap][1]]), 2);
6660 double denom = alleleFreq[patch][0] * alleleFreq[patch][1] * alleleFreq[patch][2] * alleleFreq[patch][3];
6663 rSquare[patch][pcomb] = 0;
6665 rSquare[patch][pcomb] = pow(hapFreq[patch][0] - alleleFreq[patch][0]*alleleFreq[patch][2], 2)
6670 Dis[pcomb] /= extant_patches;
6671 Dst[pcomb] /= extant_patches;
6672 Disp[pcomb] /= extant_patches;
6673 Dstp[pcomb] /= extant_patches;
6704 FILE.open(filename.c_str(), ios::out);
6705 std::ios_base::sync_with_stdio(
false);
6707 if(!FILE)
fatal(
"Trait quanti could not open output file: \"%s\"\n",filename.c_str());
6710 message(
"TTQOhtaStats::FHwrite (%s)\n",filename.c_str());
6715 FILE <<
"loc1\tloc2\tDst\tDis\tDstp\tDisp";
6717 for(
int patch = 0; patch < patchNbr; patch++)
6718 FILE <<
"\tr_" << patch+1;
6721 for (
size_t pcomb = 0; pcomb < num_comb; pcomb++) {
6724 << Dst[pcomb] <<
"\t" << Dis[pcomb] <<
"\t" << Dstp[pcomb] <<
"\t" << Disp[pcomb];
6725 for(
int patch = 0; patch < patchNbr; patch++)
6726 FILE <<
"\t" << rSquare[patch][pcomb];
6732 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:151
Metapop * _pop
Pointer to the current metapop, set during initialization within the init function.
Definition: filehandler.h:103
FileServices * get_service()
Returns pointer to the FileServices.
Definition: filehandler.h:139
Metapop * getSampledPop()
Sets the down-sampled population and provides accessor to file handlers.
Definition: fileservices.cc:198
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:434
unsigned int get_num_locus()
Definition: ttquanti.h:424
unsigned int get_num_traits()
Definition: ttquanti.h:423
TMatrix _pairwiseCombs
Definition: ttquanti.h:857
TTQuanti_diallelic.
Definition: ttquanti.h:281
virtual bool get_allele_bit(unsigned int position, unsigned int allele) const
Definition: ttquanti.cc:3970
int _FHLinkedTraitIndex
Definition: filehandler.h:224
TProtoQuanti * _FHLinkedTrait
Definition: filehandler.h:223
void fatal(const char *str,...)
Definition: output.cc:100
int error(const char *str,...)
Definition: output.cc:79
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