]> git.donarmstrong.com Git - mothur.git/commitdiff
Merge remote-tracking branch 'mothur/master'
authorSarah Westcott <mothur.westcott@gmail.com>
Mon, 30 Apr 2012 15:21:21 +0000 (11:21 -0400)
committerSarah Westcott <mothur.westcott@gmail.com>
Mon, 30 Apr 2012 15:21:21 +0000 (11:21 -0400)
1  2 
trialSwap2.cpp

diff --cc trialSwap2.cpp
index 9a5232b526851d704ac9e7d738cf18a4f6326945,ea43de551d2a8df83abd5594754f17ebca2f6760..812077d16c98c5a3c865f197d1ba6f50fbce07a6
@@@ -218,411 -22,9 +218,317 @@@ int TrialSwap2::sim4(vector<int> column
        }
  }
  /**************************************************************************************************/
 +/*
 + * 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;