/* hap_freq - array to store haplotype frequencies gl1 - GLs for site1 for all "n" individuals gl2 - GLs for site2 for all "n" individuals maf1 - minor allele frequency at site1 maf2 - minor allele frequency at site2 n_ind - number of individuals log_scale - are GLs in log scale? */ uint64_t haplo_freq(double hap_freq[4], double **gl1, double **gl2, double maf1, double maf2, uint64_t n_ind, bool log_scale){ uint64_t i; double hap_freq_last[4]; if(maf1 < 0 || maf1 > 1 || maf2 < 0 || maf2 > 1) error("__FUNCTION__", "invalid allele frequencies"); // Initialize haplotype frequencies hap_freq[0] = (1 - maf1) * (1 - maf2); // P_BA hap_freq[1] = (1 - maf1) * maf2; // P_Ba hap_freq[2] = maf1 * (1 - maf2); // P_bA hap_freq[3] = maf1 * maf2; // P_ba // iteration for(i = 0; i < ITER_MAX; i++) { double eps = 0; memcpy(hap_freq_last, hap_freq, 4 * sizeof(double)); if(log_scale) pair_freq_iter_log(hap_freq, gl1, gl2, n_ind); else pair_freq_iter(hap_freq, gl1, gl2, n_ind); for (uint64_t j = 0; j < 4; j++) { double x = fabs(hap_freq[j] - hap_freq_last[j]); if (x > eps) eps = x; } if(eps < EPSILON) break; } return i; }
double bcf_pair_freq(const bcf1_t *b0, const bcf1_t *b1, double f[4]) { const bcf1_t *b[2]; int i, j, n_smpl; double *pdg[2], flast[4], r, f0[2]; // initialize others if (b0->n_smpl != b1->n_smpl) return -1; // different number of samples n_smpl = b0->n_smpl; b[0] = b0; b[1] = b1; f[0] = f[1] = f[2] = f[3] = -1.; if (b[0]->n_alleles < 2 || b[1]->n_alleles < 2) return -1; // one allele only pdg[0] = get_pdg3(b0); pdg[1] = get_pdg3(b1); if (pdg[0] == 0 || pdg[1] == 0) { free(pdg[0]); free(pdg[1]); return -1; } // set the initial value f0[0] = est_freq(n_smpl, pdg[0]); f0[1] = est_freq(n_smpl, pdg[1]); f[0] = (1 - f0[0]) * (1 - f0[1]); f[3] = f0[0] * f0[1]; f[1] = (1 - f0[0]) * f0[1]; f[2] = f0[0] * (1 - f0[1]); // iteration for (j = 0; j < ITER_MAX; ++j) { double eps = 0; memcpy(flast, f, 4 * sizeof(double)); pair_freq_iter(n_smpl, pdg, f); for (i = 0; i < 4; ++i) { double x = fabs(f[i] - flast[i]); if (x > eps) eps = x; } if (eps < EPS) break; } // free free(pdg[0]); free(pdg[1]); { // calculate r^2 double p[2], q[2], D; p[0] = f[0] + f[1]; q[0] = 1 - p[0]; p[1] = f[0] + f[2]; q[1] = 1 - p[1]; D = f[0] * f[3] - f[1] * f[2]; r = sqrt(D * D / (p[0] * p[1] * q[0] * q[1])); // printf("R(%lf,%lf,%lf,%lf)=%lf\n", f[0], f[1], f[2], f[3], r); if (_isnan(r)) r = -1.; } return r; }