X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=summarysharedcommand.cpp;h=fdec2ec483c67f1d37c23477d1411b5c97e8a215;hb=3504e4e2feeb05aabb0c79aa42cb696522030924;hp=77961f1c807387080b914af5059fea03d3844ff4;hpb=2009a1a1f47e7467094d844e7c07ab8ddf7bb447;p=mothur.git diff --git a/summarysharedcommand.cpp b/summarysharedcommand.cpp index 77961f1..fdec2ec 100644 --- a/summarysharedcommand.cpp +++ b/summarysharedcommand.cpp @@ -13,18 +13,18 @@ //********************************************************************************************************************** vector SummarySharedCommand::setParameters(){ try { - CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pshared); - CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel); - CommandParameter psubsample("subsample", "String", "", "", "", "", "",false,false); parameters.push_back(psubsample); - CommandParameter pdistance("distance", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pdistance); - CommandParameter pcalc("calc", "Multiple", "sharedchao-sharedsobs-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan-kstest-whittaker-sharednseqs-ochiai-anderberg-kulczynski-kulczynskicody-lennon-morisitahorn-braycurtis-odum-canberra-structeuclidean-structchord-hellinger-manhattan-structpearson-soergel-spearman-structkulczynski-speciesprofile-structchi2-hamming-gower-memchi2-memchord-memeuclidean-mempearson", "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan", "", "", "",true,false); parameters.push_back(pcalc); - CommandParameter poutput("output", "Multiple", "lt-square", "lt", "", "", "",false,false); parameters.push_back(poutput); - CommandParameter pall("all", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pall); - CommandParameter piters("iters", "Number", "", "1000", "", "", "",false,false); parameters.push_back(piters); - CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors); - CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups); - CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir); - CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir); + CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none","summary",false,true,true); parameters.push_back(pshared); + CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel); + CommandParameter psubsample("subsample", "String", "", "", "", "", "","phylip",false,false); parameters.push_back(psubsample); + CommandParameter pdistance("distance", "Boolean", "", "F", "", "", "","phylip",false,false); parameters.push_back(pdistance); + CommandParameter pcalc("calc", "Multiple", "sharedchao-sharedsobs-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan-kstest-whittaker-sharednseqs-ochiai-anderberg-kulczynski-kulczynskicody-lennon-morisitahorn-braycurtis-odum-canberra-structeuclidean-structchord-hellinger-manhattan-structpearson-soergel-spearman-structkulczynski-speciesprofile-structchi2-hamming-gower-memchi2-memchord-memeuclidean-mempearson", "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan", "", "", "","",true,false,true); parameters.push_back(pcalc); + CommandParameter poutput("output", "Multiple", "lt-square", "lt", "", "", "","",false,false); parameters.push_back(poutput); + CommandParameter pall("all", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(pall); + CommandParameter piters("iters", "Number", "", "1000", "", "", "","",false,false); parameters.push_back(piters); + CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors); + CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups); + CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir); + CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir); vector myArray; for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); } @@ -64,12 +64,29 @@ string SummarySharedCommand::getHelpString(){ } } //********************************************************************************************************************** +string SummarySharedCommand::getOutputPattern(string type) { + try { + string pattern = ""; + + if (type == "summary") { pattern = "[filename],summary-[filename],[tag],summary"; } + else if (type == "phylip") { pattern = "[filename],[calc],[distance],[outputtag],[tag2],dist"; } + else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; } + + return pattern; + } + catch(exception& e) { + m->errorOut(e, "SummarySharedCommand", "getOutputPattern"); + exit(1); + } +} +//********************************************************************************************************************** SummarySharedCommand::SummarySharedCommand(){ try { abort = true; calledHelp = true; setParameters(); vector tempOutNames; outputTypes["summary"] = tempOutNames; + outputTypes["phylip"] = tempOutNames; } catch(exception& e) { m->errorOut(e, "SummarySharedCommand", "SummarySharedCommand"); @@ -104,6 +121,7 @@ SummarySharedCommand::SummarySharedCommand(string option) { //initialize outputTypes vector tempOutNames; outputTypes["summary"] = tempOutNames; + outputTypes["phylip"] = 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); @@ -172,7 +190,9 @@ SummarySharedCommand::SummarySharedCommand(string option) { temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; } m->mothurConvert(temp, iters); - output = validParameter.validFile(parameters, "output", false); if(output == "not found"){ output = "lt"; } + output = validParameter.validFile(parameters, "output", false); + if(output == "not found"){ output = "lt"; } + else { createPhylip = true; } if ((output != "lt") && (output != "square")) { m->mothurOut(output + " is not a valid output form. Options are lt and square. I will use lt."); m->mothurOutEndLine(); output = "lt"; } temp = validParameter.validFile(parameters, "subsample", false); if (temp == "not found") { temp = "F"; } @@ -182,7 +202,7 @@ SummarySharedCommand::SummarySharedCommand(string option) { else { subsample = false; } } - if (subsample == false) { iters = 1; } + if (subsample == false) { iters = 0; } temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); } m->setProcessors(temp); @@ -294,7 +314,9 @@ int SummarySharedCommand::execute(){ if (abort == true) { if (calledHelp) { return 0; } return 2; } ofstream outputFileHandle, outAll; - string outputFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "shared.summary"; + map variables; + variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile)); + string outputFileName = getOutputFileName("summary",variables); //if the users entered no valid calculators don't execute command if (sumCalculators.size() == 0) { return 0; } @@ -325,7 +347,8 @@ int SummarySharedCommand::execute(){ outputFileHandle.close(); //create file and put column headers for multiple groups file - string outAllFileName = ((m->getRootName(sharedfile)) + "sharedmultiple.summary"); + variables["[tag]"]= "multiple"; + string outAllFileName = getOutputFileName("summary",variables); if (mult == true) { m->openOutputFile(outAllFileName, outAll); outputNames.push_back(outAllFileName); @@ -365,11 +388,38 @@ int SummarySharedCommand::execute(){ return 0; } /******************************************************/ - + if (subsample) { + if (subsampleSize == -1) { //user has not set size, set size = smallest samples size + subsampleSize = lookup[0]->getNumSeqs(); + for (int i = 1; i < lookup.size(); i++) { + int thisSize = lookup[i]->getNumSeqs(); + + if (thisSize < subsampleSize) { subsampleSize = thisSize; } + } + }else { + m->clearGroups(); + Groups.clear(); + vector temp; + for (int i = 0; i < lookup.size(); i++) { + if (lookup[i]->getNumSeqs() < subsampleSize) { + m->mothurOut(lookup[i]->getGroup() + " contains " + toString(lookup[i]->getNumSeqs()) + ". Eliminating."); m->mothurOutEndLine(); + delete lookup[i]; + }else { + Groups.push_back(lookup[i]->getGroup()); + temp.push_back(lookup[i]); + } + } + lookup = temp; + m->setGroups(Groups); + } + + if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); m->control_pressed = true; delete input; return 0; } + } + /******************************************************/ //comparison breakup to be used by different processes later - numGroups = m->getNumGroups(); + numGroups = lookup.size(); lines.resize(processors); for (int i = 0; i < processors; i++) { lines[i].start = int (sqrt(float(i)/float(processors)) * numGroups); @@ -527,11 +577,11 @@ int SummarySharedCommand::process(vector thisLookup, string vector< vector< vector > > calcDistsTotals; //each iter, one for each calc, then each groupCombos dists. this will be used to make .dist files vector< vector > calcDists; calcDists.resize(sumCalculators.size()); - for (int thisIter = 0; thisIter < iters; thisIter++) { + for (int thisIter = 0; thisIter < iters+1; thisIter++) { vector thisItersLookup = thisLookup; - if (subsample) { + if (subsample && (thisIter != 0)) { //we want the summary results for the whole dataset, then the subsampling SubSample sample; vector tempLabels; //dont need since we arent printing the sampled sharedRabunds @@ -710,18 +760,54 @@ int SummarySharedCommand::process(vector thisLookup, string #endif } - calcDistsTotals.push_back(calcDists); - if (subsample) { + if (subsample && (thisIter != 0)) { //we want the summary results for the whole dataset, then the subsampling + calcDistsTotals.push_back(calcDists); //clean up memory for (int i = 0; i < thisItersLookup.size(); i++) { delete thisItersLookup[i]; } thisItersLookup.clear(); - for (int i = 0; i < calcDists.size(); i++) { calcDists[i].clear(); } + }else { + if (createPhylip) { + for (int i = 0; i < calcDists.size(); i++) { + if (m->control_pressed) { break; } + + //initialize matrix + vector< vector > matrix; //square matrix to represent the distance + matrix.resize(thisLookup.size()); + for (int k = 0; k < thisLookup.size(); k++) { matrix[k].resize(thisLookup.size(), 0.0); } + + for (int j = 0; j < calcDists[i].size(); j++) { + int row = calcDists[i][j].seq1; + int column = calcDists[i][j].seq2; + double dist = calcDists[i][j].dist; + + matrix[row][column] = dist; + matrix[column][row] = dist; + } + + map variables; + variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile)); + variables["[calc]"] = sumCalculators[i]->getName(); + variables["[distance]"] = thisLookup[0]->getLabel(); + variables["[outputtag]"] = output; + variables["[tag2]"] = ""; + string distFileName = getOutputFileName("phylip",variables); + outputNames.push_back(distFileName); outputTypes["phylip"].push_back(distFileName); + ofstream outDist; + m->openOutputFile(distFileName, outDist); + outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint); + + printSims(outDist, matrix); + + outDist.close(); + } + } } + for (int i = 0; i < calcDists.size(); i++) { calcDists[i].clear(); } } - if (iters != 1) { + if (iters != 0) { //we need to find the average distance and standard deviation for each groups distance vector< vector > calcAverages; calcAverages.resize(sumCalculators.size()); @@ -799,7 +885,13 @@ int SummarySharedCommand::process(vector thisLookup, string stdmatrix[column][row] = stdDist; } - string distFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + sumCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + "." + output + ".ave.dist"; + map variables; + variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile)); + variables["[calc]"] = sumCalculators[i]->getName(); + variables["[distance]"] = thisLookup[0]->getLabel(); + variables["[outputtag]"] = output; + variables["[tag2]"] = "ave"; + string distFileName = getOutputFileName("phylip",variables); outputNames.push_back(distFileName); outputTypes["phylip"].push_back(distFileName); ofstream outAve; m->openOutputFile(distFileName, outAve); @@ -809,7 +901,8 @@ int SummarySharedCommand::process(vector thisLookup, string outAve.close(); - distFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + sumCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + "." + output + ".std.dist"; + variables["[tag2]"] = "std"; + distFileName = getOutputFileName("phylip",variables); outputNames.push_back(distFileName); outputTypes["phylip"].push_back(distFileName); ofstream outSTD; m->openOutputFile(distFileName, outSTD); @@ -820,38 +913,8 @@ int SummarySharedCommand::process(vector thisLookup, string outSTD.close(); } - }else { - if (createPhylip) { - for (int i = 0; i < calcDists.size(); i++) { - if (m->control_pressed) { break; } - - //initialize matrix - vector< vector > matrix; //square matrix to represent the distance - matrix.resize(thisLookup.size()); - for (int k = 0; k < thisLookup.size(); k++) { matrix[k].resize(thisLookup.size(), 0.0); } - - for (int j = 0; j < calcDists[i].size(); j++) { - int row = calcDists[i][j].seq1; - int column = calcDists[i][j].seq2; - double dist = calcDists[i][j].dist; - - matrix[row][column] = dist; - matrix[column][row] = dist; - } - - string distFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + sumCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + "." + output + ".dist"; - outputNames.push_back(distFileName); outputTypes["phylip"].push_back(distFileName); - ofstream outDist; - m->openOutputFile(distFileName, outDist); - outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint); - - printSims(outDist, matrix); - - outDist.close(); - } - } } - + return 0; } catch(exception& e) {