X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=removerarecommand.cpp;h=24d5bef750e32e06493f7e91351bbf8af0b3099b;hb=6b32d112bb60e9f7eb6d4407a4eed4c49b67bced;hp=ded26bbae7b81082f45710c03066dd27145c95a9;hpb=035f86272c776e1cccaa47021e26782e49cd41e7;p=mothur.git diff --git a/removerarecommand.cpp b/removerarecommand.cpp index ded26bb..24d5bef 100644 --- a/removerarecommand.cpp +++ b/removerarecommand.cpp @@ -16,18 +16,18 @@ //********************************************************************************************************************** vector RemoveRareCommand::setParameters(){ try { - CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(plist); - CommandParameter prabund("rabund", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(prabund); - CommandParameter psabund("sabund", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(psabund); - CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pshared); - CommandParameter pcount("count", "InputTypes", "", "", "CountGroup", "none", "none",false,false); parameters.push_back(pcount); - CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "none", "none",false,false); parameters.push_back(pgroup); - CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups); - CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel); - CommandParameter pnseqs("nseqs", "Number", "", "0", "", "", "",false,true); parameters.push_back(pnseqs); - CommandParameter pbygroup("bygroup", "Boolean", "", "f", "", "", "",false,true); parameters.push_back(pbygroup); - CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir); - CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir); + CommandParameter plist("list", "InputTypes", "", "", "none", "atleast", "none","list",false,false,true); parameters.push_back(plist); + CommandParameter prabund("rabund", "InputTypes", "", "", "none", "atleast", "none","rabund",false,false,true); parameters.push_back(prabund); + CommandParameter psabund("sabund", "InputTypes", "", "", "none", "atleast", "none","sabund",false,false,true); parameters.push_back(psabund); + CommandParameter pshared("shared", "InputTypes", "", "", "none", "atleast", "none","shared",false,false,true); parameters.push_back(pshared); + CommandParameter pcount("count", "InputTypes", "", "", "CountGroup", "none", "none","count",false,false); parameters.push_back(pcount); + CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "none", "none","group",false,false); parameters.push_back(pgroup); + CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups); + CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel); + CommandParameter pnseqs("nseqs", "Number", "", "0", "", "", "","",false,true,true); parameters.push_back(pnseqs); + CommandParameter pbygroup("bygroup", "Boolean", "", "f", "", "", "","",false,false); parameters.push_back(pbygroup); + 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); } @@ -60,31 +60,25 @@ string RemoveRareCommand::getHelpString(){ } } //********************************************************************************************************************** -string RemoveRareCommand::getOutputFileNameTag(string type, string inputName=""){ - try { - string outputFileName = ""; - map >::iterator it; +string RemoveRareCommand::getOutputPattern(string type) { + try { + string pattern = ""; - //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 == "rabund") { outputFileName = "pick" + m->getExtension(inputName); } - else if (type == "sabund") { outputFileName = "pick" + m->getExtension(inputName); } - else if (type == "shared") { outputFileName = "pick" + m->getExtension(inputName); } - else if (type == "group") { outputFileName = "pick" + m->getExtension(inputName); } - else if (type == "count") { outputFileName = "pick" + m->getExtension(inputName); } - else if (type == "list") { outputFileName = "pick" + m->getExtension(inputName); } - 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, "RemoveRareCommand", "getOutputFileNameTag"); - exit(1); - } + if (type == "rabund") { pattern = "[filename],pick,[extension]"; } + else if (type == "sabund") { pattern = "[filename],pick,[extension]"; } + else if (type == "group") { pattern = "[filename],pick,[extension]"; } + else if (type == "count") { pattern = "[filename],pick,[extension]"; } + else if (type == "list") { pattern = "[filename],[tag],pick,[extension]"; } + else if (type == "shared") { pattern = "[filename],[tag],pick,[extension]"; } + else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; } + + return pattern; + } + catch(exception& e) { + m->errorOut(e, "RemoveRareCommand", "getOutputPattern"); + exit(1); + } } - //********************************************************************************************************************** RemoveRareCommand::RemoveRareCommand(){ try { @@ -350,17 +344,7 @@ int RemoveRareCommand::execute(){ //********************************************************************************************************************** int RemoveRareCommand::processList(){ try { - string thisOutputDir = outputDir; - if (outputDir == "") { thisOutputDir += m->hasPath(listfile); } - string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + getOutputFileNameTag("list", listfile); - string outputGroupFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + getOutputFileNameTag("group", groupfile); - string outputCountFileName = thisOutputDir + m->getRootName(m->getSimpleName(countfile)) + getOutputFileNameTag("count", countfile); - - ofstream out, outGroup; - m->openOutputFile(outputFileName, out); - - bool wroteSomething = false; - + //you must provide a label because the names in the listfile need to be consistent string thisLabel = ""; if (allLines) { m->mothurOut("For the listfile you must select one label, using first label in your listfile."); m->mothurOutEndLine(); } @@ -399,6 +383,26 @@ int RemoveRareCommand::processList(){ list = input.getListVector(lastLabel); } } + + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(listfile); } + map variables; + variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(listfile)); + variables["[extension]"] = m->getExtension(listfile); + variables["[tag]"] = list->getLabel(); + string outputFileName = getOutputFileName("list", variables); + variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)); + variables["[extension]"] = m->getExtension(groupfile); + string outputGroupFileName = getOutputFileName("group", variables); + variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile)); + variables["[extension]"] = m->getExtension(countfile); + string outputCountFileName = getOutputFileName("count", variables); + + ofstream out, outGroup; + m->openOutputFile(outputFileName, out); + + bool wroteSomething = false; + //if groupfile is given then use it GroupMap* groupMap; @@ -410,7 +414,7 @@ int RemoveRareCommand::processList(){ util.setGroups(Groups, namesGroups); m->openOutputFile(outputGroupFileName, outGroup); }else if (countfile != "") { - ct.readTable(countfile); + ct.readTable(countfile, true, false); if (ct.hasGroupInfo()) { vector namesGroups = ct.getNamesOfGroups(); SharedUtil util; @@ -419,7 +423,11 @@ int RemoveRareCommand::processList(){ } - if (list != NULL) { + if (list != NULL) { + + vector binLabels = list->getLabels(); + vector newLabels; + //make a new list vector ListVector newList; newList.setLabel(list->getLabel()); @@ -478,6 +486,7 @@ int RemoveRareCommand::processList(){ if (binsize > nseqs) { //keep bin newList.push_back(saveBinNames); + newLabels.push_back(binLabels[i]); if (groupfile != "") { for(int k = 0; k < newGroupFile.size(); k++) { outGroup << newGroupFile[k] << endl; } } else if (countfile != "") { for(int k = 0; k < newGroupFile.size(); k++) { ct.remove(newGroupFile[k]); } } }else { if (countfile != "") { for(int k = 0; k < names.size(); k++) { ct.remove(names[k]); } } } @@ -486,7 +495,9 @@ int RemoveRareCommand::processList(){ //print new listvector if (newList.getNumBins() != 0) { wroteSomething = true; - newList.print(out); + newList.setLabels(newLabels); + newList.printHeaders(out); + newList.print(out); } } @@ -519,7 +530,10 @@ int RemoveRareCommand::processSabund(){ try { string thisOutputDir = outputDir; if (outputDir == "") { thisOutputDir += m->hasPath(sabundfile); } - string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile)) + getOutputFileNameTag("sabund", sabundfile); + map variables; + variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile)); + variables["[extension]"] = m->getExtension(sabundfile); + string outputFileName = getOutputFileName("sabund", variables); outputTypes["sabund"].push_back(outputFileName); outputNames.push_back(outputFileName); ofstream out; @@ -618,7 +632,10 @@ int RemoveRareCommand::processRabund(){ try { string thisOutputDir = outputDir; if (outputDir == "") { thisOutputDir += m->hasPath(rabundfile); } - string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile)) + getOutputFileNameTag("rabund", rabundfile); + map variables; + variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile)); + variables["[extension]"] = m->getExtension(rabundfile); + string outputFileName = getOutputFileName("rabund", variables); outputTypes["rabund"].push_back(outputFileName); outputNames.push_back(outputFileName); ofstream out; @@ -723,14 +740,6 @@ int RemoveRareCommand::processShared(){ try { m->setGroups(Groups); - string thisOutputDir = outputDir; - if (outputDir == "") { thisOutputDir += m->hasPath(sharedfile); } - string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + getOutputFileNameTag("shared", sharedfile); - outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName); - - ofstream out; - m->openOutputFile(outputFileName, out); - //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label. InputData input(sharedfile, "sharedfile"); vector lookup = input.getSharedRAbundVectors(); @@ -740,7 +749,7 @@ int RemoveRareCommand::processShared(){ while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) { - if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } out.close(); return 0; } + if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; } if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){ @@ -748,8 +757,7 @@ int RemoveRareCommand::processShared(){ processedLabels.insert(lookup[0]->getLabel()); userLabels.erase(lookup[0]->getLabel()); - if (!m->printedHeaders) { lookup[0]->printHeaders(out); } - processLookup(lookup, out); + processLookup(lookup); } if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) { @@ -762,8 +770,7 @@ int RemoveRareCommand::processShared(){ processedLabels.insert(lookup[0]->getLabel()); userLabels.erase(lookup[0]->getLabel()); - if (!m->printedHeaders) { lookup[0]->printHeaders(out); } - processLookup(lookup, out); + processLookup(lookup); //restore real lastlabel to save below lookup[0]->setLabel(saveLabel); @@ -775,7 +782,7 @@ int RemoveRareCommand::processShared(){ lookup = input.getSharedRAbundVectors(); } - if (m->control_pressed) { out.close(); return 0; } + if (m->control_pressed) { return 0; } //output error messages about any remaining user labels set::iterator it; @@ -796,9 +803,7 @@ int RemoveRareCommand::processShared(){ lookup = input.getSharedRAbundVectors(lastLabel); m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); - - if (!m->printedHeaders) { lookup[0]->printHeaders(out); } - processLookup(lookup, out); + processLookup(lookup); for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } } @@ -811,10 +816,23 @@ int RemoveRareCommand::processShared(){ } } //********************************************************************************************************************** -int RemoveRareCommand::processLookup(vector& lookup, ofstream& out){ +int RemoveRareCommand::processLookup(vector& lookup){ try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(sharedfile); } + map variables; + variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)); + variables["[extension]"] = m->getExtension(sharedfile); + variables["[tag]"] = lookup[0]->getLabel(); + string outputFileName = getOutputFileName("shared", variables); + outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName); + + ofstream out; + m->openOutputFile(outputFileName, out); + vector newRabunds; newRabunds.resize(lookup.size()); + vector headers; for (int i = 0; i < lookup.size(); i++) { newRabunds[i].setGroup(lookup[i]->getGroup()); newRabunds[i].setLabel(lookup[i]->getLabel()); @@ -826,7 +844,7 @@ int RemoveRareCommand::processLookup(vector& lookup, ofstre for (int i = 0; i < lookup[0]->getNumBins(); i++) { bool allZero = true; - if (m->control_pressed) { return 0; } + if (m->control_pressed) { out.close(); return 0; } //for each group for (int j = 0; j < lookup.size(); j++) { @@ -842,12 +860,13 @@ int RemoveRareCommand::processLookup(vector& lookup, ofstre //eliminates zero otus if (allZero) { for (int j = 0; j < newRabunds.size(); j++) { newRabunds[j].pop_back(); } } + else { headers.push_back(m->currentSharedBinLabels[i]); } } }else { //for each otu for (int i = 0; i < lookup[0]->getNumBins(); i++) { - if (m->control_pressed) { return 0; } + if (m->control_pressed) { out.close(); return 0; } int totalAbund = 0; //get total otu abundance @@ -858,17 +877,23 @@ int RemoveRareCommand::processLookup(vector& lookup, ofstre //eliminates otus below rare cutoff if (totalAbund <= nseqs) { for (int j = 0; j < newRabunds.size(); j++) { newRabunds[j].pop_back(); } } + else { headers.push_back(m->currentSharedBinLabels[i]); } } } //do we have any otus above the rare cutoff - if (newRabunds[0].getNumBins() != 0) { + if (newRabunds[0].getNumBins() != 0) { + out << "label\tGroup\tnumOtus\t"; + for (int j = 0; j < headers.size(); j++) { out << headers[j] << '\t'; } + out << endl; for (int j = 0; j < newRabunds.size(); j++) { out << newRabunds[j].getLabel() << '\t' << newRabunds[j].getGroup() << '\t'; newRabunds[j].print(out); } } + out.close(); + return 0; } catch(exception& e) {