4 //The following code has been modified using the original Metastats program from White, J.R., Nagarajan, N. & Pop, M. Statistical methods for detecting differentially abundant features in clinical metagenomic samples. PLoS Comput Biol 5, e1000352 (2009).
6 int metastat_main (char* outputFileName, int numRows, int numCols, double threshold, int numPermutations, double** data, int secondGroupingStart){
8 int size,c=0,i=0,j=0,counter=0, bflag=0;
12 int g = secondGroupingStart;
13 double thresh=threshold;
17 strcpy(output, outputFileName);
21 printf("Check your g value\n");
24 // Initialize the matrices
26 double matrix[row][col];
27 double pmatrix[size],permuted[size];
28 double storage[row][9];
38 matrix[i][j]=data[i][j];
39 pmatrix[c]=0; // initializing to zero
45 // Produces the sum of each column
46 double total[col],total1=0,total2=0;
55 total[i]=total[i]+matrix[j][i];
60 total1=total1+total[i];}
63 total2=total2+total[i];}
66 // Creates the ratios by first finding the minimum of totals
69 if (total[0]<total[1]){
79 printf("Error, the sum of one of the columns <= 0.");
86 ratio[i]=total[i]/min;
89 //Change matrix into an array as received by R for compatibility.
94 pmatrix[c]=matrix[j][i];
100 for (i =0; i<col;i++){
101 pmatrix[i]=pmatrix[i]/ratio[i];
107 for (i=0; i<size; i++) {
108 if (counter % row == 0) {
111 pmatrix[i]=pmatrix[i]/ratio[j];
115 // pass everything to the rest of the code using pointers. then
116 // write to output file. below pointers for most of the values are
117 // created to send everything by reference.
119 int ptt_size, *permutes,*nc,*nr,*gvalue;
128 //changing ptt_size to row
129 double permuted_ttests[row], pvalues[row], tinitial[row];
132 permuted_ttests[i]=0;}
138 // Find the initial values for the matrix.
139 start(pmatrix,gvalue,nr,nc,tinitial,storage);
141 // Start the calculations.
143 if ( (col==2) || ((g-1)<8) || ((col-g+1) < 8) ){
145 double fish[row], fish2[row];
153 fish[i]=fish[i]+matrix[i][j];
156 for(j=g-1;j<col;j++){
157 fish2[i]=fish2[i]+matrix[i][j];
160 double f11,f12,f21,f22;
168 double data[] = {f11, f12, f21, f22};
170 // CONTINGENGCY TABLE:
174 int *nr, *nc, *ldtabl, *work;
175 int nrow=2, ncol=2, ldtable=2, workspace=100000;
176 double *expect, *prc, *emin,*prt,*pre;
177 double e=0, prc1=0, emin1=0, prt1=0, pre1=0;
190 //MothurFisher fishtere;
191 //double mothurFex = fishtere.fexact(f11, f12, f21, f22);
193 fexact(nr,nc,data, ldtabl,expect,prc,emin,prt,pre,work);
195 if (*pre>.999999999){
199 //printf("feaxt = %f\t%f\t%f\t%f\t%f\t%f\n", *expect, *pre, f11, f12, f21, f22);
200 storage[i][8] = *pre;
206 testp(permuted_ttests, permutes, permuted,pmatrix, nc, nr, gvalue,tinitial,pvalues);
208 // Checks to make sure the matrix isn't sparse.
209 double sparse[row], sparse2[row];
218 sparse[i]=sparse[i]+matrix[i][j];
221 if(sparse[i] < (double)(g-1)){
224 for(j=g-1;j<col;j++){ // ?<= for col
225 sparse2[i]=sparse2[i]+matrix[i][j];
228 if( (sparse2[i] <(double)(col-g+1))) {
235 double f11,f12,f21,f22;
246 double data[] = {f11, f12, f21, f22};
248 int *nr, *nc, *ldtabl, *work;
249 int nrow=2, ncol=2, ldtable=2, workspace=10000000; // I added two zeros for larger data sets
250 double *expect, *prc, *emin,*prt,*pre;
251 double e=0, prc1=0, emin1=0, prt1=0, pre1=0;
264 fexact(nr,nc,data, ldtabl,expect,prc,emin,prt,pre,work);
266 if (*pre>.999999999){
269 storage[i][8] = *pre;
273 // End of else statement
277 // Calculates the mean of counts (not normalized)
287 for (i=1; i<=(g-1); i++){
288 temp[j][0]=temp[j][0]+matrix[j][i-1];
290 temp[j][0]= (double) temp[j][0]/(g-1);
292 temp[j][1]=temp[j][1]+matrix[j][i-1];
294 temp[j][1]= (double) temp[j][1]/(col-g+1);
298 storage[i][3]=temp[i][0];
299 storage[i][7]=temp[i][1];
300 storage[i][8]=pvalues[i];
306 if(pvalues[i]<thresh){
307 printf("Feature %d is significant, p = %.10lf \n",i+1,pvalues[i]);
311 // And now we write the files to a text file.
315 local = localtime(&t);
317 out = fopen(output,"w");
319 fprintf(out,"Local time and date of test: %s\n", asctime(local));
320 fprintf(out,"# rows = %d, # col = %d, g = %d\n\n",row,col,g);
322 fprintf(out,"%d permutations\n\n",B);
325 //output column headings - not really sure... documentation labels 9 columns, there are 10 in the output file
326 //storage 0 = meanGroup1 - line 529, 1 = varGroup1 - line 532, 2 = err rate1 - line 534, 3 = mean of counts group1?? - line 291, 4 = meanGroup2 - line 536, 5 = varGroup2 - line 539, 6 = err rate2 - line 541, 7 = mean of counts group2?? - line 292, 8 = pvalues - line 293
327 fprintf(out, "OTU\tmean(group1)\tvariance(group1)\tstderr(group1)\tmean_of_counts(group1)\tmean(group2)\tvariance(group2)\tstderr(group2)\tmean_of_counts(group1)\tp-value\n");
329 for(i=0; i<row; i++){
330 fprintf(out,"%d",(i+1));
333 fprintf(out,"\t%.12lf",storage[i][j]);
338 fprintf(out,"\n \n");
346 void testp(double *permuted_ttests,int *B,double *permuted,
347 double *Imatrix,int *nc,int *nr,int *g,double *Tinitial,double
363 for (j=1; j<=*B; j++){
364 permute_matrix(Imatrix,nc,nr,permuted,g,Tvalues,Tinitial,counter);
365 // for(i=0;i<*nr;i++){
366 // permuted_ttests[k]=fabs(Tvalues[i]);
372 ps[j]=((counter[j]+1)/(double)(a+1));
376 void permute_matrix(double *Imatrix,int *nc,int *nr,double *permuted,
377 int *g,double *trial_ts,double *Tinitial,double *counter1){
379 int i=0,j=0,n=0,a=0,b=0,f=0,c=0,k=0;
381 a = *nr; // number of rows
387 for (i=1; i<=*nc; i++){
393 for (i=0; i<*nc; i++){
394 f = y[i]; //column number
400 } // starting value position in the Imatrix
401 for(j=1; j<=*nr; j++){
402 permuted[k] = Imatrix[c];
408 calc_twosample_ts(permuted,g,nc,nr,trial_ts,Tinitial,counter1);
411 void permute_array(int *array, int n) {
412 static int seeded = 0;
420 for (i = 0; i < n; i++) {
421 int selection = rand() % (n - i);
422 int tmp = array[i + selection];
423 array[i + selection] = array[i];
428 void calc_twosample_ts(double *Pmatrix,int *g,int *nc,int *nr,
429 double *Ts,double *Tinitial,double *counter) {
434 double C1[*nr][3], C2[*nr][3], storage[a],tool[a];
435 double nrows,ncols,gvalue, xbardiff=0, denom=0;
437 nrows = (double) *nr;
438 ncols = (double) *nc;
441 meanvar(Pmatrix,g,nr,nc,storage);
445 for (i=0; i<*nr;i++){
447 C1[i][1]=tool[i+*nr+*nr];
448 C1[i][2]=C1[i][1]/(gvalue-1);
450 C2[i][0]=tool[i+*nr];
451 C2[i][1]=tool[i+*nr+*nr+*nr]; // var group 2
452 C2[i][2]=C2[i][1]/(ncols-gvalue+1);
455 for (i=0; i<*nr; i++){
456 xbardiff = C1[i][0]-C2[i][0];
457 denom = sqrt(C1[i][2]+C2[i][2]);
458 Ts[i]=fabs(xbardiff/denom);
459 if (fabs(Ts[i])>(fabs(Tinitial[i])+.0000000000001)){ //13th place
465 void meanvar(double *pmatrix,int *g,int *nr,int *nc,double *store){
466 double temp[*nr], temp2[*nr],var[*nr],var2[*nr],a,b;
471 b = (double) (*nc-a);
473 for (i = 0; i<*nr; i++){
480 k = *nr; // number of rows
487 m*=k; // m = g * nr now
489 temp[i%k]=temp[i%k]+pmatrix[i];
492 temp2[i%k]=temp2[i%k]+pmatrix[i];
495 temp2[i]=temp2[i]-temp[i];
497 for (i=0;i<=*nr-1;i++){
499 store[i+*nr]=temp2[i]/b;
502 // That completes the mean calculations.
505 var[i%k]=var[i%k]+pow((pmatrix[i]-store[i%k]),2);
508 var2[i%k]=var2[i%k]+pow((pmatrix[i]-store[(i%k)+*nr]),2);
511 for (i=0;i<=*nr-1;i++){
512 store[i+2*k]=var[i]/(a-1);
513 store[i+3*k]=var2[i]/(b-1);
515 // That completes var calculations.
518 void start(double *Imatrix,int *g,int *nr,int *nc,double *initial,
519 double storage[][9]){
523 double store[a], tool[a], C1[*nr][3], C2[*nr][3];
524 double nrows,ncols,gvalue, xbardiff=0, denom=0;
526 nrows = (double) *nr;
527 ncols = (double) *nc;
530 meanvar(Imatrix,g,nr,nc,store);
535 for (i=0; i<*nr;i++){
536 C1[i][0]=tool[i]; //mean group 1
537 storage[i][0]=C1[i][0];
538 C1[i][1]=tool[i+*nr+*nr]; // var group 1
539 storage[i][1]=C1[i][1];
540 C1[i][2]=C1[i][1]/(gvalue-1);
541 storage[i][2]=sqrt(C1[i][2]);
543 C2[i][0]=tool[i+*nr]; // mean group 2
544 storage[i][4]=C2[i][0];
545 C2[i][1]=tool[i+*nr+*nr+*nr]; // var group 2
546 storage[i][5]=C2[i][1];
547 C2[i][2]=C2[i][1]/(ncols-gvalue+1);
548 storage[i][6]=sqrt(C2[i][2]);
550 for (i=0; i<*nr; i++){
551 xbardiff = C1[i][0]-C2[i][0];
552 denom = sqrt(C1[i][2]+C2[i][2]);
553 initial[i]=fabs(xbardiff/denom);