1 #include "trialswap2.h"
4 //The sum_of_squares, havel_hakimi and calc_c_score algorithms have been adapted from I. Miklos and J. Podani. 2004. Randomization of presence-absence matrices: comments and new algorithms. Ecology 85:86-92.
7 /**************************************************************************************************
8 int TrialSwap2::intrand(int n){
12 z = (double)random() * (double)n / (double)RAND_MAX;
17 return((int)floor(z));
20 m->errorOut(e, "TrialSwap2", "intrand");
24 /**************************************************************************************************/
26 /**************************************************************************************************/
27 double TrialSwap2::calc_c_score (vector<vector<int> > &co_matrix, vector<int> rowtotal, int ncols, int nrows)
33 double normcscore = 0.0;
35 //int ncols = co_matrix[0].size(); int nrows = rowtotal.size();
36 vector<vector<double> > s; s.resize(nrows);
37 for (int i = 0; i < nrows; i++) { s[i].resize(nrows,0.0); }//only fill half the matrix
40 for(int i=0;i<nrows-1;i++)
43 for(int j=i+1;j<nrows;j++)
45 if (m->control_pressed) { return 0; }
46 for(int k=0;k<ncols;k++)
48 if((co_matrix[i][k]==1)&&(co_matrix[j][k]==1)) //if both are 1s ie co-occurrence
49 s[i][j]++; //s counts co-occurrences
52 //rowtotal[i] = A, rowtotal[j] = B, ncols = P, s[i][j] = J
53 cscore += (rowtotal[i]-s[i][j])*(rowtotal[j]-s[i][j]);///(nrows*(nrows-1)/2);
54 D = (rowtotal[i]-s[i][j])*(rowtotal[j]-s[i][j]);
56 if(ncols < (rowtotal[i] + rowtotal[j]))
58 maxD = (ncols-rowtotal[i])*(ncols-rowtotal[j]);
62 maxD = rowtotal[i] * rowtotal[j];
73 cscore = cscore/(double)(nrows*(nrows-1)/2);
74 //cout << "normalized c score: " << normcscore/nonzeros << endl;
79 m->errorOut(e, "TrialSwap2", "calc_c_score");
83 /**************************************************************************************************/
84 int TrialSwap2::calc_checker (vector<vector<int> > &co_matrix, vector<int> rowtotal, int ncols, int nrows)
88 //int s[nrows][ncols];
89 //int ncols = co_matrix[0].size(); int nrows = rowtotal.size();
90 vector<vector<int> > s; s.resize(nrows);
91 for (int i = 0; i < nrows; i++) { s[i].resize(nrows,0); }//only fill half the matrix
93 for(int i=0;i<nrows-1;i++)
95 for(int j=i+1;j<nrows;j++)
97 if (m->control_pressed) { return 0; }
99 for(int k=0;k<ncols;k++)
101 //cout << s[i][j] << endl;
102 //iterates through the row and counts co-occurrences. The total number of co-occurrences for each row pair is kept in matrix s at location s[i][j].
103 if((co_matrix[i][k]==1)&&(co_matrix[j][k]==1)) //if both are 1s ie co-occurrence
104 s[i][j]++; //s counts co-occurrences
107 //cout << "rowtotal: " << rowtotal[i] << endl;
108 //cout << "co-occurrences: " << s[i][j] << endl;
109 //cunits+=(rowtotal[i]-s[i][j])*(rowtotal[j]-s[i][j]);
120 catch(exception& e) {
121 m->errorOut(e, "TrialSwap2", "calc_checker");
125 /**************************************************************************************************/
126 double TrialSwap2::calc_vratio (int nrows, int ncols, vector<int> rowtotal, vector<int> columntotal)
129 //int nrows = rowtotal.size();
130 //int ncols = columntotal.size();
131 int sumCol = accumulate(columntotal.begin(), columntotal.end(), 0 );
132 // int sumRow = accumulate(rowtotal.begin(), rowtotal.end(), 0 );
134 double colAvg = (double) sumCol / (double) ncols;
135 // double rowAvg = (double) sumRow / (double) nrows;
139 // double totalRowVar = 0.0;
143 for(int i=0;i<nrows;i++)
145 if (m->control_pressed) { return 0; }
146 p = (double) rowtotal[i]/(double) ncols;
147 rowVar += p * (1.0-p);
150 for(int i=0;i<ncols;i++)
152 if (m->control_pressed) { return 0; }
153 colVar += pow(((double) columntotal[i]-colAvg),2);
156 colVar = (1.0/(double)ncols) * colVar;
158 return colVar/rowVar;
160 catch(exception& e) {
161 m->errorOut(e, "TrialSwap2", "calc_vratio");
166 /**************************************************************************************************/
167 int TrialSwap2::calc_combo (int nrows, int ncols, vector<vector<int> > &nullmatrix)
170 //need to transpose so we can compare rows (row-major order)
171 int tmpnrows = nrows;
172 vector<vector<int> > tmpmatrix;
175 if(!tmpmatrix.empty())
177 for (int i=0;i<ncols;i++)
179 for (int j=0;j<nrows;j++)
181 tmprow.push_back(nullmatrix[j][i]);
184 tmpmatrix.push_back(tmprow);
190 for(int j=0;j<ncols;j++)
193 for(int i=j+1;i<=ncols;i++)
195 //comparing matrix rows
196 if( (tmpmatrix[j] == tmpmatrix[i]))
203 //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
209 catch(exception& e) {
210 m->errorOut(e, "TrialSwap2", "calc_combo");
214 /**************************************************************************************************/
215 int TrialSwap2::swap_checkerboards (vector<vector<int> > &co_matrix)
218 int ncols = co_matrix[0].size(); int nrows = co_matrix.size();
220 i = m->getRandomIndex(nrows-1);
221 while((j = m->getRandomIndex(nrows-1) ) == i ) {;if (m->control_pressed) { return 0; }}
222 k = m->getRandomIndex(ncols-1);
223 while((l = m->getRandomIndex(ncols-1)) == k ) {;if (m->control_pressed) { return 0; }}
225 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
227 co_matrix[i][k]=1-co_matrix[i][k];
228 co_matrix[i][l]=1-co_matrix[i][l];
229 co_matrix[j][k]=1-co_matrix[j][k];
230 co_matrix[j][l]=1-co_matrix[j][l];
236 catch(exception& e) {
237 m->errorOut(e, "TrialSwap2", "swap_checkerboards");
241 /**************************************************************************************************/
242 double TrialSwap2::calc_pvalue_greaterthan (vector<double> scorevec, double initialscore)
245 int runs = scorevec.size();
247 for( int i=0;i<runs;i++)
249 if (m->control_pressed) { return 0; }
250 if(scorevec[i]>=initialscore)
253 return p/(double)runs;
255 catch(exception& e) {
256 m->errorOut(e, "TrialSwap2", "calc_pvalue_greaterthan");
260 /**************************************************************************************************/
261 double TrialSwap2::calc_pvalue_lessthan (vector<double> scorevec, double initialscore)
264 int runs = scorevec.size();
266 for( int i=0;i<runs;i++)
268 if (m->control_pressed) { return 0; }
269 if(scorevec[i]<=initialscore)
272 return p/(double)runs;
274 catch(exception& e) {
275 m->errorOut(e, "TrialSwap2", "calc_pvalue_lessthan");
279 /**************************************************************************************************/
280 double TrialSwap2::t_test (double initialscore, int runs, double nullMean, vector<double> scorevec)
287 for(int i=0;i<runs;i++)
289 if (m->control_pressed) { return 0; }
290 sum += pow((scorevec[i] - nullMean),2);
291 //cout << "scorevec[" << i << "]" << scorevec[i] << endl;
294 m->mothurOut("nullMean: " + toString(nullMean)); m->mothurOutEndLine();
296 m->mothurOut("sum: " + toString(sum)); m->mothurOutEndLine();
298 sampleSD = sqrt( (1/runs) * sum );
300 m->mothurOut("samplSD: " + toString(sampleSD)); m->mothurOutEndLine();
302 t = (nullMean - initialscore) / (sampleSD / sqrt(runs));
306 catch(exception& e) {
307 m->errorOut(e, "TrialSwap2", "t_test");
311 /**************************************************************************************************/
312 int TrialSwap2::print_matrix(vector<vector<int> > &matrix, int nrows, int ncols)
315 m->mothurOut("matrix:"); m->mothurOutEndLine();
317 for (int i = 0; i < nrows; i++)
319 if (m->control_pressed) { return 0; }
320 for (int j = 0; j < ncols; j++)
322 m->mothurOut(toString(matrix[i][j]));
324 m->mothurOutEndLine();
328 catch(exception& e) {
329 m->errorOut(e, "TrialSwap2", "print_matrix");
333 /**************************************************************************************************/