]> git.donarmstrong.com Git - mothur.git/blobdiff - metastats2.c
added get.otus and remove.otus commands
[mothur.git] / metastats2.c
index 3b801a0b110c5685fcc3a62f66ab3ad516a862d2..81ffb00347de5f51795cf74e6c605e664cc80f99 100644 (file)
@@ -1,6 +1,8 @@
 
 #include "metastats.h"
 
+//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).
+
 int metastat_main (char* outputFileName, int numRows, int numCols, double threshold, int numPermutations, double** data, int secondGroupingStart){
   
   int size,c=0,i=0,j=0,k,counter=0, bflag=0; 
@@ -20,17 +22,23 @@ int metastat_main (char* outputFileName, int numRows, int numCols, double thresh
   }
  
   // Initialize the matrices
-  size = row*col;
-  double matrix[row][col];
-  double pmatrix[size],pmatrix2[size],permuted[size];  
-  double storage[row][9];
-  
-  for (i=0;i<row;i++){
-       for (j =0;j<9;j++){
-      storage[i][j]=0;                 
-       }       
-  }
-   
+       size = row*col;
+       
+       double ** matrix, * pmatrix, * permuted, ** storage;
+       matrix = malloc(row*sizeof(double *));
+       storage =  malloc(row*sizeof(double *));
+       for (i = 0; i<row; i++){
+               matrix[i] = malloc(col*sizeof(double));
+       }
+       for (i = 0; i<row;i++){
+               storage[i] = malloc(9*sizeof(double));
+       }
+       
+       pmatrix = (double *) malloc(size*sizeof(double *));
+       permuted = (double *) malloc(size*sizeof(double *));
+       
+               
+       
   for(i=0; i<row; i++){
     for(j=0; j<col;j++){
       matrix[i][j]=data[i][j];
@@ -122,9 +130,12 @@ int metastat_main (char* outputFileName, int numRows, int numCols, double thresh
   
   permutes = &B;
   ptt_size = B*row;
-  
+
   //changing ptt_size to row
-  double permuted_ttests[row], pvalues[row], tinitial[row];
+  double * permuted_ttests, * pvalues, * tinitial;
+  permuted_ttests = (double *) malloc(size*sizeof(double *));
+  pvalues = (double *) malloc(size*sizeof(double *));
+  tinitial = (double *) malloc(size*sizeof(double *));
   
   for(i=0;i<row;i++){
     permuted_ttests[i]=0;}
@@ -134,13 +145,17 @@ int metastat_main (char* outputFileName, int numRows, int numCols, double thresh
     tinitial[i]=0; }
   
   // Find the initial values for the matrix.
   start(pmatrix,gvalue,nr,nc,tinitial,storage);
   
   // Start the calculations.
  
   if ( (col==2) || ((g-1)<8) || ((col-g+1) < 8) ){  
 
-  double fish[row], fish2[row];
+  double * fish, *fish2;
+  fish = (double *) malloc(size*sizeof(double *));
+  fish2 = (double *) malloc(size*sizeof(double *));
+         
   for(i=0;i<row;i++){
        fish[i]=0;
        fish2[i]=0;}
@@ -170,9 +185,13 @@ int metastat_main (char* outputFileName, int numRows, int numCols, double thresh
        //   f21   f22
        
        int *nr, *nc, *ldtabl, *work;
-       int nrow=2, ncol=2, ldtable=2, workspace=100000;
+       int nrow=2, ncol=2, ldtable=2;
+       int workspace = 2*(row*col*sizeof(double *)); 
        double *expect, *prc, *emin,*prt,*pre;
        double e=0, prc1=0, emin1=0, prt1=0, pre1=0;
+         
+       prt = (double *) malloc(size*sizeof(double *));
+       prc = (double *) malloc(size*sizeof(double *));
 
        nr = &nrow;
        nc = &ncol;
@@ -184,7 +203,7 @@ int metastat_main (char* outputFileName, int numRows, int numCols, double thresh
        emin=&emin1;
        prt=&prt1;
        pre=&pre1;
-       
+
        fexact(nr,nc,data, ldtabl,expect,prc,emin,prt,pre,work);
        
        if (*pre>.999999999){
@@ -195,11 +214,14 @@ int metastat_main (char* outputFileName, int numRows, int numCols, double thresh
     }
   }
   else{
-        
+printf("here before testp\n");          
   testp(permuted_ttests, permutes, permuted,pmatrix, nc, nr, gvalue,tinitial,pvalues);
         
               // Checks to make sure the matrix isn't sparse.
-  double sparse[row], sparse2[row];
+  double * sparse, * sparse2;
+  sparse = (double *) malloc(size*sizeof(double *));
+  sparse2 = (double *) malloc(size*sizeof(double *));
+         
   for(i=0;i<row;i++){
        sparse[i]=0;
        sparse2[i]=0;}
@@ -239,7 +261,7 @@ int metastat_main (char* outputFileName, int numRows, int numCols, double thresh
        double data[] = {f11, f12, f21, f22};
 
        int *nr, *nc, *ldtabl, *work;
-       int nrow=2, ncol=2, ldtable=2, workspace=10000000; // I added two zeros for larger data sets
+       int nrow=2, ncol=2, ldtable=2, workspace=INT_MAX; // I added two zeros for larger data sets
        double *expect, *prc, *emin,*prt,*pre;
        double e=0, prc1=0, emin1=0, prt1=0, pre1=0;
 
@@ -253,7 +275,7 @@ int metastat_main (char* outputFileName, int numRows, int numCols, double thresh
        emin=&emin1;
        prt=&prt1;
        pre=&pre1;
-       
+printf("here before fexact2\n");               
        fexact(nr,nc,data, ldtabl,expect,prc,emin,prt,pre,work);
        
        if (*pre>.999999999){
@@ -268,8 +290,12 @@ int metastat_main (char* outputFileName, int numRows, int numCols, double thresh
   }
   
   // Calculates the mean of counts (not normalized)
-  double temp[row][2];
-  
+  double ** temp;
+  temp = malloc(row*sizeof(double *));
+  for (i = 0; i<row; i++){
+        temp[i] = malloc(col*sizeof(double));
+  }
+printf("here\n");        
   for(j=0;j<row;j++){
        for(i=0;i<2;i++){
                temp[j][i]=0;
@@ -407,11 +433,11 @@ void permute_array(int *array, int n) {
   
   if (! seeded) {
     seeded = 1;
-    srandom(time(NULL));
+    srand(time(NULL));
   }
   
   for (i = 0; i < n; i++) {
-    int selection = random() % (n - i);
+    int selection = rand() % (n - i);
     int tmp = array[i + selection];
     array[i + selection] = array[i];
     array[i] = tmp;
@@ -509,42 +535,45 @@ void meanvar(double *pmatrix,int *g,int *nr,int *nc,double *store){
 }
 
 void start(double *Imatrix,int *g,int *nr,int *nc,double *initial,
-               double storage[][9]){
+               double** storage){
   int i, a = *nr;
   a*=4;
-  
+       
   double store[a], tool[a], C1[*nr][3], C2[*nr][3];
   double nrows,ncols,gvalue, xbardiff=0, denom=0;
-  
   nrows = (double) *nr;
   ncols = (double) *nc;
   gvalue= (double) *g;
-  
+   
   meanvar(Imatrix,g,nr,nc,store);
-  
+   
   for(i=0;i<=a-1;i++){
     tool[i]=store[i];
   }
-  for (i=0; i<*nr;i++){
+
+  for (i=0; i<*nr;i++){ 
     C1[i][0]=tool[i]; //mean group 1
     storage[i][0]=C1[i][0];
     C1[i][1]=tool[i+*nr+*nr]; // var group 1
     storage[i][1]=C1[i][1];
     C1[i][2]=C1[i][1]/(gvalue-1);
     storage[i][2]=sqrt(C1[i][2]);
-    
+    printf("here2\n"); 
     C2[i][0]=tool[i+*nr]; // mean group 2
-    storage[i][4]=C2[i][0];    
+    storage[i][4]=C2[i][0]; 
     C2[i][1]=tool[i+*nr+*nr+*nr]; // var group 2 
-    storage[i][5]=C2[i][1];        
-    C2[i][2]=C2[i][1]/(ncols-gvalue+1);
+    storage[i][5]=C2[i][1];  
+       C2[i][2]=C2[i][1]/(ncols-gvalue+1);
     storage[i][6]=sqrt(C2[i][2]);   
+        
   }
   for (i=0; i<*nr; i++){
     xbardiff = C1[i][0]-C2[i][0];
     denom = sqrt(C1[i][2]+C2[i][2]);
     initial[i]=fabs(xbardiff/denom);
-  }                                                                                    
+  }     
 }