]> git.donarmstrong.com Git - mothur.git/commitdiff
re-wrote co-occurrence command
authorKathryn Iverson <kd.iverson@gmail.com>
Mon, 30 Apr 2012 14:37:00 +0000 (10:37 -0400)
committerKathryn Iverson <kd.iverson@gmail.com>
Mon, 30 Apr 2012 14:37:00 +0000 (10:37 -0400)
.gitignore
cooccurrencecommand.cpp
trialSwap2.cpp
trialswap2.h

index e43b0f988953ae3a84b00331d0ccf5f7d51cb3cf..1a3b8bd98ef0ed25aa2dcfd12fa9671e256d0e5d 100644 (file)
@@ -1 +1,2 @@
 .DS_Store
+/nbproject/private/
\ No newline at end of file
index 85379d1a286f6a822a3a4a5fdb9525a9b7b5f2ca..17ec19978ddfaf7d125235fd12306a5e76635e61 100644 (file)
@@ -299,102 +299,287 @@ int CooccurrenceCommand::getCooccurrence(vector<SharedRAbundVector*>& thisLookUp
         int ncols = thisLookUp.size();//groups
         double initscore = 0.0;
                 
-        vector<int> initcolumntotal; initcolumntotal.resize(ncols, 0);
-        vector<int> initrowtotal; initrowtotal.resize(nrows, 0);
+        vector<int> columntotal; columntotal.resize(ncols, 0);
+        vector<int> rowtotal; rowtotal.resize(nrows, 0);
         vector<double> stats;
+        double probabilityMatrix[ncols * nrows];
+        vector<vector<int> > nullmatrix(nrows, vector<int>(ncols, 0));
                
         TrialSwap2 trial;
         
-        initcolumntotal = rowtotal;
-        initrowtotal = columntotal;
-        trial.update_row_col_totals(co_matrix, rowtotal, columntotal);
+        n = accumulate( columntotal.begin(), columntotal.end(), 0 );
         
-        if (metric == "cscore")         { initscore = trial.calc_c_score(co_matrix, rowtotal);    }
-        else if (metric == "checker")   { initscore = trial.calc_checker(co_matrix, rowtotal);    }
-        else if (metric == "vratio")    { initscore = trial.calc_vratio(rowtotal, columntotal);   }
-        else if (metric == "combo")     { initscore = trial.calc_combo(co_matrix);                }
+        //============================================================
+        
+        //generate a probability matrix. Only do this once.
+        float start = 0.0;
+    
+        if (matrix == "sim1") {
+            for(int i=0;i<nrows;i++) {
+                for(int j=0;j<ncols;j++) {
+                    probabilityMatrix[ncols * i + j] = start + 1/double(nrows*ncols);                    
+                    start = start + 1/double(nrows*ncols);
+                }
+            }
+        }
+        else if (matrix == "sim2") {
+            for(int i=0;i<nrows;i++) {
+                start = 0.0;
+                for(int j=0;j<ncols;j++) {
+                    probabilityMatrix[ncols * i + j] = start + 1/double(ncols);
+                    start = start + 1/double(ncols); 
+                }
+            }
+        }
+
+        else if (matrix == "sim3") {
+            for(int j=0;j<ncols;j++) {
+                start = 0.0;
+                for(int i=0;i<nrows;i++) {
+                    probabilityMatrix[ncols * i + j] = start + 1/double(nrows);                    
+                    start =  start + 1/double(nrows);
+                }
+            }
+        }
+
+        else if (matrix == "sim4") {
+            for(int i=0;i<nrows;i++) {
+                start = 0.0;
+                for(int j=0;j<ncols;j++) {
+                    probabilityMatrix[ncols * i + j] = start + columntotal[j]/double(n);                    
+                    start =  start + columntotal[j]/double(n);
+                }
+            }
+        }
+
+        else if (matrix == "sim5") {
+            for(int j=0;j<ncols;j++) {
+                start = 0.0;
+                for(int i=0;i<nrows;i++) {
+                    probabilityMatrix[ncols * i + j] = start + rowtotal[i]/double(n);                    
+                    start =  start + rowtotal[i]/double(n);
+                }
+            }
+        }
+
+        else if (matrix == "sim6") {
+            for(int i=0;i<nrows;i++) {
+                for(int j=0;j<ncols;j++) {
+                    probabilityMatrix[ncols * i + j] = start + columntotal[j]/double(n*nrows);                    
+                    start =  start + columntotal[j]/double(n*nrows);
+                }
+            }
+        }
+
+
+        else if (matrix == "sim7") {
+            for(int i=0;i<nrows;i++) {
+                for(int j=0;j<ncols;j++) {
+                    probabilityMatrix[ncols * i + j] = start + rowtotal[i]/double(n*ncols);                    
+                    start =  start + rowtotal[i]/double(n*ncols);
+                }
+            }
+        }
+
+        else if (matrix == "sim8") {
+            for(int i=0;i<nrows;i++) {
+                for(int j=0;j<ncols;j++) {
+                    probabilityMatrix[ncols * i + j] = start + (rowtotal[i]*columntotal[j])/double(n*n);                    
+                    start =  start + (rowtotal[i]*columntotal[j])/double(n*n);
+                }
+            }
+        }
+
+        else {
+            if(sim != 9) {
+                m->mothurOut("[ERROR]: No model selected! \n");
+                m->control_pressed = true;
+            }
+        }
+
+        
+              
+        if (metric == "cscore")         { initscore = trial.calc_c_score(initmatrix, rowtotal, ncols, nrows);    }
+        else if (metric == "checker")   { initscore = trial.calc_checker(initmatrix, rowtotal, ncols, nrows);    }
+        else if (metric == "vratio")    { initscore = trial.calc_vratio(nrows, ncols, rowtotal, columntotal);   }
+        else if (metric == "combo")     { initscore = trial.calc_combo(nrows, ncols, initmatrix);                }
         else                            {  m->mothurOut("[ERROR]: No metric selected!\n");  m->control_pressed = true; return 1;            }
         
         m->mothurOut("Initial c score: " + toString(initscore)); m->mothurOutEndLine();
         
-        //nullmatrix burn in
-        for(int i=0;i<10000;i++) {
-            if (m->control_pressed) { return 0; }
-            if (matrix == "sim1") {
-                trial.sim1(co_matrix);
-            }else if (matrix == "sim2") {
-                trial.sim2(co_matrix);
-            }else if (matrix == "sim3") {
-                trial.sim3(initmatrix);
-                co_matrix = initmatrix;
-            }else if (matrix == "sim4") {
-                trial.sim4(columntotal, rowtotal, co_matrix);
-            }else if (matrix == "sim5") {
-                trial.sim5(initcolumntotal, initrowtotal, initmatrix);
-                trial.transpose_matrix(initmatrix,co_matrix);
-            }else if (matrix == "sim6") {
-                trial.sim6(columntotal, co_matrix);
-            }else if (matrix == "sim7") {
-                trial.sim7(initcolumntotal, initmatrix);          
-                co_matrix = initmatrix;
-            }else if (matrix == "sim8") {
-                trial.sim8(columntotal, rowtotal, co_matrix);
-            }else if (matrix == "sim9") {
-                trial.swap_checkerboards (co_matrix);
-            }else{
-                m->mothurOut("[ERROR]: No model selected! \n");
-                m->control_pressed = true;
+        double previous;
+        double current;
+        double randnum;
+        int count;
+
+        //burn-in
+        for(int i=0;i<10000;i++){                
+            nullmatrix.clear();
+            //zero-fill the null matrix
+            nullmatrix.assign(nrows, vector<int>(ncols, 0));
+
+            if(matrix == "sim1" || matrix == "sim6" || matrix == "sim8" || matrix == "sim7") {
+                count = 0;
+                while(count < n) {
+                    nextnum:
+                    previous = 0.0;
+                    randnum = rand() / double(RAND_MAX);
+                    for(int i=0;i<nrows;i++) {
+                        for(int j=0;j<ncols;j++) { 
+                            current = probabilityMatrix[ncols * i + j];                    
+                            if(randnum <= current && randnum > previous) {
+                                nullmatrix[i][j] = 1;
+                                count++;
+                                if (count > n) break;
+                                else
+                                goto nextnum;
+                            }
+                            previous = current;
+                        }
+                    }
+                }
+            }
+
+            else if(matrix == "sim2" || matrix == "sim4") {
+                    for(int i=0;i<nrows;i++) {
+                        previous = 0.0;
+                        count = 0;
+                        while(count < rowtotal[i]) {
+                            randnum = rand() / double(RAND_MAX);
+                            for(int j=0;j<ncols;j++) { 
+                                current = probabilityMatrix[ncols * i + j];                       
+                                if(randnum <= current && randnum > previous && nullmatrix[i][j] != 1) {
+                                    nullmatrix[i][j] = 1;
+                                    count++;
+                                    previous = 0.0;
+                                    break;
+                                }
+                                previous = current;
+                            }
+                        }
+                    }
             }
+
+            else if(matrix == "sim3" || matrix == "sim5") {
+                //columns
+                for(int j=0;j<ncols;j++) {
+                    count = 0;
+                    while(count < columntotal[j]) {
+                        randnum = rand() / double(RAND_MAX);
+                        for(int i=0;i<nrows;i++) {
+                            current = probabilityMatrix[ncols * i + j];
+                            if(randnum <= current && randnum > previous && nullmatrix[i][j] != 1) {
+                                nullmatrix[i][j] = 1;
+                                count++;
+                                previous = 0.0;
+                                break;
+                            }
+                            previous = current;
+                        }
+                    }
+                }
+            }
+            
         }
-                
-        //run
-        for(int i=0;i<runs;i++) {
-            if (m->control_pressed) { return 0; }
-            //calc metric of nullmatrix
-            if (matrix == "sim1") {
-                trial.sim1(co_matrix);
-            }else if (matrix == "sim2") {
-                trial.sim2(co_matrix);
-            }else if (matrix == "sim3") {
-                trial.sim3(initmatrix);
-                co_matrix = initmatrix;
-            }else if (matrix == "sim4") {
-                trial.sim4(columntotal, rowtotal, co_matrix);
-            }else if (matrix == "sim5") {
-                trial.sim5(initcolumntotal, initrowtotal, initmatrix);
-                trial.transpose_matrix(initmatrix,co_matrix);
-            }else if (matrix == "sim6") {
-                trial.sim6(columntotal, co_matrix);
-            }else if (matrix == "sim7") {
-                trial.sim7(initcolumntotal, initmatrix);          
-                co_matrix = initmatrix;
-            }else if (matrix == "sim8") {
-                trial.sim8(columntotal, rowtotal, co_matrix);
-            }else if (matrix == "sim9") {
-                trial.swap_checkerboards (co_matrix);
-            }else{
-                 m->mothurOut("[ERROR]: No model selected! \n");
-                 m->control_pressed = true;
+        
+        //populate null matrix from probability matrix, do this a lot.  
+        for(int i=0;i<runs;i++){                
+            nullmatrix.clear();
+            //zero-fill the null matrix
+            nullmatrix.assign(nrows, vector<int>(ncols, 0));
+
+            if(matrix == "sim1" || matrix == "sim6" || matrix == "sim8" || matrix == "sim7") {
+                count = 0;
+                while(count < n) {
+                    nextnum:
+                    previous = 0.0;
+                    randnum = rand() / double(RAND_MAX);
+                    for(int i=0;i<nrows;i++) {
+                        for(int j=0;j<ncols;j++) { 
+                            current = probabilityMatrix[ncols * i + j];                    
+                            if(randnum <= current && randnum > previous) {
+                                nullmatrix[i][j] = 1;
+                                count++;
+                                if (count > n) break;
+                                else
+                                goto nextnum;
+                            }
+                            previous = current;
+                        }
+                    }
+                }
+            }
+
+            else if(matrix == "sim2" || matrix == "sim4") {
+                    for(int i=0;i<nrows;i++) {
+                        previous = 0.0;
+                        count = 0;
+                        while(count < rowtotal[i]) {
+                            randnum = rand() / double(RAND_MAX);
+                            for(int j=0;j<ncols;j++) { 
+                                current = probabilityMatrix[ncols * i + j];                       
+                                if(randnum <= current && randnum > previous && nullmatrix[i][j] != 1) {
+                                    nullmatrix[i][j] = 1;
+                                    count++;
+                                    previous = 0.0;
+                                    break;
+                                }
+                                previous = current;
+                            }
+                        }
+                    }
+            }
+
+            else if(matrix == "sim3" || matrix == "sim5") {
+                //columns
+                for(int j=0;j<ncols;j++) {
+                    count = 0;
+                    while(count < columntotal[j]) {
+                        randnum = rand() / double(RAND_MAX);
+                        for(int i=0;i<nrows;i++) {
+                            current = probabilityMatrix[ncols * i + j];
+                            if(randnum <= current && randnum > previous && nullmatrix[i][j] != 1) {
+                                nullmatrix[i][j] = 1;
+                                count++;
+                                previous = 0.0;
+                                break;
+                            }
+                            previous = current;
+                        }
+                    }
+                }
             }
-            //
-            //            
-            //trial.update_row_col_totals(co_matrix, rowtotal, columntotal); 
             
-            if (metric == "cscore") { 
-                stats.push_back(trial.calc_c_score(co_matrix, rowtotal));
-            }else if (metric == "checker") { 
-                stats.push_back(trial.calc_checker(co_matrix, rowtotal));
-            }else if (metric == "vratio") { 
-                stats.push_back(trial.calc_vratio(rowtotal, columntotal));
-            }else if (metric == "combo") { 
-                stats.push_back(trial.calc_combo(co_matrix));
-            }else {
-                m->mothurOut("[ERROR]: No metric selected!\n");
-                m->control_pressed = true;
+            //swap_checkerboards takes the original matrix and swaps checkerboards
+            else if(matrix == "sim9") {
+                trial.swap_checkerboards (initmatrix, rowtotal, columntotal, ncols, nrows);
+            }
+            else {
+                cout << "[ERROR]: No null model selected!\n" << endl;
                 return 1;
             }
-            
+
+            //run metric on null matrix and add score to the stats vector
+            if (metric == "cscore"){
+                stats.push_back(trial.calc_c_score(nullmatrix, rowtotal, ncols, nrows));        
+            }
+            else if (metric == "checker") {
+                stats.push_back(trial.calc_checker(nullmatrix, rowtotal, ncols, nrows));
+            }
+            else if (metric == "vratio") {
+                stats.push_back(trial.calc_vratio(nrows, ncols, rowtotal, columntotal));
+            }
+            else if (metric == "combo") {
+                stats.push_back(trial.calc_combo(nrows, ncols, nullmatrix));
+            }
+            else {
+                cout << "[ERROR]: No metric selected!\n" << endl;
+                return 1;
+            }
+
         }
+        
+
 
         double total = 0.0;
         for (int i=0; i<stats.size();i++)   {   total+=stats[i];   }
index c580436b3b3d542150502d2b771f426bc4aed8a7..ea43de551d2a8df83abd5594754f17ebca2f6760 100644 (file)
@@ -22,629 +22,9 @@ int TrialSwap2::intrand(int n){
        }
 }
 /**************************************************************************************************/
-/* completely random matrix, all column and row totals are variable, matrix size is the same
- *
- *
- */
-/**************************************************************************************************/
-int TrialSwap2::sim1(vector<vector<int> > &co_matrix){ 
-    try {
-        vector<int> randRow;
-        vector<vector<int> > tmpmatrix;
-        int nrows = co_matrix.size();
-        int ncols = co_matrix[0].size();
-        
-        //clear co_matrix
-        //     for(i=0;i<nrows;i++)
-        //     {
-        //         co_matrix.clear();
-        //     }
-        
-        //cout << "building matrix" << endl;
-        for(int i=0;i<nrows;i++){
-            if (m->control_pressed) { break; }
-            
-            for(int j=0;j<ncols;j++){
-                double randNum = rand() / double(RAND_MAX);
-                //cout << randNum << endl;
-                
-                if(randNum > 0.5) {
-                    randRow.push_back(1);
-                }else{
-                    randRow.push_back(0);
-                }
-            }
-            tmpmatrix.push_back(randRow);
-            randRow.clear();
-            //cout << endl;
-        }
-        co_matrix = tmpmatrix;
-        
-        return 0;
-    }
-       catch(exception& e) {
-               m->errorOut(e, "TrialSwap2", "sim1");
-               exit(1);
-       }
-}
-/**************************************************************************************************/
-/*
- *row sums fixed, columns equiprobable 
- */
-void TrialSwap2::sim2(vector<vector<int> > &co_matrix)
-{ 
-    try {
-        
-        for(int i=0;i<co_matrix.size();i++)
-        {
-            if (m->control_pressed) { break; }
-            random_shuffle( co_matrix[i].begin(), co_matrix[i].end() ); 
-        }
-    }
-       catch(exception& e) {
-               m->errorOut(e, "TrialSwap2", "sim2");
-               exit(1);
-       }
-}
-/**************************************************************************************************/
-int TrialSwap2::sim2plus(vector<int> rowtotal, vector<vector<int> > &co_matrix)
-{
-    try {
-        int nrows = co_matrix.size();
-        int ncols = co_matrix[0].size();
-        double cellprob = 1.0/ncols;
-        vector<double> cellprobvec;
-        vector<int> tmprow;
-        vector<vector<int> > tmpmatrix;
-        //double randNum;
-        
-        double start = 0.0;
-        
-        for(int i=0; i<ncols; i++)
-        {
-            if (m->control_pressed) { return 0; }
-            cellprobvec.push_back(start + cellprob);
-            start = cellprobvec[i];
-        }
-        
-        for(int i=0; i<nrows; i++)
-        {
-            tmprow.assign(ncols, 0);
-            
-            while( accumulate( tmprow.begin(), tmprow.end(), 0 ) < rowtotal[i])
-            {
-                if (m->control_pressed) { return 0; }
-                double randNum = rand() / double(RAND_MAX);
-                //cout << randNum << endl;
-                if(randNum <= cellprobvec[0])
-                {
-                    tmprow[0] = 1;
-                    continue;
-                }
-                for(int j=1;j<ncols;j++)
-                {
-                    //cout << range[j] << endl;
-                    if(randNum <= cellprobvec[j] && randNum > cellprobvec[j-1] && tmprow[j] != 1)
-                    {
-                        tmprow[j] = 1;
-                    }
-                }
-            }
-            tmpmatrix.push_back(tmprow);
-            tmprow.clear();
-        }
-        co_matrix = tmpmatrix;
-        tmpmatrix.clear();
-        cellprobvec.clear();
-        
-        return 0;
-    }
-       catch(exception& e) {
-               m->errorOut(e, "TrialSwap2", "sim2plus");
-               exit(1);
-       }
-}
-/**************************************************************************************************/
-/*
- * same as sim2 but using initmatrix which is the initial co-occurrence matrix before transposition
- * may have to be changed depending on what matrix 'seed' is used. One way to use is to transpose
- * every null matrix before using an index and use the random matrix as a seed for the next null.
- */
-/**************************************************************************************************/
-void TrialSwap2::sim3(vector<vector<int> > &initmatrix)
-{
-    try {
-        for(int i=0;i<initmatrix.size();i++)
-        {
-            if (m->control_pressed) { break; }
-            random_shuffle( initmatrix[i].begin(), initmatrix[i].end() ); 
-        }
-        
-    }
-       catch(exception& e) {
-               m->errorOut(e, "TrialSwap2", "sim3");
-               exit(1);
-       }
-}
-/**************************************************************************************************/
-/*
- *
- *
- *
- */
-/**************************************************************************************************/
-int TrialSwap2::sim4(vector<int> columntotal, vector<int> rowtotal, vector<vector<int> > &co_matrix)
-{   
-    try {
-        vector<double> colProb;
-        vector<int> tmprow;//(ncols, 7);
-        vector<vector<int> > tmpmatrix;
-        vector<double> range;
-        vector<double> randNums;
-        int ncols = columntotal.size();
-        int nrows = rowtotal.size();
-        tmprow.clear();
-        
-        double colSum = accumulate( columntotal.begin(), columntotal.end(), 0 );
-        //cout << "col sum: " << colSum << endl;
-        for(int i=0;i<ncols;i++)
-        {
-            if (m->control_pressed) { return 0; }
-            colProb.push_back(columntotal[i]/colSum);
-        }
-        
-        double start = 0.0;
-        
-        for(int i=0;i<ncols;i++)
-        {
-            if (m->control_pressed) { return 0; }
-            range.push_back(start + colProb[i]);
-            start = range[i];
-        }
-        
-        for(int i=0;i<nrows;i++)
-        {
-            tmprow.assign(ncols, 0);
-            if (m->control_pressed) { return 0; }
-            
-            while ( accumulate( tmprow.begin(), tmprow.end(), 0 ) < rowtotal[i])
-            {
-                if (m->control_pressed) { return 0; }
-                
-                double randNum = rand() / double(RAND_MAX);
-                if(randNum <= range[0])
-                {
-                    tmprow[0] = 1;
-                    continue;
-                }
-                for(int j=1;j<ncols;j++)
-                {
-                    if(randNum <= range[j] && randNum > range[j-1] && tmprow[j] != 1)
-                    {
-                        tmprow[j] = 1;
-                    }
-                    
-                }
-            }
-            tmpmatrix.push_back(tmprow);
-            tmprow.clear();
-        }
-        
-        co_matrix = tmpmatrix;
-        
-        return 0;
-    }
-       catch(exception& e) {
-               m->errorOut(e, "TrialSwap2", "sim4");
-               exit(1);
-       }
-}
-/**************************************************************************************************/
-/*
- * inverse of sim4, MUST BE TRANSPOSED BEFORE CO-OCCURRENCE ANALYSIS
- *
- *
- */
-/**************************************************************************************************/
-int TrialSwap2::sim5(vector<int> initcolumntotal,vector<int> initrowtotal, vector<vector<int> > &initmatrix)
-{
-    try {
-        vector<double> colProb;
-        vector<int> tmprow;//(ncols, 7);
-        vector<vector<int> > tmpmatrix;
-        vector<double> range;
-        vector<double> randNums;
-        int ncols = initcolumntotal.size();
-        int nrows = initrowtotal.size();
-        
-        tmprow.clear();
-        
-        double colSum = accumulate( initcolumntotal.begin(), initcolumntotal.end(), 0 );
-        //cout << "col sum: " << colSum << endl;
-        for(int i=0;i<ncols;i++)
-        {
-            if (m->control_pressed) { return 0; }
-            colProb.push_back(initcolumntotal[i]/colSum);
-        }
-        
-        double start = 0.0;
-        
-        for(int i=0;i<ncols;i++)
-        {
-            if (m->control_pressed) { return 0; }
-            range.push_back(start + colProb[i]);
-            start = range[i];
-        }
-        
-        for(int i=0;i<nrows;i++)
-        {
-            tmprow.assign(ncols, 0);
-            if (m->control_pressed) { return 0; }
-            
-            while ( accumulate( tmprow.begin(), tmprow.end(), 0 ) < initrowtotal[i])
-            {
-                if (m->control_pressed) { return 0; }
-                
-                double randNum = rand() / double(RAND_MAX);
-                if(randNum <= range[0])
-                {
-                    tmprow[0] = 1;
-                    continue;
-                }
-                for(int j=1;j<ncols;j++)
-                {
-                    if(randNum <= range[j] && randNum > range[j-1] && tmprow[j] != 1)
-                    {
-                        tmprow[j] = 1;
-                    }
-                    
-                }
-            }
-            tmpmatrix.push_back(tmprow);
-            tmprow.clear();
-        }
-        
-        initmatrix = tmpmatrix;
-        return 0;
-    }
-       catch(exception& e) {
-               m->errorOut(e, "TrialSwap2", "sim5");
-               exit(1);
-       }
-}
-/**************************************************************************************************/
-/*
- *
- *
- *
- */
-/**************************************************************************************************/
-int TrialSwap2::sim6(vector<int> columntotal, vector<vector<int> > &co_matrix)
-{
-    try {
-        vector<vector<int> > tmpmatrix;
-        vector<double> colProb;
-        vector<int> tmprow;
-        vector<double> range;
-        int ncols = columntotal.size();
-        int nrows = co_matrix.size();
-        
-        int colSum = accumulate( columntotal.begin(), columntotal.end(), 0 );
-        
-        for(int i=0;i<ncols;i++)
-        {
-            if (m->control_pressed) { return 0; }
-            colProb.push_back(columntotal[i]/double (colSum));
-        }
-        
-        double start = 0.0;
-        
-        for(int i=0;i<ncols;i++)
-        {
-            if (m->control_pressed) { return 0; }
-            range.push_back(start + colProb[i]);
-            start = range[i];
-        }
-        
-        for(int i=0;i<nrows;i++)
-        {
-            if (m->control_pressed) { return 0; }
-            tmprow.assign(ncols, 0);
-            int tmprowtotal;
-            tmprowtotal = (rand() / double (RAND_MAX)) * 10;
-            while ( tmprowtotal > ncols) {
-                if (m->control_pressed) { return 0; }
-                tmprowtotal = (rand() / double (RAND_MAX)) * 10;
-            }
-            //cout << tmprowtotal << endl;
-            //cout << accumulate( tmprow.begin(), tmprow.end(), 0 ) << endl;
-            
-            while ( accumulate( tmprow.begin(), tmprow.end(), 0 ) < tmprowtotal)
-            {
-                if (m->control_pressed) { return 0; }
-                double randNum = rand() / double(RAND_MAX);
-                //cout << randNum << endl;
-                if(randNum <= range[0])
-                {
-                    tmprow[0] = 1;
-                    continue;
-                }
-                for(int j=1;j<ncols;j++)
-                {
-                    //cout << range[j] << endl;
-                    if(randNum <= range[j] && randNum > range[j-1] && tmprow[j] != 1)
-                    {
-                        tmprow[j] = 1;
-                    }
-                    
-                }
-                
-                
-            }
-            
-            tmpmatrix.push_back(tmprow);
-            tmprow.clear();
-        }
-        
-        co_matrix = tmpmatrix;
-        tmpmatrix.clear();
-        
-        return 0;
-    }
-       catch(exception& e) {
-               m->errorOut(e, "TrialSwap2", "sim6");
-               exit(1);
-       }
-}
-/**************************************************************************************************/
-/*
- * MUST BE TRANSPOSED BEFORE CO-OCCURRENCE ANALYSIS
- *
- *
- */
-/**************************************************************************************************/
-int TrialSwap2::sim7(vector<int> initrowtotal, vector<vector<int> > &co_matrix)
-{
-    try {
-        vector<vector<double> > probmatrix;
-        vector<vector<int> > tmpmatrix;
-        vector<double> colProb;
-        vector<double> probrow;
-        vector<int> tmprow;
-        vector<double> range;
-        double nc;
-        int ncols = co_matrix[0].size(); int nrows = co_matrix.size(); 
-        
-        tmpmatrix.assign(nrows, vector<int>(ncols, 0.));
-        
-        int rowsum = accumulate( initrowtotal.begin(), initrowtotal.end(), 0 );
-        
-        nc = rowsum * ncols;
-        //cout << nc << endl;
-        
-        //assign null matrix based on probabilities
-        
-        double start = 0.0; // don't reset start -- probs should be from 0-1 thoughout the entire matrix 
-        
-        for(int i=0;i<nrows;i++)
-        {
-            if (m->control_pressed) { return 0; }
-            //cout << initrowtotal[i]/double(nc) << endl;
-            double cellprob = initrowtotal[i]/double(nc);
-            //cout << cellprob << endl;
-            for(int j=0;j<ncols;j++)
-            {
-                
-                probrow.push_back(start + cellprob);
-                //cout << probrow[j] << endl;
-                //cout << start << endl;
-                start = start + cellprob;
-            }
-            probmatrix.push_back(probrow);
-            probrow.clear();
-        }
-        
-        
-        //while(tmprowsum < rowsum)
-        //for(int k=0;k<rowsum;k++)
-        int k = 0;
-        while(k < rowsum)
-        {
-            if (m->control_pressed) { return 0; }
-        done:
-            //cout << k << endl;
-            //tmprowsum = accumulate( tmprowtotal.begin(), tmprowtotal.end(), 0 );
-            double randNum = rand() / double(RAND_MAX);
-            //cout << randNum << "+" << endl;
-            //special case for the first entry
-            if(randNum <= probmatrix[0][0] && tmpmatrix[0][0] != 1)
-            {
-                tmpmatrix[0][0] = 1;
-                k++;
-                //cout << k << endl;
-                continue;
-            }
-            
-            
-            for(int i=0;i<nrows;i++)
-            {
-                if (m->control_pressed) { return 0; }
-                for(int j=0;j<ncols;j++)
-                {
-                    //cout << probmatrix[i][j] << endl;
-                    if(randNum <= probmatrix[i][j] && randNum > probmatrix[i][j-1] && tmpmatrix[i][j] != 1)
-                    {
-                        tmpmatrix[i][j] = 1;
-                        k++;
-                        //cout << k << endl;
-                        goto done;
-                    }
-                    //else
-                    //k = k-1;
-                }
-                
-            }
-            
-        }
-        
-        co_matrix = tmpmatrix;
-        return 0;
-    //build probibility matrix
-    /* for(int i=0;i<nrows;i++)
-     {
-     for(int j=0;j<ncols;j++)
-     {
-     probrow.push_back(rowtotal[i]/nc);
-     }
-     probmatrix.pushback(probrow);
-     probrow.clear;
-     }
-     */
-    
-    /* int colSum = accumulate( initcolumntotal.begin(), initcolumntotal.end(), 0 );
-        
-        for(int i=0;i<ncols;i++)
-        {
-            colProb.push_back(initcolumntotal[i]/double (colSum));
-        }
-        
-        double start = 0.0;
-        
-        for(int i=0;i<ncols;i++)
-        {
-            range.push_back(start + colProb[i]);
-            start = range[i];
-        }
-        
-        for(int i=0;i<nrows;i++)
-        {
-            tmprow.assign(ncols, 0);
-            int tmprowtotal;
-            tmprowtotal = (rand() / double (RAND_MAX)) * 10;
-            while ( tmprowtotal > ncols)
-                tmprowtotal = (rand() / double (RAND_MAX)) * 10;
-            //cout << tmprowtotal << endl;
-            //cout << accumulate( tmprow.begin(), tmprow.end(), 0 ) << endl;
-            
-            while ( accumulate( tmprow.begin(), tmprow.end(), 0 ) < tmprowtotal)
-            {
-                double randNum = rand() / double(RAND_MAX);
-                //cout << randNum << endl;
-                if(randNum <= range[0])
-                {
-                    tmprow[0] = 1;
-                    continue;
-                }
-                for(int j=1;j<ncols;j++)
-                {
-                    //cout << range[j] << endl;
-                    if(randNum <= range[j] && randNum > range[j-1] && tmprow[j] != 1)
-                    {
-                        tmprow[j] = 1;
-                    }
-                }
-            }
-            
-            tmpmatrix.push_back(tmprow);
-            tmprow.clear();
-        }
 
-        initmatrix = tmpmatrix;
-     */
-    }
-       catch(exception& e) {
-               m->errorOut(e, "TrialSwap2", "sim7");
-               exit(1);
-       }
-}
-/**************************************************************************************************/
-/*
- *
- *
- *
- */
 /**************************************************************************************************/
-int TrialSwap2::sim8(vector<int> columntotal, vector<int> rowtotal, vector<vector<int> > &co_matrix)
-{   
-    try {
-        double prob; 
-        double start = 0.0;
-        int ncols = columntotal.size(); int nrows = rowtotal.size(); 
-        double probarray[nrows * ncols];
-        double randnum;
-        int grandtotal; 
-        int total = 0;
-        
-        //double colSum = accumulate( columntotal.begin(), columntotal.end(), 0 );
-        double rowSum = accumulate( rowtotal.begin(), rowtotal.end(), 0 );
-        
-        if (m->control_pressed) { return 0; }
-        
-        //cout << "rowsum: " << rowSum << endl;
-        
-        grandtotal = rowSum;
-        
-        //create probability matrix with each site being between 0 and 1
-        for (int i=0;i<nrows;i++) {
-            if (m->control_pressed) { return 0; }
-            for (int j=0;j<ncols;j++) {
-                prob = (rowtotal[i] * columntotal[j])/(rowSum*rowSum);
-                if (prob == 0.0)
-                    probarray[ncols * i + j] = -1;
-                else
-                    probarray[ncols * i + j] = start + prob;
-                //probmatrixrow.pushback(start + prob);
-                start += prob;
-            }
-        }
-        //cout << "prbarray" << endl;
-        //for(int i=0;i<(nrows*ncols);i++)
-        //cout << probarray[i] << " ";
-        //cout << endl;
-        
-        //generate random muber between 0 and 1 and interate through probarray until found
-        while (total < grandtotal)  {
-            if (m->control_pressed) { return 0; }
-            randnum = rand() / double(RAND_MAX);
-            //cout << "rand num: " << randnum << endl;
-            if((randnum <= probarray[0]) && (probarray[0] != 2) ) {
-                probarray[0] = 2;
-                total++;
-                continue;
-            }
-            for(int i=1;i<(nrows*ncols);i++) {
-                if (m->control_pressed) { return 0; }
-                if((randnum <= probarray[i]) && (randnum > probarray[i-1]) && (probarray[i] != 2) ) {
-                    probarray[i] = 2;
-                    total++;
-                    break;
-                }
-                else
-                    continue;
-            }
-        }
-        //cout << "prbarray" << endl;
-        //for(int i=0;i<(nrows*ncols);i++)
-        //cout << probarray[i] << " ";
-        //cout << endl;
-        for(int i=0;i<nrows;i++) {
-            if (m->control_pressed) { return 0; }
-            for(int j=0;j<ncols;j++) {
-                if(probarray[ncols * i + j] == 2)
-                    co_matrix[i][j] = 1;
-                else
-                    co_matrix[i][j] = 0;
-            }
-        }
-        return 0;
-    }
-       catch(exception& e) {
-               m->errorOut(e, "TrialSwap2", "sim8");
-               exit(1);
-       }
-}
-/**************************************************************************************************/
-double TrialSwap2::calc_c_score (vector<vector<int> > &co_matrix,vector<int>  rowtotal)
+double TrialSwap2::calc_c_score (vector<vector<int> > &co_matrix, vector<int>  rowtotal, int ncols, int nrows)
 {
     try {
         double cscore = 0.0;
@@ -652,7 +32,7 @@ double TrialSwap2::calc_c_score (vector<vector<int> > &co_matrix,vector<int>  ro
         double D;
         double normcscore = 0.0;
         int nonzeros = 0;
-        int ncols = co_matrix[0].size(); int nrows = rowtotal.size(); 
+        //int ncols = co_matrix[0].size(); int nrows = rowtotal.size(); 
         vector<vector<double> > s; s.resize(nrows);
         for (int i = 0; i < nrows; i++) { s[i].resize(nrows,0.0); }//only fill half the matrix
 
@@ -701,12 +81,12 @@ double TrialSwap2::calc_c_score (vector<vector<int> > &co_matrix,vector<int>  ro
        }
 }
 /**************************************************************************************************/
-int TrialSwap2::calc_checker (vector<vector<int> > &co_matrix, vector<int>  rowtotal)
+int TrialSwap2::calc_checker (vector<vector<int> > &co_matrix, vector<int>  rowtotal, int ncols, int nrows)
 {
     try {
         int cunits=0;
         //int s[nrows][ncols];
-        int ncols = co_matrix[0].size(); int nrows = rowtotal.size(); 
+        //int ncols = co_matrix[0].size(); int nrows = rowtotal.size(); 
         vector<vector<int> > s; s.resize(nrows);
         for (int i = 0; i < nrows; i++) { s[i].resize(nrows,0); }//only fill half the matrix
         
@@ -743,11 +123,11 @@ int TrialSwap2::calc_checker (vector<vector<int> > &co_matrix, vector<int>  rowt
        }
 }
 /**************************************************************************************************/
-double TrialSwap2::calc_vratio (vector<int> rowtotal, vector<int> columntotal)
+double TrialSwap2::calc_vratio (int nrows, int ncols, vector<int> rowtotal, vector<int> columntotal)
 {
     try {
-        int nrows = rowtotal.size();
-        int ncols = columntotal.size();
+        //int nrows = rowtotal.size();
+        //int ncols = columntotal.size();
         int sumCol = accumulate(columntotal.begin(), columntotal.end(), 0 );
        // int sumRow = accumulate(rowtotal.begin(), rowtotal.end(), 0 );
         
@@ -784,32 +164,47 @@ double TrialSwap2::calc_vratio (vector<int> rowtotal, vector<int> columntotal)
          
 }
 /**************************************************************************************************/
-int TrialSwap2::calc_combo (vector<vector<int> > &initmatrix)
+int TrialSwap2::calc_combo (int nrows, int ncols, vector<vector<int> > &nullmatrix)
 {
     try {
-        int initrows = initmatrix.size();
+        //need to transpose so we can compare rows (row-major order)
+        int tmpnrows = nrows;
+        vector<vector<int> > tmpmatrix;
+
+        vector<int> tmprow;
+        if(!tmpmatrix.empty())
+            tmpmatrix.clear();
+        for (int i=0;i<ncols;i++)
+        {       
+            for (int j=0;j<nrows;j++)
+            {
+                tmprow.push_back(nullmatrix[j][i]);
+            }
+
+            tmpmatrix.push_back(tmprow);
+            tmprow.clear();
+        }
+
         int unique = 0;
         int match = 0;
-        int matches = 0;
-        for(int i=0;i<initrows;i++)
+        for(int j=0;j<ncols;j++)
         {
             match = 0;
-            for(int j=i+1;j<=initrows;j++)
+            for(int i=j+1;i<=ncols;i++)
             {
-                if (m->control_pressed) { return 0; }
-                if( (initmatrix[i] == initmatrix[j])) 
+                //comparing matrix rows
+                if( (tmpmatrix[j] == tmpmatrix[i])) 
                 {
                     match++;
-                    matches++;
                     break;
                 }
             }        
-            
+
             //on the last iteration of a previously matched row it will add itself because it doesn't match any following rows, so that combination is counted
             if (match == 0)
                 unique++;
-        }
-        return unique;
+    }
+    return unique;
     }
     catch(exception& e) {
         m->errorOut(e, "TrialSwap2", "calc_combo");
@@ -826,20 +221,16 @@ int TrialSwap2::swap_checkerboards (vector<vector<int> > &co_matrix)
         while((j = m->getRandomIndex(nrows-1) ) == i ) {;if (m->control_pressed) { return 0; }}
         k = m->getRandomIndex(ncols-1);
         while((l = m->getRandomIndex(ncols-1)) == k ) {;if (m->control_pressed) { return 0; }}
-                
-        //cout << co_matrix[i][k] << " " << co_matrix[j][l] << endl;
-        //cout << co_matrix[i][l] << " " << co_matrix[j][k] << endl;
-        //cout << co_matrix[i][l] << " " << co_matrix[j][k] << endl;
-        //cout << co_matrix[i][l] << " " << co_matrix[j][k] << endl;
+
         if((co_matrix[i][k]*co_matrix[j][l]==1 && co_matrix[i][l]+co_matrix[j][k]==0)||(co_matrix[i][k]+co_matrix[j][l]==0 && co_matrix[i][l]*co_matrix[j][k]==1)) //checking for checkerboard value and swap
         {
             co_matrix[i][k]=1-co_matrix[i][k];
             co_matrix[i][l]=1-co_matrix[i][l];
             co_matrix[j][k]=1-co_matrix[j][k];
             co_matrix[j][l]=1-co_matrix[j][l];
-            //cout << "swapped!" << endl;
+
         }
-        //cout << "i: " << i << " j: " << j << " k: " << " l: " << l << endl;
+
         return 0;
     }
     catch(exception& e) {
@@ -940,83 +331,7 @@ int TrialSwap2::print_matrix(vector<vector<int> > &matrix, int nrows, int ncols)
     }
 }
 /**************************************************************************************************/
-int TrialSwap2::transpose_matrix (vector<vector<int> > &initmatrix, vector<vector<int> > &co_matrix)//, int nrows, int nocols)
-{    
-    try {
-        int ncols = initmatrix.size(); int nrows = initmatrix[0].size(); 
-        int tmpnrows = nrows;
-        //vector<vector<int> > tmpvec;
-        vector<int> tmprow;
-        if(!co_matrix.empty())
-            co_matrix.clear();
-        for (int i=0;i<nrows;i++)
-        {       
-            if (m->control_pressed) { return 0; }
-            for (int j=0;j<ncols;j++)
-            {
-                tmprow.push_back(initmatrix[j][i]);
-            }
-            /*if (accumulate( tmprow.begin(), tmprow.end(), 0 ) == 0)
-             {
-             tmpnrows--;
-             }
-             else */
-            co_matrix.push_back(tmprow);
-            tmprow.clear();
-        }
-        nrows = tmpnrows;
-        return 0;
-    }
-    catch(exception& e) {
-        m->errorOut(e, "TrialSwap2", "transpose_matrix");
-        exit(1);
-    }
-}
-/**************************************************************************************************/
-int TrialSwap2::update_row_col_totals(vector<vector<int> > &co_matrix, vector<int> &rowtotal, vector<int> &columntotal)
-{
-    try {
-        //rowtotal.clear();
-        //columntotal.clear();
-        //generate (rowtotal.begin(), rowtotal.end(), 0);
-        //generate (columntotal.begin(), columntotal.end(), 0);
-        int nrows = co_matrix.size();
-        int ncols = co_matrix[0].size();
-        vector<int> tmpcolumntotal; tmpcolumntotal.resize(ncols, 0);
-        vector<int> tmprowtotal; tmprowtotal.resize(nrows, 0);
-        
-        int rowcount = 0;
-        
-        for (int i = 0; i < nrows; i++)
-        {
-            if (m->control_pressed) { return 0; }
-            for (int j = 0; j < ncols; j++)
-            {
-                if (co_matrix[i][j] == 1)
-                {
-                    rowcount++;
-                    tmpcolumntotal[j]++;
-                }           
-            }    
-            tmprowtotal[i] = rowcount;
-            rowcount = 0;
-        }
-        columntotal = tmpcolumntotal;
-        rowtotal = tmprowtotal;
-        /*cout << "rowtotal: ";
-        for(int i = 0; i<nrows; i++) { cout << rowtotal[i]; }
-        cout << "  ";
-        cout << " coltotal: ";
-        for(int i = 0; i<ncols; i++) { cout << columntotal[i]; }
-        cout << endl;*/
-        return 0;
-    }
-    catch(exception& e) {
-        m->errorOut(e, "TrialSwap2", "update_row_col_totals");
-        exit(1);
-    }
-}
-/**************************************************************************************************/
+
 
 
 
index 6e68e95d440c22b3d63a0040aad5f9c3d51ef6ae..027c946c157e200e3e63859ee27b8607ba42ab88 100644 (file)
@@ -23,21 +23,9 @@ public:
     double calc_pvalue_greaterthan (vector<double>, double);
     int swap_checkerboards (vector<vector<int> > &);
     int calc_combo (vector<vector<int> > &);
-    double calc_vratio (vector<int>, vector<int>);
-    int calc_checker (vector<vector<int> > &,vector<int>);
-    double calc_c_score (vector<vector<int> > &,vector<int>);
-    
-    int sim1 (vector<vector<int> > &);
-    void sim2(vector<vector<int> >&);
-    int sim2plus(vector<int>, vector<vector<int> > &);
-    void sim3(vector<vector<int> > &);
-    int sim4(vector<int>, vector<int>, vector<vector<int> > &);
-    int sim5(vector<int>, vector<int>, vector<vector<int> > &);
-    int sim6(vector<int>, vector<vector<int> > &);
-    int sim7(vector<int>, vector<vector<int> > &);
-    int sim8(vector<int>, vector<int>, vector<vector<int> > &);
-    int transpose_matrix (vector<vector<int> > &, vector<vector<int> > &);
-    int update_row_col_totals(vector<vector<int> > &, vector<int>&, vector<int>&);
+    double calc_vratio (int, int, vector<int>, vector<int>);
+    int calc_checker (vector<vector<int> > &, vector<int>, int, int);
+    double calc_c_score (vector<vector<int> > &, vector<int>, int, int);
 
     
 private: