#include "includes.h" #include "grid.h" #include "vector.h" #include "time.h" #include extern ofstream errorfs; #include "declare.h" /* This file contains source code for estimating two-locus haplotype frequencies jointly using an HMM-- this complication is necessary to get "consistent" estimates across overlapping pairs of loci when there are missing alleles, ie., alleles not typed */ /* Following this, there is source code for adjustments to these frequencies for the following cases: 1. Allele appears in affected genotypes/haps. but not in controls (assigning freq. of 0 seems to harsh and will lead to loglik of -inf) 2. Haplotype appears in affecteds (or is compatible with genotypes) but either does not appear in controls or the estimate of freq. is vanishingly small (once again, seems too harsh a penalty) This is not trivial, because changing one joint frequency could change freq's at neighboring marker-pairs, etc., if not done correctly. */ void makehap3fr_hmm_HconBAYES(dvectors &h3_freq_lists, const ivectors &a_lists, const igrid &controls, const int &bayes, const int &debug) { //estimates three-marker haplotype frequencies, using HMM, assuming //haplotype controls int l,h,i,j,k,iter,conv; double diff,sum; const int numloc=controls.dimy(); const int numhap=controls.dimx(); //double llik=0.0,lik=0.0; //make vector indicating haplotypes with missing values int is_missing=0; ivector missing; missing.init(numhap); make_missing_Hfr(missing, is_missing, controls, 0); //delete me for (i=0;i0) { conv=1; break; } if (conv==0) trans[l][i*a_lists[l+1].dim()*a_lists[l+2].dim()+ j*a_lists[l+2].dim() ]=1; } } }//l //check trans for (l=0;l=0;l--) { for (i=0;i=0;l--) { for (i=0;i0) xi[h][l][ (i*a_lists[l+1].dim()+j)*alpha[h][l+1].dim()+ (j*a_lists[l+2].dim()+k)]= alpha[h][l][i*a_lists[l+1].dim()+j]* trans[l][ i*a_lists[l+1].dim()*a_lists[l+2].dim()+ j*a_lists[l+2].dim()+k]* b[h][l+1][j*a_lists[l+2].dim()+k]* beta[h][l+1][j*a_lists[l+2].dim()+k]; } } } } //make denominator denom=0.0; for (i=0;i0) { conv=1; break; } if (conv==0) trans[l][i*a_lists[l+1].dim()*a_lists[l+2].dim()+ j*a_lists[l+2].dim() ]=1; } } }//l //delete me // cout << "iter " << iter << " " //< trans[70][4] << " " << trans[70][5] << endl; //check trans for (l=0;l0) alpha[g][l+1][place]+= alpha[g][l][ i* a_lists[l ].dim()* a_lists[l+1].dim()* a_lists[l+1].dim()+ j* a_lists[l+1].dim()* a_lists[l+1].dim()+ k* a_lists[l+1].dim()+ m ]* trans[l][ i*a_lists[l+1].dim()*a_lists[l+2].dim()+ k*a_lists[l+2].dim()+ n]* trans[l][ j*a_lists[l+1].dim()*a_lists[l+2].dim()+ m*a_lists[l+2].dim()+ o]* b[g][l+1][place]; } } } } } } } } if (debug) { g=0; cout << "alpha [for genotype " << g << "]" << endl; for (l=0;l=0;l--) { for (i=0;i0) beta[g][l][ i* a_lists[l ].dim()* a_lists[l+1].dim()* a_lists[l+1].dim()+ j* a_lists[l+1].dim()* a_lists[l+1].dim()+ k* a_lists[l+1].dim()+ m ]+= beta[g][l+1][place]* trans[l][ i*a_lists[l+1].dim()*a_lists[l+2].dim()+ k*a_lists[l+2].dim()+ n]* trans[l][ j*a_lists[l+1].dim()*a_lists[l+2].dim()+ m*a_lists[l+2].dim()+ o]* b[g][l+1][place]; } } } } } } } } if (debug) { g=0; cout << "beta [for genotype " << g << "]" << endl; for (l=0;l0) xi[g][l][ i*a_lists[l].dim()* a_lists[l+1].dim()*a_lists[l+1].dim()* a_lists[l+2].dim()*a_lists[l+2].dim()+ j* a_lists[l+1].dim()*a_lists[l+1].dim()* a_lists[l+2].dim()*a_lists[l+2].dim()+ k* a_lists[l+1].dim()* a_lists[l+2].dim()*a_lists[l+2].dim()+ m* a_lists[l+2].dim()*a_lists[l+2].dim()+ n* a_lists[l+2].dim()+ o ]= alpha[g][l][placel]* trans[l][ i*a_lists[l+1].dim()*a_lists[l+2].dim()+ k*a_lists[l+2].dim()+ n]* trans[l][ j*a_lists[l+1].dim()*a_lists[l+2].dim()+ m*a_lists[l+2].dim()+ o]* b[g][l+1][placelp1]* beta[g][l+1][placelp1]; //delete me //if (g==75 && l==0 && // i==0 && j==0 && k==0 && m==0 && n==0 && o==0) //cout << "te " << alpha[g][l][placel] << " " // << beta[g][l+1][placelp1] << " " //<< b[g][l+1][placelp1] << endl; } } } } } } } //make denominator denom=0.0; for (i=0;i0) { conv=1; break; } if (conv==0) trans[l][i*a_lists[l+1].dim() ]=1.0; } }//l //check trans for (l=0;l=0;l--) { for (i=0;i