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
|
/*
* cooccurrencecommand.cpp
* Mothur
*
* Created by kiverson on 1/2/12.
* Copyright 2012 Schloss Lab. All rights reserved.
*
*/
#include "cooccurrencecommand.h"
//**********************************************************************************************************************
vector<string> CooccurrenceCommand::setParameters() {
try {
CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none","summary",false,true,true); parameters.push_back(pshared);
CommandParameter pmetric("metric", "Multiple", "cscore-checker-combo-vratio", "cscore", "", "", "","",false,false); parameters.push_back(pmetric);
CommandParameter pmatrix("matrixmodel", "Multiple", "sim1-sim2-sim3-sim4-sim5-sim6-sim7-sim8-sim9", "sim2", "", "", "","",false,false); parameters.push_back(pmatrix);
CommandParameter pruns("iters", "Number", "", "1000", "", "", "","",false,false); parameters.push_back(pruns);
CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
vector<string> myArray;
for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
return myArray;
}
catch(exception& e) {
m->errorOut(e, "CooccurrenceCommand", "setParameters");
exit(1);
}
}
//**********************************************************************************************************************
string CooccurrenceCommand::getHelpString(){
try {
string helpString = "The cooccurrence command calculates four metrics and tests their significance to assess whether presence-absence patterns are different than what one would expect by chance.";
helpString += "The cooccurrence command parameters are shared, metric, matrixmodel, iters, label and groups.";
helpString += "The matrixmodel parameter options are sim1, sim2, sim3, sim4, sim5, sim6, sim7, sim8 and sim9. Default=sim2";
helpString += "The metric parameter options are cscore, checker, combo and vratio. Default=cscore";
helpString += "The label parameter is used to analyze specific labels in your input.\n";
helpString += "The groups parameter allows you to specify which of the groups you would like analyzed.\n";
helpString += "The cooccurrence command should be in the following format: \n";
helpString += "cooccurrence(shared=yourSharedFile) \n";
helpString += "Example cooccurrence(shared=final.an.shared).\n";
helpString += "Note: No spaces between parameter labels (i.e. shared), '=' and parameters (i.e.yourShared).\n";
return helpString;
}
catch(exception& e) {
m->errorOut(e, "CooccurrenceCommand", "getHelpString");
exit(1);
}
}
//**********************************************************************************************************************
string CooccurrenceCommand::getOutputPattern(string type) {
try {
string pattern = "";
if (type == "summary") { pattern = "[filename],cooccurence.summary"; }
else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
return pattern;
}
catch(exception& e) {
m->errorOut(e, "CooccurrenceCommand", "getOutputPattern");
exit(1);
}
}
//**********************************************************************************************************************
CooccurrenceCommand::CooccurrenceCommand(){
try {
abort = true; calledHelp = true;
setParameters();
vector<string> tempOutNames;
outputTypes["summary"] = tempOutNames;
}
catch(exception& e) {
m->errorOut(e, "CooccurrenceCommand", "CooccurrenceCommand");
exit(1);
}
}
//**********************************************************************************************************************
CooccurrenceCommand::CooccurrenceCommand(string option) {
try {
abort = false; calledHelp = false;
allLines = 1;
//allow user to run help
if(option == "help") { help(); abort = true; calledHelp = true; }
else if(option == "citation") { citation(); abort = true; calledHelp = true;}
else {
vector<string> myArray = setParameters();
OptionParser parser(option);
map<string,string> parameters = parser.getParameters();
map<string,string>::iterator it;
ValidParameters validParameter;
//check to make sure all parameters are valid for command
for (it = parameters.begin(); it != parameters.end(); it++) {
if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
}
//if the user changes the input directory command factory will send this info to us in the output parameter
string inputDir = validParameter.validFile(parameters, "inputdir", false);
if (inputDir == "not found"){ inputDir = ""; }
else {
string path;
it = parameters.find("shared");
//user has given a template file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["shared"] = inputDir + it->second; }
}
}
vector<string> tempOutNames;
outputTypes["summary"] = tempOutNames;
//check for optional parameter and set defaults
// ...at some point should added some additional type checking...
label = validParameter.validFile(parameters, "label", false);
if (label == "not found") { label = ""; }
else {
if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
else { allLines = 1; }
}
//get shared file
sharedfile = validParameter.validFile(parameters, "shared", true);
if (sharedfile == "not open") { sharedfile = ""; abort = true; }
else if (sharedfile == "not found") {
//if there is a current shared file, use it
sharedfile = m->getSharedFile();
if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
else { m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
}else { m->setSharedFile(sharedfile); }
//if the user changes the output directory command factory will send this info to us in the output parameter
outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(sharedfile); }
metric = validParameter.validFile(parameters, "metric", false); if (metric == "not found") { metric = "cscore"; }
if ((metric != "cscore") && (metric != "checker") && (metric != "combo") && (metric != "vratio")) {
m->mothurOut("[ERROR]: " + metric + " is not a valid metric option for the cooccurrence command. Choices are cscore, checker, combo, vratio."); m->mothurOutEndLine(); abort = true;
}
matrix = validParameter.validFile(parameters, "matrixmodel", false); if (matrix == "not found") { matrix = "sim2"; }
if ((matrix != "sim1") && (matrix != "sim2") && (matrix != "sim3") && (matrix != "sim4") && (matrix != "sim5" ) && (matrix != "sim6" ) && (matrix != "sim7" ) && (matrix != "sim8" ) && (matrix != "sim9" )) {
m->mothurOut("[ERROR]: " + matrix + " is not a valid matrix option for the cooccurrence command. Choices are sim1, sim2, sim3, sim4, sim5, sim6, sim7, sim8, sim9."); m->mothurOutEndLine(); abort = true;
}
groups = validParameter.validFile(parameters, "groups", false);
if (groups == "not found") { groups = ""; }
else {
m->splitAtDash(groups, Groups);
}
m->setGroups(Groups);
string temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
m->mothurConvert(temp, runs);
}
}
catch(exception& e) {
m->errorOut(e, "CooccurrenceCommand", "CooccurrenceCommand");
exit(1);
}
}
//**********************************************************************************************************************
int CooccurrenceCommand::execute(){
try {
if (abort == true) { if (calledHelp) { return 0; } return 2; }
InputData* input = new InputData(sharedfile, "sharedfile");
vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
string lastLabel = lookup[0]->getLabel();
//if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
set<string> processedLabels;
set<string> userLabels = labels;
ofstream out;
map<string, string> variables;
variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile));
string outputFileName = getOutputFileName("summary", variables);
m->openOutputFile(outputFileName, out);
outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
out << "metric\tlabel\tScore\tzScore\tstandardDeviation\tnp_Pvalue\n";
//as long as you are not at the end of the file or done wih the lines you want
while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } delete input; out.close(); m->mothurRemove(outputFileName); return 0; }
if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
getCooccurrence(lookup, out);
processedLabels.insert(lookup[0]->getLabel());
userLabels.erase(lookup[0]->getLabel());
}
if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
string saveLabel = lookup[0]->getLabel();
for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
lookup = input->getSharedRAbundVectors(lastLabel);
m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
getCooccurrence(lookup, out);
processedLabels.insert(lookup[0]->getLabel());
userLabels.erase(lookup[0]->getLabel());
//restore real lastlabel to save below
lookup[0]->setLabel(saveLabel);
}
lastLabel = lookup[0]->getLabel();
//prevent memory leak
for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
if (m->control_pressed) { outputTypes.clear(); delete input; out.close(); m->mothurRemove(outputFileName); return 0; }
//get next line to process
lookup = input->getSharedRAbundVectors();
}
if (m->control_pressed) { delete input; out.close(); m->mothurRemove(outputFileName); return 0; }
//output error messages about any remaining user labels
set<string>::iterator it;
bool needToRun = false;
for (it = userLabels.begin(); it != userLabels.end(); it++) {
m->mothurOut("Your file does not include the label " + *it);
if (processedLabels.count(lastLabel) != 1) {
m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
needToRun = true;
}else {
m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
}
}
//run last label if you need to
if (needToRun == true) {
for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
lookup = input->getSharedRAbundVectors(lastLabel);
m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
getCooccurrence(lookup, out);
for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
}
out.close();
//reset groups parameter
delete input;
m->clearGroups();
m->mothurOutEndLine();
m->mothurOut("Output File Names: "); m->mothurOutEndLine();
m->mothurOut(outputFileName); m->mothurOutEndLine();
m->mothurOutEndLine();
return 0;
}
catch(exception& e) {
m->errorOut(e, "CooccurrenceCommand", "execute");
exit(1);
}
}
//**********************************************************************************************************************
int CooccurrenceCommand::getCooccurrence(vector<SharedRAbundVector*>& thisLookUp, ofstream& out){
try {
int numOTUS = thisLookUp[0]->getNumBins();
if(numOTUS < 2) {
m->mothurOut("Not enough OTUs for co-occurrence analysis, skipping"); m->mothurOutEndLine();
return 0;
}
vector< vector<int> > co_matrix; co_matrix.resize(thisLookUp[0]->getNumBins());
for (int i = 0; i < thisLookUp[0]->getNumBins(); i++) { co_matrix[i].resize((thisLookUp.size()), 0); }
vector<int> columntotal; columntotal.resize(thisLookUp.size(), 0);
vector<int> rowtotal; rowtotal.resize(numOTUS, 0);
for (int i = 0; i < thisLookUp.size(); i++) { //nrows in the shared file
for (int j = 0; j < thisLookUp[i]->getNumBins(); j++) { //cols of original shared file
if (m->control_pressed) { return 0; }
int abund = thisLookUp[i]->getAbundance(j);
if(abund > 0) {
co_matrix[j][i] = 1;
rowtotal[j]++;
columntotal[i]++;
}
}
}
//nrows is ncols of inital matrix. All the functions need this value. They assume the transposition has already taken place and nrows and ncols refer to that matrix.
//comatrix and initmatrix are still vectors of vectors of ints as in the original script. The abundancevector is only what was read in ie not a co-occurrence matrix!
int nrows = numOTUS;//rows of inital matrix
int ncols = thisLookUp.size();//groups
double initscore = 0.0;
vector<double> stats;
vector<double> probabilityMatrix; probabilityMatrix.resize(ncols * nrows, 0);
vector<vector<int> > nullmatrix(nrows, vector<int>(ncols, 0));
TrialSwap2 trial;
int n = accumulate( columntotal.begin(), columntotal.end(), 0 );
//============================================================
//generate a probability matrix. Only do this once.
float start = 0.0;
if (matrix == "sim1") {
for(int i=0;i<nrows;i++) {
for(int j=0;j<ncols;j++) {
probabilityMatrix[ncols * i + j] = start + 1/double(nrows*ncols);
start = start + 1/double(nrows*ncols);
}
}
}
//don't need a prob matrix because we just shuffle the rows, may use this in the future
else if (matrix == "sim2") { }
// for(int i=0;i<nrows;i++) {
// start = 0.0;
// for(int j=0;j<ncols;j++) {
// probabilityMatrix[ncols * i + j] = start + 1/double(ncols);
// start = start + 1/double(ncols);
// }
// }
// }
else if (matrix == "sim3") {
for(int j=0;j<ncols;j++) {
start = 0.0;
for(int i=0;i<nrows;i++) {
probabilityMatrix[ncols * i + j] = start + 1/double(nrows);
start = start + 1/double(nrows);
}
}
}
else if (matrix == "sim4") {
for(int i=0;i<nrows;i++) {
start = 0.0;
for(int j=0;j<ncols;j++) {
probabilityMatrix[ncols * i + j] = start + columntotal[j]/double(n);
start = start + columntotal[j]/double(n);
}
}
}
else if (matrix == "sim5") {
for(int j=0;j<ncols;j++) {
start = 0.0;
for(int i=0;i<nrows;i++) {
probabilityMatrix[ncols * i + j] = start + rowtotal[i]/double(n);
start = start + rowtotal[i]/double(n);
}
}
}
else if (matrix == "sim6") {
for(int i=0;i<nrows;i++) {
for(int j=0;j<ncols;j++) {
probabilityMatrix[ncols * i + j] = start + columntotal[j]/double(n*nrows);
start = start + columntotal[j]/double(n*nrows);
}
}
}
else if (matrix == "sim7") {
for(int i=0;i<nrows;i++) {
for(int j=0;j<ncols;j++) {
probabilityMatrix[ncols * i + j] = start + rowtotal[i]/double(n*ncols);
start = start + rowtotal[i]/double(n*ncols);
}
}
}
else if (matrix == "sim8") {
for(int i=0;i<nrows;i++) {
for(int j=0;j<ncols;j++) {
probabilityMatrix[ncols * i + j] = start + (rowtotal[i]*columntotal[j])/double(n*n);
start = start + (rowtotal[i]*columntotal[j])/double(n*n);
}
}
}
else if (matrix == "sim9" || matrix == "sim2") { }
else {
m->mothurOut("[ERROR]: No model selected! \n");
m->control_pressed = true;
}
//co_matrix is the transposed shared file, initmatrix is the original shared file
if (metric == "cscore") { initscore = trial.calc_c_score(co_matrix, rowtotal, ncols, nrows); }
else if (metric == "checker") { initscore = trial.calc_checker(co_matrix, rowtotal, ncols, nrows); }
else if (metric == "vratio") { initscore = trial.calc_vratio(nrows, ncols, rowtotal, columntotal); }
else if (metric == "combo") { initscore = trial.calc_combo(nrows, ncols, co_matrix); }
else { m->mothurOut("[ERROR]: No metric selected!\n"); m->control_pressed = true; return 1; }
m->mothurOut("Initial c score: " + toString(initscore)); m->mothurOutEndLine();
double previous;
double current;
double randnum;
int count;
//burn-in for sim9
if(matrix == "sim9") {
for(int i=0;i<10000;i++) trial.swap_checkerboards (co_matrix, ncols, nrows);
}
//populate null matrix from probability matrix, do this a lot.
for(int k=0;k<runs;k++){
nullmatrix.clear();
//zero-fill the null matrix
nullmatrix.assign(nrows, vector<int>(ncols, 0));
if(matrix == "sim1" || matrix == "sim6" || matrix == "sim8" || matrix == "sim7") {
count = 0;
while(count < n) {
if (m->control_pressed) { return 0; }
nextnum2:
previous = 0.0;
randnum = rand() / double(RAND_MAX);
for(int i=0;i<nrows;i++) {
for(int j=0;j<ncols;j++) {
current = probabilityMatrix[ncols * i + j];
if(randnum <= current && randnum > previous) {
nullmatrix[i][j] = 1;
count++;
if (count > n) break;
else
goto nextnum2;
}
previous = current;
}
}
}
}
else if (matrix == "sim2") {
for(int i=0;i<nrows;i++) {
random_shuffle( co_matrix[i].begin(), co_matrix[i].end() );
}
//do this for the scoring since those all have nullmatrix as a parameter
//nullmatrix gets cleared at the begining of each run
nullmatrix = co_matrix;
}
else if(matrix == "sim4") {
for(int i=0;i<nrows;i++) {
count = 0;
while(count < rowtotal[i]) {
previous = 0.0;
if (m->control_pressed) { return 0; }
randnum = rand() / double(RAND_MAX);
for(int j=0;j<ncols;j++) {
current = probabilityMatrix[ncols * i + j];
if(randnum <= current && randnum > previous && nullmatrix[i][j] != 1) {
nullmatrix[i][j] = 1;
count++;
previous = 0.0;
break;
}
previous = current;
}
}
}
}
else if(matrix == "sim3" || matrix == "sim5") {
//columns
for(int j=0;j<ncols;j++) {
count = 0;
while(count < columntotal[j]) {
if (m->control_pressed) { return 0; }
randnum = rand() / double(RAND_MAX);
for(int i=0;i<nrows;i++) {
current = probabilityMatrix[ncols * i + j];
if(randnum <= current && randnum > previous && nullmatrix[i][j] != 1) {
nullmatrix[i][j] = 1;
count++;
previous = 0.0;
break;
}
previous = current;
}
}
}
}
//swap_checkerboards takes the original matrix and swaps checkerboards
else if(matrix == "sim9") {
trial.swap_checkerboards (co_matrix, ncols, nrows);
nullmatrix = co_matrix;
}
else {
m->mothurOut("[ERROR]: No null model selected!\n\n"); m->control_pressed = true;
return 1;
}
//run metric on null matrix and add score to the stats vector
if (metric == "cscore"){
stats.push_back(trial.calc_c_score(nullmatrix, rowtotal, ncols, nrows));
}
else if (metric == "checker") {
stats.push_back(trial.calc_checker(nullmatrix, rowtotal, ncols, nrows));
}
else if (metric == "vratio") {
stats.push_back(trial.calc_vratio(nrows, ncols, rowtotal, columntotal));
}
else if (metric == "combo") {
stats.push_back(trial.calc_combo(nrows, ncols, nullmatrix));
}
else {
m->mothurOut("[ERROR]: No metric selected!\n\n"); m->control_pressed = true;
return 1;
}
}
double total = 0.0;
for (int i=0; i<stats.size();i++) { total+=stats[i]; }
double nullMean = double (total/(double)stats.size());
m->mothurOutEndLine(); m->mothurOut("average metric score: " + toString(nullMean)); m->mothurOutEndLine();
//calc_p_value is not a statistical p-value, it's just the average that are either > or < the initscore.
//All it does is show what is expected in a competitively structured community
//zscore is output so p-value can be looked up in a ztable
double pvalue = 0.0;
if (metric == "cscore" || metric == "checker") { pvalue = trial.calc_pvalue_greaterthan (stats, initscore); }
else{ pvalue = trial.calc_pvalue_lessthan (stats, initscore); }
double sd = trial.getSD(runs, stats, nullMean);
double zscore = trial.get_zscore(sd, nullMean, initscore);
m->mothurOut("zscore: " + toString(zscore)); m->mothurOutEndLine();
m->mothurOut("standard deviation: " + toString(sd)); m->mothurOutEndLine();
m->mothurOut("non-parametric p-value: " + toString(pvalue)); m->mothurOutEndLine();
out << metric << '\t' << thisLookUp[0]->getLabel() << '\t' << nullMean << '\t' << zscore << '\t' << sd << '\t' << pvalue << endl;
return 0;
}
catch(exception& e) {
m->errorOut(e, "CooccurrenceCommand", "Cooccurrence");
exit(1);
}
}
//**********************************************************************************************************************
|