From e0b6040c7b6ad4ecdad4aa90b4326c0528379178 Mon Sep 17 00:00:00 2001 From: Sarah Westcott Date: Mon, 21 May 2012 14:00:07 -0400 Subject: [PATCH] added get.otulabels and remove.otulabels commands per user request --- Mothur.xcodeproj/project.pbxproj | 16 +- classifyotucommand.cpp | 12 +- commandfactory.cpp | 10 + getotulabelscommand.cpp | 381 +++++++++++++++++++++++++++++++ getotulabelscommand.h | 53 +++++ listotulabelscommand.cpp | 6 +- listotulabelscommand.h | 2 +- removeotulabelscommand.cpp | 380 ++++++++++++++++++++++++++++++ removeotulabelscommand.h | 55 +++++ 9 files changed, 908 insertions(+), 7 deletions(-) create mode 100644 getotulabelscommand.cpp create mode 100644 getotulabelscommand.h create mode 100644 removeotulabelscommand.cpp create mode 100644 removeotulabelscommand.h diff --git a/Mothur.xcodeproj/project.pbxproj b/Mothur.xcodeproj/project.pbxproj index f40cb4b..d98b284 100644 --- a/Mothur.xcodeproj/project.pbxproj +++ b/Mothur.xcodeproj/project.pbxproj @@ -11,6 +11,8 @@ 219C1DE41559BCCF004209F9 /* getcoremicrobiomecommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 219C1DE31559BCCD004209F9 /* getcoremicrobiomecommand.cpp */; }; 7E6BE10A12F710D8007ADDBE /* refchimeratest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7E6BE10912F710D8007ADDBE /* refchimeratest.cpp */; }; 8DD76FB00486AB0100D96B5E /* mothur.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = C6A0FF2C0290799A04C91782 /* mothur.1 */; }; + A70056E6156A93D000924A2D /* getotulabelscommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A70056E5156A93D000924A2D /* getotulabelscommand.cpp */; }; + A70056EB156AB6E500924A2D /* removeotulabelscommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A70056EA156AB6E500924A2D /* removeotulabelscommand.cpp */; }; A70332B712D3A13400761E33 /* makefile in Sources */ = {isa = PBXBuildFile; fileRef = A70332B512D3A13400761E33 /* makefile */; }; A713EBAC12DC7613000092AC /* readphylipvector.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A713EBAB12DC7613000092AC /* readphylipvector.cpp */; }; A713EBED12DC7C5E000092AC /* nmdscommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A713EBEC12DC7C5E000092AC /* nmdscommand.cpp */; }; @@ -370,6 +372,10 @@ 7E6BE10912F710D8007ADDBE /* refchimeratest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = refchimeratest.cpp; sourceTree = ""; }; 7E78911B135F3E8600E725D2 /* eachgapdistignorens.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = eachgapdistignorens.h; sourceTree = ""; }; 8DD76FB20486AB0100D96B5E /* mothur */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = mothur; sourceTree = BUILT_PRODUCTS_DIR; }; + A70056E5156A93D000924A2D /* getotulabelscommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = getotulabelscommand.cpp; sourceTree = ""; }; + A70056E8156A93E300924A2D /* getotulabelscommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = getotulabelscommand.h; sourceTree = ""; }; + A70056E9156AB6D400924A2D /* removeotulabelscommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = removeotulabelscommand.h; sourceTree = ""; }; + A70056EA156AB6E500924A2D /* removeotulabelscommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = removeotulabelscommand.cpp; sourceTree = ""; }; A70332B512D3A13400761E33 /* makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; path = makefile; sourceTree = ""; }; A713EBAA12DC7613000092AC /* readphylipvector.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = readphylipvector.h; sourceTree = ""; }; A713EBAB12DC7613000092AC /* readphylipvector.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = readphylipvector.cpp; sourceTree = ""; }; @@ -1271,6 +1277,8 @@ A7E9B6F412D37EC400DA6239 /* getgroupscommand.cpp */, A7E9B6F712D37EC400DA6239 /* getlabelcommand.h */, A7E9B6F612D37EC400DA6239 /* getlabelcommand.cpp */, + A70056E8156A93E300924A2D /* getotulabelscommand.h */, + A70056E5156A93D000924A2D /* getotulabelscommand.cpp */, A7E9B6F912D37EC400DA6239 /* getlineagecommand.h */, A7E9B6F812D37EC400DA6239 /* getlineagecommand.cpp */, A7E9B6FB12D37EC400DA6239 /* getlistcountcommand.h */, @@ -1303,10 +1311,10 @@ A7E9B72B12D37EC400DA6239 /* indicatorcommand.cpp */, A7E9B73C12D37EC400DA6239 /* libshuffcommand.h */, A7E9B73B12D37EC400DA6239 /* libshuffcommand.cpp */, - A7E9B73E12D37EC400DA6239 /* listseqscommand.h */, - A7E9B73D12D37EC400DA6239 /* listseqscommand.cpp */, A7A0671C156294810095C8C5 /* listotulabelscommand.h */, A7A067191562946F0095C8C5 /* listotulabelscommand.cpp */, + A7E9B73E12D37EC400DA6239 /* listseqscommand.h */, + A7E9B73D12D37EC400DA6239 /* listseqscommand.cpp */, A7FA10001302E096003860FE /* mantelcommand.h */, A7FA10011302E096003860FE /* mantelcommand.cpp */, A724D2B4153C8600000A826F /* makebiomcommand.h */, @@ -1377,6 +1385,8 @@ A7E9B7C512D37EC400DA6239 /* removelineagecommand.cpp */, A7E9B7C812D37EC400DA6239 /* removeotuscommand.h */, A7E9B7C712D37EC400DA6239 /* removeotuscommand.cpp */, + A70056E9156AB6D400924A2D /* removeotulabelscommand.h */, + A70056EA156AB6E500924A2D /* removeotulabelscommand.cpp */, A727864212E9E28C00F86ABA /* removerarecommand.h */, A727864312E9E28C00F86ABA /* removerarecommand.cpp */, A7E9B7CA12D37EC400DA6239 /* removeseqscommand.h */, @@ -2180,6 +2190,8 @@ 219C1DE41559BCCF004209F9 /* getcoremicrobiomecommand.cpp in Sources */, A7A0671A1562946F0095C8C5 /* listotulabelscommand.cpp in Sources */, A7A0671F1562AC3E0095C8C5 /* makecontigscommand.cpp in Sources */, + A70056E6156A93D000924A2D /* getotulabelscommand.cpp in Sources */, + A70056EB156AB6E500924A2D /* removeotulabelscommand.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/classifyotucommand.cpp b/classifyotucommand.cpp index ec22de8..ef41ee1 100644 --- a/classifyotucommand.cpp +++ b/classifyotucommand.cpp @@ -544,7 +544,9 @@ int ClassifyOtuCommand::process(ListVector* processList) { taxaSum = new PhyloSummary(groupfile); } + //for each bin in the list vector + string snumBins = toString(processList->getNumBins()); for (int i = 0; i < processList->getNumBins(); i++) { if (m->control_pressed) { break; } @@ -555,7 +557,15 @@ int ClassifyOtuCommand::process(ListVector* processList) { if (m->control_pressed) { out.close(); return 0; } //output to new names file - out << (i+1) << '\t' << size << '\t' << conTax << endl; + 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; + + out << binLabel << '\t' << size << '\t' << conTax << endl; string noConfidenceConTax = conTax; m->removeConfidences(noConfidenceConTax); diff --git a/commandfactory.cpp b/commandfactory.cpp index 1241a04..2822d0b 100644 --- a/commandfactory.cpp +++ b/commandfactory.cpp @@ -133,6 +133,8 @@ #include "makebiomcommand.h" #include "getcoremicrobiomecommand.h" #include "listotulabelscommand.h" +#include "getotulabelscommand.h" +#include "removeotulabelscommand.h" #include "makecontigscommand.h" /*******************************************************/ @@ -288,6 +290,8 @@ CommandFactory::CommandFactory(){ commands["make.biom"] = "make.biom"; commands["get.coremicrobiome"] = "get.coremicrobiome"; commands["list.otulabels"] = "list.otulabels"; + commands["get.otulabels"] = "get.otulabels"; + commands["remove.otulabels"] = "remove.otulabels"; commands["make.contigs"] = "make.contigs"; commands["quit"] = "MPIEnabled"; @@ -501,6 +505,8 @@ Command* CommandFactory::getCommand(string commandName, string optionString){ else if(commandName == "make.biom") { command = new MakeBiomCommand(optionString); } else if(commandName == "get.coremicrobiome") { command = new GetCoreMicroBiomeCommand(optionString); } else if(commandName == "list.otulabels") { command = new ListOtuLabelsCommand(optionString); } + else if(commandName == "get.otulabels") { command = new GetOtuLabelsCommand(optionString); } + else if(commandName == "remove.otulabels") { command = new RemoveOtuLabelsCommand(optionString); } else if(commandName == "make.contigs") { command = new MakeContigsCommand(optionString); } else { command = new NoCommand(optionString); } @@ -655,6 +661,8 @@ Command* CommandFactory::getCommand(string commandName, string optionString, str else if(commandName == "make.biom") { pipecommand = new MakeBiomCommand(optionString); } else if(commandName == "get.coremicrobiome") { pipecommand = new GetCoreMicroBiomeCommand(optionString); } else if(commandName == "list.otulabels") { pipecommand = new ListOtuLabelsCommand(optionString); } + else if(commandName == "get.otulabels") { pipecommand = new GetOtuLabelsCommand(optionString); } + else if(commandName == "remove.otulabels") { pipecommand = new RemoveOtuLabelsCommand(optionString); } else if(commandName == "make.contigs") { pipecommand = new MakeContigsCommand(optionString); } else { pipecommand = new NoCommand(optionString); } @@ -795,6 +803,8 @@ Command* CommandFactory::getCommand(string commandName){ else if(commandName == "make.biom") { shellcommand = new MakeBiomCommand(); } else if(commandName == "get.coremicrobiome") { shellcommand = new GetCoreMicroBiomeCommand(); } else if(commandName == "list.otulabels") { shellcommand = new ListOtuLabelsCommand(); } + else if(commandName == "get.otulabels") { shellcommand = new GetOtuLabelsCommand(); } + else if(commandName == "remove.otulabels") { shellcommand = new RemoveOtuLabelsCommand(); } else if(commandName == "make.contigs") { shellcommand = new MakeContigsCommand(); } else { shellcommand = new NoCommand(); } diff --git a/getotulabelscommand.cpp b/getotulabelscommand.cpp new file mode 100644 index 0000000..40ad69f --- /dev/null +++ b/getotulabelscommand.cpp @@ -0,0 +1,381 @@ +// +// getotulabelscommand.cpp +// Mothur +// +// Created by Sarah Westcott on 5/21/12. +// Copyright (c) 2012 Schloss Lab. All rights reserved. +// + +#include "getotulabelscommand.h" + +//********************************************************************************************************************** +vector GetOtuLabelsCommand::setParameters(){ + try { + CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(paccnos); + CommandParameter pconstaxonomy("constaxonomy", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pconstaxonomy); + CommandParameter potucorr("otucorr", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(potucorr); + CommandParameter pcorraxes("corraxes", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pcorraxes); + 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, "GetOtuLabelsCommand", "setParameters"); + exit(1); + } +} +//********************************************************************************************************************** +string GetOtuLabelsCommand::getHelpString(){ + try { + string helpString = ""; + helpString += "The get.otulabels command can be used to select specific otus with the output from classify.otu, otu.association, or corr.axes.\n"; + helpString += "The get.otulabels parameters are: constaxonomy, otucorr, corraxes, and accnos.\n"; + helpString += "The constaxonomy parameter is input the results of the classify.otu command.\n"; + helpString += "The otucorr parameter is input the results of the otu.association command.\n"; + helpString += "The corraxes parameter is input the results of the corr.axes command.\n"; + helpString += "The get.otulabels commmand should be in the following format: \n"; + helpString += "get.otulabels(accnos=yourListOfOTULabels, corraxes=yourCorrAxesFile)\n"; + return helpString; + } + catch(exception& e) { + m->errorOut(e, "GetOtuLabelsCommand", "getHelpString"); + exit(1); + } +} +//********************************************************************************************************************** +GetOtuLabelsCommand::GetOtuLabelsCommand(){ + try { + abort = true; calledHelp = true; + setParameters(); + vector tempOutNames; + outputTypes["contaxonomy"] = tempOutNames; + outputTypes["otu.corr"] = tempOutNames; + outputTypes["corr.axes"] = tempOutNames; + } + catch(exception& e) { + m->errorOut(e, "GetOtuLabelsCommand", "GetOtuLabelsCommand"); + exit(1); + } +} +//********************************************************************************************************************** +GetOtuLabelsCommand::GetOtuLabelsCommand(string option) { + try { + abort = false; calledHelp = false; + + //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 + vector myArray = setParameters(); + + OptionParser parser(option); + map parameters = parser.getParameters(); + + ValidParameters validParameter; + map::iterator it; + //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 { + + //edit file types below to include only the types you added as parameters + + string path; + it = parameters.find("constaxonomy"); + //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["constaxonomy"] = inputDir + it->second; } + } + + it = parameters.find("accnos"); + //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["accnos"] = inputDir + it->second; } + } + + it = parameters.find("corraxes"); + //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["corraxes"] = inputDir + it->second; } + } + + it = parameters.find("otucorr"); + //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["otucorr"] = inputDir + it->second; } + } + } + + vector tempOutNames; + outputTypes["contaxonomy"] = tempOutNames; + outputTypes["otu.corr"] = tempOutNames; + outputTypes["corr.axes"] = tempOutNames; + + //check for parameters + accnosfile = validParameter.validFile(parameters, "accnos", true); + if (accnosfile == "not open") { abort = true; } + else if (accnosfile == "not found") { + accnosfile = m->getAccnosFile(); + if (accnosfile != "") { m->mothurOut("Using " + accnosfile + " as input file for the accnos parameter."); m->mothurOutEndLine(); } + else { + m->mothurOut("You have no valid accnos file and accnos is required."); m->mothurOutEndLine(); + abort = true; + } + }else { m->setAccnosFile(accnosfile); } + + constaxonomyfile = validParameter.validFile(parameters, "constaxonomy", true); + if (constaxonomyfile == "not open") { constaxonomyfile = ""; abort = true; } + else if (constaxonomyfile == "not found") { constaxonomyfile = ""; } + + corraxesfile = validParameter.validFile(parameters, "corraxes", true); + if (corraxesfile == "not open") { corraxesfile = ""; abort = true; } + else if (corraxesfile == "not found") { corraxesfile = ""; } + + otucorrfile = validParameter.validFile(parameters, "otucorr", true); + if (otucorrfile == "not open") { otucorrfile = ""; abort = true; } + else if (otucorrfile == "not found") { otucorrfile = ""; } + + + //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 = ""; } + + if ((constaxonomyfile == "") && (corraxesfile == "") && (otucorrfile == "")) { m->mothurOut("You must provide one of the following: constaxonomy, corraxes or otucorr."); m->mothurOutEndLine(); abort = true; } + } + + } + catch(exception& e) { + m->errorOut(e, "GetOtuLabelsCommand", "GetOtuLabelsCommand"); + exit(1); + } +} +//********************************************************************************************************************** + +int GetOtuLabelsCommand::execute(){ + try { + + if (abort == true) { if (calledHelp) { return 0; } return 2; } + + //get labels you want to keep + readAccnos(); + + if (m->control_pressed) { return 0; } + + //read through the correct file and output lines you want to keep + if (constaxonomyfile != "") { readClassifyOtu(); } + if (corraxesfile != "") { readCorrAxes(); } + if (otucorrfile != "") { readOtuAssociation(); } + + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } + + //output files created by command + 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(); + + return 0; + } + catch(exception& e) { + m->errorOut(e, "GetOtuLabelsCommand", "execute"); + exit(1); + } +} +//********************************************************************************************************************** +int GetOtuLabelsCommand::readClassifyOtu(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(constaxonomyfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(constaxonomyfile)) + "pick.taxonomy"; + ofstream out; + m->openOutputFile(outputFileName, out); + + ifstream in; + m->openInputFile(constaxonomyfile, in); + + bool wroteSomething = false; + int selectedCount = 0; + + //read headers + string headers = m->getline(in); + out << headers << endl; + + while (!in.eof()) { + + if (m->control_pressed) { break; } + + string otu = ""; string tax = "unknown"; + int size = 0; + + in >> otu >> size >> tax; m->gobble(in); + + if (labels.count(otu) != 0) { + wroteSomething = true; + selectedCount++; + + out << otu << '\t' << size << '\t' << tax << endl; + } + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file does not contain any labels from the .accnos file."); m->mothurOutEndLine(); } + outputNames.push_back(outputFileName); outputTypes["constaxonomy"].push_back(outputFileName); + + m->mothurOut("Selected " + toString(selectedCount) + " otus from your constaxonomy file."); m->mothurOutEndLine(); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "GetOtuLabelsCommand", "readClassifyOtu"); + exit(1); + } +} +//********************************************************************************************************************** +int GetOtuLabelsCommand::readOtuAssociation(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(otucorrfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(otucorrfile)) + "pick.corr"; + ofstream out; + m->openOutputFile(outputFileName, out); + + ifstream in; + m->openInputFile(otucorrfile, in); + + bool wroteSomething = false; + int selectedCount = 0; + + //read headers + string headers = m->getline(in); + out << headers << endl; + + while (!in.eof()) { + + if (m->control_pressed) { break; } + + string otu1 = ""; + string otu2 = ""; + in >> otu1 >> otu2; + string line = m->getline(in); m->gobble(in); + + if ((labels.count(otu1) != 0) && (labels.count(otu2) != 0)){ + wroteSomething = true; + selectedCount++; + + out << otu1 << '\t' << otu2 << '\t' << line << endl; + } + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file does not contain any labels from the .accnos file."); m->mothurOutEndLine(); } + outputNames.push_back(outputFileName); outputTypes["otu.corr"].push_back(outputFileName); + + m->mothurOut("Selected " + toString(selectedCount) + " lines from your otu.corr file."); m->mothurOutEndLine(); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "GetOtuLabelsCommand", "readOtuAssociation"); + exit(1); + } +} +//********************************************************************************************************************** +int GetOtuLabelsCommand::readCorrAxes(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(corraxesfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(corraxesfile)) + "pick.axes"; + ofstream out; + m->openOutputFile(outputFileName, out); + + + ifstream in; + m->openInputFile(corraxesfile, in); + + bool wroteSomething = false; + int selectedCount = 0; + + //read headers + string headers = m->getline(in); + out << headers << endl; + + while (!in.eof()) { + + if (m->control_pressed) { break; } + + string otu = ""; + in >> otu; + string line = m->getline(in); m->gobble(in); + + if (labels.count(otu) != 0) { + wroteSomething = true; + selectedCount++; + + out << otu << '\t' << line << endl; + } + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file does not contain any labels from the .accnos file."); m->mothurOutEndLine(); } + outputNames.push_back(outputFileName); outputTypes["corr.axes"].push_back(outputFileName); + + m->mothurOut("Selected " + toString(selectedCount) + " lines from your corr.axes file."); m->mothurOutEndLine(); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "GetOtuLabelsCommand", "readCorrAxes"); + exit(1); + } +} + +//********************************************************************************************************************** +int GetOtuLabelsCommand::readAccnos(){ + try { + + ifstream in; + m->openInputFile(accnosfile, in); + string name; + + while(!in.eof()){ + in >> name; + + labels.insert(name); + + m->gobble(in); + } + in.close(); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "GetOtuLabelsCommand", "readAccnos"); + exit(1); + } +} +//********************************************************************************************************************** diff --git a/getotulabelscommand.h b/getotulabelscommand.h new file mode 100644 index 0000000..bad0f55 --- /dev/null +++ b/getotulabelscommand.h @@ -0,0 +1,53 @@ +#ifndef Mothur_getotulabelscommand_h +#define Mothur_getotulabelscommand_h + +// +// getotulabelscommand.h +// Mothur +// +// Created by Sarah Westcott on 5/21/12. +// Copyright (c) 2012 Schloss Lab. All rights reserved. +// + + +#include "command.hpp" + +/**************************************************************************************************/ + +class GetOtuLabelsCommand : public Command { +public: + GetOtuLabelsCommand(string); + GetOtuLabelsCommand(); + ~GetOtuLabelsCommand(){} + + vector setParameters(); + string getCommandName() { return "get.otulabels"; } + string getCommandCategory() { return "OTU-Based Approaches"; } + string getHelpString(); + string getCitation() { return "http://www.mothur.org/wiki/Get.otulabels"; } + string getDescription() { return "Can be used with output from classify.otu, otu.association, or corr.axes to select specific otus."; } + + int execute(); + void help() { m->mothurOut(getHelpString()); } + +private: + bool abort; + string outputDir, accnosfile, constaxonomyfile, otucorrfile, corraxesfile; + vector outputNames; + set labels; + + int readClassifyOtu(); + int readOtuAssociation(); + int readCorrAxes(); + int readAccnos(); + +}; + +/**************************************************************************************************/ + + + + + + +#endif diff --git a/listotulabelscommand.cpp b/listotulabelscommand.cpp index becfae3..93b1510 100644 --- a/listotulabelscommand.cpp +++ b/listotulabelscommand.cpp @@ -33,11 +33,11 @@ vector ListOtuLabelsCommand::setParameters(){ string ListOtuLabelsCommand::getHelpString(){ try { string helpString = ""; - helpString += "The list.labels lists otu labels from shared or relabund file. The results can be used by the get.labels to select specific otus with the output from classify.otu, otu.association, or corr.axes.\n"; - helpString += "The list.labels parameters are: shared, relabund, label and groups.\n"; + helpString += "The list.otulabels lists otu labels from shared or relabund file. The results can be used by the get.otulabels to select specific otus with the output from classify.otu, otu.association, or corr.axes.\n"; + helpString += "The list.otulabels parameters are: shared, relabund, label and groups.\n"; 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 list.labels commmand should be in the following format: \n"; + helpString += "The list.otulabels commmand should be in the following format: \n"; helpString += "list.otulabels(shared=yourSharedFile, groups=yourGroup1-yourGroup2)\n"; return helpString; } diff --git a/listotulabelscommand.h b/listotulabelscommand.h index ea41539..dbfd5d4 100644 --- a/listotulabelscommand.h +++ b/listotulabelscommand.h @@ -27,7 +27,7 @@ public: //commmand category choices: Sequence Processing, OTU-Based Approaches, Hypothesis Testing, Phylotype Analysis, General, Clustering and Hidden string getHelpString(); string getCitation() { return "http://www.mothur.org/wiki/List.otulabels"; } - string getDescription() { return "lists otu labels from shared or relabund file. Can be used with output from classify.otu, otu.association, or corr.axes to select specific otus."; } + string getDescription() { return "lists otu labels from shared or relabund file. Can be used by get.otulabels with output from classify.otu, otu.association, or corr.axes to select specific otus."; } int execute(); void help() { m->mothurOut(getHelpString()); } diff --git a/removeotulabelscommand.cpp b/removeotulabelscommand.cpp new file mode 100644 index 0000000..77305fb --- /dev/null +++ b/removeotulabelscommand.cpp @@ -0,0 +1,380 @@ +// +// removeotulabels.cpp +// Mothur +// +// Created by Sarah Westcott on 5/21/12. +// Copyright (c) 2012 Schloss Lab. All rights reserved. +// + +#include "removeotulabelscommand.h" + +//********************************************************************************************************************** +vector RemoveOtuLabelsCommand::setParameters(){ + try { + CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(paccnos); + CommandParameter pconstaxonomy("constaxonomy", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pconstaxonomy); + CommandParameter potucorr("otucorr", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(potucorr); + CommandParameter pcorraxes("corraxes", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pcorraxes); + 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, "RemoveOtuLabelsCommand", "setParameters"); + exit(1); + } +} +//********************************************************************************************************************** +string RemoveOtuLabelsCommand::getHelpString(){ + try { + string helpString = ""; + helpString += "The remove.otulabels command can be used to remove specific otus with the output from classify.otu, otu.association, or corr.axes.\n"; + helpString += "The remove.otulabels parameters are: constaxonomy, otucorr, corraxes, and accnos.\n"; + helpString += "The constaxonomy parameter is input the results of the classify.otu command.\n"; + helpString += "The otucorr parameter is input the results of the otu.association command.\n"; + helpString += "The corraxes parameter is input the results of the corr.axes command.\n"; + helpString += "The remove.otulabels commmand should be in the following format: \n"; + helpString += "remove.otulabels(accnos=yourListOfOTULabels, corraxes=yourCorrAxesFile)\n"; + return helpString; + } + catch(exception& e) { + m->errorOut(e, "RemoveOtuLabelsCommand", "getHelpString"); + exit(1); + } +} +//********************************************************************************************************************** +RemoveOtuLabelsCommand::RemoveOtuLabelsCommand(){ + try { + abort = true; calledHelp = true; + setParameters(); + vector tempOutNames; + outputTypes["contaxonomy"] = tempOutNames; + outputTypes["otu.corr"] = tempOutNames; + outputTypes["corr.axes"] = tempOutNames; + } + catch(exception& e) { + m->errorOut(e, "RemoveOtuLabelsCommand", "RemoveOtuLabelsCommand"); + exit(1); + } +} +//********************************************************************************************************************** +RemoveOtuLabelsCommand::RemoveOtuLabelsCommand(string option) { + try { + abort = false; calledHelp = false; + + //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 + vector myArray = setParameters(); + + OptionParser parser(option); + map parameters = parser.getParameters(); + + ValidParameters validParameter; + map::iterator it; + //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 { + + //edit file types below to include only the types you added as parameters + + string path; + it = parameters.find("constaxonomy"); + //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["constaxonomy"] = inputDir + it->second; } + } + + it = parameters.find("accnos"); + //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["accnos"] = inputDir + it->second; } + } + + it = parameters.find("corraxes"); + //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["corraxes"] = inputDir + it->second; } + } + + it = parameters.find("otucorr"); + //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["otucorr"] = inputDir + it->second; } + } + } + + vector tempOutNames; + outputTypes["contaxonomy"] = tempOutNames; + outputTypes["otu.corr"] = tempOutNames; + outputTypes["corr.axes"] = tempOutNames; + + //check for parameters + accnosfile = validParameter.validFile(parameters, "accnos", true); + if (accnosfile == "not open") { abort = true; } + else if (accnosfile == "not found") { + accnosfile = m->getAccnosFile(); + if (accnosfile != "") { m->mothurOut("Using " + accnosfile + " as input file for the accnos parameter."); m->mothurOutEndLine(); } + else { + m->mothurOut("You have no valid accnos file and accnos is required."); m->mothurOutEndLine(); + abort = true; + } + }else { m->setAccnosFile(accnosfile); } + + constaxonomyfile = validParameter.validFile(parameters, "constaxonomy", true); + if (constaxonomyfile == "not open") { constaxonomyfile = ""; abort = true; } + else if (constaxonomyfile == "not found") { constaxonomyfile = ""; } + + corraxesfile = validParameter.validFile(parameters, "corraxes", true); + if (corraxesfile == "not open") { corraxesfile = ""; abort = true; } + else if (corraxesfile == "not found") { corraxesfile = ""; } + + otucorrfile = validParameter.validFile(parameters, "otucorr", true); + if (otucorrfile == "not open") { otucorrfile = ""; abort = true; } + else if (otucorrfile == "not found") { otucorrfile = ""; } + + + //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 = ""; } + + if ((constaxonomyfile == "") && (corraxesfile == "") && (otucorrfile == "")) { m->mothurOut("You must provide one of the following: constaxonomy, corraxes or otucorr."); m->mothurOutEndLine(); abort = true; } + } + + } + catch(exception& e) { + m->errorOut(e, "RemoveOtuLabelsCommand", "RemoveOtuLabelsCommand"); + exit(1); + } +} +//********************************************************************************************************************** + +int RemoveOtuLabelsCommand::execute(){ + try { + + if (abort == true) { if (calledHelp) { return 0; } return 2; } + + //get labels you want to keep + readAccnos(); + + if (m->control_pressed) { return 0; } + + //read through the correct file and output lines you want to keep + if (constaxonomyfile != "") { readClassifyOtu(); } + if (corraxesfile != "") { readCorrAxes(); } + if (otucorrfile != "") { readOtuAssociation(); } + + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } + + //output files created by command + 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(); + + return 0; + } + catch(exception& e) { + m->errorOut(e, "GetOtuLabelsCommand", "execute"); + exit(1); + } +} +//********************************************************************************************************************** +int RemoveOtuLabelsCommand::readClassifyOtu(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(constaxonomyfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(constaxonomyfile)) + "pick.taxonomy"; + ofstream out; + m->openOutputFile(outputFileName, out); + + ifstream in; + m->openInputFile(constaxonomyfile, in); + + bool wroteSomething = false; + int removedCount = 0; + + //read headers + string headers = m->getline(in); + out << headers << endl; + + while (!in.eof()) { + + if (m->control_pressed) { break; } + + string otu = ""; string tax = "unknown"; + int size = 0; + + in >> otu >> size >> tax; m->gobble(in); + + if (labels.count(otu) == 0) { + wroteSomething = true; + out << otu << '\t' << size << '\t' << tax << endl; + }else { removedCount++; } + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine(); } + outputNames.push_back(outputFileName); outputTypes["constaxonomy"].push_back(outputFileName); + + m->mothurOut("Removed " + toString(removedCount) + " otus from your constaxonomy file."); m->mothurOutEndLine(); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "RemoveOtuLabelsCommand", "readClassifyOtu"); + exit(1); + } +} +//********************************************************************************************************************** +int RemoveOtuLabelsCommand::readOtuAssociation(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(otucorrfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(otucorrfile)) + "pick.corr"; + ofstream out; + m->openOutputFile(outputFileName, out); + + ifstream in; + m->openInputFile(otucorrfile, in); + + bool wroteSomething = false; + int removedCount = 0; + + //read headers + string headers = m->getline(in); + out << headers << endl; + + while (!in.eof()) { + + if (m->control_pressed) { break; } + + string otu1 = ""; + string otu2 = ""; + in >> otu1 >> otu2; + string line = m->getline(in); m->gobble(in); + + if ((labels.count(otu1) == 0) && (labels.count(otu2) == 0)){ + wroteSomething = true; + + out << otu1 << '\t' << otu2 << '\t' << line << endl; + }else { removedCount++; } + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine(); } + outputNames.push_back(outputFileName); outputTypes["otu.corr"].push_back(outputFileName); + + m->mothurOut("Removed " + toString(removedCount) + " lines from your otu.corr file."); m->mothurOutEndLine(); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "RemoveOtuLabelsCommand", "readOtuAssociation"); + exit(1); + } +} +//********************************************************************************************************************** +int RemoveOtuLabelsCommand::readCorrAxes(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(corraxesfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(corraxesfile)) + "pick.axes"; + ofstream out; + m->openOutputFile(outputFileName, out); + + + ifstream in; + m->openInputFile(corraxesfile, in); + + bool wroteSomething = false; + int removedCount = 0; + + //read headers + string headers = m->getline(in); + out << headers << endl; + + while (!in.eof()) { + + if (m->control_pressed) { break; } + + string otu = ""; + in >> otu; + string line = m->getline(in); m->gobble(in); + + if (labels.count(otu) == 0) { + wroteSomething = true; + + out << otu << '\t' << line << endl; + }else { removedCount++; } + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine(); } + outputNames.push_back(outputFileName); outputTypes["corr.axes"].push_back(outputFileName); + + m->mothurOut("Removed " + toString(removedCount) + " lines from your corr.axes file."); m->mothurOutEndLine(); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "RemoveOtuLabelsCommand", "readCorrAxes"); + exit(1); + } +} + +//********************************************************************************************************************** +int RemoveOtuLabelsCommand::readAccnos(){ + try { + + ifstream in; + m->openInputFile(accnosfile, in); + string name; + + while(!in.eof()){ + in >> name; + + labels.insert(name); + + m->gobble(in); + } + in.close(); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "RemoveOtuLabelsCommand", "readAccnos"); + exit(1); + } +} +//********************************************************************************************************************** + + + diff --git a/removeotulabelscommand.h b/removeotulabelscommand.h new file mode 100644 index 0000000..da2441d --- /dev/null +++ b/removeotulabelscommand.h @@ -0,0 +1,55 @@ +#ifndef Mothur_removeotulabelscommand_h +#define Mothur_removeotulabelscommand_h + + +// +// removeotulabelscommand.h +// Mothur +// +// Created by Sarah Westcott on 5/21/12. +// Copyright (c) 2012 Schloss Lab. All rights reserved. +// + +#include "command.hpp" + +/**************************************************************************************************/ + +class RemoveOtuLabelsCommand : public Command { +public: + RemoveOtuLabelsCommand(string); + RemoveOtuLabelsCommand(); + ~RemoveOtuLabelsCommand(){} + + vector setParameters(); + string getCommandName() { return "remove.otulabels"; } + string getCommandCategory() { return "OTU-Based Approaches"; } + string getHelpString(); + string getCitation() { return "http://www.mothur.org/wiki/Get.otulabels"; } + string getDescription() { return "Can be used with output from classify.otu, otu.association, or corr.axes to remove specific otus."; } + + int execute(); + void help() { m->mothurOut(getHelpString()); } + +private: + bool abort; + string outputDir, accnosfile, constaxonomyfile, otucorrfile, corraxesfile; + vector outputNames; + set labels; + + int readClassifyOtu(); + int readOtuAssociation(); + int readCorrAxes(); + int readAccnos(); + +}; + +/**************************************************************************************************/ + + + + + + + + +#endif -- 2.39.2