/* ********************************************************************************* * * write_outfile * * ------------- * * Descriptions: * * write the test-statistics, unadjusted p-values, adjusted pvalues and Adjusted * * p-values lower to the file. * * input parameters: * * filename: the file to write * * pdata: the pointer of the whole data * * T,P,Adj_P,Adj_Lower: the array stores the test-statistics, * * unadjusted p-values, adjusted pvalues and adjusted p-values lower, respectively * * if Adj_Lower==NULL, it will not print this item * * * * ********************************************************************************* */ void write_outfile(FILE *fh, GENE_DATA *pdata, double *T, double *P, double *Adj_P, double *Adj_Lower) { int i, nrow; // Double num,denum // The length of the array T,P,etc. nrow=pdata->nrow; if(myDEBUG) { fprintf(stderr, "\nThe results of T,P Adj_P and Adj_Lower"); print_farray(stderr, T, nrow); print_farray(stderr, P, nrow); print_farray(stderr, Adj_P, nrow); if(Adj_Lower) print_farray(stderr, Adj_Lower, nrow); } fprintf(stderr, "\nWe're writing the output\n"); fprintf(fh, "%20s %10s %10s %10s", "gene_id", "test-stat", "unadj-p", "adjusted-p"); if(Adj_Lower) fprintf(fh, "%10s", "p-lower"); fprintf(fh, "\n"); for (i=0; i<nrow; i++) { // t_stat_num_den(pdata->d[i],pdata->L, pdata->ncol,pdata->na,&num,&denum); fprintf(fh, "%20s %10.6f %7g %7g", pdata->id[i], T[i], P[i], Adj_P[i]); if(Adj_Lower){ fprintf(fh," %7g", Adj_Lower[i]); } fprintf(fh, "\n"); } }
/*get all the samples of T and they're also ordered.It's used only for diagonsis*/ void get_all_samples_T(float* V, int n,float* T,float na, FUNC_STAT func_stat,FUNC_SAMPLE func_first_sample, FUNC_SAMPLE func_next_sample,const void* extra) { int *L,*R,is_next,b=0,B; B=(*func_first_sample)(NULL); /*allocate the spaces*/ assert(L=(int*)Calloc(n,int)); assert(R=(int*)Calloc(B,int)); /*compute all the test_stat*/ (*func_first_sample)(L); is_next=1; while(is_next){ T[b]=func_stat(V,L,n,na,extra); b++; is_next=(*func_next_sample)(L); } if(B!=b){ fprintf(stderr,"Error we have b(%d)!=B(%d)\n",b,B); return;/*exit(1)*/; } if(myDEBUG) print_farray(stderr,T,B); Free(L); Free(R); }
/* Descriptions: Try to get all the unadjusted p-values for a gene with experssion values at V with n experiemtns. int first_sample(int *L) get the first sample of the labelling. if L==NULL, then it returns all the possible simulations, which depends on the initial function create_sampling. int next_sample(int* L) get the next sample, if it's done all the sampling, then it returns 0, otherwise it returns 1. output is P*/ void get_all_samples_P(float* V, int n,float* P,float na, FUNC_STAT func_stat,FUNC_SAMPLE func_first_sample, FUNC_SAMPLE func_next_sample,FUNC_CMP func_cmp,const void* extra) { int *L,*R,i,oldb,is_next,b=0,B_new,B; float* T=P,oldf;/*it first stores T, then switch to P*/ B=(*func_first_sample)(NULL); /*allocate the spaces*/ assert(L=(int*)Calloc(n,int)); assert(R=(int*)Calloc(B,int)); /*compute all the test_stat*/ (*func_first_sample)(L); is_next=1; B_new=0; while(is_next){ T[b]=func_stat(V,L,n,na,extra); if(T[b]!=NA_FLOAT) B_new++; b++; is_next=(*func_next_sample)(L); } if(B!=b){ fprintf(stderr,"Error we have b(%d)!=B(%d)\n",b,B); return;/*exit(1)*/; } if(myDEBUG) print_farray(stderr,T,B); /*order the test_stat*/ order_data(T,R,B,func_cmp); /*note the last elements of B-B_new has NA T-value*/ /*assign the probabilites*/ oldb=0; oldf=T[R[0]]; for(b=1;b<B_new;b++){ if((func_cmp==cmp_high)&(T[R[b]]>=oldf-EPSILON)) continue; else if ((func_cmp==cmp_low ) &&(T[R[b]]<=oldf+EPSILON)) continue; else if((func_cmp==cmp_abs )&& fabs(T[R[b]])>=fabs(oldf)-EPSILON) continue; for(i=oldb;i<b;i++) P[R[i]]=(b+0.0)/B_new; oldb=b; if(b<B_new-1) oldf=T[R[b]]; } for(i=oldb;i<b;i++) P[R[i]]=1.0; /*for NA test_stat, assign NA probabilites*/ for(b=B_new;b<B;b++) P[R[b]]=NA_FLOAT; /*free the space*/ Free(L); Free(R); }
void adj_pvalue_quick(GENE_DATA* pdata,float*T, float* P, float* Adj_P,float* Adj_Lower, FUNC_STAT func_stat,FUNC_STAT func_stat_T, FUNC_SAMPLE func_first_sample, FUNC_SAMPLE func_next_sample,FUNC_CMP func_cmp,const void* extra) { int *L,b,B,B_new,i,*R,neq; /*b for simulation*, neq is for the number of equal signs*/ float* all_P,*all_Q,count; int ncol=pdata->ncol,nrow=pdata->nrow; /*allocate the space*/ B=(*func_first_sample)(NULL); assert(L=(int*)Calloc(ncol,int)); assert(R=(int*)Calloc(nrow,int)); assert(all_P=(float*)Calloc(B,float)); assert(all_Q=(float*)Calloc(B,float)); /*get the original unadjusted p-values first we'll use the normalized t-statistics*/ get1pvalue(pdata,pdata->L,T,P,func_stat_T,func_first_sample,func_next_sample,func_cmp,extra); if(myDEBUG) { print_farray(stderr,T,pdata->nrow); print_farray(stderr,P,pdata->nrow); } /*sort the test_stat*/ order_mult_data(R,nrow,2,P,cmp_low,T,func_cmp); /*order_data(P,R,nrow,func_cmp);*/ /*rearrange the data according the unadjusted p-values*/ sort_gene_data(pdata,R); sort_vector(T,R,nrow); sort_vector(P,R,nrow); /*initialze all_Q[]=NA_FLOAT*/ for(b=0;b<B;b++) all_Q[b]=NA_FLOAT; /*loop for each gene*/ for(i=nrow-1;i>=0;i--){ get_all_samples_P(pdata->d[i],ncol,all_P,pdata->na, func_stat,func_first_sample,func_next_sample,func_cmp,extra); if(myDEBUG) print_farray(stderr,all_P,B); /*update all_Q*/ count=0; B_new=0; neq=0; for(b=0;b<B;b++){ if (all_P[b]==NA_FLOAT) break;/*we don't need care about NA pvlaues*/ if(all_Q[b]>all_P[b]) all_Q[b]=all_P[b];/*update q* by the value p*/ if(all_Q[b]==NA_FLOAT) continue;/*skip NA q*/ if(all_Q[b]<P[i]){ count+=1; }else if (all_Q[b]<=P[i]+EPSILON)/*it'd already > */ neq++; B_new++; } if(myDEBUG) { print_farray(stderr,all_Q,B); fprintf(stderr,"P[%d]=%5.3f,count=%5.2f,neq=%d\n",i,P[i],count,neq); } /*assign the Adj_P and Adj_Lower for gene i */ if(B_new!=0) { Adj_P[i]=(count+neq)/B_new; if(neq==0) Adj_Lower[i]=count/B_new; else Adj_Lower[i]=(count+1)/B_new; } else { Adj_P[i]=NA_FLOAT; Adj_Lower[i]=NA_FLOAT; } /*************************** */ print_b((nrow-i),nrow,"r="); } /* to make monotone of Adj_P and Adj_Lower*/ for(i=1;i<nrow;i++) if(Adj_P[i]<Adj_P[i-1]) Adj_P[i]=Adj_P[i-1]; for(i=1;i<nrow;i++) if(Adj_Lower[i]<Adj_Lower[i-1]) Adj_Lower[i]=Adj_Lower[i-1]; /*free the spaces*/ Free(L); Free(R); Free(all_P); Free(all_Q); }