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 /**************************************************************************************************/
25 /* completely random matrix, all column and row totals are variable, matrix size is the same
29 /**************************************************************************************************/
30 int TrialSwap2::sim1(vector<vector<int> > &co_matrix){
33 vector<vector<int> > tmpmatrix;
34 int nrows = co_matrix.size();
35 int ncols = co_matrix[0].size();
38 // for(i=0;i<nrows;i++)
43 //cout << "building matrix" << endl;
44 for(int i=0;i<nrows;i++){
45 if (m->control_pressed) { break; }
47 for(int j=0;j<ncols;j++){
48 double randNum = rand() / double(RAND_MAX);
49 //cout << randNum << endl;
57 tmpmatrix.push_back(randRow);
61 co_matrix = tmpmatrix;
66 m->errorOut(e, "TrialSwap2", "sim1");
70 /**************************************************************************************************/
72 *row sums fixed, columns equiprobable
74 void TrialSwap2::sim2(vector<vector<int> > &co_matrix)
78 for(int i=0;i<co_matrix.size();i++)
80 if (m->control_pressed) { break; }
81 random_shuffle( co_matrix[i].begin(), co_matrix[i].end() );
85 m->errorOut(e, "TrialSwap2", "sim2");
89 /**************************************************************************************************/
90 int TrialSwap2::sim2plus(vector<int> rowtotal, vector<vector<int> > &co_matrix)
93 int nrows = co_matrix.size();
94 int ncols = co_matrix[0].size();
95 double cellprob = 1.0/ncols;
96 vector<double> cellprobvec;
98 vector<vector<int> > tmpmatrix;
103 for(int i=0; i<ncols; i++)
105 if (m->control_pressed) { return 0; }
106 cellprobvec.push_back(start + cellprob);
107 start = cellprobvec[i];
110 for(int i=0; i<nrows; i++)
112 tmprow.assign(ncols, 0);
114 while( accumulate( tmprow.begin(), tmprow.end(), 0 ) < rowtotal[i])
116 if (m->control_pressed) { return 0; }
117 double randNum = rand() / double(RAND_MAX);
118 //cout << randNum << endl;
119 if(randNum <= cellprobvec[0])
124 for(int j=1;j<ncols;j++)
126 //cout << range[j] << endl;
127 if(randNum <= cellprobvec[j] && randNum > cellprobvec[j-1] && tmprow[j] != 1)
133 tmpmatrix.push_back(tmprow);
136 co_matrix = tmpmatrix;
142 catch(exception& e) {
143 m->errorOut(e, "TrialSwap2", "sim2plus");
147 /**************************************************************************************************/
149 * same as sim2 but using initmatrix which is the initial co-occurrence matrix before transposition
150 * may have to be changed depending on what matrix 'seed' is used. One way to use is to transpose
151 * every null matrix before using an index and use the random matrix as a seed for the next null.
153 /**************************************************************************************************/
154 void TrialSwap2::sim3(vector<vector<int> > &initmatrix)
157 for(int i=0;i<initmatrix.size();i++)
159 if (m->control_pressed) { break; }
160 random_shuffle( initmatrix[i].begin(), initmatrix[i].end() );
164 catch(exception& e) {
165 m->errorOut(e, "TrialSwap2", "sim3");
169 /**************************************************************************************************/
175 /**************************************************************************************************/
176 int TrialSwap2::sim4(vector<int> columntotal, vector<int> rowtotal, vector<vector<int> > &co_matrix)
179 vector<double> colProb;
180 vector<int> tmprow;//(ncols, 7);
181 vector<vector<int> > tmpmatrix;
182 vector<double> range;
183 vector<double> randNums;
184 int ncols = columntotal.size();
185 int nrows = rowtotal.size();
188 double colSum = accumulate( columntotal.begin(), columntotal.end(), 0 );
189 //cout << "col sum: " << colSum << endl;
190 for(int i=0;i<ncols;i++)
192 if (m->control_pressed) { return 0; }
193 colProb.push_back(columntotal[i]/colSum);
198 for(int i=0;i<ncols;i++)
200 if (m->control_pressed) { return 0; }
201 range.push_back(start + colProb[i]);
205 for(int i=0;i<nrows;i++)
207 tmprow.assign(ncols, 0);
208 if (m->control_pressed) { return 0; }
210 while ( accumulate( tmprow.begin(), tmprow.end(), 0 ) < rowtotal[i])
212 if (m->control_pressed) { return 0; }
214 double randNum = rand() / double(RAND_MAX);
215 if(randNum <= range[0])
220 for(int j=1;j<ncols;j++)
222 if(randNum <= range[j] && randNum > range[j-1] && tmprow[j] != 1)
229 tmpmatrix.push_back(tmprow);
233 co_matrix = tmpmatrix;
237 catch(exception& e) {
238 m->errorOut(e, "TrialSwap2", "sim4");
242 /**************************************************************************************************/
244 * inverse of sim4, MUST BE TRANSPOSED BEFORE CO-OCCURRENCE ANALYSIS
248 /**************************************************************************************************/
249 int TrialSwap2::sim5(vector<int> initcolumntotal,vector<int> initrowtotal, vector<vector<int> > &initmatrix)
252 vector<double> colProb;
253 vector<int> tmprow;//(ncols, 7);
254 vector<vector<int> > tmpmatrix;
255 vector<double> range;
256 vector<double> randNums;
257 int ncols = initcolumntotal.size();
258 int nrows = initrowtotal.size();
262 double colSum = accumulate( initcolumntotal.begin(), initcolumntotal.end(), 0 );
263 //cout << "col sum: " << colSum << endl;
264 for(int i=0;i<ncols;i++)
266 if (m->control_pressed) { return 0; }
267 colProb.push_back(initcolumntotal[i]/colSum);
272 for(int i=0;i<ncols;i++)
274 if (m->control_pressed) { return 0; }
275 range.push_back(start + colProb[i]);
279 for(int i=0;i<nrows;i++)
281 tmprow.assign(ncols, 0);
282 if (m->control_pressed) { return 0; }
284 while ( accumulate( tmprow.begin(), tmprow.end(), 0 ) < initrowtotal[i])
286 if (m->control_pressed) { return 0; }
288 double randNum = rand() / double(RAND_MAX);
289 if(randNum <= range[0])
294 for(int j=1;j<ncols;j++)
296 if(randNum <= range[j] && randNum > range[j-1] && tmprow[j] != 1)
303 tmpmatrix.push_back(tmprow);
307 initmatrix = tmpmatrix;
310 catch(exception& e) {
311 m->errorOut(e, "TrialSwap2", "sim5");
315 /**************************************************************************************************/
321 /**************************************************************************************************/
322 int TrialSwap2::sim6(vector<int> columntotal, vector<vector<int> > &co_matrix)
325 vector<vector<int> > tmpmatrix;
326 vector<double> colProb;
328 vector<double> range;
329 int ncols = columntotal.size();
330 int nrows = co_matrix.size();
332 int colSum = accumulate( columntotal.begin(), columntotal.end(), 0 );
334 for(int i=0;i<ncols;i++)
336 if (m->control_pressed) { return 0; }
337 colProb.push_back(columntotal[i]/double (colSum));
342 for(int i=0;i<ncols;i++)
344 if (m->control_pressed) { return 0; }
345 range.push_back(start + colProb[i]);
349 for(int i=0;i<nrows;i++)
351 if (m->control_pressed) { return 0; }
352 tmprow.assign(ncols, 0);
354 tmprowtotal = (rand() / double (RAND_MAX)) * 10;
355 while ( tmprowtotal > ncols) {
356 if (m->control_pressed) { return 0; }
357 tmprowtotal = (rand() / double (RAND_MAX)) * 10;
359 //cout << tmprowtotal << endl;
360 //cout << accumulate( tmprow.begin(), tmprow.end(), 0 ) << endl;
362 while ( accumulate( tmprow.begin(), tmprow.end(), 0 ) < tmprowtotal)
364 if (m->control_pressed) { return 0; }
365 double randNum = rand() / double(RAND_MAX);
366 //cout << randNum << endl;
367 if(randNum <= range[0])
372 for(int j=1;j<ncols;j++)
374 //cout << range[j] << endl;
375 if(randNum <= range[j] && randNum > range[j-1] && tmprow[j] != 1)
385 tmpmatrix.push_back(tmprow);
389 co_matrix = tmpmatrix;
394 catch(exception& e) {
395 m->errorOut(e, "TrialSwap2", "sim6");
399 /**************************************************************************************************/
401 * MUST BE TRANSPOSED BEFORE CO-OCCURRENCE ANALYSIS
405 /**************************************************************************************************/
406 int TrialSwap2::sim7(vector<int> initrowtotal, vector<vector<int> > &co_matrix)
409 vector<vector<double> > probmatrix;
410 vector<vector<int> > tmpmatrix;
411 vector<double> colProb;
412 vector<double> probrow;
414 vector<double> range;
416 int ncols = co_matrix[0].size(); int nrows = co_matrix.size();
418 tmpmatrix.assign(nrows, vector<int>(ncols, 0.));
420 int rowsum = accumulate( initrowtotal.begin(), initrowtotal.end(), 0 );
423 //cout << nc << endl;
425 //assign null matrix based on probabilities
427 double start = 0.0; // don't reset start -- probs should be from 0-1 thoughout the entire matrix
429 for(int i=0;i<nrows;i++)
431 if (m->control_pressed) { return 0; }
432 //cout << initrowtotal[i]/double(nc) << endl;
433 double cellprob = initrowtotal[i]/double(nc);
434 //cout << cellprob << endl;
435 for(int j=0;j<ncols;j++)
438 probrow.push_back(start + cellprob);
439 //cout << probrow[j] << endl;
440 //cout << start << endl;
441 start = start + cellprob;
443 probmatrix.push_back(probrow);
448 //while(tmprowsum < rowsum)
449 //for(int k=0;k<rowsum;k++)
453 if (m->control_pressed) { return 0; }
456 //tmprowsum = accumulate( tmprowtotal.begin(), tmprowtotal.end(), 0 );
457 double randNum = rand() / double(RAND_MAX);
458 //cout << randNum << "+" << endl;
459 //special case for the first entry
460 if(randNum <= probmatrix[0][0] && tmpmatrix[0][0] != 1)
469 for(int i=0;i<nrows;i++)
471 if (m->control_pressed) { return 0; }
472 for(int j=0;j<ncols;j++)
474 //cout << probmatrix[i][j] << endl;
475 if(randNum <= probmatrix[i][j] && randNum > probmatrix[i][j-1] && tmpmatrix[i][j] != 1)
490 co_matrix = tmpmatrix;
492 //build probibility matrix
493 /* for(int i=0;i<nrows;i++)
495 for(int j=0;j<ncols;j++)
497 probrow.push_back(rowtotal[i]/nc);
499 probmatrix.pushback(probrow);
504 /* int colSum = accumulate( initcolumntotal.begin(), initcolumntotal.end(), 0 );
506 for(int i=0;i<ncols;i++)
508 colProb.push_back(initcolumntotal[i]/double (colSum));
513 for(int i=0;i<ncols;i++)
515 range.push_back(start + colProb[i]);
519 for(int i=0;i<nrows;i++)
521 tmprow.assign(ncols, 0);
523 tmprowtotal = (rand() / double (RAND_MAX)) * 10;
524 while ( tmprowtotal > ncols)
525 tmprowtotal = (rand() / double (RAND_MAX)) * 10;
526 //cout << tmprowtotal << endl;
527 //cout << accumulate( tmprow.begin(), tmprow.end(), 0 ) << endl;
529 while ( accumulate( tmprow.begin(), tmprow.end(), 0 ) < tmprowtotal)
531 double randNum = rand() / double(RAND_MAX);
532 //cout << randNum << endl;
533 if(randNum <= range[0])
538 for(int j=1;j<ncols;j++)
540 //cout << range[j] << endl;
541 if(randNum <= range[j] && randNum > range[j-1] && tmprow[j] != 1)
548 tmpmatrix.push_back(tmprow);
552 initmatrix = tmpmatrix;
555 catch(exception& e) {
556 m->errorOut(e, "TrialSwap2", "sim7");
560 /**************************************************************************************************/
566 /**************************************************************************************************/
567 int TrialSwap2::sim8(vector<int> columntotal, vector<int> rowtotal, vector<vector<int> > &co_matrix)
572 int ncols = columntotal.size(); int nrows = rowtotal.size();
573 double probarray[nrows * ncols];
578 //double colSum = accumulate( columntotal.begin(), columntotal.end(), 0 );
579 double rowSum = accumulate( rowtotal.begin(), rowtotal.end(), 0 );
581 if (m->control_pressed) { return 0; }
583 //cout << "rowsum: " << rowSum << endl;
587 //create probability matrix with each site being between 0 and 1
588 for (int i=0;i<nrows;i++) {
589 if (m->control_pressed) { return 0; }
590 for (int j=0;j<ncols;j++) {
591 prob = (rowtotal[i] * columntotal[j])/(rowSum*rowSum);
593 probarray[ncols * i + j] = -1;
595 probarray[ncols * i + j] = start + prob;
596 //probmatrixrow.pushback(start + prob);
600 //cout << "prbarray" << endl;
601 //for(int i=0;i<(nrows*ncols);i++)
602 //cout << probarray[i] << " ";
605 //generate random muber between 0 and 1 and interate through probarray until found
606 while (total < grandtotal) {
607 if (m->control_pressed) { return 0; }
608 randnum = rand() / double(RAND_MAX);
609 //cout << "rand num: " << randnum << endl;
610 if((randnum <= probarray[0]) && (probarray[0] != 2) ) {
615 for(int i=1;i<(nrows*ncols);i++) {
616 if (m->control_pressed) { return 0; }
617 if((randnum <= probarray[i]) && (randnum > probarray[i-1]) && (probarray[i] != 2) ) {
626 //cout << "prbarray" << endl;
627 //for(int i=0;i<(nrows*ncols);i++)
628 //cout << probarray[i] << " ";
630 for(int i=0;i<nrows;i++) {
631 if (m->control_pressed) { return 0; }
632 for(int j=0;j<ncols;j++) {
633 if(probarray[ncols * i + j] == 2)
641 catch(exception& e) {
642 m->errorOut(e, "TrialSwap2", "sim8");
646 /**************************************************************************************************/
647 int TrialSwap2::havel_hakimi(vector<int> rowtotal,vector<int> columntotal,vector<vector<int> > &co_matrix)
650 int nrows = co_matrix.size();
651 int ncols = co_matrix[0].size();
653 vector<int> r1; r1.resize(nrows,0);
654 vector<int> c; c.resize(ncols,0);
655 vector<int> c1; c1.resize(ncols,0);
658 for(i=0;i<nrows;i++) {
659 for(j=0;j<ncols;j++) {
663 for(i=0;i<nrows;i++) {
675 if (m->control_pressed) { return 0; }
678 if (m->control_pressed) { return 0; }
683 for(j=0;j<rowtotal[i];j++)
685 if (m->control_pressed) { return 0; }
686 co_matrix[i][c1[j]]=1;
689 m->mothurOut("Uhh! " + toString(c1[j]) + "\n");
694 catch(exception& e) {
695 m->errorOut(e, "TrialSwap2", "havel_hakimi");
699 /**************************************************************************************************/
700 int TrialSwap2::sho(vector<int> c, vector<int> c1, int k)
707 if (m->control_pressed) { return 0; }
723 if (m->control_pressed) { return 0; }
737 catch(exception& e) {
738 m->errorOut(e, "TrialSwap2", "sho");
742 /**************************************************************************************************/
743 double TrialSwap2::calc_c_score (vector<vector<int> > &co_matrix,vector<int> rowtotal)
749 double normcscore = 0.0;
751 int ncols = co_matrix[0].size(); int nrows = rowtotal.size();
752 vector<vector<double> > s(nrows, vector<double>(nrows,0.0)); //only fill half the matrix
754 for(int i=0;i<nrows-1;i++)
757 for(int j=i+1;j<nrows;j++)
759 if (m->control_pressed) { return 0; }
760 for(int k=0;k<ncols;k++)
762 if((co_matrix[i][k]==1)&&(co_matrix[j][k]==1)) //if both are 1s ie co-occurrence
763 s[i][j]++; //s counts co-occurrences
766 //rowtotal[i] = A, rowtotal[j] = B, ncols = P, s[i][j] = J
767 cscore += (rowtotal[i]-s[i][j])*(rowtotal[j]-s[i][j]);///(nrows*(nrows-1)/2);
768 D = (rowtotal[i]-s[i][j])*(rowtotal[j]-s[i][j]);
770 if(ncols < (rowtotal[i] + rowtotal[j]))
772 maxD = (ncols-rowtotal[i])*(ncols-rowtotal[j]);
776 maxD = rowtotal[i] * rowtotal[j];
781 normcscore += D/maxD;
787 cscore = cscore/(double)(nrows*(nrows-1)/2);
788 //cout << "normalized c score: " << normcscore/nonzeros << endl;
792 catch(exception& e) {
793 m->errorOut(e, "TrialSwap2", "calc_c_score");
797 /**************************************************************************************************/
798 int TrialSwap2::calc_checker (vector<vector<int> > &co_matrix, vector<int> rowtotal)
802 //int s[nrows][ncols];
803 int ncols = co_matrix[0].size(); int nrows = rowtotal.size();
804 vector<vector<int> > s(nrows, vector<int>(nrows,0)); //only fill half the matrix
806 for(int i=0;i<nrows-1;i++)
808 for(int j=i+1;j<nrows;j++)
810 if (m->control_pressed) { return 0; }
812 for(int k=0;k<ncols;k++)
814 //cout << s[i][j] << endl;
815 //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].
816 if((co_matrix[i][k]==1)&&(co_matrix[j][k]==1)) //if both are 1s ie co-occurrence
817 s[i][j]++; //s counts co-occurrences
820 //cout << "rowtotal: " << rowtotal[i] << endl;
821 //cout << "co-occurrences: " << s[i][j] << endl;
822 //cunits+=(rowtotal[i]-s[i][j])*(rowtotal[j]-s[i][j]);
833 catch(exception& e) {
834 m->errorOut(e, "TrialSwap2", "calc_checker");
838 /**************************************************************************************************/
839 double TrialSwap2::calc_vratio (vector<int> rowtotal, vector<int> columntotal)
842 int nrows = rowtotal.size();
843 int ncols = columntotal.size();
844 int sumCol = accumulate(columntotal.begin(), columntotal.end(), 0 );
845 // int sumRow = accumulate(rowtotal.begin(), rowtotal.end(), 0 );
847 double colAvg = (double) sumCol / (double) ncols;
848 // double rowAvg = (double) sumRow / (double) nrows;
852 // double totalRowVar = 0.0;
856 for(int i=0;i<nrows;i++)
858 if (m->control_pressed) { return 0; }
859 p = (double) rowtotal[i]/(double) ncols;
860 rowVar += p * (1.0-p);
863 for(int i=0;i<ncols;i++)
865 if (m->control_pressed) { return 0; }
866 colVar += pow(((double) columntotal[i]-colAvg),2);
869 colVar = (1.0/(double)ncols) * colVar;
871 return colVar/rowVar;
873 catch(exception& e) {
874 m->errorOut(e, "TrialSwap2", "calc_vratio");
879 /**************************************************************************************************/
880 int TrialSwap2::calc_combo (vector<vector<int> > &initmatrix)
883 int initrows = initmatrix.size();
887 for(int i=0;i<initrows;i++)
890 for(int j=i+1;j<=initrows;j++)
892 if (m->control_pressed) { return 0; }
893 if( (initmatrix[i] == initmatrix[j]))
901 //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
907 catch(exception& e) {
908 m->errorOut(e, "TrialSwap2", "calc_combo");
912 /**************************************************************************************************/
913 int TrialSwap2::swap_checkerboards (vector<vector<int> > &co_matrix)
916 int ncols = co_matrix[0].size(); int nrows = co_matrix.size();
919 while((j = intrand(nrows) ) == i ) {;if (m->control_pressed) { return 0; }}
921 while((l = intrand(ncols) ) == k ) {;if (m->control_pressed) { return 0; }}
922 //cout << co_matrix[i][k] << " " << co_matrix[j][l] << endl;
923 //cout << co_matrix[i][l] << " " << co_matrix[j][k] << endl;
924 //cout << co_matrix[i][l] << " " << co_matrix[j][k] << endl;
925 //cout << co_matrix[i][l] << " " << co_matrix[j][k] << endl;
926 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
928 co_matrix[i][k]=1-co_matrix[i][k];
929 co_matrix[i][l]=1-co_matrix[i][l];
930 co_matrix[j][k]=1-co_matrix[j][k];
931 co_matrix[j][l]=1-co_matrix[j][l];
932 //cout << "swapped!" << endl;
934 //cout << "i: " << i << " j: " << j << " k: " << " l: " << l << endl;
937 catch(exception& e) {
938 m->errorOut(e, "TrialSwap2", "swap_checkerboards");
942 /**************************************************************************************************/
943 double TrialSwap2::calc_pvalue_greaterthan (vector<double> scorevec, double initialscore)
946 int runs = scorevec.size();
948 for( int i=0;i<runs;i++)
950 if (m->control_pressed) { return 0; }
951 if(scorevec[i]>=initialscore)
954 return p/(double)runs;
956 catch(exception& e) {
957 m->errorOut(e, "TrialSwap2", "calc_pvalue_greaterthan");
961 /**************************************************************************************************/
962 double TrialSwap2::calc_pvalue_lessthan (vector<double> scorevec, double initialscore)
965 int runs = scorevec.size();
967 for( int i=0;i<runs;i++)
969 if (m->control_pressed) { return 0; }
970 if(scorevec[i]<=initialscore)
973 return p/(double)runs;
975 catch(exception& e) {
976 m->errorOut(e, "TrialSwap2", "calc_pvalue_lessthan");
980 /**************************************************************************************************/
981 double TrialSwap2::t_test (double initialscore, int runs, double nullMean, vector<double> scorevec)
988 for(int i=0;i<runs;i++)
990 if (m->control_pressed) { return 0; }
991 sum += pow((scorevec[i] - nullMean),2);
992 //cout << "scorevec[" << i << "]" << scorevec[i] << endl;
995 m->mothurOut("nullMean: " + toString(nullMean)); m->mothurOutEndLine();
997 m->mothurOut("sum: " + toString(sum)); m->mothurOutEndLine();
999 sampleSD = sqrt( (1/runs) * sum );
1001 m->mothurOut("samplSD: " + toString(sampleSD)); m->mothurOutEndLine();
1003 t = (nullMean - initialscore) / (sampleSD / sqrt(runs));
1007 catch(exception& e) {
1008 m->errorOut(e, "TrialSwap2", "t_test");
1012 /**************************************************************************************************/
1013 int TrialSwap2::print_matrix(vector<vector<int> > &matrix, int nrows, int ncols)
1016 m->mothurOut("matrix:"); m->mothurOutEndLine();
1018 for (int i = 0; i < nrows; i++)
1020 if (m->control_pressed) { return 0; }
1021 for (int j = 0; j < ncols; j++)
1023 m->mothurOut(toString(matrix[i][j]));
1025 m->mothurOutEndLine();
1029 catch(exception& e) {
1030 m->errorOut(e, "TrialSwap2", "print_matrix");
1034 /**************************************************************************************************/
1035 int TrialSwap2::transpose_matrix (vector<vector<int> > &initmatrix, vector<vector<int> > &co_matrix)//, int nrows, int nocols)
1038 int ncols = initmatrix.size(); int nrows = initmatrix[0].size();
1039 int tmpnrows = nrows;
1040 //vector<vector<int> > tmpvec;
1042 if(!co_matrix.empty())
1044 for (int i=0;i<nrows;i++)
1046 if (m->control_pressed) { return 0; }
1047 for (int j=0;j<ncols;j++)
1049 tmprow.push_back(initmatrix[j][i]);
1051 /*if (accumulate( tmprow.begin(), tmprow.end(), 0 ) == 0)
1056 co_matrix.push_back(tmprow);
1062 catch(exception& e) {
1063 m->errorOut(e, "TrialSwap2", "transpose_matrix");
1067 /**************************************************************************************************/
1068 int TrialSwap2::update_row_col_totals(vector<vector<int> > &co_matrix, vector<int> &rowtotal, vector<int> &columntotal)
1072 //columntotal.clear();
1073 //generate (rowtotal.begin(), rowtotal.end(), 0);
1074 //generate (columntotal.begin(), columntotal.end(), 0);
1075 int nrows = co_matrix.size();
1076 int ncols = co_matrix[0].size();
1077 vector<int> tmpcolumntotal(ncols, 0);
1078 vector<int> tmprowtotal(nrows, 0);
1082 for (int i = 0; i < nrows; i++)
1084 if (m->control_pressed) { return 0; }
1085 for (int j = 0; j < ncols; j++)
1087 if (co_matrix[i][j] == 1)
1090 tmpcolumntotal[j]++;
1093 tmprowtotal[i] = rowcount;
1096 columntotal = tmpcolumntotal;
1097 rowtotal = tmprowtotal;
1098 /*cout << "rowtotal: ";
1099 for(int i = 0; i<nrows; i++) { cout << rowtotal[i]; }
1101 cout << " coltotal: ";
1102 for(int i = 0; i<ncols; i++) { cout << columntotal[i]; }
1106 catch(exception& e) {
1107 m->errorOut(e, "TrialSwap2", "update_row_col_totals");
1111 /**************************************************************************************************/
1112 /*int main(int argc, char *argv[])
1115 char* input_filename = argv[1];
1116 std::ifstream infile (input_filename);
1118 //skip the first line of headers
1119 getline(infile, line);
1120 //get the first line of data
1121 getline(infile, line);
1126 //int numspaces = 0;
1129 for (int i=0; i<int(line.length()); i++)
1131 nextChar = line.at(i); // gets a character
1132 if (isspace(line[i]))
1138 cout << "number of OTUs: ";
1139 cout << ncols << endl;
1143 std::ifstream infile2 (input_filename);
1145 //skip first line of headers
1146 getline(infile2, line);
1148 while (!infile2.eof())
1150 getline(infile2, line);
1155 cout << "number of sites: ";
1156 cout << nrows << endl;
1160 std::ifstream infile3 (input_filename);
1163 getline(infile3, line);
1165 //variables that depend on info from initial matrix
1166 vector<vector<int> > co_matrix;//[nrows][ncols];
1167 vector<vector<int> > initmatrix;
1169 vector<double> stats;
1170 int tmpnrows = nrows;
1172 for (int row1=0; row1<nrows; row1++) // first line was skipped when counting, so we can start from 0
1174 //ignore first 3 cols in each row, data starts on the 3th col
1175 for (int i = 0; i < 3; i++)
1178 for (int col=0; col<ncols; col++)
1181 //cout << tmp << endl;
1182 if (atoi(tmp.c_str()) > 0)
1183 tmprow.push_back(1);
1185 tmprow.push_back(0);
1187 if (accumulate( tmprow.begin(), tmprow.end(), 0 ) == 0)
1192 initmatrix.push_back(tmprow);
1193 //add the row to the matrix
1194 //initmatrix.push_back(tmprow);
1196 //cout << tmprow << endl;
1203 /* cout << "original matrix:" << endl;
1205 for (int i = 0; i < nrows; i++)
1207 for (int j = 0; j < ncols; j++)
1209 cout << initmatrix[i][j];
1214 //for (i=0;i<ncols;i++)
1215 //cout << "col "<< i<< ": " << columntotal[i] << endl;
1217 //co_matrix is now initmatrix and newmatrix is now co_matrix
1219 //remove cols where sum is 0
1222 /* int newmatrows = ncols;
1223 int newmatcols = nrows;
1224 int initcols = ncols; //for the combo metric
1225 int initrows = nrows; //for the combo metric
1226 //swap for transposed matrix
1227 nrows = newmatrows;//ncols;
1228 ncols = newmatcols;//nrows;
1230 vector<int> columntotal(ncols, 0);
1231 vector<int> initcolumntotal(ncols, 0);
1232 vector<int> initrowtotal(nrows, 0);
1233 vector<int> rowtotal(nrows, 0);
1235 transpose_matrix(initmatrix,co_matrix);
1236 //remove degenerate rows and cols
1238 //cout << "transposed matrix:" << endl;
1240 for (int i = 0; i < nrows; i++)
1242 for (int j = 0; j < ncols; j++)
1244 if (co_matrix[i][j] == 1)
1249 //cout << co_matrix[i][j];
1251 //cout << " row total: " << rowcount << endl;
1253 rowtotal[i] = rowcount;
1257 initcolumntotal = rowtotal;
1258 initrowtotal = columntotal;
1262 runs = atol(argv[2]);
1263 int metric = atol(argv[3]);
1264 int nullModel = atol(argv[4]);
1266 update_row_col_totals(co_matrix, rowtotal, columntotal, ncols, nrows);
1267 //do initial metric: checker, c score, v ratio or combo
1272 initscore = calc_c_score(co_matrix, rowtotal);
1273 cout << "initial c score: " << initscore << endl;
1274 //print_matrix(co_matrix, nrows, ncols);
1279 initscore = calc_checker(co_matrix, rowtotal);
1280 cout << "initial checker score: " << initscore << endl;
1285 initscore = calc_vratio(nrows, ncols, rowtotal, columntotal);
1286 cout << "initial v ratio: " << initscore << endl;
1291 initscore = calc_combo(initrows, initcols, initmatrix);
1292 cout << "initial combo score: " << initscore << endl;
1293 //set co_matrix equal to initmatrix because combo requires row comparisons
1294 co_matrix = initmatrix;
1300 //print_matrix(co_matrix, nrows, ncols);
1301 //sim1(nrows, ncols, co_matrix);
1302 //sim2(nrows, ncols, co_matrix);
1303 //sim3(initrows, initcols, initmatrix);
1304 //sim4(columntotal, rowtotal, co_matrix);
1305 //sim5(initcolumntotal, initmatrix);
1306 //sim6(columntotal, co_matrix);
1307 //sim7(initcolumntotal, initmatrix);
1308 sim8(columntotal, rowtotal, co_matrix);
1309 //print_matrix(initmatrix, initrows, initcols);
1310 //print_matrix(co_matrix, nrows, ncols);
1315 cout << "no metric selected!" << endl;
1320 //matrix initialization
1321 //havel_hakimi(nrows, ncols, rowtotal, columntotal, co_matrix);
1322 //sum_of_square(nrows, ncols, rowtotal, columntotal, co_matrix);
1323 //co-matrix is now a random matrix with the same row and column totals as the initial matrix
1325 //null matrix burn in
1326 cout << "initializing null matrix...";
1327 for(int l=0;l<10000;l++)
1329 //swap_checkerboards (co_matrix);
1335 sim1(nrows, ncols, co_matrix);
1340 sim2(nrows, ncols, co_matrix);
1341 //sim2plus(nrows, ncols, initrowtotal, co_matrix);
1346 sim3(initrows, initcols, initmatrix);
1347 //transpose_matrix(initmatrix,co_matrix);
1348 co_matrix = initmatrix;
1353 sim4(columntotal, rowtotal, co_matrix);
1358 sim5(initcolumntotal, initrowtotal, initmatrix);
1359 transpose_matrix(initmatrix,co_matrix);
1360 //co_matrix = initmatrix;
1364 sim6(columntotal, co_matrix);
1368 //sim7(ncols, nrows, initrowtotal, co_matrix);
1369 //transpose_matrix(initmatrix,co_matrix);
1370 //co_matrix = initmatrix;
1374 sim8(columntotal, rowtotal, co_matrix);
1378 //swap_checkerboards
1379 swap_checkerboards (co_matrix);
1383 cout << "no null model selected!" << endl;
1387 cout << "done!" << endl;
1389 //generate null matrices and calculate the metrics
1391 cout << "run: " << endl;
1392 for(int trial=0;trial<runs;trial++) //runs
1394 printf("\b\b\b\b\b\b\b%7d",trial+1);
1401 sim1(nrows, ncols, co_matrix);
1406 sim2(nrows, ncols, co_matrix);
1407 //for(int i=0;i<nrows;i++)
1408 //cout << rowtotal[i] << " ";
1409 //sim2plus(nrows, ncols, initrowtotal, co_matrix);
1414 for(int i=0;i<nrows;i++)
1415 cout << " " << rowtotal[i];
1416 sim3(initrows, initcols, initmatrix);
1417 transpose_matrix(initmatrix,co_matrix);
1422 sim4(columntotal, rowtotal, co_matrix);
1427 sim5(initcolumntotal, initrowtotal, initmatrix);
1428 transpose_matrix(initmatrix,co_matrix);
1432 sim6(columntotal, co_matrix);
1436 sim7(ncols, nrows, initrowtotal, co_matrix);
1437 //print_matrix(co_matrix, nrows, ncols);
1438 //transpose_matrix(initmatrix,co_matrix);
1442 //sim8(initcolumntotal, initrowtotal, co_matrix);
1443 //initrow and initcol are flipped because of transposition. this is super ugly!
1444 sim8(initrowtotal, initcolumntotal, co_matrix);
1448 //swap_checkerboards
1449 swap_checkerboards (co_matrix);
1453 cout << "no null model selected!" << endl;
1458 //print_matrix(co_matrix, nrows, ncols);
1459 update_row_col_totals(co_matrix, rowtotal, columntotal, ncols, nrows);
1460 //cout << columntotal[1]<<endl;
1466 //swap_checkerboards(co_matrix);
1467 //cout << "%" << tmp << " nrows " << nrows << " ncols " << ncols << " rowtotals ";
1468 //for(int i = 0; i<nrows; i++) { cout << rowtotal[i]; }
1470 tmp = calc_c_score(co_matrix, rowtotal);
1471 //cout << "%" << tmp << " ";
1472 stats.push_back(tmp);
1473 //cout << "%" << tmp << " ";
1474 //print_matrix(co_matrix, nrows, ncols);
1479 //swap_checkerboards(co_matrix);
1480 //cout << "%" << tmp << " nrows " << nrows << " ncols " << ncols << " rowtotals ";
1481 //for(int i = 0; i<nrows; i++) { cout << rowtotal[i]; }
1483 tmp = calc_checker(co_matrix, rowtotal);
1484 stats.push_back(tmp);
1485 //cout << "%" << tmp << endl;
1490 stats.push_back(calc_vratio(nrows, ncols, rowtotal, columntotal));
1495 stats.push_back(calc_combo(nrows, ncols, co_matrix));
1503 cout << "no metric selected!" << endl;
1508 //print_matrix(co_matrix, nrows, ncols);
1509 //print_matrix(initmatrix, initrows, initcols);
1516 for (int i=0; i<stats.size();i++)
1521 double nullMean = double (total/stats.size());
1523 cout << '\n' << "average metric score: " << nullMean << endl;
1525 //cout << "t-test: " << t_test(initscore, runs, nullMean, stats) << endl;
1527 if (metric == 1 || metric == 2)
1528 cout << "pvalue: " << calc_pvalue_greaterthan (stats, initscore) << endl;
1530 cout << "pvalue: " << calc_pvalue_lessthan (stats, initscore) << endl;
1532 //print_matrix(co_matrix, nrows, ncols);
1537 /**************************************************************************************************/