forked from mothur/mothur
/
trialSwap2.cpp
349 lines (306 loc) · 11.4 KB
/
trialSwap2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "trialswap2.h"
//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.
double TrialSwap2::calc_c_score (vector<vector<int> > &co_matrix, vector<int> rowtotal, int ncols, int nrows)
{
try {
double cscore = 0.0;
double maxD;
double D;
double normcscore = 0.0;
int nonzeros = 0;
//int ncols = co_matrix[0].size(); int nrows = rowtotal.size();
vector<vector<double> > s; s.resize(nrows);
for (int i = 0; i < nrows; i++) { s[i].resize(nrows,0.0); }//only fill half the matrix
for(int i=0;i<nrows-1;i++)
{
for(int j=i+1;j<nrows;j++)
{
if (m->control_pressed) { return 0; }
for(int k=0;k<ncols;k++)
{
if((co_matrix[i][k]==1)&&(co_matrix[j][k]==1)) //if both are 1s ie co-occurrence
s[i][j]++; //s counts co-occurrences
}
//rowtotal[i] = A, rowtotal[j] = B, ncols = P, s[i][j] = J
cscore += (rowtotal[i]-s[i][j])*(rowtotal[j]-s[i][j]);///(nrows*(nrows-1)/2);
D = (rowtotal[i]-s[i][j])*(rowtotal[j]-s[i][j]);
if(ncols < (rowtotal[i] + rowtotal[j]))
{
maxD = (ncols-rowtotal[i])*(ncols-rowtotal[j]);
}
else
{
maxD = rowtotal[i] * rowtotal[j];
}
if(maxD != 0)
{
normcscore += D/maxD;
nonzeros++;
}
}
}
//cscore = cscore/(double)(nrows*(nrows-1)/2); //not normalized
//cout << "normalized c score: " << normcscore/nonzeros << endl;
cscore = normcscore/(double)nonzeros;
return cscore;
}
catch(exception& e) {
m->errorOut(e, "TrialSwap2", "calc_c_score");
exit(1);
}
}
/**************************************************************************************************/
int TrialSwap2::calc_checker (vector<vector<int> > &co_matrix, vector<int> rowtotal, int ncols, int nrows)
{
try {
int cunits=0;
//int s[nrows][ncols];
//int ncols = co_matrix[0].size(); int nrows = rowtotal.size();
vector<vector<int> > s; s.resize(nrows);
for (int i = 0; i < nrows; i++) { s[i].resize(nrows,0); }//only fill half the matrix
for(int i=0;i<nrows-1;i++)
{
for(int j=i+1;j<nrows;j++)
{
if (m->control_pressed) { return 0; }
//s[i][j]=0;
for(int k=0;k<ncols;k++)
{
//cout << s[i][j] << endl;
//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].
if((co_matrix[i][k]==1)&&(co_matrix[j][k]==1)) //if both are 1s ie co-occurrence
s[i][j]++; //s counts co-occurrences
}
//cout << "rowtotal: " << rowtotal[i] << endl;
//cout << "co-occurrences: " << s[i][j] << endl;
//cunits+=(rowtotal[i]-s[i][j])*(rowtotal[j]-s[i][j]);
if (s[i][j] == 0)
{
cunits+=1;
}
//cunits+=s[i][j];
}
}
return cunits;
}
catch(exception& e) {
m->errorOut(e, "TrialSwap2", "calc_checker");
exit(1);
}
}
/**************************************************************************************************/
double TrialSwap2::calc_vratio (int nrows, int ncols, vector<int> rowtotal, vector<int> columntotal)
{
try {
//int nrows = rowtotal.size();
//int ncols = columntotal.size();
int sumCol = accumulate(columntotal.begin(), columntotal.end(), 0 );
// int sumRow = accumulate(rowtotal.begin(), rowtotal.end(), 0 );
double colAvg = (double) sumCol / (double) ncols;
// double rowAvg = (double) sumRow / (double) nrows;
double p = 0.0;
// double totalRowVar = 0.0;
double rowVar = 0.0;
double colVar = 0.0;
for(int i=0;i<nrows;i++)
{
if (m->control_pressed) { return 0; }
p = (double) rowtotal[i]/(double) ncols;
rowVar += p * (1.0-p);
}
for(int i=0;i<ncols;i++)
{
if (m->control_pressed) { return 0; }
colVar += pow(((double) columntotal[i]-colAvg),2);
}
colVar = (1.0/(double)ncols) * colVar;
return colVar/rowVar;
}
catch(exception& e) {
m->errorOut(e, "TrialSwap2", "calc_vratio");
exit(1);
}
}
/**************************************************************************************************/
int TrialSwap2::calc_combo (int nrows, int ncols, vector<vector<int> > &nullmatrix)
{
try {
//need to transpose so we can compare rows (row-major order)
//int tmpnrows = nrows;
vector<vector<int> > tmpmatrix;
vector<int> tmprow;
if(!tmpmatrix.empty())
tmpmatrix.clear();
for (int i=0;i<ncols;i++)
{
for (int j=0;j<nrows;j++)
{
tmprow.push_back(nullmatrix[j][i]);
}
tmpmatrix.push_back(tmprow);
tmprow.clear();
}
int unique = 0;
int match = 0;
for(int j=0;j<ncols;j++)
{
match = 0;
for(int i=j+1;i<=ncols;i++)
{
//comparing matrix rows
if( (tmpmatrix[j] == tmpmatrix[i]))
{
match++;
break;
}
}
//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
if (match == 0)
unique++;
}
return unique;
}
catch(exception& e) {
m->errorOut(e, "TrialSwap2", "calc_combo");
exit(1);
}
}
/**************************************************************************************************/
int TrialSwap2::swap_checkerboards (vector<vector<int> > &co_matrix, int ncols, int nrows)
{
try {
//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.
for(int a=0;a<1000;a++){
int i, j, k, l;
i = m->getRandomIndex(nrows-1);
while((j = m->getRandomIndex(nrows-1) ) == i ) {;if (m->control_pressed) { return 0; }}
k = m->getRandomIndex(ncols-1);
while((l = m->getRandomIndex(ncols-1)) == k ) {;if (m->control_pressed) { return 0; }}
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
{
co_matrix[i][k]=1-co_matrix[i][k];
co_matrix[i][l]=1-co_matrix[i][l];
co_matrix[j][k]=1-co_matrix[j][k];
co_matrix[j][l]=1-co_matrix[j][l];
}
}
return 0;
}
catch(exception& e) {
m->errorOut(e, "TrialSwap2", "swap_checkerboards");
exit(1);
}
}
/**************************************************************************************************/
double TrialSwap2::calc_pvalue_greaterthan (vector<double> scorevec, double initialscore)
{
try {
int runs = scorevec.size();
double p = 0.0;
for( int i=0;i<runs;i++)
{
if (m->control_pressed) { return 0; }
if(scorevec[i]>=initialscore)
p++;
}
return p/(double)runs;
}
catch(exception& e) {
m->errorOut(e, "TrialSwap2", "calc_pvalue_greaterthan");
exit(1);
}
}
/**************************************************************************************************/
double TrialSwap2::calc_pvalue_lessthan (vector<double> scorevec, double initialscore)
{
try {
int runs = scorevec.size();
double p = 0.0;
for( int i=0;i<runs;i++)
{
if (m->control_pressed) { return 0; }
if(scorevec[i]<=initialscore)
p++;
}
return p/(double)runs;
}
catch(exception& e) {
m->errorOut(e, "TrialSwap2", "calc_pvalue_lessthan");
exit(1);
}
}
/**************************************************************************************************/
double TrialSwap2::t_test (double initialscore, int runs, double nullMean, vector<double> scorevec)
{
try {
double t;
double sampleSD;
double sum = 0;
for(int i=0;i<runs;i++)
{
if (m->control_pressed) { return 0; }
sum += pow((scorevec[i] - nullMean),2);
//cout << "scorevec[" << i << "]" << scorevec[i] << endl;
}
m->mothurOut("nullMean: " + toString(nullMean)); m->mothurOutEndLine();
m->mothurOut("sum: " + toString(sum)); m->mothurOutEndLine();
sampleSD = sqrt( (1/runs) * sum );
m->mothurOut("samplSD: " + toString(sampleSD)); m->mothurOutEndLine();
t = (nullMean - initialscore) / (sampleSD / sqrt(runs));
return t;
}
catch(exception& e) {
m->errorOut(e, "TrialSwap2", "t_test");
exit(1);
}
}
/**************************************************************************************************/
double TrialSwap2::getSD (int runs, vector<double> scorevec, double nullMean)
{
try{
double sum = 0;
for(int i=0;i<runs;i++)
{
if (m->control_pressed) { return 0; }
sum += pow((scorevec[i] - nullMean),2);
}
return sqrt( (1/double(runs)) * sum );
}
catch(exception& e) {
m->errorOut(e, "TrialSwap2", "getSD");
exit(1);
}
}
/**************************************************************************************************/
double TrialSwap2::get_zscore (double sd, double nullMean, double initscore)
{
try {
return (initscore - nullMean) / sd;
}
catch(exception& e) {
m->errorOut(e, "TrialSwap2", "get_zscore");
exit(1);
}
}
/**************************************************************************************************/
int TrialSwap2::print_matrix(vector<vector<int> > &matrix, int nrows, int ncols)
{
try {
m->mothurOut("matrix:"); m->mothurOutEndLine();
for (int i = 0; i < nrows; i++)
{
if (m->control_pressed) { return 0; }
for (int j = 0; j < ncols; j++)
{
m->mothurOut(toString(matrix[i][j]));
}
m->mothurOutEndLine();
}
return 0;
}
catch(exception& e) {
m->errorOut(e, "TrialSwap2", "print_matrix");
exit(1);
}
}
/**************************************************************************************************/