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,k,counter=0, bflag=0;
12 int g = secondGroupingStart;
13 double thresh=threshold;
14 double placeholder=0,min=0;
17 strcpy(output, outputFileName);
21 printf("Check your g value\n");
24 // Initialize the matrices
27 double ** matrix, * pmatrix, * permuted, ** storage;
28 matrix = malloc(row*sizeof(double *));
29 storage = malloc(row*sizeof(double *));
30 for (i = 0; i<row; i++){
31 matrix[i] = malloc(col*sizeof(double));
33 for (i = 0; i<row;i++){
34 storage[i] = malloc(9*sizeof(double));
37 pmatrix = (double *) malloc(size*sizeof(double));
38 permuted = (double *) malloc(size*sizeof(double));
42 matrix[i][j]=data[i][j];
43 pmatrix[c]=0; // initializing to zero
49 // Produces the sum of each column
50 double total[col],total1=0,total2=0;
59 total[i]=total[i]+matrix[j][i];
64 total1=total1+total[i];}
67 total2=total2+total[i];}
70 // Creates the ratios by first finding the minimum of totals
73 if (total[0]<total[1]){
83 printf("Error, the sum of one of the columns <= 0.");
90 ratio[i]=total[i]/min;
93 // Change matrix into an array as received by R for compatibility.
98 pmatrix[c]=matrix[j][i];
104 for (i =0; i<col;i++){
105 pmatrix[i]=pmatrix[i]/ratio[i];
111 for (i=0; i<size; i++) {
112 if (counter % row == 0) {
115 pmatrix[i]=pmatrix[i]/ratio[j];
119 // pass everything to the rest of the code using pointers. then
120 // write to output file. below pointers for most of the values are
121 // created to send everything by reference.
123 int ptt_size, *permutes,*nc,*nr,*gvalue;
132 //changing ptt_size to row
133 double * permuted_ttests, * pvalues, * tinitial;
134 permuted_ttests = (double *) malloc(size*sizeof(double));
135 pvalues = (double *) malloc(size*sizeof(double));
136 tinitial = (double *) malloc(size*sizeof(double));
139 permuted_ttests[i]=0;}
145 // Find the initial values for the matrix.
147 start(pmatrix,gvalue,nr,nc,tinitial,storage);
149 // Start the calculations.
151 if ( (col==2) || ((g-1)<8) || ((col-g+1) < 8) ){
153 double * fish, *fish2;
154 fish = (double *) malloc(size*sizeof(double));
155 fish2 = (double *) malloc(size*sizeof(double));
164 fish[i]=fish[i]+matrix[i][j];
167 for(j=g-1;j<col;j++){
168 fish2[i]=fish2[i]+matrix[i][j];
171 double f11,f12,f21,f22;
179 double data[] = {f11, f12, f21, f22};
181 // CONTINGENGCY TABLE:
185 int *nr, *nc, *ldtabl, *work;
186 int nrow=2, ncol=2, ldtable=2;
187 int workspace = 6*(row*col*sizeof(double *));
188 double *expect, *prc, *emin,*prt,*pre;
189 double e=0, prc1=0, emin1=0, prt1=0, pre1=0;
191 prt = (double *) malloc(size*sizeof(double));
192 prc = (double *) malloc(size*sizeof(double));
205 fexact(nr,nc,data, ldtabl,expect,prc,emin,prt,pre,work);
207 if (*pre>.999999999){
210 storage[i][8] = *pre;
216 testp(permuted_ttests, permutes, permuted,pmatrix, nc, nr, gvalue,tinitial,pvalues);
218 // Checks to make sure the matrix isn't sparse.
219 double * sparse, * sparse2;
220 sparse = (double *) malloc(size*sizeof(double));
221 sparse2 = (double *) malloc(size*sizeof(double));
231 sparse[i]=sparse[i]+matrix[i][j];
234 if(sparse[i] < (double)(g-1)){
237 for(j=g-1;j<col;j++){ // ?<= for col
238 sparse2[i]=sparse2[i]+matrix[i][j];
241 if( (sparse2[i] <(double)(col-g+1))) {
248 double f11,f12,f21,f22;
259 double data[] = {f11, f12, f21, f22};
261 int *nr, *nc, *ldtabl, *work;
262 int nrow=2, ncol=2, ldtable=2, workspace=INT_MAX; // I added two zeros for larger data sets
263 double *expect, *prc, *emin,*prt,*pre;
264 double e=0, prc1=0, emin1=0, prt1=0, pre1=0;
277 fexact(nr,nc,data, ldtabl,expect,prc,emin,prt,pre,work);
279 if (*pre>.999999999){
282 storage[i][8] = *pre;
286 // End of else statement
290 // Calculates the mean of counts (not normalized)
292 temp = malloc(row*sizeof(double *));
293 for (i = 0; i<row; i++){
294 temp[i] = malloc(col*sizeof(double));
304 for (i=1; i<=(g-1); i++){
305 temp[j][0]=temp[j][0]+matrix[j][i-1];
307 temp[j][0]= (double) temp[j][0]/(g-1);
309 temp[j][1]=temp[j][1]+matrix[j][i-1];
311 temp[j][1]= (double) temp[j][1]/(col-g+1);
315 storage[i][3]=temp[i][0];
316 storage[i][7]=temp[i][1];
317 storage[i][8]=pvalues[i];
323 if(pvalues[i]<thresh){
324 printf("Feature %d is significant, p = %.10lf \n",i+1,pvalues[i]);
328 // And now we write the files to a text file.
332 local = localtime(&t);
334 out = fopen(output,"w");
336 fprintf(out,"Local time and date of test: %s\n", asctime(local));
337 fprintf(out,"# rows = %d, # col = %d, g = %d\n\n",row,col,g);
339 fprintf(out,"%d permutations\n\n",B);
342 //output column headings - not really sure... documentation labels 9 columns, there are 10 in the output file
343 //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
344 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");
346 for(i=0; i<row; i++){
347 fprintf(out,"%d",(i+1));
350 fprintf(out,"\t%.12lf",storage[i][j]);
355 fprintf(out,"\n \n");
363 void testp(double *permuted_ttests,int *B,double *permuted,
364 double *Imatrix,int *nc,int *nr,int *g,double *Tinitial,double
368 int a, b, n, i, j,k=0;
380 for (j=1; j<=*B; j++){
381 permute_matrix(Imatrix,nc,nr,permuted,g,Tvalues,Tinitial,counter);
382 // for(i=0;i<*nr;i++){
383 // permuted_ttests[k]=fabs(Tvalues[i]);
389 ps[j]=((counter[j]+1)/(double)(a+1));
393 void permute_matrix(double *Imatrix,int *nc,int *nr,double *permuted,
394 int *g,double *trial_ts,double *Tinitial,double *counter1){
396 int i=0,j=0,n=0,a=0,b=0,f=0,c=0,k=0;
398 a = *nr; // number of rows
404 for (i=1; i<=*nc; i++){
410 for (i=0; i<*nc; i++){
411 f = y[i]; //column number
417 } // starting value position in the Imatrix
418 for(j=1; j<=*nr; j++){
419 permuted[k] = Imatrix[c];
425 calc_twosample_ts(permuted,g,nc,nr,trial_ts,Tinitial,counter1);
428 void permute_array(int *array, int n) {
429 static int seeded = 0;
437 for (i = 0; i < n; i++) {
438 int selection = rand() % (n - i);
439 int tmp = array[i + selection];
440 array[i + selection] = array[i];
445 void calc_twosample_ts(double *Pmatrix,int *g,int *nc,int *nr,
446 double *Ts,double *Tinitial,double *counter) {
451 double C1[*nr][3], C2[*nr][3], storage[a],tool[a];
452 double nrows,ncols,gvalue, xbardiff=0, denom=0;
454 nrows = (double) *nr;
455 ncols = (double) *nc;
458 meanvar(Pmatrix,g,nr,nc,storage);
462 for (i=0; i<*nr;i++){
464 C1[i][1]=tool[i+*nr+*nr];
465 C1[i][2]=C1[i][1]/(gvalue-1);
467 C2[i][0]=tool[i+*nr];
468 C2[i][1]=tool[i+*nr+*nr+*nr]; // var group 2
469 C2[i][2]=C2[i][1]/(ncols-gvalue+1);
472 for (i=0; i<*nr; i++){
473 xbardiff = C1[i][0]-C2[i][0];
474 denom = sqrt(C1[i][2]+C2[i][2]);
475 Ts[i]=fabs(xbardiff/denom);
476 if (fabs(Ts[i])>(fabs(Tinitial[i])+.0000000000001)){ //13th place
482 void meanvar(double *pmatrix,int *g,int *nr,int *nc,double *store){
483 double temp[*nr], temp2[*nr],var[*nr],var2[*nr],a,b;
488 b = (double) (*nc-a);
490 for (i = 0; i<*nr; i++){
497 k = *nr; // number of rows
504 m*=k; // m = g * nr now
506 temp[i%k]=temp[i%k]+pmatrix[i];
509 temp2[i%k]=temp2[i%k]+pmatrix[i];
512 temp2[i]=temp2[i]-temp[i];
514 for (i=0;i<=*nr-1;i++){
516 store[i+*nr]=temp2[i]/b;
519 // That completes the mean calculations.
522 var[i%k]=var[i%k]+pow((pmatrix[i]-store[i%k]),2);
525 var2[i%k]=var2[i%k]+pow((pmatrix[i]-store[(i%k)+*nr]),2);
528 for (i=0;i<=*nr-1;i++){
529 store[i+2*k]=var[i]/(a-1);
530 store[i+3*k]=var2[i]/(b-1);
532 // That completes var calculations.
535 void start(double *Imatrix,int *g,int *nr,int *nc,double *initial,
540 double store[a], tool[a], C1[*nr][3], C2[*nr][3];
541 double nrows,ncols,gvalue, xbardiff=0, denom=0;
543 nrows = (double) *nr;
544 ncols = (double) *nc;
547 meanvar(Imatrix,g,nr,nc,store);
553 for (i=0; i<*nr;i++){
554 C1[i][0]=tool[i]; //mean group 1
555 storage[i][0]=C1[i][0];
556 C1[i][1]=tool[i+*nr+*nr]; // var group 1
557 storage[i][1]=C1[i][1];
558 C1[i][2]=C1[i][1]/(gvalue-1);
559 storage[i][2]=sqrt(C1[i][2]);
560 C2[i][0]=tool[i+*nr]; // mean group 2
561 storage[i][4]=C2[i][0];
562 C2[i][1]=tool[i+*nr+*nr+*nr]; // var group 2
563 storage[i][5]=C2[i][1];
564 C2[i][2]=C2[i][1]/(ncols-gvalue+1);
565 storage[i][6]=sqrt(C2[i][2]);
569 for (i=0; i<*nr; i++){
570 xbardiff = C1[i][0]-C2[i][0];
571 denom = sqrt(C1[i][2]+C2[i][2]);
572 initial[i]=fabs(xbardiff/denom);