4 int metastat_main (char* outputFileName, int numRows, int numCols, double threshold, int numPermutations, double** data, int secondGroupingStart){
6 int size,c=0,i=0,j=0,k,counter=0, bflag=0;
10 int g = secondGroupingStart;
11 double thresh=threshold;
12 double placeholder=0,min=0;
15 strcpy(output, outputFileName);
19 printf("Check your g value\n");
22 // Initialize the matrices
24 double matrix[row][col];
25 double pmatrix[size],pmatrix2[size],permuted[size];
26 double storage[row][9];
36 matrix[i][j]=data[i][j];
37 pmatrix[c]=0; // initializing to zero
43 // Produces the sum of each column
44 double total[col],total1=0,total2=0;
53 total[i]=total[i]+matrix[j][i];
58 total1=total1+total[i];}
61 total2=total2+total[i];}
64 // Creates the ratios by first finding the minimum of totals
67 if (total[0]<total[1]){
77 printf("Error, the sum of one of the columns <= 0.");
84 ratio[i]=total[i]/min;
87 // Change matrix into an array as received by R for compatibility.
92 pmatrix[c]=matrix[j][i];
98 for (i =0; i<col;i++){
99 pmatrix[i]=pmatrix[i]/ratio[i];
105 for (i=0; i<size; i++) {
106 if (counter % row == 0) {
109 pmatrix[i]=pmatrix[i]/ratio[j];
113 // pass everything to the rest of the code using pointers. then
114 // write to output file. below pointers for most of the values are
115 // created to send everything by reference.
117 int ptt_size, *permutes,*nc,*nr,*gvalue;
126 //changing ptt_size to row
127 double permuted_ttests[row], pvalues[row], tinitial[row];
130 permuted_ttests[i]=0;}
136 // Find the initial values for the matrix.
137 start(pmatrix,gvalue,nr,nc,tinitial,storage);
139 // Start the calculations.
141 if ( (col==2) || ((g-1)<8) || ((col-g+1) < 8) ){
143 double fish[row], fish2[row];
151 fish[i]=fish[i]+matrix[i][j];
154 for(j=g-1;j<col;j++){
155 fish2[i]=fish2[i]+matrix[i][j];
158 double f11,f12,f21,f22;
166 double data[] = {f11, f12, f21, f22};
168 // CONTINGENGCY TABLE:
172 int *nr, *nc, *ldtabl, *work;
173 int nrow=2, ncol=2, ldtable=2, workspace=100000;
174 double *expect, *prc, *emin,*prt,*pre;
175 double e=0, prc1=0, emin1=0, prt1=0, pre1=0;
188 fexact(nr,nc,data, ldtabl,expect,prc,emin,prt,pre,work);
190 if (*pre>.999999999){
193 storage[i][8] = *pre;
199 testp(permuted_ttests, permutes, permuted,pmatrix, nc, nr, gvalue,tinitial,pvalues);
201 // Checks to make sure the matrix isn't sparse.
202 double sparse[row], sparse2[row];
211 sparse[i]=sparse[i]+matrix[i][j];
214 if(sparse[i] < (double)(g-1)){
217 for(j=g-1;j<col;j++){ // ?<= for col
218 sparse2[i]=sparse2[i]+matrix[i][j];
221 if( (sparse2[i] <(double)(col-g+1))) {
228 double f11,f12,f21,f22;
239 double data[] = {f11, f12, f21, f22};
241 int *nr, *nc, *ldtabl, *work;
242 int nrow=2, ncol=2, ldtable=2, workspace=10000000; // I added two zeros for larger data sets
243 double *expect, *prc, *emin,*prt,*pre;
244 double e=0, prc1=0, emin1=0, prt1=0, pre1=0;
257 fexact(nr,nc,data, ldtabl,expect,prc,emin,prt,pre,work);
259 if (*pre>.999999999){
262 storage[i][8] = *pre;
266 // End of else statement
270 // Calculates the mean of counts (not normalized)
280 for (i=1; i<=(g-1); i++){
281 temp[j][0]=temp[j][0]+matrix[j][i-1];
283 temp[j][0]= (double) temp[j][0]/(g-1);
285 temp[j][1]=temp[j][1]+matrix[j][i-1];
287 temp[j][1]= (double) temp[j][1]/(col-g+1);
291 storage[i][3]=temp[i][0];
292 storage[i][7]=temp[i][1];
293 storage[i][8]=pvalues[i];
299 if(pvalues[i]<thresh){
300 printf("Feature %d is significant, p = %.10lf \n",i+1,pvalues[i]);
304 // And now we write the files to a text file.
308 local = localtime(&t);
310 out = fopen(output,"w");
312 fprintf(out,"Local time and date of test: %s\n", asctime(local));
313 fprintf(out,"# rows = %d, # col = %d, g = %d\n\n",row,col,g);
315 fprintf(out,"%d permutations\n\n",B);
318 //output column headings - not really sure... documentation labels 9 columns, there are 10 in the output file
319 //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
320 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");
322 for(i=0; i<row; i++){
323 fprintf(out,"%d",(i+1));
326 fprintf(out,"\t%.12lf",storage[i][j]);
331 fprintf(out,"\n \n");
339 void testp(double *permuted_ttests,int *B,double *permuted,
340 double *Imatrix,int *nc,int *nr,int *g,double *Tinitial,double
344 int a, b, n, i, j,k=0;
356 for (j=1; j<=*B; j++){
357 permute_matrix(Imatrix,nc,nr,permuted,g,Tvalues,Tinitial,counter);
358 // for(i=0;i<*nr;i++){
359 // permuted_ttests[k]=fabs(Tvalues[i]);
365 ps[j]=((counter[j]+1)/(double)(a+1));
369 void permute_matrix(double *Imatrix,int *nc,int *nr,double *permuted,
370 int *g,double *trial_ts,double *Tinitial,double *counter1){
372 int i=0,j=0,n=0,a=0,b=0,f=0,c=0,k=0;
374 a = *nr; // number of rows
380 for (i=1; i<=*nc; i++){
386 for (i=0; i<*nc; i++){
387 f = y[i]; //column number
393 } // starting value position in the Imatrix
394 for(j=1; j<=*nr; j++){
395 permuted[k] = Imatrix[c];
401 calc_twosample_ts(permuted,g,nc,nr,trial_ts,Tinitial,counter1);
404 void permute_array(int *array, int n) {
405 static int seeded = 0;
413 for (i = 0; i < n; i++) {
414 int selection = random() % (n - i);
415 int tmp = array[i + selection];
416 array[i + selection] = array[i];
421 void calc_twosample_ts(double *Pmatrix,int *g,int *nc,int *nr,
422 double *Ts,double *Tinitial,double *counter) {
427 double C1[*nr][3], C2[*nr][3], storage[a],tool[a];
428 double nrows,ncols,gvalue, xbardiff=0, denom=0;
430 nrows = (double) *nr;
431 ncols = (double) *nc;
434 meanvar(Pmatrix,g,nr,nc,storage);
438 for (i=0; i<*nr;i++){
440 C1[i][1]=tool[i+*nr+*nr];
441 C1[i][2]=C1[i][1]/(gvalue-1);
443 C2[i][0]=tool[i+*nr];
444 C2[i][1]=tool[i+*nr+*nr+*nr]; // var group 2
445 C2[i][2]=C2[i][1]/(ncols-gvalue+1);
448 for (i=0; i<*nr; i++){
449 xbardiff = C1[i][0]-C2[i][0];
450 denom = sqrt(C1[i][2]+C2[i][2]);
451 Ts[i]=fabs(xbardiff/denom);
452 if (fabs(Ts[i])>(fabs(Tinitial[i])+.0000000000001)){ //13th place
458 void meanvar(double *pmatrix,int *g,int *nr,int *nc,double *store){
459 double temp[*nr], temp2[*nr],var[*nr],var2[*nr],a,b;
464 b = (double) (*nc-a);
466 for (i = 0; i<*nr; i++){
473 k = *nr; // number of rows
480 m*=k; // m = g * nr now
482 temp[i%k]=temp[i%k]+pmatrix[i];
485 temp2[i%k]=temp2[i%k]+pmatrix[i];
488 temp2[i]=temp2[i]-temp[i];
490 for (i=0;i<=*nr-1;i++){
492 store[i+*nr]=temp2[i]/b;
495 // That completes the mean calculations.
498 var[i%k]=var[i%k]+pow((pmatrix[i]-store[i%k]),2);
501 var2[i%k]=var2[i%k]+pow((pmatrix[i]-store[(i%k)+*nr]),2);
504 for (i=0;i<=*nr-1;i++){
505 store[i+2*k]=var[i]/(a-1);
506 store[i+3*k]=var2[i]/(b-1);
508 // That completes var calculations.
511 void start(double *Imatrix,int *g,int *nr,int *nc,double *initial,
512 double storage[][9]){
516 double store[a], tool[a], C1[*nr][3], C2[*nr][3];
517 double nrows,ncols,gvalue, xbardiff=0, denom=0;
519 nrows = (double) *nr;
520 ncols = (double) *nc;
523 meanvar(Imatrix,g,nr,nc,store);
528 for (i=0; i<*nr;i++){
529 C1[i][0]=tool[i]; //mean group 1
530 storage[i][0]=C1[i][0];
531 C1[i][1]=tool[i+*nr+*nr]; // var group 1
532 storage[i][1]=C1[i][1];
533 C1[i][2]=C1[i][1]/(gvalue-1);
534 storage[i][2]=sqrt(C1[i][2]);
536 C2[i][0]=tool[i+*nr]; // mean group 2
537 storage[i][4]=C2[i][0];
538 C2[i][1]=tool[i+*nr+*nr+*nr]; // var group 2
539 storage[i][5]=C2[i][1];
540 C2[i][2]=C2[i][1]/(ncols-gvalue+1);
541 storage[i][6]=sqrt(C2[i][2]);
543 for (i=0; i<*nr; i++){
544 xbardiff = C1[i][0]-C2[i][0];
545 denom = sqrt(C1[i][2]+C2[i][2]);
546 initial[i]=fabs(xbardiff/denom);