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 *));
44 matrix[i][j]=data[i][j];
45 pmatrix[c]=0; // initializing to zero
51 // Produces the sum of each column
52 double total[col],total1=0,total2=0;
61 total[i]=total[i]+matrix[j][i];
66 total1=total1+total[i];}
69 total2=total2+total[i];}
72 // Creates the ratios by first finding the minimum of totals
75 if (total[0]<total[1]){
85 printf("Error, the sum of one of the columns <= 0.");
92 ratio[i]=total[i]/min;
95 // Change matrix into an array as received by R for compatibility.
100 pmatrix[c]=matrix[j][i];
106 for (i =0; i<col;i++){
107 pmatrix[i]=pmatrix[i]/ratio[i];
113 for (i=0; i<size; i++) {
114 if (counter % row == 0) {
117 pmatrix[i]=pmatrix[i]/ratio[j];
121 // pass everything to the rest of the code using pointers. then
122 // write to output file. below pointers for most of the values are
123 // created to send everything by reference.
125 int ptt_size, *permutes,*nc,*nr,*gvalue;
134 //changing ptt_size to row
135 double * permuted_ttests, * pvalues, * tinitial;
136 permuted_ttests = (double *) malloc(size*sizeof(double *));
137 pvalues = (double *) malloc(size*sizeof(double *));
138 tinitial = (double *) malloc(size*sizeof(double *));
141 permuted_ttests[i]=0;}
147 // Find the initial values for the matrix.
149 start(pmatrix,gvalue,nr,nc,tinitial,storage);
151 // Start the calculations.
153 if ( (col==2) || ((g-1)<8) || ((col-g+1) < 8) ){
155 double * fish, *fish2;
156 fish = (double *) malloc(size*sizeof(double *));
157 fish2 = (double *) malloc(size*sizeof(double *));
166 fish[i]=fish[i]+matrix[i][j];
169 for(j=g-1;j<col;j++){
170 fish2[i]=fish2[i]+matrix[i][j];
173 double f11,f12,f21,f22;
181 double data[] = {f11, f12, f21, f22};
183 // CONTINGENGCY TABLE:
187 int *nr, *nc, *ldtabl, *work;
188 int nrow=2, ncol=2, ldtable=2;
189 int workspace=(row*sizeof(double *)+size*sizeof(double *));
190 double *expect, *prc, *emin,*prt,*pre;
191 double e=0, prc1=0, emin1=0, prt1=0, pre1=0;
204 fexact(nr,nc,data, ldtabl,expect,prc,emin,prt,pre,work);
206 if (*pre>.999999999){
209 storage[i][8] = *pre;
214 printf("here before testp\n");
215 testp(permuted_ttests, permutes, permuted,pmatrix, nc, nr, gvalue,tinitial,pvalues);
217 // Checks to make sure the matrix isn't sparse.
218 double * sparse, * sparse2;
219 sparse = (double *) malloc(size*sizeof(double *));
220 sparse2 = (double *) malloc(size*sizeof(double *));
230 sparse[i]=sparse[i]+matrix[i][j];
233 if(sparse[i] < (double)(g-1)){
236 for(j=g-1;j<col;j++){ // ?<= for col
237 sparse2[i]=sparse2[i]+matrix[i][j];
240 if( (sparse2[i] <(double)(col-g+1))) {
247 double f11,f12,f21,f22;
258 double data[] = {f11, f12, f21, f22};
260 int *nr, *nc, *ldtabl, *work;
261 int nrow=2, ncol=2, ldtable=2, workspace=10000000; // I added two zeros for larger data sets
262 double *expect, *prc, *emin,*prt,*pre;
263 double e=0, prc1=0, emin1=0, prt1=0, pre1=0;
275 printf("here before fexact2\n");
276 fexact(nr,nc,data, ldtabl,expect,prc,emin,prt,pre,work);
278 if (*pre>.999999999){
281 storage[i][8] = *pre;
285 // End of else statement
289 // Calculates the mean of counts (not normalized)
291 temp = malloc(row*sizeof(double *));
292 for (i = 0; i<row; i++){
293 temp[i] = malloc(col*sizeof(double));
303 for (i=1; i<=(g-1); i++){
304 temp[j][0]=temp[j][0]+matrix[j][i-1];
306 temp[j][0]= (double) temp[j][0]/(g-1);
308 temp[j][1]=temp[j][1]+matrix[j][i-1];
310 temp[j][1]= (double) temp[j][1]/(col-g+1);
314 storage[i][3]=temp[i][0];
315 storage[i][7]=temp[i][1];
316 storage[i][8]=pvalues[i];
322 if(pvalues[i]<thresh){
323 printf("Feature %d is significant, p = %.10lf \n",i+1,pvalues[i]);
327 // And now we write the files to a text file.
331 local = localtime(&t);
333 out = fopen(output,"w");
335 fprintf(out,"Local time and date of test: %s\n", asctime(local));
336 fprintf(out,"# rows = %d, # col = %d, g = %d\n\n",row,col,g);
338 fprintf(out,"%d permutations\n\n",B);
341 //output column headings - not really sure... documentation labels 9 columns, there are 10 in the output file
342 //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
343 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");
345 for(i=0; i<row; i++){
346 fprintf(out,"%d",(i+1));
349 fprintf(out,"\t%.12lf",storage[i][j]);
354 fprintf(out,"\n \n");
362 void testp(double *permuted_ttests,int *B,double *permuted,
363 double *Imatrix,int *nc,int *nr,int *g,double *Tinitial,double
367 int a, b, n, i, j,k=0;
379 for (j=1; j<=*B; j++){
380 permute_matrix(Imatrix,nc,nr,permuted,g,Tvalues,Tinitial,counter);
381 // for(i=0;i<*nr;i++){
382 // permuted_ttests[k]=fabs(Tvalues[i]);
388 ps[j]=((counter[j]+1)/(double)(a+1));
392 void permute_matrix(double *Imatrix,int *nc,int *nr,double *permuted,
393 int *g,double *trial_ts,double *Tinitial,double *counter1){
395 int i=0,j=0,n=0,a=0,b=0,f=0,c=0,k=0;
397 a = *nr; // number of rows
403 for (i=1; i<=*nc; i++){
409 for (i=0; i<*nc; i++){
410 f = y[i]; //column number
416 } // starting value position in the Imatrix
417 for(j=1; j<=*nr; j++){
418 permuted[k] = Imatrix[c];
424 calc_twosample_ts(permuted,g,nc,nr,trial_ts,Tinitial,counter1);
427 void permute_array(int *array, int n) {
428 static int seeded = 0;
436 for (i = 0; i < n; i++) {
437 int selection = rand() % (n - i);
438 int tmp = array[i + selection];
439 array[i + selection] = array[i];
444 void calc_twosample_ts(double *Pmatrix,int *g,int *nc,int *nr,
445 double *Ts,double *Tinitial,double *counter) {
450 double C1[*nr][3], C2[*nr][3], storage[a],tool[a];
451 double nrows,ncols,gvalue, xbardiff=0, denom=0;
453 nrows = (double) *nr;
454 ncols = (double) *nc;
457 meanvar(Pmatrix,g,nr,nc,storage);
461 for (i=0; i<*nr;i++){
463 C1[i][1]=tool[i+*nr+*nr];
464 C1[i][2]=C1[i][1]/(gvalue-1);
466 C2[i][0]=tool[i+*nr];
467 C2[i][1]=tool[i+*nr+*nr+*nr]; // var group 2
468 C2[i][2]=C2[i][1]/(ncols-gvalue+1);
471 for (i=0; i<*nr; i++){
472 xbardiff = C1[i][0]-C2[i][0];
473 denom = sqrt(C1[i][2]+C2[i][2]);
474 Ts[i]=fabs(xbardiff/denom);
475 if (fabs(Ts[i])>(fabs(Tinitial[i])+.0000000000001)){ //13th place
481 void meanvar(double *pmatrix,int *g,int *nr,int *nc,double *store){
482 double temp[*nr], temp2[*nr],var[*nr],var2[*nr],a,b;
487 b = (double) (*nc-a);
489 for (i = 0; i<*nr; i++){
496 k = *nr; // number of rows
503 m*=k; // m = g * nr now
505 temp[i%k]=temp[i%k]+pmatrix[i];
508 temp2[i%k]=temp2[i%k]+pmatrix[i];
511 temp2[i]=temp2[i]-temp[i];
513 for (i=0;i<=*nr-1;i++){
515 store[i+*nr]=temp2[i]/b;
518 // That completes the mean calculations.
521 var[i%k]=var[i%k]+pow((pmatrix[i]-store[i%k]),2);
524 var2[i%k]=var2[i%k]+pow((pmatrix[i]-store[(i%k)+*nr]),2);
527 for (i=0;i<=*nr-1;i++){
528 store[i+2*k]=var[i]/(a-1);
529 store[i+3*k]=var2[i]/(b-1);
531 // That completes var calculations.
534 void start(double *Imatrix,int *g,int *nr,int *nc,double *initial,
539 double store[a], tool[a], C1[*nr][3], C2[*nr][3];
540 double nrows,ncols,gvalue, xbardiff=0, denom=0;
542 nrows = (double) *nr;
543 ncols = (double) *nc;
546 meanvar(Imatrix,g,nr,nc,store);
552 for (i=0; i<*nr;i++){
553 C1[i][0]=tool[i]; //mean group 1
554 storage[i][0]=C1[i][0];
555 C1[i][1]=tool[i+*nr+*nr]; // var group 1
556 storage[i][1]=C1[i][1];
557 C1[i][2]=C1[i][1]/(gvalue-1);
558 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);