X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=removerarecommand.cpp;h=a551681fb6721e4b3670fe912e1b6bfe909dd388;hp=0c656a053a7293f4ef7fa3325edead27339c9196;hb=df7e3ff9f68ef157b0328a2d353c3258c5d45d89;hpb=c69e2e9749626cfbf1d6cb0125ae94f869e00b18 diff --git a/removerarecommand.cpp b/removerarecommand.cpp index 0c656a0..a551681 100644 --- a/removerarecommand.cpp +++ b/removerarecommand.cpp @@ -14,70 +14,101 @@ #include "inputdata.h" //********************************************************************************************************************** -vector RemoveRareCommand::getValidParameters(){ +vector RemoveRareCommand::setParameters(){ try { - string Array[] = {"rabund","sabund", "group", "list", "shared","bygroup","nseqs","groups","label","outputdir","inputdir"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + 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); } return myArray; } catch(exception& e) { - m->errorOut(e, "RemoveRareCommand", "getValidParameters"); + m->errorOut(e, "RemoveRareCommand", "setParameters"); exit(1); } } //********************************************************************************************************************** -RemoveRareCommand::RemoveRareCommand(){ +string RemoveRareCommand::getHelpString(){ try { - abort = true; calledHelp = true; - vector tempOutNames; - outputTypes["rabund"] = tempOutNames; - outputTypes["sabund"] = tempOutNames; - outputTypes["list"] = tempOutNames; - outputTypes["group"] = tempOutNames; - outputTypes["shared"] = tempOutNames; + string helpString = ""; + helpString += "The remove.rare command parameters are list, rabund, sabund, shared, group, count, label, groups, bygroup and nseqs.\n"; + helpString += "The remove.rare command reads one of the following file types: list, rabund, sabund or shared file. It outputs a new file after removing the rare otus.\n"; + helpString += "The groups parameter allows you to specify which of the groups you would like analyzed. Default=all. You may separate group names with dashes.\n"; + helpString += "The label parameter is used to analyze specific labels in your input. default=all. You may separate label names with dashes.\n"; + helpString += "The bygroup parameter is only valid with the shared file. default=f, meaning remove any OTU that has nseqs or fewer sequences across all groups.\n"; + helpString += "bygroups=T means remove any OTU that has nseqs or fewer sequences in each group (if groupA has 1 sequence and group B has 100 sequences in OTUZ and nseqs=1, then set the groupA count for OTUZ to 0 and keep groupB's count at 100.) \n"; + helpString += "The nseqs parameter allows you to set the cutoff for an otu to be deemed rare. It is required.\n"; + helpString += "The remove.rare command should be in the following format: remove.rare(shared=yourSharedFile, nseqs=yourRareCutoff).\n"; + helpString += "Example remove.rare(shared=amazon.fn.shared, nseqs=2).\n"; + helpString += "Note: No spaces between parameter labels (i.e. shared), '=' and parameters (i.e.yourSharedFile).\n"; + return helpString; } catch(exception& e) { - m->errorOut(e, "RemoveRareCommand", "RemoveRareCommand"); + m->errorOut(e, "RemoveRareCommand", "getHelpString"); exit(1); } } //********************************************************************************************************************** -vector RemoveRareCommand::getRequiredParameters(){ - try { - string Array[] = {"nseqs"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); - return myArray; - } - catch(exception& e) { - m->errorOut(e, "RemoveRareCommand", "getRequiredParameters"); - exit(1); - } +string RemoveRareCommand::getOutputPattern(string type) { + try { + string pattern = ""; + + 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],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); + } } //********************************************************************************************************************** -vector RemoveRareCommand::getRequiredFiles(){ +RemoveRareCommand::RemoveRareCommand(){ try { - vector myArray; - return myArray; + abort = true; calledHelp = true; + setParameters(); + vector tempOutNames; + outputTypes["rabund"] = tempOutNames; + outputTypes["sabund"] = tempOutNames; + outputTypes["list"] = tempOutNames; + outputTypes["group"] = tempOutNames; + outputTypes["count"] = tempOutNames; + outputTypes["shared"] = tempOutNames; } catch(exception& e) { - m->errorOut(e, "RemoveRareCommand", "getRequiredFiles"); + m->errorOut(e, "RemoveRareCommand", "RemoveRareCommand"); exit(1); } } //********************************************************************************************************************** RemoveRareCommand::RemoveRareCommand(string option) { try { - globaldata = GlobalData::getInstance(); 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 { - //valid paramters for this command - string Array[] = {"rabund","sabund", "group", "list", "shared", "bygroup", "nseqs","groups","label","outputdir","inputdir"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + vector myArray = setParameters(); OptionParser parser(option); map parameters = parser.getParameters(); @@ -97,6 +128,7 @@ RemoveRareCommand::RemoveRareCommand(string option) { outputTypes["list"] = tempOutNames; outputTypes["group"] = tempOutNames; outputTypes["shared"] = tempOutNames; + outputTypes["count"] = tempOutNames; //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 = ""; } @@ -145,31 +177,75 @@ RemoveRareCommand::RemoveRareCommand(string option) { //if the user has not given a path then, add inputdir. else leave path alone. if (path == "") { parameters["shared"] = inputDir + it->second; } } + + it = parameters.find("count"); + //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["count"] = inputDir + it->second; } + } } //check for file parameters listfile = validParameter.validFile(parameters, "list", true); if (listfile == "not open") { abort = true; } - else if (listfile == "not found") { listfile = ""; } + else if (listfile == "not found") { listfile = ""; } + else { m->setListFile(listfile); } sabundfile = validParameter.validFile(parameters, "sabund", true); if (sabundfile == "not open") { abort = true; } else if (sabundfile == "not found") { sabundfile = ""; } + else { m->setSabundFile(sabundfile); } rabundfile = validParameter.validFile(parameters, "rabund", true); if (rabundfile == "not open") { abort = true; } else if (rabundfile == "not found") { rabundfile = ""; } + else { m->setRabundFile(rabundfile); } groupfile = validParameter.validFile(parameters, "group", true); if (groupfile == "not open") { groupfile = ""; abort = true; } else if (groupfile == "not found") { groupfile = ""; } + else { m->setGroupFile(groupfile); } sharedfile = validParameter.validFile(parameters, "shared", true); if (sharedfile == "not open") { sharedfile = ""; abort = true; } else if (sharedfile == "not found") { sharedfile = ""; } - - if ((sabundfile == "") && (rabundfile == "") && (sharedfile == "") && (listfile == "")) { m->mothurOut("You must provide at least one of the following: rabund, sabund, shared or list."); m->mothurOutEndLine(); abort = true; } + else { m->setSharedFile(sharedfile); } + + countfile = validParameter.validFile(parameters, "count", true); + if (countfile == "not open") { countfile = ""; abort = true; } + else if (countfile == "not found") { countfile = ""; } + else { m->setCountTableFile(countfile); } + + if ((groupfile != "") && (countfile != "")) { + m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true; + } + + if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) { + //is there are current file available for any of these? + //give priority to shared, then list, then rabund, then sabund + //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 { + listfile = m->getListFile(); + if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); } + else { + rabundfile = m->getRabundFile(); + if (rabundfile != "") { m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); } + else { + sabundfile = m->getSabundFile(); + if (sabundfile != "") { m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); } + else { + m->mothurOut("No valid current files. You must provide a list, sabund, rabund or shared file."); m->mothurOutEndLine(); + abort = true; + } + } + } + } + } groups = validParameter.validFile(parameters, "groups", false); if (groups == "not found") { groups = "all"; } @@ -184,14 +260,14 @@ RemoveRareCommand::RemoveRareCommand(string option) { string temp = validParameter.validFile(parameters, "nseqs", false); if (temp == "not found") { m->mothurOut("nseqs is a required parameter."); m->mothurOutEndLine(); abort = true; } - else { convert(temp, nseqs); } + else { m->mothurConvert(temp, nseqs); } temp = validParameter.validFile(parameters, "bygroup", false); if (temp == "not found") { temp = "f"; } byGroup = m->isTrue(temp); if (byGroup && (sharedfile == "")) { m->mothurOut("The byGroup parameter is only valid with a shared file."); m->mothurOutEndLine(); } - if ((groupfile != "") && (listfile == "")) { m->mothurOut("A groupfile is only valid with a list file."); m->mothurOutEndLine(); groupfile = ""; } + if (((groupfile != "") || (countfile != "")) && (listfile == "")) { m->mothurOut("A group or count file is only valid with a list file."); m->mothurOutEndLine(); groupfile = ""; countfile = ""; } } } @@ -202,27 +278,6 @@ RemoveRareCommand::RemoveRareCommand(string option) { } //********************************************************************************************************************** -void RemoveRareCommand::help(){ - try { - m->mothurOut("The remove.rare command parameters are list, rabund, sabund, shared, group, label, groups, bygroup and nseqs.\n"); - m->mothurOut("The remove.rare command reads one of the following file types: list, rabund, sabund or shared file. It outputs a new file after removing the rare otus.\n"); - m->mothurOut("The groups parameter allows you to specify which of the groups you would like analyzed. Default=all. You may separate group names with dashes.\n"); - m->mothurOut("The label parameter is used to analyze specific labels in your input. default=all. You may separate label names with dashes.\n"); - m->mothurOut("The bygroup parameter is only valid with the shared file. default=f, meaning remove any OTU that has nseqs or fewer sequences across all groups.\n"); - m->mothurOut("bygroups=T means remove any OTU that has nseqs or fewer sequences in each group (if groupA has 1 sequence and group B has 100 sequences in OTUZ and nseqs=1, then set the groupA count for OTUZ to 0 and keep groupB's count at 100.) \n"); - m->mothurOut("The nseqs parameter allows you to set the cutoff for an otu to be deemed rare. It is required.\n"); - m->mothurOut("The remove.rare command should be in the following format: remove.rare(shared=yourSharedFile, nseqs=yourRareCutoff).\n"); - m->mothurOut("Example remove.rare(shared=amazon.fn.shared, nseqs=2).\n"); - m->mothurOut("Note: No spaces between parameter labels (i.e. shared), '=' and parameters (i.e.yourSharedFile).\n\n"); - } - catch(exception& e) { - m->errorOut(e, "RemoveRareCommand", "help"); - exit(1); - } -} - -//********************************************************************************************************************** - int RemoveRareCommand::execute(){ try { @@ -236,13 +291,45 @@ int RemoveRareCommand::execute(){ if (listfile != "") { processList(); } if (sharedfile != "") { processShared(); } - if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; } + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } if (outputNames.size() != 0) { m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine(); for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); } m->mothurOutEndLine(); + + //set rabund file as new current rabundfile + string current = ""; + itTypes = outputTypes.find("rabund"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); } + } + + itTypes = outputTypes.find("sabund"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); } + } + + itTypes = outputTypes.find("group"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); } + } + + itTypes = outputTypes.find("list"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); } + } + + itTypes = outputTypes.find("shared"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); } + } + + itTypes = outputTypes.find("count"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); } + } } return 0; @@ -259,9 +346,17 @@ int RemoveRareCommand::processList(){ try { string thisOutputDir = outputDir; if (outputDir == "") { thisOutputDir += m->hasPath(listfile); } - string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile); - string outputGroupFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile); - + map variables; + variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(listfile)); + variables["[extension]"] = m->getExtension(listfile); + 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); @@ -308,12 +403,21 @@ int RemoveRareCommand::processList(){ //if groupfile is given then use it GroupMap* groupMap; + CountTable ct; if (groupfile != "") { groupMap = new GroupMap(groupfile); groupMap->readMap(); SharedUtil util; - util.setGroups(Groups, groupMap->namesOfGroups); + vector namesGroups = groupMap->getNamesOfGroups(); + util.setGroups(Groups, namesGroups); m->openOutputFile(outputGroupFileName, outGroup); - } + }else if (countfile != "") { + ct.readTable(countfile, true); + if (ct.hasGroupInfo()) { + vector namesGroups = ct.getNamesOfGroups(); + SharedUtil util; + util.setGroups(Groups, namesGroups); + } + } if (list != NULL) { @@ -323,13 +427,14 @@ int RemoveRareCommand::processList(){ //for each bin for (int i = 0; i < list->getNumBins(); i++) { - if (m->control_pressed) { if (groupfile != "") { delete groupMap; outGroup.close(); remove(outputGroupFileName.c_str()); } out.close(); remove(outputFileName.c_str()); return 0; } + if (m->control_pressed) { if (groupfile != "") { delete groupMap; outGroup.close(); m->mothurRemove(outputGroupFileName); } out.close(); m->mothurRemove(outputFileName); return 0; } //parse out names that are in accnos file string binnames = list->get(i); vector names; string saveBinNames = binnames; m->splitAtComma(binnames, names); + int binsize = names.size(); vector newGroupFile; if (groupfile != "") { @@ -345,14 +450,38 @@ int RemoveRareCommand::processList(){ saveBinNames += names[k] + ","; } } - names = newNames; + names = newNames; binsize = names.size(); saveBinNames = saveBinNames.substr(0, saveBinNames.length()-1); - } + }else if (countfile != "") { + saveBinNames = ""; + binsize = 0; + for(int k = 0; k < names.size(); k++) { + if (ct.hasGroupInfo()) { + vector thisSeqsGroups = ct.getGroups(names[k]); + + int thisSeqsCount = 0; + for (int n = 0; n < thisSeqsGroups.size(); n++) { + if (m->inUsersGroups(thisSeqsGroups[n], Groups)) { + thisSeqsCount += ct.getGroupCount(names[k], thisSeqsGroups[n]); + } + } + binsize += thisSeqsCount; + //if you don't have any seqs from the groups the user wants, then remove you. + if (thisSeqsCount == 0) { newGroupFile.push_back(names[k]); } + else { saveBinNames += names[k] + ","; } + }else { + binsize += ct.getNumSeqs(names[k]); + saveBinNames += names[k] + ","; + } + } + saveBinNames = saveBinNames.substr(0, saveBinNames.length()-1); + } - if (names.size() > nseqs) { //keep bin + if (binsize > nseqs) { //keep bin newList.push_back(saveBinNames); - for(int k = 0; k < newGroupFile.size(); k++) { outGroup << newGroupFile[k] << endl; } - } + 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]); } } } } //print new listvector @@ -364,6 +493,17 @@ int RemoveRareCommand::processList(){ out.close(); if (groupfile != "") { outGroup.close(); outputTypes["group"].push_back(outputGroupFileName); outputNames.push_back(outputGroupFileName); } + if (countfile != "") { + if (ct.hasGroupInfo()) { + vector allGroups = ct.getNamesOfGroups(); + for (int i = 0; i < allGroups.size(); i++) { + if (!m->inUsersGroups(allGroups[i], Groups)) { ct.removeGroup(allGroups[i]); } + } + + } + ct.printTable(outputCountFileName); + outputTypes["count"].push_back(outputCountFileName); outputNames.push_back(outputCountFileName); + } if (wroteSomething == false) { m->mothurOut("Your file contains only rare sequences."); m->mothurOutEndLine(); } outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName); @@ -380,7 +520,10 @@ int RemoveRareCommand::processSabund(){ try { string thisOutputDir = outputDir; if (outputDir == "") { thisOutputDir += m->hasPath(sabundfile); } - string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile)) + "pick" + m->getExtension(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; @@ -479,7 +622,10 @@ int RemoveRareCommand::processRabund(){ try { string thisOutputDir = outputDir; if (outputDir == "") { thisOutputDir += m->hasPath(rabundfile); } - string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile)) + "pick" + m->getExtension(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; @@ -582,15 +728,7 @@ int RemoveRareCommand::processRabund(){ //********************************************************************************************************************** int RemoveRareCommand::processShared(){ try { - globaldata->Groups = Groups; - - string thisOutputDir = outputDir; - if (outputDir == "") { thisOutputDir += m->hasPath(sharedfile); } - string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + "pick" + m->getExtension(sharedfile); - outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName); - - ofstream out; - m->openOutputFile(outputFileName, out); + m->setGroups(Groups); //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"); @@ -601,7 +739,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){ @@ -609,7 +747,7 @@ int RemoveRareCommand::processShared(){ processedLabels.insert(lookup[0]->getLabel()); userLabels.erase(lookup[0]->getLabel()); - processLookup(lookup, out); + processLookup(lookup); } if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) { @@ -622,7 +760,7 @@ int RemoveRareCommand::processShared(){ processedLabels.insert(lookup[0]->getLabel()); userLabels.erase(lookup[0]->getLabel()); - processLookup(lookup, out); + processLookup(lookup); //restore real lastlabel to save below lookup[0]->setLabel(saveLabel); @@ -634,7 +772,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; @@ -655,8 +793,7 @@ int RemoveRareCommand::processShared(){ lookup = input.getSharedRAbundVectors(lastLabel); m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); - - processLookup(lookup, out); + processLookup(lookup); for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } } @@ -669,10 +806,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()); @@ -684,7 +834,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++) { @@ -700,12 +850,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->currentBinLabels[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 @@ -716,17 +867,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->currentBinLabels[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) {