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 fexact(nr,nc,data, ldtabl,expect,prc,emin,prt,pre,work);
192 if (*pre>.999999999){
195 storage[i][8] = *pre;
201 testp(permuted_ttests, permutes, permuted,pmatrix, nc, nr, gvalue,tinitial,pvalues);
203 // Checks to make sure the matrix isn't sparse.
204 double sparse[row], sparse2[row];
213 sparse[i]=sparse[i]+matrix[i][j];
216 if(sparse[i] < (double)(g-1)){
219 for(j=g-1;j<col;j++){ // ?<= for col
220 sparse2[i]=sparse2[i]+matrix[i][j];
223 if( (sparse2[i] <(double)(col-g+1))) {
230 double f11,f12,f21,f22;
241 double data[] = {f11, f12, f21, f22};
243 int *nr, *nc, *ldtabl, *work;
244 int nrow=2, ncol=2, ldtable=2, workspace=10000000; // I added two zeros for larger data sets
245 double *expect, *prc, *emin,*prt,*pre;
246 double e=0, prc1=0, emin1=0, prt1=0, pre1=0;
259 fexact(nr,nc,data, ldtabl,expect,prc,emin,prt,pre,work);
261 if (*pre>.999999999){
264 storage[i][8] = *pre;
268 // End of else statement
272 // Calculates the mean of counts (not normalized)
282 for (i=1; i<=(g-1); i++){
283 temp[j][0]=temp[j][0]+matrix[j][i-1];
285 temp[j][0]= (double) temp[j][0]/(g-1);
287 temp[j][1]=temp[j][1]+matrix[j][i-1];
289 temp[j][1]= (double) temp[j][1]/(col-g+1);
293 storage[i][3]=temp[i][0];
294 storage[i][7]=temp[i][1];
295 storage[i][8]=pvalues[i];
301 if(pvalues[i]<thresh){
302 printf("Feature %d is significant, p = %.10lf \n",i+1,pvalues[i]);
306 // And now we write the files to a text file.
310 local = localtime(&t);
312 out = fopen(output,"w");
314 fprintf(out,"Local time and date of test: %s\n", asctime(local));
315 fprintf(out,"# rows = %d, # col = %d, g = %d\n\n",row,col,g);
317 fprintf(out,"%d permutations\n\n",B);
320 //output column headings - not really sure... documentation labels 9 columns, there are 10 in the output file
321 //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
322 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");
324 for(i=0; i<row; i++){
325 fprintf(out,"%d",(i+1));
328 fprintf(out,"\t%.12lf",storage[i][j]);
333 fprintf(out,"\n \n");
341 void testp(double *permuted_ttests,int *B,double *permuted,
342 double *Imatrix,int *nc,int *nr,int *g,double *Tinitial,double
358 for (j=1; j<=*B; j++){
359 permute_matrix(Imatrix,nc,nr,permuted,g,Tvalues,Tinitial,counter);
360 // for(i=0;i<*nr;i++){
361 // permuted_ttests[k]=fabs(Tvalues[i]);
367 ps[j]=((counter[j]+1)/(double)(a+1));
371 void permute_matrix(double *Imatrix,int *nc,int *nr,double *permuted,
372 int *g,double *trial_ts,double *Tinitial,double *counter1){
374 int i=0,j=0,n=0,a=0,b=0,f=0,c=0,k=0;
376 a = *nr; // number of rows
382 for (i=1; i<=*nc; i++){
388 for (i=0; i<*nc; i++){
389 f = y[i]; //column number
395 } // starting value position in the Imatrix
396 for(j=1; j<=*nr; j++){
397 permuted[k] = Imatrix[c];
403 calc_twosample_ts(permuted,g,nc,nr,trial_ts,Tinitial,counter1);
406 void permute_array(int *array, int n) {
407 static int seeded = 0;
415 for (i = 0; i < n; i++) {
416 int selection = rand() % (n - i);
417 int tmp = array[i + selection];
418 array[i + selection] = array[i];
423 void calc_twosample_ts(double *Pmatrix,int *g,int *nc,int *nr,
424 double *Ts,double *Tinitial,double *counter) {
429 double C1[*nr][3], C2[*nr][3], storage[a],tool[a];
430 double nrows,ncols,gvalue, xbardiff=0, denom=0;
432 nrows = (double) *nr;
433 ncols = (double) *nc;
436 meanvar(Pmatrix,g,nr,nc,storage);
440 for (i=0; i<*nr;i++){
442 C1[i][1]=tool[i+*nr+*nr];
443 C1[i][2]=C1[i][1]/(gvalue-1);
445 C2[i][0]=tool[i+*nr];
446 C2[i][1]=tool[i+*nr+*nr+*nr]; // var group 2
447 C2[i][2]=C2[i][1]/(ncols-gvalue+1);
450 for (i=0; i<*nr; i++){
451 xbardiff = C1[i][0]-C2[i][0];
452 denom = sqrt(C1[i][2]+C2[i][2]);
453 Ts[i]=fabs(xbardiff/denom);
454 if (fabs(Ts[i])>(fabs(Tinitial[i])+.0000000000001)){ //13th place
460 void meanvar(double *pmatrix,int *g,int *nr,int *nc,double *store){
461 double temp[*nr], temp2[*nr],var[*nr],var2[*nr],a,b;
466 b = (double) (*nc-a);
468 for (i = 0; i<*nr; i++){
475 k = *nr; // number of rows
482 m*=k; // m = g * nr now
484 temp[i%k]=temp[i%k]+pmatrix[i];
487 temp2[i%k]=temp2[i%k]+pmatrix[i];
490 temp2[i]=temp2[i]-temp[i];
492 for (i=0;i<=*nr-1;i++){
494 store[i+*nr]=temp2[i]/b;
497 // That completes the mean calculations.
500 var[i%k]=var[i%k]+pow((pmatrix[i]-store[i%k]),2);
503 var2[i%k]=var2[i%k]+pow((pmatrix[i]-store[(i%k)+*nr]),2);
506 for (i=0;i<=*nr-1;i++){
507 store[i+2*k]=var[i]/(a-1);
508 store[i+3*k]=var2[i]/(b-1);
510 // That completes var calculations.
513 void start(double *Imatrix,int *g,int *nr,int *nc,double *initial,
514 double storage[][9]){
518 double store[a], tool[a], C1[*nr][3], C2[*nr][3];
519 double nrows,ncols,gvalue, xbardiff=0, denom=0;
521 nrows = (double) *nr;
522 ncols = (double) *nc;
525 meanvar(Imatrix,g,nr,nc,store);
530 for (i=0; i<*nr;i++){
531 C1[i][0]=tool[i]; //mean group 1
532 storage[i][0]=C1[i][0];
533 C1[i][1]=tool[i+*nr+*nr]; // var group 1
534 storage[i][1]=C1[i][1];
535 C1[i][2]=C1[i][1]/(gvalue-1);
536 storage[i][2]=sqrt(C1[i][2]);
538 C2[i][0]=tool[i+*nr]; // mean group 2
539 storage[i][4]=C2[i][0];
540 C2[i][1]=tool[i+*nr+*nr+*nr]; // var group 2
541 storage[i][5]=C2[i][1];
542 C2[i][2]=C2[i][1]/(ncols-gvalue+1);
543 storage[i][6]=sqrt(C2[i][2]);
545 for (i=0; i<*nr; i++){
546 xbardiff = C1[i][0]-C2[i][0];
547 denom = sqrt(C1[i][2]+C2[i][2]);
548 initial[i]=fabs(xbardiff/denom);