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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
|
/*
* mothurmetastats.cpp
* Mothur
*
* Created by westcott on 7/6/11.
* Copyright 2011 Schloss Lab. All rights reserved.
*
*/
#include "mothurmetastats.h"
#include "mothurfisher.h"
#include "spline.h"
/***********************************************************/
MothurMetastats::MothurMetastats(double t, int n) {
try {
m = MothurOut::getInstance();
threshold = t;
numPermutations = n;
}catch(exception& e) {
m->errorOut(e, "MothurMetastats", "MothurMetastats");
exit(1);
}
}
/***********************************************************/
MothurMetastats::~MothurMetastats() {}
/***********************************************************/
//main metastats function
int MothurMetastats::runMetastats(string outputFileName, vector< vector<double> >& data, int secGroupingStart) {
try {
row = data.size(); //numBins
column = data[0].size(); //numGroups in subset
secondGroupingStart = secGroupingStart; //g number of samples in group 1
vector< vector<double> > Pmatrix; Pmatrix.resize(row);
for (int i = 0; i < row; i++) { Pmatrix[i].resize(column, 0.0); } // the relative proportion matrix
vector< vector<double> > C1; C1.resize(row);
for (int i = 0; i < row; i++) { C1[i].resize(3, 0.0); } // statistic profiles for class1 and class 2
vector< vector<double> > C2; C2.resize(row); // mean[1], variance[2], standard error[3]
for (int i = 0; i < row; i++) { C2[i].resize(3, 0.0); }
vector<double> T_statistics; T_statistics.resize(row, 1); // a place to store the true t-statistics
vector<double> pvalues; pvalues.resize(row, 1); // place to store pvalues
vector<double> qvalues; qvalues.resize(row, 1); // stores qvalues
//*************************************
// convert to proportions
// generate Pmatrix
//*************************************
vector<double> totals; totals.resize(column, 0); // sum of columns
//total[i] = total abundance for group[i]
for (int i = 0; i < column; i++) {
for (int j = 0; j < row; j++) {
totals[i] += data[j][i];
}
}
for (int i = 0; i < column; i++) {
for (int j = 0; j < row; j++) {
Pmatrix[j][i] = data[j][i]/totals[i];
}
}
//#********************************************************************************
//# ************************** STATISTICAL TESTING ********************************
//#********************************************************************************
if (column == 2){ //# then we have a two sample comparison
//#************************************************************
//# generate p values fisher's exact test
//#************************************************************
double total1, total2; total1 = 0; total2 = 0;
//total for first grouping
for (int i = 0; i < secondGroupingStart; i++) { total1 += totals[i]; }
//total for second grouping
for (int i = secondGroupingStart; i < column; i++) { total2 += totals[i]; }
vector<double> fish; fish.resize(row, 0.0);
vector<double> fish2; fish2.resize(row, 0.0);
for(int i = 0; i < row; i++){
for(int j = 0; j < secondGroupingStart; j++) { fish[i] += data[i][j]; }
for(int j = secondGroupingStart; j < column; j++) { fish2[i] += data[i][j]; }
double f11, f12, f21, f22;
f11 = fish[i];
f12 = fish2[i];
f21 = total1 - fish[i];
f22 = total2 - fish2[i];
MothurFisher fisher;
double pre = fisher.fexact(f11, f12, f21, f22);
if (pre > 0.999999999) { pre = 1.0; }
if (m->control_pressed) { return 1; }
pvalues[i] = pre;
}
//#*************************************
//# calculate q values from p values
//#*************************************
qvalues = calc_qvalues(pvalues);
}else { //we have multiple subjects per population
//#*************************************
//# generate statistics mean, var, stderr
//#*************************************
for(int i = 0; i < row; i++){ // for each taxa
//# find the mean of each group
double g1Total = 0.0; double g2Total = 0.0;
for (int j = 0; j < secondGroupingStart; j++) { g1Total += Pmatrix[i][j]; }
C1[i][0] = g1Total/(double)(secondGroupingStart);
for (int j = secondGroupingStart; j < column; j++) { g2Total += Pmatrix[i][j]; }
C2[i][0] = g2Total/(double)(column-secondGroupingStart);
//# find the variance of each group
double g1Var = 0.0; double g2Var = 0.0;
for (int j = 0; j < secondGroupingStart; j++) { g1Var += pow((Pmatrix[i][j]-C1[i][0]), 2); }
C1[i][1] = g1Var/(double)(secondGroupingStart-1);
for (int j = secondGroupingStart; j < column; j++) { g2Var += pow((Pmatrix[i][j]-C2[i][0]), 2); }
C2[i][1] = g2Var/(double)(column-secondGroupingStart-1);
//# find the std error of each group -std err^2 (will change to std err at end)
C1[i][2] = C1[i][1]/(double)(secondGroupingStart);
C2[i][2] = C2[i][1]/(double)(column-secondGroupingStart);
}
//#*************************************
//# two sample t-statistics
//#*************************************
for(int i = 0; i < row; i++){ // # for each taxa
double xbar_diff = C1[i][0] - C2[i][0];
double denom = sqrt(C1[i][2] + C2[i][2]);
T_statistics[i] = xbar_diff/denom; // calculate two sample t-statistic
}
/*for (int i = 0; i < row; i++) {
for (int j = 0; j < 3; j++) {
cout << "C1[" << i+1 << "," << j+1 << "]=" << C1[i][j] << ";" << endl;
cout << "C2[" << i+1 << "," << j+1 << "]=" << C2[i][j] << ";" << endl;
}
cout << "T_statistics[" << i+1 << "]=" << T_statistics[i] << ";" << endl;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
cout << "Fmatrix[" << i+1 << "," << j+1 << "]=" << data[i][j] << ";" << endl;
}
}*/
//#*************************************
//# generate initial permuted p-values
//#*************************************
pvalues = permuted_pvalues(Pmatrix, T_statistics, data);
//#*************************************
//# generate p values for sparse data
//# using fisher's exact test
//#*************************************
double total1, total2; total1 = 0; total2 = 0;
//total for first grouping
for (int i = 0; i < secondGroupingStart; i++) { total1 += totals[i]; }
//total for second grouping
for (int i = secondGroupingStart; i < column; i++) { total2 += totals[i]; }
vector<double> fish; fish.resize(row, 0.0);
vector<double> fish2; fish2.resize(row, 0.0);
for(int i = 0; i < row; i++){
for(int j = 0; j < secondGroupingStart; j++) { fish[i] += data[i][j]; }
for(int j = secondGroupingStart; j < column; j++) { fish2[i] += data[i][j]; }
if ((fish[i] < secondGroupingStart) && (fish2[i] < (column-secondGroupingStart))) {
double f11, f12, f21, f22;
f11 = fish[i];
f12 = fish2[i];
f21 = total1 - fish[i];
f22 = total2 - fish2[i];
MothurFisher fisher;
double pre = fisher.fexact(f11, f12, f21, f22);
if (pre > 0.999999999) { pre = 1.0; }
if (m->control_pressed) { return 1; }
pvalues[i] = pre;
}
}
//#*************************************
//# calculate q values from p values
//#*************************************
qvalues = calc_qvalues(pvalues);
//#*************************************
//# convert stderr^2 to std error
//#*************************************
for(int i = 0; i < row; i++){
C1[i][2] = sqrt(C1[i][2]);
C2[i][2] = sqrt(C2[i][2]);
}
}
// And now we write the files to a text file.
struct tm *local;
time_t t; t = time(NULL);
local = localtime(&t);
ofstream out;
m->openOutputFile(outputFileName, out);
out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
out << "Local time and date of test: " << asctime(local) << endl;
out << "# rows = " << row << ", # col = " << column << ", g = " << secondGroupingStart << endl << endl;
out << numPermutations << " permutations" << endl << endl;
//output column headings - not really sure... documentation labels 9 columns, there are 10 in the output file
//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
out << "OTU\tmean(group1)\tvariance(group1)\tstderr(group1)\tmean(group2)\tvariance(group2)\tstderr(group2)\tp-value\tq-value\n";
for(int i = 0; i < row; i++){
if (m->control_pressed) { out.close(); return 0; }
//if there are binlabels use them otherwise count.
if (i < m->currentSharedBinLabels.size()) { out << m->currentSharedBinLabels[i] << '\t'; }
else { out << (i+1) << '\t'; }
out << C1[i][0] << '\t' << C1[i][1] << '\t' << C1[i][2] << '\t' << C2[i][0] << '\t' << C2[i][1] << '\t' << C2[i][2] << '\t' << pvalues[i] << '\t' << qvalues[i] << endl;
}
out << endl << endl;
out.close();
return 0;
}catch(exception& e) {
m->errorOut(e, "MothurMetastats", "runMetastats");
exit(1);
}
}
/***********************************************************/
vector<double> MothurMetastats::permuted_pvalues(vector< vector<double> >& Imatrix, vector<double>& tstats, vector< vector<double> >& Fmatrix) {
try {
//# matrix stores tstats for each taxa(row) for each permuted trial(column)
vector<double> ps; ps.resize(row, 0.0); //# to store the pvalues
vector< vector<double> > permuted_ttests; permuted_ttests.resize(numPermutations);
for (int i = 0; i < numPermutations; i++) { permuted_ttests[i].resize(row, 0.0); }
//# calculate null version of tstats using B permutations.
for (int i = 0; i < numPermutations; i++) {
permuted_ttests[i] = permute_and_calc_ts(Imatrix);
}
//# calculate each pvalue using the null ts
if ((secondGroupingStart) < 8 || (column-secondGroupingStart) < 8){
vector< vector<double> > cleanedpermuted_ttests; cleanedpermuted_ttests.resize(numPermutations); //# the array pooling just the frequently observed ts
//# then pool the t's together!
//# count how many high freq taxa there are
int hfc = 1;
for (int i = 0; i < row; i++) { // # for each taxa
double group1Total = 0.0; double group2Total = 0.0;
for(int j = 0; j < secondGroupingStart; j++) { group1Total += Fmatrix[i][j]; }
for(int j = secondGroupingStart; j < column; j++) { group2Total += Fmatrix[i][j]; }
if (group1Total >= secondGroupingStart || group2Total >= (column-secondGroupingStart)){
hfc++;
for (int j = 0; j < numPermutations; j++) { cleanedpermuted_ttests[j].push_back(permuted_ttests[j][i]); }
}
}
//#now for each taxa
for (int i = 0; i < row; i++) {
//number of cleanedpermuted_ttests greater than tstat[i]
int numGreater = 0;
for (int j = 0; j < numPermutations; j++) {
for (int k = 0; k < hfc; k++) {
if (cleanedpermuted_ttests[j][k] > abs(tstats[i])) { numGreater++; }
}
}
ps[i] = (1/(double)(numPermutations*hfc))*numGreater;
}
}else{
for (int i = 0; i < row; i++) {
//number of permuted_ttests[i] greater than tstat[i] //(sum(permuted_ttests[i,] > abs(tstats[i]))+1)
int numGreater = 1;
for (int j = 0; j < numPermutations; j++) { if (permuted_ttests[j][i] > abs(tstats[i])) { numGreater++; } }
ps[i] = (1/(double)(numPermutations+1))*numGreater;
}
}
return ps;
}catch(exception& e) {
m->errorOut(e, "MothurMetastats", "permuted_pvalues");
exit(1);
}
}
/***********************************************************/
vector<double> MothurMetastats::permute_and_calc_ts(vector< vector<double> >& Imatrix) {
try {
vector< vector<double> > permutedMatrix = Imatrix;
//randomize columns, ie group abundances.
map<int, int> randomMap;
vector<int> randoms;
for (int i = 0; i < column; i++) { randoms.push_back(i); }
random_shuffle(randoms.begin(), randoms.end());
for (int i = 0; i < randoms.size(); i++) { randomMap[i] = randoms[i]; }
//calc ts
vector< vector<double> > C1; C1.resize(row);
for (int i = 0; i < row; i++) { C1[i].resize(3, 0.0); } // statistic profiles for class1 and class 2
vector< vector<double> > C2; C2.resize(row); // mean[1], variance[2], standard error[3]
for (int i = 0; i < row; i++) { C2[i].resize(3, 0.0); }
vector<double> Ts; Ts.resize(row, 0.0); // a place to store the true t-statistics
//#*************************************
//# generate statistics mean, var, stderr
//#*************************************
for(int i = 0; i < row; i++){ // for each taxa
//# find the mean of each group
double g1Total = 0.0; double g2Total = 0.0;
for (int j = 0; j < secondGroupingStart; j++) { g1Total += permutedMatrix[i][randomMap[j]]; }
C1[i][0] = g1Total/(double)(secondGroupingStart);
for (int j = secondGroupingStart; j < column; j++) { g2Total += permutedMatrix[i][randomMap[j]]; }
C2[i][0] = g2Total/(double)(column-secondGroupingStart);
//# find the variance of each group
double g1Var = 0.0; double g2Var = 0.0;
for (int j = 0; j < secondGroupingStart; j++) { g1Var += pow((permutedMatrix[i][randomMap[j]]-C1[i][0]), 2); }
C1[i][1] = g1Var/(double)(secondGroupingStart-1);
for (int j = secondGroupingStart; j < column; j++) { g2Var += pow((permutedMatrix[i][randomMap[j]]-C2[i][0]), 2); }
C2[i][1] = g2Var/(double)(column-secondGroupingStart-1);
//# find the std error of each group -std err^2 (will change to std err at end)
C1[i][2] = C1[i][1]/(double)(secondGroupingStart);
C2[i][2] = C2[i][1]/(double)(column-secondGroupingStart);
}
//#*************************************
//# two sample t-statistics
//#*************************************
for(int i = 0; i < row; i++){ // # for each taxa
double xbar_diff = C1[i][0] - C2[i][0];
double denom = sqrt(C1[i][2] + C2[i][2]);
Ts[i] = abs(xbar_diff/denom); // calculate two sample t-statistic
}
return Ts;
}catch(exception& e) {
m->errorOut(e, "MothurMetastats", "permuted_ttests");
exit(1);
}
}
/***********************************************************/
vector<double> MothurMetastats::calc_qvalues(vector<double>& pValues) {
try {
int numRows = pValues.size();
vector<double> qvalues(numRows, 0.0);
//fill lambdas with 0.00, 0.01, 0.02... 0.95
vector<double> lambdas(96, 0);
for (int i = 1; i < lambdas.size(); i++) { lambdas[i] = lambdas[i-1] + 0.01; }
vector<double> pi0_hat(lambdas.size(), 0);
//calculate pi0_hat
for (int l = 0; l < lambdas.size(); l++){ // for each lambda value
int count = 0;
for (int i = 0; i < numRows; i++){ // for each p-value in order
if (pValues[i] > lambdas[l]){ count++; }
}
pi0_hat[l] = count/(double)(numRows*(1.0-lambdas[l]));
//cout << lambdas[l] << '\t' << count << '\t' << numRows*(1.0-lambdas[l]) << '\t' << pi0_hat[l] << endl;
}
double pi0 = smoothSpline(lambdas, pi0_hat, 3);
//order p-values
vector<double> ordered_qs = qvalues;
vector<int> ordered_ps(pValues.size(), 0);
for (int i = 1; i < ordered_ps.size(); i++) { ordered_ps[i] = ordered_ps[i-1] + 1; }
vector<double> tempPvalues = pValues;
OrderPValues(0, numRows-1, tempPvalues, ordered_ps);
ordered_qs[numRows-1] = min((pValues[ordered_ps[numRows-1]]*pi0), 1.0);
for (int i = (numRows-2); i >= 0; i--){
double p = pValues[ordered_ps[i]];
double temp = p*numRows*pi0/(double)(i+1);
ordered_qs[i] = min(temp, ordered_qs[i+1]);
}
//re-distribute calculated qvalues to appropriate rows
for (int i = 0; i < numRows; i++){
qvalues[ordered_ps[i]] = ordered_qs[i];
}
return qvalues;
}catch(exception& e) {
m->errorOut(e, "MothurMetastats", "calc_qvalues");
exit(1);
}
}
/***********************************************************/
int MothurMetastats::OrderPValues(int low, int high, vector<double>& p, vector<int>& order) {
try {
if (low < high) {
int i = low+1;
int j = high;
int pivot = (low+high) / 2;
swapElements(low, pivot, p, order); //puts pivot in final spot
/* compare value */
double key = p[low];
/* partition */
while(i <= j) {
/* find member above ... */
while((i <= high) && (p[i] <= key)) { i++; }
/* find element below ... */
while((j >= low) && (p[j] > key)) { j--; }
if(i < j) {
swapElements(i, j, p, order);
}
}
swapElements(low, j, p, order);
/* recurse */
OrderPValues(low, j-1, p, order);
OrderPValues(j+1, high, p, order);
}
return 0;
}catch(exception& e) {
m->errorOut(e, "MothurMetastats", "OrderPValues");
exit(1);
}
}
/***********************************************************/
int MothurMetastats::swapElements(int i, int j, vector<double>& p, vector<int>& order) {
try {
double z = p[i];
p[i] = p[j];
p[j] = z;
int temp = order[i];
order[i] = order[j];
order[j] = temp;
return 0;
}catch(exception& e) {
m->errorOut(e, "MothurMetastats", "swapElements");
exit(1);
}
}
/***********************************************************/
double MothurMetastats::smoothSpline(vector<double>& x, vector<double>& y, int df) {
try {
double result = 0.0;
int n = x.size();
vector<double> w(n, 1);
double* xb = new double[n];
double* yb = new double[n];
double* wb = new double[n];
double yssw = 0.0;
for (int i = 0; i < n; i++) {
wb[i] = w[i];
yb[i] = w[i]*y[i];
yssw += (w[i] * y[i] * y[i]) - wb[i] * (yb[i] * yb[i]);
xb[i] = (x[i] - x[0]) / (x[n-1] - x[0]);
}
vector<double> knot = sknot1(xb, n);
int nk = knot.size() - 4;
double low = -1.5; double high = 1.5; double tol = 1e-04; double eps = 2e-08; int maxit = 500;
int ispar = 0; int icrit = 3; double dofoff = 3.0;
double penalty = 1.0;
int ld4 = 4; int isetup = 0; int ldnk = 1; int ier; double spar = 1.0; double crit;
double* knotb = new double[knot.size()];
double* coef1 = new double[nk];
double* lev1 = new double[n];
double* sz1 = new double[n];
for (int i = 0; i < knot.size(); i++) { knotb[i] = knot[i]; }
Spline spline;
spline.sbart(&penalty, &dofoff, &xb[0], &yb[0], &wb[0], &yssw, &n, &knotb[0], &nk, &coef1[0], &sz1[0], &lev1[0], &crit,
&icrit, &spar, &ispar, &maxit, &low, &high, &tol, &eps, &isetup, &ld4, &ldnk, &ier);
result = coef1[nk-1];
//free memory
delete [] xb;
delete [] yb;
delete [] wb;
delete [] knotb;
delete [] coef1;
delete [] lev1;
delete [] sz1;
return result;
}catch(exception& e) {
m->errorOut(e, "MothurMetastats", "smoothSpline");
exit(1);
}
}
/***********************************************************/
vector<double> MothurMetastats::sknot1(double* x, int n) {
try {
vector<double> knots;
int nk = nkn(n);
//R equivalent - rep(x[1L], 3L)
knots.push_back(x[0]); knots.push_back(x[0]); knots.push_back(x[0]);
//generate a sequence of nk equally spaced values from 1 to n. R equivalent = seq.int(1, n, length.out = nk)
vector<int> indexes = getSequence(0, n-1, nk);
for (int i = 0; i < indexes.size(); i++) { knots.push_back(x[indexes[i]]); }
//R equivalent - rep(x[n], 3L)
knots.push_back(x[n-1]); knots.push_back(x[n-1]); knots.push_back(x[n-1]);
return knots;
}catch(exception& e) {
m->errorOut(e, "MothurMetastats", "sknot1");
exit(1);
}
}
/***********************************************************/
vector<int> MothurMetastats::getSequence(int start, int end, int length) {
try {
vector<int> sequence;
double increment = (end-start) / (double) (length-1);
sequence.push_back(start);
for (int i = 1; i < length-1; i++) {
sequence.push_back(int(i*increment));
}
sequence.push_back(end);
return sequence;
}catch(exception& e) {
m->errorOut(e, "MothurMetastats", "getSequence");
exit(1);
}
}
/***********************************************************/
//not right, havent fully figured out the variable types yet...
int MothurMetastats::nkn(int n) {
try {
if (n < 50) { return n; }
else {
double a1 = log2(50);
double a2 = log2(100);
double a3 = log2(140);
double a4 = log2(200);
if (n < 200) {
return (int)pow(2.0, (a1 + (a2 - a1) * (n - 50)/(float)150));
}else if (n < 800) {
return (int)pow(2.0, (a2 + (a3 - a2) * (n - 200)/(float)600));
}else if (n < 3200) {
return (int)pow(2.0, (a3 + (a4 - a3) * (n - 800)/(float)2400));
}else {
return (int)pow((double)(200 + (n - 3200)), 0.2);
}
}
return 0;
}catch(exception& e) {
m->errorOut(e, "MothurMetastats", "nkn");
exit(1);
}
}
/***********************************************************/
|