#include #include #include #include #include #include #include #include #include #include #define SAMPLE_SIZE 187 #define MARKER_LENGTH 85 #define N_ITERATIONS 25000 #define N_BURNS 5000 #define N_STEPS 10 #define N_PARS 2 #define N_GROUPS 50 #define COE 1.0 #define JITTER 0.01 #define ALPHA 0.02 #define SEP 10 #define EPS 2 #define DELTA 1e-3 #define NN 10 int main(void){ FILE *fp_in,*fp_out; int i,j,k,i_iter,i_real,s,**x; int do_eta_sample=0,do_sigma_update=1,update_sigma0=1; int n_used_groups,group_index[SAMPLE_SIZE],n_each_group[N_GROUPS]; int group_change[N_GROUPS]; int nonparametric=0; double t_age[SAMPLE_SIZE],t_group_value[N_GROUPS]; double rdum,fs[N_GROUPS],fss[N_GROUPS],bb,ss; double epsilon,aa,mu1,sigma11,mu_bar,sigma_bar; double y_qtl[SAMPLE_SIZE],alpha[N_PARS]; double b[MARKER_LENGTH],bs[MARKER_LENGTH],sigma0,sigma[MARKER_LENGTH]; double bss[MARKER_LENGTH],eta[SAMPLE_SIZE],tau; double beta1,beta_bar,gamma11,gamma_bar; double bj_bar,sj_bar,xb,**x_temp,*y_temp,x_square; double t_age_min,t_age_max,delta_age,p_max; double at,bt,tsum,ybar,ytbar; int chromosome[MARKER_LENGTH],index,nn[4],**x0; int left[MARKER_LENGTH],middle[MARKER_LENGTH],right[MARKER_LENGTH]; int fixed[MARKER_LENGTH],**missing,include[MARKER_LENGTH]; double cc=100,is[MARKER_LENGTH]; double pos[MARKER_LENGTH],setoff; double low_bound,up_bound,delta=2.0,lambda_temp; double pl,pr,pb[4],rho; double pp[MARKER_LENGTH][4],xdb,range,range_star; double lambda[MARKER_LENGTH]; double sep1,sep2,sep,prob1,prob2; double rlr,prob,rl[MARKER_LENGTH],rr[MARKER_LENGTH]; double lambda_s[MARKER_LENGTH]; const gsl_rng_type *T; gsl_rng *r; gsl_ran_discrete_t *g; gsl_vector *mu_res,*mu_v,*mu_star,*mu_temp,*y_res,*y_v,*eta_res,*eta_temp; gsl_vector *s12,*s12c,*eta_v,*eta_c,*b_v,*alpha_v,*alpha_res,*alpha_temp; gsl_vector *g12,*g12c,*beta_v,*beta_star; gsl_vector_view eta_view,mu_view,beta_view,alpha_view; gsl_matrix *cov,*cov_org,*cov_res,*cov_inv,*cov_star,*cov_temp,*id_m,*m_m,*id_g; gsl_matrix *gamma_left,*gamma_middle,*gamma_star,*gamma_m,*gamma_inv; gsl_matrix *sigma_v,*x_v; gsl_matrix_view cov1_view,cov2_view; gsl_matrix_view gamma1_view,gamma2_view; gsl_permutation *p,*p1,*p2,*p3; gsl_rng_env_setup(); T=gsl_rng_default; r=gsl_rng_alloc(T); alpha_v=gsl_vector_alloc(N_PARS); alpha_res=gsl_vector_alloc(N_PARS); alpha_temp=gsl_vector_alloc(N_PARS); beta_v=gsl_vector_alloc(N_PARS); beta_star=gsl_vector_alloc(N_PARS); g12=gsl_vector_alloc(N_PARS-1); g12c=gsl_vector_alloc(N_PARS-1); b_v=gsl_vector_alloc(MARKER_LENGTH); x = (int **)malloc(SAMPLE_SIZE*sizeof(int **)); x0 = (int **)malloc(SAMPLE_SIZE*sizeof(int **)); missing = (int **)malloc(SAMPLE_SIZE*sizeof(int **)); for(i = 0; i < SAMPLE_SIZE;i++){ x[i] = (int *)malloc(MARKER_LENGTH*sizeof(int *)); x0[i] = (int *)malloc(MARKER_LENGTH*sizeof(int *)); missing[i] = (int *)malloc(MARKER_LENGTH*sizeof(int *)); } /*Read input data for marker positions and chromosome*/ fp_in = fopen("lbwt_pos.txt","r"); for(j = 0;j < MARKER_LENGTH;j++) fscanf(fp_in,"%d",&chromosome[j]); for(j = 0;j < MARKER_LENGTH;j++) fscanf(fp_in,"%lf",&pos[j]); fclose(fp_in); setoff = 0.0; for(j = 0;j < MARKER_LENGTH;j++){ fixed[j] = 0; middle[j] = 0; right[j] = 0; } left[0] = 1; right[MARKER_LENGTH - 1] = 1; for(j = 0;j < MARKER_LENGTH;j++){ if(j > 0){ if(chromosome[j] != chromosome[j - 1]){ setoff = pos[j - 1] + 10; left[j] = 1; right[j - 1] = 1; middle[j -1] = 1; } if(chromosome[j] != chromosome[j - 1]){ if(j == MARKER_LENGTH - 1){ fixed[j] = 1; middle[j] = 0; }else if(chromosome[j] != chromosome[j + 1]){ fixed[j] = 1; middle[j] = 0; } } pos[j] += setoff; }else{ left[j] = 1; } } if(!fixed[MARKER_LENGTH - 1]) middle[MARKER_LENGTH - 1] = 1; /*Read input data to fill x[][] and y_qtl[]*/ fp_in = fopen("mes.txt","r"); tsum = 0.0; for(i = 0;i < SAMPLE_SIZE;i++){ for(j = 0;j < MARKER_LENGTH;j++){ fscanf(fp_in,"%d",&x0[i][j]); if(x0[i][j]==9){ missing[i][j] = 1; x0[i][j] = 1; /*Initialization*/ } } /*Read in the covariate and phenotypic values*/ fscanf(fp_in,"%lf",&t_age[i]); fscanf(fp_in,"%lf",&y_qtl[i]); tsum += t_age[i]*t_age[i]; } fclose(fp_in); /*Construct the eta group*/ t_age_min = gsl_stats_min(t_age,1,SAMPLE_SIZE); t_age_max = gsl_stats_max(t_age,1,SAMPLE_SIZE); delta_age = (t_age_max - t_age_min)/(N_GROUPS - 1); for(i=0;i>1); xdb = y_qtl[i] - eta[i]; for(k = 0;k < MARKER_LENGTH - 1;k++){ if(k != j) xdb -= x[i][k]*b[k]; } prob = exp( - 2.0*b[j]*xdb/sigma0); prob = pp[j][index]/ (pp[j][index] + (1.0 - pp[j][index])*prob); rdum = gsl_rng_uniform(r); if(rdum < prob) x[i][j] = 1; else x[i][j] = - 1; } }else x[i][j] = x0[i][j]; } } /*Update the x_temp which is used in the matrices calculation*/ for(i = 0;i < n_used_groups;i++){ for(j = 0;j < MARKER_LENGTH;j++){ x_temp[i][j] = 0.0; } } for(i = 0;i < SAMPLE_SIZE;i++){ for(j = 0;j < MARKER_LENGTH;j++){ x_temp[group_index[i]][j] += (double)x[i][j]; } } for(i = 0;i < n_used_groups;i++){ for(j = 0;j < MARKER_LENGTH;j++){ gsl_matrix_set(x_v,i,j,x_temp[i][j]/(double)n_each_group[i]); } } /*Update the missing genotypes*/ for(j = 0;j < MARKER_LENGTH;j++){ if(j > 0) sep1 = pos[j] - (pos[j - 1] + lambda[j - 1]); else sep1 = 0.0; if(j < MARKER_LENGTH - 1) sep2 = lambda[j]; else sep2 = 0.0; sep = sep1 + sep2; prob1 = 0.5*(1.0 - exp( - ALPHA*sep1)); prob2 = 0.5*(1.0 - exp( - ALPHA*sep2)); prob = 0.5*(1.0 - exp( - ALPHA*sep)); for(i = 0;i < SAMPLE_SIZE;i++){ if(missing[i][j]){ if(!fixed[j]){ rdum = gsl_rng_uniform(r); if(left[j]){ if(rdum < prob2) x0[i][j] = - x[i][j]; else x0[i][j] = x[i][j]; }else if(right[j]){ if(rdum < prob1) x0[i][j] = - x[i][j - 1]; else x0[i][j] = x[i][j - 1]; }else{ if(x[i][j - 1]*x[i][j]==1){ if(rdum<(prob1*prob2/(1.0 - prob))) x0[i][j] = - x[i][j - 1]; else x0[i][j] = x[i][j - 1]; }else{ if(rdum<(prob1*(1.0 - prob2)/prob)) x0[i][j] = - x[i][j - 1]; else x0[i][j] = x[i][j - 1]; } } }else{ xdb = y_qtl[i] - eta[i]; for(k = 0;k < MARKER_LENGTH;k++){ if(k != j) xdb -= x[i][k]*b[k]; } rho = 1/(1 + exp( - 2*xdb*b[j]/sigma0)); if(rdum < rho) x0[i][j] = 1; else x0[i][j] = - 1; } } } } /*Update the QTL location lambda[j]*/ for(j = 0;j < MARKER_LENGTH;j++){ if((!middle[j])&&(!fixed[j])){ for(k = 0;k < 4;k++){ nn[k] = 0; } /*Limit of old position*/ sep = pos[j + 1] - pos[j]; low_bound = lambda[j] - delta; up_bound = lambda[j] + delta; if(low_bound < 0) low_bound = 0.0; if(up_bound > sep) up_bound = sep; rdum = gsl_rng_uniform(r); lambda_temp = low_bound + rdum*(up_bound - low_bound); range = up_bound - low_bound; /*Limit of new position*/ low_bound = lambda_temp - delta; up_bound = lambda_temp + delta; if(low_bound < 0) low_bound = 0.0; if(up_bound > sep) up_bound = sep; range_star = up_bound - low_bound; /*Recombination possibility*/ pl = 0.5*(1.0 - exp( - ALPHA*lambda_temp)); pr = 0.5*(1.0 - exp( - ALPHA*(sep - lambda_temp))); pb[0] = (1.0 - pl)*(1.0 - pr)/(1.0 - rl[j])/(1.0 - rr[j]); pb[1] = (1.0 - pl)*pr/(1.0 - rl[j])/rr[j]; pb[2] = pl*pr/rl[j]/rr[j]; pb[3] = pl*(1.0 - pr)/rl[j]/(1.0 - rr[j]); for(i = 0;i < SAMPLE_SIZE;i++){ index = ((1 + x0[i][j])<<1) + (1 + x[i][j]) +((1 + x0[i][j + 1])>>1); if(index > 3) index = 7 - index; nn[index]++; } prob = 0.0; for(k = 0;k < 4;k++) prob += (double)nn[k]*log(pb[k]); prob = exp(prob)*range_star/range; rdum = gsl_rng_uniform(r); if(rdum < prob) lambda[j] = lambda_temp; } } printf("%d\t%lf\t%lf\t%lf\t%lf\n",i_iter,b[2],b[22],b[42],b[62]); i_iter++; if(i_iter>N_BURNS){ if((i_iter-N_BURNS)%N_STEPS==0){ for(j=0;j