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); //not normalized
54 //cout << "normalized c score: " << normcscore/nonzeros << endl;
55 cscore = normcscore/(double)nonzeros;
60 m->errorOut(e, "TrialSwap2", "calc_c_score");
64 /**************************************************************************************************/
65 int TrialSwap2::calc_checker (vector<vector<int> > &co_matrix, vector<int> rowtotal, int ncols, int nrows)
69 //int s[nrows][ncols];
70 //int ncols = co_matrix[0].size(); int nrows = rowtotal.size();
71 vector<vector<int> > s; s.resize(nrows);
72 for (int i = 0; i < nrows; i++) { s[i].resize(nrows,0); }//only fill half the matrix
74 for(int i=0;i<nrows-1;i++)
76 for(int j=i+1;j<nrows;j++)
78 if (m->control_pressed) { return 0; }
80 for(int k=0;k<ncols;k++)
82 //cout << s[i][j] << endl;
83 //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].
84 if((co_matrix[i][k]==1)&&(co_matrix[j][k]==1)) //if both are 1s ie co-occurrence
85 s[i][j]++; //s counts co-occurrences
88 //cout << "rowtotal: " << rowtotal[i] << endl;
89 //cout << "co-occurrences: " << s[i][j] << endl;
90 //cunits+=(rowtotal[i]-s[i][j])*(rowtotal[j]-s[i][j]);
101 catch(exception& e) {
102 m->errorOut(e, "TrialSwap2", "calc_checker");
106 /**************************************************************************************************/
107 double TrialSwap2::calc_vratio (int nrows, int ncols, vector<int> rowtotal, vector<int> columntotal)
110 //int nrows = rowtotal.size();
111 //int ncols = columntotal.size();
112 int sumCol = accumulate(columntotal.begin(), columntotal.end(), 0 );
113 // int sumRow = accumulate(rowtotal.begin(), rowtotal.end(), 0 );
115 double colAvg = (double) sumCol / (double) ncols;
116 // double rowAvg = (double) sumRow / (double) nrows;
120 // double totalRowVar = 0.0;
124 for(int i=0;i<nrows;i++)
126 if (m->control_pressed) { return 0; }
127 p = (double) rowtotal[i]/(double) ncols;
128 rowVar += p * (1.0-p);
131 for(int i=0;i<ncols;i++)
133 if (m->control_pressed) { return 0; }
134 colVar += pow(((double) columntotal[i]-colAvg),2);
137 colVar = (1.0/(double)ncols) * colVar;
139 return colVar/rowVar;
141 catch(exception& e) {
142 m->errorOut(e, "TrialSwap2", "calc_vratio");
147 /**************************************************************************************************/
148 int TrialSwap2::calc_combo (int nrows, int ncols, vector<vector<int> > &nullmatrix)
151 //need to transpose so we can compare rows (row-major order)
152 //int tmpnrows = nrows;
153 vector<vector<int> > tmpmatrix;
156 if(!tmpmatrix.empty())
158 for (int i=0;i<ncols;i++)
160 for (int j=0;j<nrows;j++)
162 tmprow.push_back(nullmatrix[j][i]);
165 tmpmatrix.push_back(tmprow);
171 for(int j=0;j<ncols;j++)
174 for(int i=j+1;i<=ncols;i++)
176 //comparing matrix rows
177 if( (tmpmatrix[j] == tmpmatrix[i]))
184 //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
190 catch(exception& e) {
191 m->errorOut(e, "TrialSwap2", "calc_combo");
195 /**************************************************************************************************/
196 int TrialSwap2::swap_checkerboards (vector<vector<int> > &co_matrix, int ncols, int nrows)
199 //do 100 runs to make sure enough swaps are happening. This does NOT mean that there will be 1000 swaps, but that is the theoretical max.
200 for(int a=0;a<1000;a++){
202 i = m->getRandomIndex(nrows-1);
203 while((j = m->getRandomIndex(nrows-1) ) == i ) {;if (m->control_pressed) { return 0; }}
204 k = m->getRandomIndex(ncols-1);
205 while((l = m->getRandomIndex(ncols-1)) == k ) {;if (m->control_pressed) { return 0; }}
207 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
209 co_matrix[i][k]=1-co_matrix[i][k];
210 co_matrix[i][l]=1-co_matrix[i][l];
211 co_matrix[j][k]=1-co_matrix[j][k];
212 co_matrix[j][l]=1-co_matrix[j][l];
219 catch(exception& e) {
220 m->errorOut(e, "TrialSwap2", "swap_checkerboards");
224 /**************************************************************************************************/
225 double TrialSwap2::calc_pvalue_greaterthan (vector<double> scorevec, double initialscore)
228 int runs = scorevec.size();
230 for( int i=0;i<runs;i++)
232 if (m->control_pressed) { return 0; }
233 if(scorevec[i]>=initialscore)
236 return p/(double)runs;
238 catch(exception& e) {
239 m->errorOut(e, "TrialSwap2", "calc_pvalue_greaterthan");
243 /**************************************************************************************************/
244 double TrialSwap2::calc_pvalue_lessthan (vector<double> scorevec, double initialscore)
247 int runs = scorevec.size();
249 for( int i=0;i<runs;i++)
251 if (m->control_pressed) { return 0; }
252 if(scorevec[i]<=initialscore)
255 return p/(double)runs;
257 catch(exception& e) {
258 m->errorOut(e, "TrialSwap2", "calc_pvalue_lessthan");
262 /**************************************************************************************************/
263 double TrialSwap2::t_test (double initialscore, int runs, double nullMean, vector<double> scorevec)
270 for(int i=0;i<runs;i++)
272 if (m->control_pressed) { return 0; }
273 sum += pow((scorevec[i] - nullMean),2);
274 //cout << "scorevec[" << i << "]" << scorevec[i] << endl;
277 m->mothurOut("nullMean: " + toString(nullMean)); m->mothurOutEndLine();
279 m->mothurOut("sum: " + toString(sum)); m->mothurOutEndLine();
281 sampleSD = sqrt( (1/runs) * sum );
283 m->mothurOut("samplSD: " + toString(sampleSD)); m->mothurOutEndLine();
285 t = (nullMean - initialscore) / (sampleSD / sqrt(runs));
289 catch(exception& e) {
290 m->errorOut(e, "TrialSwap2", "t_test");
294 /**************************************************************************************************/
295 double TrialSwap2::getSD (int runs, vector<double> scorevec, double nullMean)
299 for(int i=0;i<runs;i++)
301 if (m->control_pressed) { return 0; }
302 sum += pow((scorevec[i] - nullMean),2);
304 return sqrt( (1/double(runs)) * sum );
306 catch(exception& e) {
307 m->errorOut(e, "TrialSwap2", "getSD");
311 /**************************************************************************************************/
312 double TrialSwap2::get_zscore (double sd, double nullMean, double initscore)
315 return (initscore - nullMean) / sd;
317 catch(exception& e) {
318 m->errorOut(e, "TrialSwap2", "get_zscore");
322 /**************************************************************************************************/
323 int TrialSwap2::print_matrix(vector<vector<int> > &matrix, int nrows, int ncols)
326 m->mothurOut("matrix:"); m->mothurOutEndLine();
328 for (int i = 0; i < nrows; i++)
330 if (m->control_pressed) { return 0; }
331 for (int j = 0; j < ncols; j++)
333 m->mothurOut(toString(matrix[i][j]));
335 m->mothurOutEndLine();
339 catch(exception& e) {
340 m->errorOut(e, "TrialSwap2", "print_matrix");
344 /**************************************************************************************************/