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 double TrialSwap2::calc_c_score (vector<vector<int> > &co_matrix, vector<int> rowtotal, int ncols, int nrows)
13 double normcscore = 0.0;
15 //int ncols = co_matrix[0].size(); int nrows = rowtotal.size();
16 vector<vector<double> > s; s.resize(nrows);
17 for (int i = 0; i < nrows; i++) { s[i].resize(nrows,0.0); }//only fill half the matrix
20 for(int i=0;i<nrows-1;i++)
23 for(int j=i+1;j<nrows;j++)
25 if (m->control_pressed) { return 0; }
26 for(int k=0;k<ncols;k++)
28 if((co_matrix[i][k]==1)&&(co_matrix[j][k]==1)) //if both are 1s ie co-occurrence
29 s[i][j]++; //s counts co-occurrences
32 //rowtotal[i] = A, rowtotal[j] = B, ncols = P, s[i][j] = J
33 cscore += (rowtotal[i]-s[i][j])*(rowtotal[j]-s[i][j]);///(nrows*(nrows-1)/2);
34 D = (rowtotal[i]-s[i][j])*(rowtotal[j]-s[i][j]);
36 if(ncols < (rowtotal[i] + rowtotal[j]))
38 maxD = (ncols-rowtotal[i])*(ncols-rowtotal[j]);
42 maxD = rowtotal[i] * rowtotal[j];
53 cscore = cscore/(double)(nrows*(nrows-1)/2);
54 //cout << "normalized c score: " << normcscore/nonzeros << endl;
59 m->errorOut(e, "TrialSwap2", "calc_c_score");
63 /**************************************************************************************************/
64 int TrialSwap2::calc_checker (vector<vector<int> > &co_matrix, vector<int> rowtotal, int ncols, int nrows)
68 //int s[nrows][ncols];
69 //int ncols = co_matrix[0].size(); int nrows = rowtotal.size();
70 vector<vector<int> > s; s.resize(nrows);
71 for (int i = 0; i < nrows; i++) { s[i].resize(nrows,0); }//only fill half the matrix
73 for(int i=0;i<nrows-1;i++)
75 for(int j=i+1;j<nrows;j++)
77 if (m->control_pressed) { return 0; }
79 for(int k=0;k<ncols;k++)
81 //cout << s[i][j] << endl;
82 //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].
83 if((co_matrix[i][k]==1)&&(co_matrix[j][k]==1)) //if both are 1s ie co-occurrence
84 s[i][j]++; //s counts co-occurrences
87 //cout << "rowtotal: " << rowtotal[i] << endl;
88 //cout << "co-occurrences: " << s[i][j] << endl;
89 //cunits+=(rowtotal[i]-s[i][j])*(rowtotal[j]-s[i][j]);
100 catch(exception& e) {
101 m->errorOut(e, "TrialSwap2", "calc_checker");
105 /**************************************************************************************************/
106 double TrialSwap2::calc_vratio (int nrows, int ncols, vector<int> rowtotal, vector<int> columntotal)
109 //int nrows = rowtotal.size();
110 //int ncols = columntotal.size();
111 int sumCol = accumulate(columntotal.begin(), columntotal.end(), 0 );
112 // int sumRow = accumulate(rowtotal.begin(), rowtotal.end(), 0 );
114 double colAvg = (double) sumCol / (double) ncols;
115 // double rowAvg = (double) sumRow / (double) nrows;
119 // double totalRowVar = 0.0;
123 for(int i=0;i<nrows;i++)
125 if (m->control_pressed) { return 0; }
126 p = (double) rowtotal[i]/(double) ncols;
127 rowVar += p * (1.0-p);
130 for(int i=0;i<ncols;i++)
132 if (m->control_pressed) { return 0; }
133 colVar += pow(((double) columntotal[i]-colAvg),2);
136 colVar = (1.0/(double)ncols) * colVar;
138 return colVar/rowVar;
140 catch(exception& e) {
141 m->errorOut(e, "TrialSwap2", "calc_vratio");
146 /**************************************************************************************************/
147 int TrialSwap2::calc_combo (int nrows, int ncols, vector<vector<int> > &nullmatrix)
150 //need to transpose so we can compare rows (row-major order)
151 int tmpnrows = nrows;
152 vector<vector<int> > tmpmatrix;
155 if(!tmpmatrix.empty())
157 for (int i=0;i<ncols;i++)
159 for (int j=0;j<nrows;j++)
161 tmprow.push_back(nullmatrix[j][i]);
164 tmpmatrix.push_back(tmprow);
170 for(int j=0;j<ncols;j++)
173 for(int i=j+1;i<=ncols;i++)
175 //comparing matrix rows
176 if( (tmpmatrix[j] == tmpmatrix[i]))
183 //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
189 catch(exception& e) {
190 m->errorOut(e, "TrialSwap2", "calc_combo");
194 /**************************************************************************************************/
195 int TrialSwap2::swap_checkerboards (vector<vector<int> > &co_matrix, int ncols, int nrows)
198 //int ncols = co_matrix[0].size(); int nrows = co_matrix.size();
200 i = m->getRandomIndex(nrows-1);
201 while((j = m->getRandomIndex(nrows-1) ) == i ) {;if (m->control_pressed) { return 0; }}
202 k = m->getRandomIndex(ncols-1);
203 while((l = m->getRandomIndex(ncols-1)) == k ) {;if (m->control_pressed) { return 0; }}
205 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
207 co_matrix[i][k]=1-co_matrix[i][k];
208 co_matrix[i][l]=1-co_matrix[i][l];
209 co_matrix[j][k]=1-co_matrix[j][k];
210 co_matrix[j][l]=1-co_matrix[j][l];
216 catch(exception& e) {
217 m->errorOut(e, "TrialSwap2", "swap_checkerboards");
221 /**************************************************************************************************/
222 double TrialSwap2::calc_pvalue_greaterthan (vector<double> scorevec, double initialscore)
225 int runs = scorevec.size();
227 for( int i=0;i<runs;i++)
229 if (m->control_pressed) { return 0; }
230 if(scorevec[i]>=initialscore)
233 return p/(double)runs;
235 catch(exception& e) {
236 m->errorOut(e, "TrialSwap2", "calc_pvalue_greaterthan");
240 /**************************************************************************************************/
241 double TrialSwap2::calc_pvalue_lessthan (vector<double> scorevec, double initialscore)
244 int runs = scorevec.size();
246 for( int i=0;i<runs;i++)
248 if (m->control_pressed) { return 0; }
249 if(scorevec[i]<=initialscore)
252 return p/(double)runs;
254 catch(exception& e) {
255 m->errorOut(e, "TrialSwap2", "calc_pvalue_lessthan");
259 /**************************************************************************************************/
260 double TrialSwap2::t_test (double initialscore, int runs, double nullMean, vector<double> scorevec)
267 for(int i=0;i<runs;i++)
269 if (m->control_pressed) { return 0; }
270 sum += pow((scorevec[i] - nullMean),2);
271 //cout << "scorevec[" << i << "]" << scorevec[i] << endl;
274 m->mothurOut("nullMean: " + toString(nullMean)); m->mothurOutEndLine();
276 m->mothurOut("sum: " + toString(sum)); m->mothurOutEndLine();
278 sampleSD = sqrt( (1/runs) * sum );
280 m->mothurOut("samplSD: " + toString(sampleSD)); m->mothurOutEndLine();
282 t = (nullMean - initialscore) / (sampleSD / sqrt(runs));
286 catch(exception& e) {
287 m->errorOut(e, "TrialSwap2", "t_test");
291 /**************************************************************************************************/
292 int TrialSwap2::print_matrix(vector<vector<int> > &matrix, int nrows, int ncols)
295 m->mothurOut("matrix:"); m->mothurOutEndLine();
297 for (int i = 0; i < nrows; i++)
299 if (m->control_pressed) { return 0; }
300 for (int j = 0; j < ncols; j++)
302 m->mothurOut(toString(matrix[i][j]));
304 m->mothurOutEndLine();
308 catch(exception& e) {
309 m->errorOut(e, "TrialSwap2", "print_matrix");
313 /**************************************************************************************************/