X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=corraxescommand.cpp;h=c4454a5181c719302011e6e98410408826183735;hb=a9dbc22713bfc056a797361dd757b1a5c98e1c01;hp=cd188b8a921a33aaac255852b5ab22d6008f605e;hpb=220dc345e493cddc569521111ce32ac4d965ab7f;p=mothur.git diff --git a/corraxescommand.cpp b/corraxescommand.cpp index cd188b8..c4454a5 100644 --- a/corraxescommand.cpp +++ b/corraxescommand.cpp @@ -9,6 +9,7 @@ #include "corraxescommand.h" #include "sharedutilities.h" +#include "linearalgebra.h" //********************************************************************************************************************** vector CorrAxesCommand::setParameters(){ @@ -55,12 +56,32 @@ string CorrAxesCommand::getHelpString(){ } } //********************************************************************************************************************** +string CorrAxesCommand::getOutputFileNameTag(string type, string inputName=""){ + try { + string outputFileName = ""; + map >::iterator it; + + //is this a type this command creates + it = outputTypes.find(type); + if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); } + else { + if (type == "corraxes") { outputFileName = "corr.axes"; } + else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; } + } + return outputFileName; + } + catch(exception& e) { + m->errorOut(e, "CorrAxesCommand", "getOutputFileNameTag"); + exit(1); + } +} +//********************************************************************************************************************** CorrAxesCommand::CorrAxesCommand(){ try { abort = true; calledHelp = true; setParameters(); vector tempOutNames; - outputTypes["corr.axes"] = tempOutNames; + outputTypes["corraxes"] = tempOutNames; } catch(exception& e) { m->errorOut(e, "CorrAxesCommand", "CorrAxesCommand"); @@ -91,7 +112,7 @@ CorrAxesCommand::CorrAxesCommand(string option) { } vector tempOutNames; - outputTypes["corr.axes"] = tempOutNames; + outputTypes["corraxes"] = tempOutNames; //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); @@ -158,7 +179,7 @@ CorrAxesCommand::CorrAxesCommand(string option) { pickedGroups = true; m->splitAtDash(groups, Groups); } - m->Groups = Groups; + m->setGroups(Groups); outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputFileName); } @@ -187,7 +208,7 @@ CorrAxesCommand::CorrAxesCommand(string option) { } string temp; temp = validParameter.validFile(parameters, "numaxes", false); if (temp == "not found"){ temp = "3"; } - convert(temp, numaxes); + m->mothurConvert(temp, numaxes); method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "pearson"; } @@ -265,8 +286,8 @@ int CorrAxesCommand::execute(){ // calc the r values // /************************************************************************************/ - string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + method + ".corr.axes"; - outputNames.push_back(outputFileName); outputTypes["corr.axes"].push_back(outputFileName); + string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + method + "." + getOutputFileNameTag("corraxes"); + outputNames.push_back(outputFileName); outputTypes["corraxes"].push_back(outputFileName); ofstream out; m->openOutputFile(outputFileName, out); out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint); @@ -275,7 +296,7 @@ int CorrAxesCommand::execute(){ if (metadatafile == "") { out << "OTU"; } else { out << "Feature"; } - for (int i = 0; i < numaxes; i++) { out << '\t' << "axis" << (i+1); } + for (int i = 0; i < numaxes; i++) { out << '\t' << "axis" << (i+1) << "\tp-value"; } out << "\tlength" << endl; if (method == "pearson") { calcPearson(axes, out); } @@ -304,6 +325,8 @@ int CorrAxesCommand::execute(){ int CorrAxesCommand::calcPearson(map >& axes, ofstream& out) { try { + LinearAlgebra linear; + //find average of each axis - X vector averageAxes; averageAxes.resize(numaxes, 0.0); for (map >::iterator it = axes.begin(); it != axes.end(); it++) { @@ -318,7 +341,7 @@ int CorrAxesCommand::calcPearson(map >& axes, ofstream& ou //for each otu for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) { - if (metadatafile == "") { out << i+1; } + if (metadatafile == "") { out << m->currentBinLabels[i]; } else { out << metadataLabels[i]; } //find the averages this otu - Y @@ -349,8 +372,15 @@ int CorrAxesCommand::calcPearson(map >& axes, ofstream& ou double denom = (sqrt(denomTerm1) * sqrt(denomTerm2)); r = numerator / denom; + + if (isnan(r) || isinf(r)) { r = 0.0; } + rValues[k] = r; out << '\t' << r; + + double sig = linear.calcPearsonSig(lookupFloat.size(), r); + + out << '\t' << sig; } double sum = 0; @@ -371,6 +401,9 @@ int CorrAxesCommand::calcPearson(map >& axes, ofstream& ou int CorrAxesCommand::calcSpearman(map >& axes, ofstream& out) { try { + LinearAlgebra linear; + vector sf; + //format data vector< map > tableX; tableX.resize(numaxes); map::iterator itTable; @@ -410,6 +443,7 @@ int CorrAxesCommand::calcSpearman(map >& axes, ofstream& o vector ties; int rankTotal = 0; + double sfTemp = 0.0; for (int j = 0; j < scores[i].size(); j++) { rankTotal += (j+1); ties.push_back(scores[i][j]); @@ -421,6 +455,8 @@ int CorrAxesCommand::calcSpearman(map >& axes, ofstream& o float thisrank = rankTotal / (float) ties.size(); rankAxes[ties[k].name].push_back(thisrank); } + int t = ties.size(); + sfTemp += (t*t*t-t); ties.clear(); rankTotal = 0; } @@ -433,13 +469,14 @@ int CorrAxesCommand::calcSpearman(map >& axes, ofstream& o } } } + sf.push_back(sfTemp); } //for each otu for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) { - if (metadatafile == "") { out << i+1; } + if (metadatafile == "") { out << m->currentBinLabels[i]; } else { out << metadataLabels[i]; } //find the ranks of this otu - Y @@ -467,6 +504,7 @@ int CorrAxesCommand::calcSpearman(map >& axes, ofstream& o sort(otuScores.begin(), otuScores.end(), compareSpearman); + double sg = 0.0; map rankOtus; vector ties; int rankTotal = 0; @@ -481,6 +519,8 @@ int CorrAxesCommand::calcSpearman(map >& axes, ofstream& o float thisrank = rankTotal / (float) ties.size(); rankOtus[ties[k].name] = thisrank; } + int t = ties.size(); + sg += (t*t*t-t); ties.clear(); rankTotal = 0; } @@ -515,9 +555,15 @@ int CorrAxesCommand::calcSpearman(map >& axes, ofstream& o p = (SX2 + SY2 - di) / (2.0 * sqrt((SX2*SY2))); + if (isnan(p) || isinf(p)) { p = 0.0; } + out << '\t' << p; pValues[j] = p; + + double sig = linear.calcSpearmanSig(n, sf[j], sg, di); + out << '\t' << sig; + } double sum = 0; @@ -538,6 +584,8 @@ int CorrAxesCommand::calcSpearman(map >& axes, ofstream& o int CorrAxesCommand::calcKendall(map >& axes, ofstream& out) { try { + LinearAlgebra linear; + //format data vector< vector > scores; scores.resize(numaxes); for (map >::iterator it = axes.begin(); it != axes.end(); it++) { @@ -581,7 +629,7 @@ int CorrAxesCommand::calcKendall(map >& axes, ofstream& ou //for each otu for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) { - if (metadatafile == "") { out << i+1; } + if (metadatafile == "") { out << m->currentBinLabels[i]; } else { out << metadataLabels[i]; } //find the ranks of this otu - Y @@ -651,10 +699,14 @@ int CorrAxesCommand::calcKendall(map >& axes, ofstream& ou } double p = (numCoor - numDisCoor) / (float) count; - + if (isnan(p) || isinf(p)) { p = 0.0; } + out << '\t' << p; pValues[j] = p; - + + double sig = linear.calcKendallSig(scores[j].size(), p); + + out << '\t' << sig; } double sum = 0; @@ -759,6 +811,8 @@ int CorrAxesCommand::eliminateZeroOTUS(vector& thisloo } //for each bin + vector newBinLabels; + string snumBins = toString(thislookup[0]->getNumBins()); for (int i = 0; i < thislookup[0]->getNumBins(); i++) { if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; } @@ -773,12 +827,25 @@ int CorrAxesCommand::eliminateZeroOTUS(vector& thisloo for (int j = 0; j < thislookup.size(); j++) { newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup()); } + + //if there is a bin label use it otherwise make one + string binLabel = "Otu"; + string sbinNumber = toString(i+1); + if (sbinNumber.length() < snumBins.length()) { + int diff = snumBins.length() - sbinNumber.length(); + for (int h = 0; h < diff; h++) { binLabel += "0"; } + } + binLabel += sbinNumber; + if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; } + + newBinLabels.push_back(binLabel); } } for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; } thislookup = newLookup; + m->currentBinLabels = newBinLabels; return 0; @@ -858,19 +925,14 @@ int CorrAxesCommand::getMetadata(){ m->openInputFile(metadatafile, in); string headerLine = m->getline(in); m->gobble(in); - istringstream iss (headerLine,istringstream::in); + vector pieces = m->splitWhiteSpace(headerLine); - //read the first label, because it refers to the groups - string columnLabel; - iss >> columnLabel; m->gobble(iss); - //save names of columns you are reading - while (!iss.eof()) { - iss >> columnLabel; m->gobble(iss); - metadataLabels.push_back(columnLabel); + for (int i = 1; i < pieces.size(); i++) { + metadataLabels.push_back(pieces[i]); } int count = metadataLabels.size(); - + //read rest of file while (!in.eof()) { @@ -879,7 +941,7 @@ int CorrAxesCommand::getMetadata(){ string group = ""; in >> group; m->gobble(in); groupNames.push_back(group); - + SharedRAbundFloatVector* tempLookup = new SharedRAbundFloatVector(); tempLookup->setGroup(group); tempLookup->setLabel("1"); @@ -887,7 +949,6 @@ int CorrAxesCommand::getMetadata(){ for (int i = 0; i < count; i++) { float temp = 0.0; in >> temp; - tempLookup->push_back(temp, group); } @@ -900,12 +961,13 @@ int CorrAxesCommand::getMetadata(){ //remove any groups the user does not want, and set globaldata->groups with only valid groups SharedUtil* util; util = new SharedUtil(); - - util->setGroups(m->Groups, groupNames); + Groups = m->getGroups(); + util->setGroups(Groups, groupNames); + m->setGroups(Groups); for (int i = 0; i < lookupFloat.size(); i++) { //if this sharedrabund is not from a group the user wants then delete it. - if (util->isValidGroup(lookupFloat[i]->getGroup(), m->Groups) == false) { + if (util->isValidGroup(lookupFloat[i]->getGroup(), m->getGroups()) == false) { delete lookupFloat[i]; lookupFloat[i] = NULL; lookupFloat.erase(lookupFloat.begin()+i); i--;