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
26 //printf("size = %d\n.", size);
27 double matrix[row][col];
28 double pmatrix[size],pmatrix2[size],permuted[size];
29 double storage[row][9];
30 //printf("here\n.", size);
39 matrix[i][j]=data[i][j];
40 pmatrix[c]=0; // initializing to zero
46 // Produces the sum of each column
47 double total[col],total1=0,total2=0;
56 total[i]=total[i]+matrix[j][i];
61 total1=total1+total[i];}
64 total2=total2+total[i];}
67 // Creates the ratios by first finding the minimum of totals
70 if (total[0]<total[1]){
80 printf("Error, the sum of one of the columns <= 0.");
87 ratio[i]=total[i]/min;
90 // Change matrix into an array as received by R for compatibility.
95 pmatrix[c]=matrix[j][i];
101 for (i =0; i<col;i++){
102 pmatrix[i]=pmatrix[i]/ratio[i];
108 for (i=0; i<size; i++) {
109 if (counter % row == 0) {
112 pmatrix[i]=pmatrix[i]/ratio[j];
116 // pass everything to the rest of the code using pointers. then
117 // write to output file. below pointers for most of the values are
118 // created to send everything by reference.
120 int ptt_size, *permutes,*nc,*nr,*gvalue;
129 //changing ptt_size to row
130 double permuted_ttests[row], pvalues[row], tinitial[row];
133 permuted_ttests[i]=0;}
139 // Find the initial values for the matrix.
140 start(pmatrix,gvalue,nr,nc,tinitial,storage);
142 // Start the calculations.
144 if ( (col==2) || ((g-1)<8) || ((col-g+1) < 8) ){
146 double fish[row], fish2[row];
154 fish[i]=fish[i]+matrix[i][j];
157 for(j=g-1;j<col;j++){
158 fish2[i]=fish2[i]+matrix[i][j];
161 double f11,f12,f21,f22;
169 double data[] = {f11, f12, f21, f22};
171 // CONTINGENGCY TABLE:
175 int *nr, *nc, *ldtabl, *work;
176 int nrow=2, ncol=2, ldtable=2, workspace=100000;
177 double *expect, *prc, *emin,*prt,*pre;
178 double e=0, prc1=0, emin1=0, prt1=0, pre1=0;
191 fexact(nr,nc,data, ldtabl,expect,prc,emin,prt,pre,work);
193 if (*pre>.999999999){
196 storage[i][8] = *pre;
202 testp(permuted_ttests, permutes, permuted,pmatrix, nc, nr, gvalue,tinitial,pvalues);
204 // Checks to make sure the matrix isn't sparse.
205 double sparse[row], sparse2[row];
214 sparse[i]=sparse[i]+matrix[i][j];
217 if(sparse[i] < (double)(g-1)){
220 for(j=g-1;j<col;j++){ // ?<= for col
221 sparse2[i]=sparse2[i]+matrix[i][j];
224 if( (sparse2[i] <(double)(col-g+1))) {
231 double f11,f12,f21,f22;
242 double data[] = {f11, f12, f21, f22};
244 int *nr, *nc, *ldtabl, *work;
245 int nrow=2, ncol=2, ldtable=2, workspace=10000000; // I added two zeros for larger data sets
246 double *expect, *prc, *emin,*prt,*pre;
247 double e=0, prc1=0, emin1=0, prt1=0, pre1=0;
260 fexact(nr,nc,data, ldtabl,expect,prc,emin,prt,pre,work);
262 if (*pre>.999999999){
265 storage[i][8] = *pre;
269 // End of else statement
273 // Calculates the mean of counts (not normalized)
283 for (i=1; i<=(g-1); i++){
284 temp[j][0]=temp[j][0]+matrix[j][i-1];
286 temp[j][0]= (double) temp[j][0]/(g-1);
288 temp[j][1]=temp[j][1]+matrix[j][i-1];
290 temp[j][1]= (double) temp[j][1]/(col-g+1);
294 storage[i][3]=temp[i][0];
295 storage[i][7]=temp[i][1];
296 storage[i][8]=pvalues[i];
302 if(pvalues[i]<thresh){
303 printf("Feature %d is significant, p = %.10lf \n",i+1,pvalues[i]);
307 // And now we write the files to a text file.
311 local = localtime(&t);
313 out = fopen(output,"w");
315 fprintf(out,"Local time and date of test: %s\n", asctime(local));
316 fprintf(out,"# rows = %d, # col = %d, g = %d\n\n",row,col,g);
318 fprintf(out,"%d permutations\n\n",B);
321 //output column headings - not really sure... documentation labels 9 columns, there are 10 in the output file
322 //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
323 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");
325 for(i=0; i<row; i++){
326 fprintf(out,"%d",(i+1));
329 fprintf(out,"\t%.12lf",storage[i][j]);
334 fprintf(out,"\n \n");
342 void testp(double *permuted_ttests,int *B,double *permuted,
343 double *Imatrix,int *nc,int *nr,int *g,double *Tinitial,double
347 int a, b, n, i, j,k=0;
359 for (j=1; j<=*B; j++){
360 permute_matrix(Imatrix,nc,nr,permuted,g,Tvalues,Tinitial,counter);
361 // for(i=0;i<*nr;i++){
362 // permuted_ttests[k]=fabs(Tvalues[i]);
368 ps[j]=((counter[j]+1)/(double)(a+1));
372 void permute_matrix(double *Imatrix,int *nc,int *nr,double *permuted,
373 int *g,double *trial_ts,double *Tinitial,double *counter1){
375 int i=0,j=0,n=0,a=0,b=0,f=0,c=0,k=0;
377 a = *nr; // number of rows
383 for (i=1; i<=*nc; i++){
389 for (i=0; i<*nc; i++){
390 f = y[i]; //column number
396 } // starting value position in the Imatrix
397 for(j=1; j<=*nr; j++){
398 permuted[k] = Imatrix[c];
404 calc_twosample_ts(permuted,g,nc,nr,trial_ts,Tinitial,counter1);
407 void permute_array(int *array, int n) {
408 static int seeded = 0;
416 for (i = 0; i < n; i++) {
417 int selection = rand() % (n - i);
418 int tmp = array[i + selection];
419 array[i + selection] = array[i];
424 void calc_twosample_ts(double *Pmatrix,int *g,int *nc,int *nr,
425 double *Ts,double *Tinitial,double *counter) {
430 double C1[*nr][3], C2[*nr][3], storage[a],tool[a];
431 double nrows,ncols,gvalue, xbardiff=0, denom=0;
433 nrows = (double) *nr;
434 ncols = (double) *nc;
437 meanvar(Pmatrix,g,nr,nc,storage);
441 for (i=0; i<*nr;i++){
443 C1[i][1]=tool[i+*nr+*nr];
444 C1[i][2]=C1[i][1]/(gvalue-1);
446 C2[i][0]=tool[i+*nr];
447 C2[i][1]=tool[i+*nr+*nr+*nr]; // var group 2
448 C2[i][2]=C2[i][1]/(ncols-gvalue+1);
451 for (i=0; i<*nr; i++){
452 xbardiff = C1[i][0]-C2[i][0];
453 denom = sqrt(C1[i][2]+C2[i][2]);
454 Ts[i]=fabs(xbardiff/denom);
455 if (fabs(Ts[i])>(fabs(Tinitial[i])+.0000000000001)){ //13th place
461 void meanvar(double *pmatrix,int *g,int *nr,int *nc,double *store){
462 double temp[*nr], temp2[*nr],var[*nr],var2[*nr],a,b;
467 b = (double) (*nc-a);
469 for (i = 0; i<*nr; i++){
476 k = *nr; // number of rows
483 m*=k; // m = g * nr now
485 temp[i%k]=temp[i%k]+pmatrix[i];
488 temp2[i%k]=temp2[i%k]+pmatrix[i];
491 temp2[i]=temp2[i]-temp[i];
493 for (i=0;i<=*nr-1;i++){
495 store[i+*nr]=temp2[i]/b;
498 // That completes the mean calculations.
501 var[i%k]=var[i%k]+pow((pmatrix[i]-store[i%k]),2);
504 var2[i%k]=var2[i%k]+pow((pmatrix[i]-store[(i%k)+*nr]),2);
507 for (i=0;i<=*nr-1;i++){
508 store[i+2*k]=var[i]/(a-1);
509 store[i+3*k]=var2[i]/(b-1);
511 // That completes var calculations.
514 void start(double *Imatrix,int *g,int *nr,int *nc,double *initial,
515 double storage[][9]){
519 double store[a], tool[a], C1[*nr][3], C2[*nr][3];
520 double nrows,ncols,gvalue, xbardiff=0, denom=0;
522 nrows = (double) *nr;
523 ncols = (double) *nc;
526 meanvar(Imatrix,g,nr,nc,store);
531 for (i=0; i<*nr;i++){
532 C1[i][0]=tool[i]; //mean group 1
533 storage[i][0]=C1[i][0];
534 C1[i][1]=tool[i+*nr+*nr]; // var group 1
535 storage[i][1]=C1[i][1];
536 C1[i][2]=C1[i][1]/(gvalue-1);
537 storage[i][2]=sqrt(C1[i][2]);
539 C2[i][0]=tool[i+*nr]; // mean group 2
540 storage[i][4]=C2[i][0];
541 C2[i][1]=tool[i+*nr+*nr+*nr]; // var group 2
542 storage[i][5]=C2[i][1];
543 C2[i][2]=C2[i][1]/(ncols-gvalue+1);
544 storage[i][6]=sqrt(C2[i][2]);
546 for (i=0; i<*nr; i++){
547 xbardiff = C1[i][0]-C2[i][0];
548 denom = sqrt(C1[i][2]+C2[i][2]);
549 initial[i]=fabs(xbardiff/denom);