5 * Created by westcott on 7/6/11.
6 * Copyright 2011 Schloss Lab. All rights reserved.
10 #include "mothurmetastats.h"
11 #include "mothurfisher.h"
13 /***********************************************************/
14 MothurMetastats::MothurMetastats(double t, int n) {
16 m = MothurOut::getInstance();
20 }catch(exception& e) {
21 m->errorOut(e, "MothurMetastats", "MothurMetastats");
25 /***********************************************************/
26 MothurMetastats::~MothurMetastats() {
30 }catch(exception& e) {
31 m->errorOut(e, "MothurMetastats", "~MothurMetastats");
35 /***********************************************************/
36 //main metastats function
37 int MothurMetastats::runMetastats(string outputFileName, vector< vector<double> >& data, int secondGroupingStart) {
40 row = data.size(); //numBins
41 column = data[0].size(); //numGroups in subset
42 int size = row*column;
44 //consistent with original, but this should never be true
45 if ((secondGroupingStart >= column) || (secondGroupingStart <= 0)) { m->mothurOut("[ERROR]: Check your g value."); m->mothurOutEndLine(); return 0; }
47 //Initialize the matrices
48 vector<double> pmatrix; pmatrix.resize(size, 0.0);
49 vector<double> permuted; permuted.resize(size, 0.0);
50 vector< vector<double> > storage; storage.resize(row);
51 for (int i = 0; i < storage.size(); i++) { storage[i].resize(9, 0.0); }
53 //Produces the sum of each column
54 vector<double> total; total.resize(column, 0.0);
55 vector<double> ratio; ratio.resize(column, 0.0);
56 double total1 = 0.0; double total2 = 0.0;
58 //total[i] = total abundance for group[i]
59 for (int i = 0; i < column; i++) {
60 for (int j = 0; j < row; j++) {
61 total[i] += data[j][i];
65 //total for first grouping
66 for (int i = 0; i < (secondGroupingStart-1); i++) { total1 += total[i]; }
68 //total for second grouping
69 for (int i = (secondGroupingStart-1); i < column; i++) { total2 += total[i]; }
71 //Creates the ratios by first finding the minimum of totals
72 double min = total[0];
73 for (int i = 0; i < total.size(); i++) {
74 if (total[i] < min) { min = total[i]; }
78 if (min <= 0.0) { m->mothurOut("[ERROR]: the sum of one of the columns <= 0."); m->mothurOutEndLine(); return 0; }
81 for(int i = 0; i < ratio.size(); i++){ ratio[i] = total[i] / min; }
83 //Change matrix into an array as received by R for compatibility - kept to be consistent with original
85 for(int i = 0; i < column; i++){
86 for(int j = 0; j < row; j++){
87 pmatrix[count]=data[j][i];
93 for (int i =0; i < column; i++){ pmatrix[i] /= ratio[i]; }
97 for (int i=0; i < size; i++) {
98 if (count % row == 0) { j++; }
99 pmatrix[i] /= ratio[j];
104 vector<double> permuted_ttests; permuted_ttests.resize(row, 0.0);
105 vector<double> pvalues; pvalues.resize(row, 0.0);
106 vector<double> tinitial; tinitial.resize(row, 0.0);
108 if (m->control_pressed) { return 1; }
110 //Find the initial values for the matrix.
111 start(pmatrix, secondGroupingStart, tinitial, storage);
113 if (m->control_pressed) { return 1; }
115 // Start the calculations.
116 if ( (column == 2) || ((secondGroupingStart-1) < 8) || ((column-secondGroupingStart+1) < 8) ){
118 vector<double> fish; fish.resize(row, 0.0);
119 vector<double> fish2; fish2.resize(row, 0.0);
121 for(int i = 0; i < row; i++){
123 for(int j = 0; j < (secondGroupingStart-1); j++) { fish[i] += data[i][j]; }
124 for(int j = (secondGroupingStart-1); j < column; j++) { fish2[i] += data[i][j]; }
126 //vector<double> tempData; tempData.resize(4, 0.0);
127 double f11, f12, f21, f22;
130 f21 = total1 - fish[i];
131 f22 = total2 - fish2[i];
136 pre = fisher.fexact(f11, f12, f21, f22);
138 if (m->control_pressed) { return 1; }
140 if (pre > 0.999999999) { pre = 1.0; }
147 testp(permuted_ttests, permuted, pmatrix, secondGroupingStart, tinitial, pvalues);
149 if (m->control_pressed) { return 1; }
151 // Checks to make sure the matrix isn't sparse.
152 vector<double> sparse; sparse.resize(row, 0.0);
153 vector<double> sparse2; sparse2.resize(row, 0.0);
157 for(int i = 0; i < row; i++){
159 for(int j = 0; j < (secondGroupingStart-1); j++){ sparse[i] += data[i][j]; }
160 if(sparse[i] < (double)(secondGroupingStart-1)){ c++; }
163 for(int j = (secondGroupingStart-1); j < column; j++){ sparse2[i] += data[i][j]; }
164 if( (sparse2[i] < (double)(column-secondGroupingStart+1))) { c++; }
168 double f11,f12,f21,f22;
170 f11=sparse[i]; sparse[i]=0;
171 f12=sparse2[i]; sparse2[i]=0;
178 pre = fisher.fexact(f11, f12, f21, f22);
180 if (m->control_pressed) { return 1; }
182 if (pre > 0.999999999){
194 // Calculates the mean of counts (not normalized)
195 vector< vector<double> > temp; temp.resize(row);
196 for (int i = 0; i < temp.size(); i++) { temp[i].resize(2, 0.0); }
198 for (int j = 0; j < row; j++){
199 if (m->control_pressed) { return 1; }
201 for (int i = 1; i <= (secondGroupingStart-1); i++){ temp[j][0] += data[j][i-1]; }
202 temp[j][0] /= (double)(secondGroupingStart-1);
204 for(int i = secondGroupingStart; i <= column; i++){ temp[j][1] += data[j][i-1]; }
205 temp[j][1] /= (double)(column-secondGroupingStart+1);
208 for(int i = 0; i < row; i++){
209 if (m->control_pressed) { return 1; }
211 storage[i][3]=temp[i][0];
212 storage[i][7]=temp[i][1];
213 storage[i][8]=pvalues[i];
217 cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
218 for (int i = 0; i < row; i++){
220 if (m->control_pressed) { return 1; }
222 if(pvalues[i] < threshold){
223 m->mothurOut("Feature " + toString((i+1)) + " is significant, p = ");
225 m->mothurOutJustToLog(toString(pvalues[i])); m->mothurOutEndLine();
229 // And now we write the files to a text file.
231 time_t t; t = time(NULL);
232 local = localtime(&t);
235 m->openOutputFile(outputFileName, out);
236 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
238 out << "Local time and date of test: " << asctime(local) << endl;
239 out << "# rows = " << row << ", # col = " << column << ", g = " << secondGroupingStart << endl << endl;
240 if (bflag == 1){ out << numPermutations << " permutations" << endl << endl; }
242 //output column headings - not really sure... documentation labels 9 columns, there are 10 in the output file
243 //storage 0 = meanGroup1 - line 529, 1 = varGroup1 - line 532, 2 = err rate1 - line 534, 3 = mean of counts group1?? - line 291, 4 = meanGroup2 - line 536, 5 = varGroup2 - line 539, 6 = err rate2 - line 541, 7 = mean of counts group2?? - line 292, 8 = pvalues - line 293
244 out << "OTU\tmean(group1)\tvariance(group1)\tstderr(group1)\tmean_of_counts(group1)\tmean(group2)\tvariance(group2)\tstderr(group2)\tmean_of_counts(group1)\tp-value\n";
246 for(int i = 0; i < row; i++){
247 if (m->control_pressed) { out.close(); return 0; }
250 for(int j = 0; j < 9; j++){ out << '\t' << storage[i][j]; }
259 }catch(exception& e) {
260 m->errorOut(e, "MothurMetastats", "runMetastats");
264 /***********************************************************/
265 //Find the initial values for the matrix
266 int MothurMetastats::start(vector<double>& Imatrix, int secondGroupingStart, vector<double>& initial, vector< vector<double> >& storage) {
271 double xbardiff = 0.0; double denom = 0.0;
272 vector<double> store; store.resize(a, 0.0);
273 vector<double> tool; tool.resize(a, 0.0);
274 vector< vector<double> > C1; C1.resize(row);
275 for (int i = 0; i < C1.size(); i++) { C1[i].resize(3, 0.0); }
276 vector< vector<double> > C2; C2.resize(row);
277 for (int i = 0; i < C2.size(); i++) { C2[i].resize(3, 0.0); }
279 meanvar(Imatrix, secondGroupingStart, store);
281 if (m->control_pressed) { return 0; }
283 //copy store into tool
286 for (int i = 0; i < row; i++){
287 C1[i][0]=tool[i]; //mean group 1
288 storage[i][0]=C1[i][0];
289 C1[i][1]=tool[i+row+row]; // var group 1
290 storage[i][1]=C1[i][1];
291 C1[i][2]=C1[i][1]/(secondGroupingStart-1);
292 storage[i][2]=sqrt(C1[i][2]);
294 C2[i][0]=tool[i+row]; // mean group 2
295 storage[i][4]=C2[i][0];
296 C2[i][1]=tool[i+row+row+row]; // var group 2
297 storage[i][5]=C2[i][1];
298 C2[i][2]=C2[i][1]/(column-secondGroupingStart+1);
299 storage[i][6]=sqrt(C2[i][2]);
302 if (m->control_pressed) { return 0; }
304 for (int i = 0; i < row; i++){
305 xbardiff = C1[i][0]-C2[i][0];
306 denom = sqrt(C1[i][2]+C2[i][2]);
307 initial[i]=fabs(xbardiff/denom);
312 }catch(exception& e) {
313 m->errorOut(e, "MothurMetastats", "start");
317 /***********************************************************/
318 int MothurMetastats::meanvar(vector<double>& pmatrix, int secondGroupingStart, vector<double>& store) {
320 vector<double> temp; temp.resize(row, 0.0);
321 vector<double> temp2; temp2.resize(row, 0.0);
322 vector<double> var; var.resize(row, 0.0);
323 vector<double> var2; var2.resize(row, 0.0);
325 double a = secondGroupingStart-1;
326 double b = column - a;
328 int n = row * column;
330 for (int i = 0; i < m; i++) { temp[i%row] += pmatrix[i]; }
331 for (int i = 0; i < n; i++) { temp2[i%row]+= pmatrix[i]; }
332 for (int i = 0; i < row; i++) { temp2[i] -= temp[i]; }
333 for (int i = 0; i <= row-1;i++) {
334 store[i] = temp[i]/a;
335 store[i+row]=temp2[i]/b;
338 //That completes the mean calculations.
340 for (int i = 0; i < m; i++) { var[i%row] += pow((pmatrix[i]-store[i%row]),2); }
341 for (int i = m; i < n; i++) { var2[i%row]+= pow((pmatrix[i]-store[(i%row)+row]),2); }
342 for (int i = 0; i <= row-1; i++){
343 store[i+2*row]=var[i]/(a-1);
344 store[i+3*row]=var2[i]/(b-1);
347 // That completes var calculations.
351 }catch(exception& e) {
352 m->errorOut(e, "MothurMetastats", "meanvar");
356 /***********************************************************/
357 int MothurMetastats::testp(vector<double>& permuted_ttests, vector<double>& permuted, vector<double>& Imatrix, int secondGroupingStart, vector<double>& Tinitial, vector<double>& ps) {
360 vector<double> Tvalues; Tvalues.resize(row, 0.0);
361 vector<double> counter; counter.resize(row, 0.0);
368 for (int j = 1; j <= row; j++) {
369 if (m->control_pressed) { return 0; }
370 permute_matrix(Imatrix, permuted, secondGroupingStart, Tvalues, Tinitial, counter);
373 for(int j = 0; j < row; j++) {
374 if (m->control_pressed) { return 0; }
375 ps[j] = ((counter[j]+1)/(double)(a+1));
380 }catch(exception& e) {
381 m->errorOut(e, "MothurMetastats", "testp");
385 /***********************************************************/
386 int MothurMetastats::permute_matrix(vector<double>& Imatrix, vector<double>& permuted, int secondGroupingStart, vector<double>& trial_ts, vector<double>& Tinitial, vector<double>& counter1){
389 vector<int> y; y.resize(column, 0);
390 for (int i = 1; i <= column; i++){ y[i-1] = i; }
394 int f = 0; int c = 0; int k = 0;
395 for (int i = 0; i < column; i++){
397 if (m->control_pressed) { return 0; }
399 f = y[i]; //column number
403 if (f == 1){ c = 0; } // starting value position in the Imatrix
405 for(int j = 1; j <= row; j++){
406 permuted[k] = Imatrix[c];
411 calc_twosample_ts(permuted, secondGroupingStart, trial_ts, Tinitial, counter1);
415 }catch(exception& e) {
416 m->errorOut(e, "MothurMetastats", "permute_matrix");
420 /***********************************************************/
421 int MothurMetastats::permute_array(vector<int>& array) {
423 static int seeded = 0;
430 for (int i = 0; i < array.size(); i++) {
431 if (m->control_pressed) { return 0; }
433 int selection = rand() % (array.size() - i);
434 int tmp = array[i + selection];
435 array[i + selection] = array[i];
441 }catch(exception& e) {
442 m->errorOut(e, "MothurMetastats", "permute_array");
446 /***********************************************************/
447 int MothurMetastats::calc_twosample_ts(vector<double>& Pmatrix, int secondGroupingStart, vector<double>& Ts, vector<double>& Tinitial, vector<double>& counter) {
451 vector< vector<double> > C1; C1.resize(row);
452 for (int i = 0; i < C1.size(); i++) { C1[i].resize(3, 0.0); }
453 vector< vector<double> > C2; C2.resize(row);
454 for (int i = 0; i < C2.size(); i++) { C2[i].resize(3, 0.0); }
455 vector<double> storage; storage.resize(a, 0.0);
456 vector<double> tool; tool.resize(a, 0.0);
457 double xbardiff = 0.0; double denom = 0.0;
459 meanvar(Pmatrix, secondGroupingStart, storage);
461 for(int i = 0;i <= (a-1); i++) {
462 if (m->control_pressed) { return 0; }
463 tool[i] = storage[i];
466 for (int i = 0; i < row; i++){
467 if (m->control_pressed) { return 0; }
469 C1[i][1]=tool[i+row+row];
470 C1[i][2]=C1[i][1]/(secondGroupingStart-1);
472 C2[i][0]=tool[i+row];
473 C2[i][1]=tool[i+row+row+row]; // var group 2
474 C2[i][2]=C2[i][1]/(column-secondGroupingStart+1);
477 for (int i = 0; i < row; i++){
478 if (m->control_pressed) { return 0; }
479 xbardiff = C1[i][0]-C2[i][0];
480 denom = sqrt(C1[i][2]+C2[i][2]);
481 Ts[i]=fabs(xbardiff/denom);
482 if (fabs(Ts[i])>(fabs(Tinitial[i])+.0000000000001)){ //13th place
489 }catch(exception& e) {
490 m->errorOut(e, "MothurMetastats", "calc_twosample_ts");
494 /***********************************************************/