From: westcott Date: Wed, 10 Nov 2010 14:39:31 +0000 (+0000) Subject: added get.groups and remove.groups commands X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=commitdiff_plain;h=e3e0858c684c4608d679840024e5095ddc8e2719 added get.groups and remove.groups commands --- diff --git a/Mothur.xcodeproj/project.pbxproj b/Mothur.xcodeproj/project.pbxproj index 1c282d9..f27f7c3 100644 --- a/Mothur.xcodeproj/project.pbxproj +++ b/Mothur.xcodeproj/project.pbxproj @@ -57,6 +57,10 @@ A747E81D116365E000FB9042 /* chimeraslayercommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = chimeraslayercommand.cpp; sourceTree = ""; }; A74A9C03124B72DB000D5D25 /* clusterfragmentscommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = clusterfragmentscommand.h; sourceTree = ""; }; A74A9C04124B72DB000D5D25 /* clusterfragmentscommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = clusterfragmentscommand.cpp; sourceTree = ""; }; + A751032A128AC93F00ABB831 /* removegroupscommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = removegroupscommand.h; sourceTree = ""; }; + A751032B128AC93F00ABB831 /* removegroupscommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = removegroupscommand.cpp; sourceTree = ""; }; + A75103D6128AE34100ABB831 /* getgroupscommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = getgroupscommand.h; sourceTree = ""; }; + A75103D7128AE34100ABB831 /* getgroupscommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = getgroupscommand.cpp; sourceTree = ""; }; A7639F8D1175DF35008F5578 /* makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; path = makefile; sourceTree = ""; }; A7653CB112789EFD009D6C09 /* subsamplecommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = subsamplecommand.h; sourceTree = ""; }; A7653CB212789EFD009D6C09 /* subsamplecommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = subsamplecommand.cpp; sourceTree = ""; }; @@ -792,6 +796,8 @@ A76714DE126DE45A003F359A /* deuniqueseqscommand.cpp */, A7DA2050113FECD400BF472F /* filterseqscommand.h */, A7DA204F113FECD400BF472F /* filterseqscommand.cpp */, + A75103D6128AE34100ABB831 /* getgroupscommand.h */, + A75103D7128AE34100ABB831 /* getgroupscommand.cpp */, A7DA205B113FECD400BF472F /* getgroupcommand.h */, A7DA205A113FECD400BF472F /* getgroupcommand.cpp */, A7DA205D113FECD400BF472F /* getlabelcommand.h */, @@ -872,6 +878,8 @@ A7DA20EA113FECD400BF472F /* readotucommand.cpp */, A7DA20F1113FECD400BF472F /* readtreecommand.h */, A7DA20F0113FECD400BF472F /* readtreecommand.cpp */, + A751032A128AC93F00ABB831 /* removegroupscommand.h */, + A751032B128AC93F00ABB831 /* removegroupscommand.cpp */, A787A28E124CE1470076EB84 /* removelineagecommand.h */, A787A28F124CE1470076EB84 /* removelineagecommand.cpp */, A7DA20F3113FECD400BF472F /* removeseqscommand.h */, diff --git a/commandfactory.cpp b/commandfactory.cpp index 3ac3781..1f77b71 100644 --- a/commandfactory.cpp +++ b/commandfactory.cpp @@ -97,6 +97,8 @@ #include "pairwiseseqscommand.h" #include "clusterdoturcommand.h" #include "subsamplecommand.h" +#include "removegroupscommand.h" +#include "getgroupscommand.h" /*******************************************************/ @@ -199,6 +201,8 @@ CommandFactory::CommandFactory(){ commands["deunique.seqs"] = "deunique.seqs"; commands["cluster.classic"] = "cluster.classic"; commands["sub.sample"] = "sub.sample"; + commands["remove.groups"] = "remove.groups"; + commands["get.groups"] = "get.groups"; commands["pairwise.seqs"] = "MPIEnabled"; commands["pipeline.pds"] = "MPIEnabled"; commands["classify.seqs"] = "MPIEnabled"; @@ -341,6 +345,8 @@ Command* CommandFactory::getCommand(string commandName, string optionString){ else if(commandName == "cluster.fragments") { command = new ClusterFragmentsCommand(optionString); } else if(commandName == "get.lineage") { command = new GetLineageCommand(optionString); } else if(commandName == "remove.lineage") { command = new RemoveLineageCommand(optionString); } + else if(commandName == "get.groups") { command = new GetGroupsCommand(optionString); } + else if(commandName == "remove.groups") { command = new RemoveGroupsCommand(optionString); } else if(commandName == "fastq.info") { command = new ParseFastaQCommand(optionString); } else if(commandName == "pipeline.pds") { command = new PipelineCommand(optionString); } else if(commandName == "deunique.seqs") { command = new DeUniqueSeqsCommand(optionString); } @@ -456,7 +462,9 @@ Command* CommandFactory::getCommand(string commandName, string optionString, str else if(commandName == "split.groups") { pipecommand = new SplitGroupCommand(optionString); } else if(commandName == "cluster.fragments") { pipecommand = new ClusterFragmentsCommand(optionString); } else if(commandName == "get.lineage") { pipecommand = new GetLineageCommand(optionString); } + else if(commandName == "get.groups") { pipecommand = new GetGroupsCommand(optionString); } else if(commandName == "remove.lineage") { pipecommand = new RemoveLineageCommand(optionString); } + else if(commandName == "remove.groups") { pipecommand = new RemoveGroupsCommand(optionString); } else if(commandName == "fastq.info") { pipecommand = new ParseFastaQCommand(optionString); } else if(commandName == "deunique.seqs") { pipecommand = new DeUniqueSeqsCommand(optionString); } else if(commandName == "pairwise.seqs") { pipecommand = new PairwiseSeqsCommand(optionString); } @@ -560,6 +568,8 @@ Command* CommandFactory::getCommand(string commandName){ else if(commandName == "cluster.fragments") { shellcommand = new ClusterFragmentsCommand(); } else if(commandName == "get.lineage") { shellcommand = new GetLineageCommand(); } else if(commandName == "remove.lineage") { shellcommand = new RemoveLineageCommand(); } + else if(commandName == "get.groups") { shellcommand = new GetGroupsCommand(); } + else if(commandName == "remove.groups") { shellcommand = new RemoveGroupsCommand(); } else if(commandName == "fastq.info") { shellcommand = new ParseFastaQCommand(); } else if(commandName == "deunique.seqs") { shellcommand = new DeUniqueSeqsCommand(); } else if(commandName == "pairwise.seqs") { shellcommand = new PairwiseSeqsCommand(); } diff --git a/getgroupscommand.cpp b/getgroupscommand.cpp new file mode 100644 index 0000000..266fc52 --- /dev/null +++ b/getgroupscommand.cpp @@ -0,0 +1,599 @@ +/* + * getgroupscommand.cpp + * Mothur + * + * Created by westcott on 11/10/10. + * Copyright 2010 Schloss Lab. All rights reserved. + * + */ + +#include "getgroupscommand.h" +#include "sequence.hpp" +#include "listvector.hpp" +#include "sharedutilities.h" + +//********************************************************************************************************************** +vector GetGroupsCommand::getValidParameters(){ + try { + string Array[] = {"fasta","name", "group", "accnos", "groups","list","taxonomy","outputdir","inputdir" }; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + return myArray; + } + catch(exception& e) { + m->errorOut(e, "GetGroupsCommand", "getValidParameters"); + exit(1); + } +} +//********************************************************************************************************************** +GetGroupsCommand::GetGroupsCommand(){ + try { + abort = true; + //initialize outputTypes + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["taxonomy"] = tempOutNames; + outputTypes["name"] = tempOutNames; + outputTypes["group"] = tempOutNames; + outputTypes["list"] = tempOutNames; + } + catch(exception& e) { + m->errorOut(e, "GetGroupsCommand", "GetGroupsCommand"); + exit(1); + } +} +//********************************************************************************************************************** +vector GetGroupsCommand::getRequiredParameters(){ + try { + string Array[] = {"group"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + return myArray; + } + catch(exception& e) { + m->errorOut(e, "GetGroupsCommand", "getRequiredParameters"); + exit(1); + } +} +//********************************************************************************************************************** +vector GetGroupsCommand::getRequiredFiles(){ + try { + vector myArray; + return myArray; + } + catch(exception& e) { + m->errorOut(e, "GetGroupsCommand", "getRequiredFiles"); + exit(1); + } +} +//********************************************************************************************************************** +GetGroupsCommand::GetGroupsCommand(string option) { + try { + abort = false; + + //allow user to run help + if(option == "help") { help(); abort = true; } + + else { + //valid paramters for this command + string Array[] = {"fasta","name", "group", "accnos", "groups", "list","taxonomy","outputdir","inputdir" }; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + + 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; } + } + + //initialize outputTypes + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["taxonomy"] = tempOutNames; + outputTypes["name"] = tempOutNames; + outputTypes["group"] = tempOutNames; + outputTypes["list"] = 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 = ""; } + + //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 { + string path; + it = parameters.find("fasta"); + //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["fasta"] = 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("list"); + //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["list"] = inputDir + it->second; } + } + + it = parameters.find("name"); + //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["name"] = inputDir + it->second; } + } + + it = parameters.find("group"); + //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["group"] = inputDir + it->second; } + } + + it = parameters.find("taxonomy"); + //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["taxonomy"] = inputDir + it->second; } + } + } + + + //check for required parameters + accnosfile = validParameter.validFile(parameters, "accnos", true); + if (accnosfile == "not open") { abort = true; } + else if (accnosfile == "not found") { accnosfile = ""; } + + fastafile = validParameter.validFile(parameters, "fasta", true); + if (fastafile == "not open") { abort = true; } + else if (fastafile == "not found") { fastafile = ""; } + + namefile = validParameter.validFile(parameters, "name", true); + if (namefile == "not open") { abort = true; } + else if (namefile == "not found") { namefile = ""; } + + groupfile = validParameter.validFile(parameters, "group", true); + if (groupfile == "not open") { abort = true; } + else if (groupfile == "not found") { groupfile = ""; m->mothurOut("You must provide a group file."); m->mothurOutEndLine(); abort = true; } + + listfile = validParameter.validFile(parameters, "list", true); + if (listfile == "not open") { abort = true; } + else if (listfile == "not found") { listfile = ""; } + + taxfile = validParameter.validFile(parameters, "taxonomy", true); + if (taxfile == "not open") { abort = true; } + else if (taxfile == "not found") { taxfile = ""; } + + groups = validParameter.validFile(parameters, "groups", false); + if (groups == "not found") { groups = ""; } + else { + m->splitAtDash(groups, Groups); + } + + if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; } + + if ((fastafile == "") && (namefile == "") && (groupfile == "") && (listfile == "") && (taxfile == "")) { m->mothurOut("You must provide at least one of the following: fasta, name, taxonomy, group or list."); m->mothurOutEndLine(); abort = true; } + } + + } + catch(exception& e) { + m->errorOut(e, "GetGroupsCommand", "GetGroupsCommand"); + exit(1); + } +} +//********************************************************************************************************************** + +void GetGroupsCommand::help(){ + try { + m->mothurOut("The get.groups command selects sequences from a specfic group or set of groups from the following file types: fasta, name, group, list, taxonomy.\n"); + m->mothurOut("It outputs a file containing the sequences in the those specified groups.\n"); + m->mothurOut("The get.groups command parameters are accnos, fasta, name, group, list, taxonomy and groups. The group parameter is required.\n"); + m->mothurOut("You must also provide an accnos containing the list of groups to get or set the groups parameter to the groups you wish to select.\n"); + m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like. You can separate group names with dashes.\n"); + m->mothurOut("The get.groups command should be in the following format: get.groups(accnos=yourAccnos, fasta=yourFasta, group=yourGroupFile).\n"); + m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta, group=amazon.groups).\n"); + m->mothurOut("or get.seqs(groups=pasture, fasta=amazon.fasta, amazon.groups).\n"); + m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n"); + } + catch(exception& e) { + m->errorOut(e, "GetGroupsCommand", "help"); + exit(1); + } +} + +//********************************************************************************************************************** + +int GetGroupsCommand::execute(){ + try { + + if (abort == true) { return 0; } + + groupMap = new GroupMap(groupfile); + groupMap->readMap(); + + //get groups you want to remove + if (accnosfile != "") { readAccnos(); } + + //make sure groups are valid + //takes care of user setting groupNames that are invalid or setting groups=all + SharedUtil* util = new SharedUtil(); + util->setGroups(Groups, groupMap->namesOfGroups); + delete util; + + //fill names with names of sequences that are from the groups we want to remove + fillNames(); + + if (m->control_pressed) { delete groupMap; return 0; } + + //read through the correct file and output lines you want to keep + if (namefile != "") { readName(); } + if (fastafile != "") { readFasta(); } + if (groupfile != "") { readGroup(); } + if (listfile != "") { readList(); } + if (taxfile != "") { readTax(); } + + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; } + + m->mothurOut("Selected " + toString(names.size()) + " sequences. From the groups: "); m->mothurOutEndLine(); + for (int i = 0; i < Groups.size(); i++) { m->mothurOut(Groups[i]); m->mothurOutEndLine(); } + m->mothurOutEndLine(); + + 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(); + } + + return 0; + } + + catch(exception& e) { + m->errorOut(e, "GetGroupsCommand", "execute"); + exit(1); + } +} + +//********************************************************************************************************************** +int GetGroupsCommand::readFasta(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile); + + ofstream out; + m->openOutputFile(outputFileName, out); + + ifstream in; + m->openInputFile(fastafile, in); + string name; + + bool wroteSomething = false; + + while(!in.eof()){ + if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; } + + Sequence currSeq(in); + name = currSeq.getName(); + + if (name != "") { + //if this name is in the accnos file + if (names.count(name) != 0) { + wroteSomething = true; + + currSeq.printSequence(out); + } + } + m->gobble(in); + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine(); } + outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "GetGroupsCommand", "readFasta"); + exit(1); + } +} + +//********************************************************************************************************************** +int GetGroupsCommand::readList(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(listfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile); + + ofstream out; + m->openOutputFile(outputFileName, out); + + ifstream in; + m->openInputFile(listfile, in); + + bool wroteSomething = false; + + while(!in.eof()){ + //read in list vector + ListVector list(in); + + //make a new list vector + ListVector newList; + newList.setLabel(list.getLabel()); + + //for each bin + for (int i = 0; i < list.getNumBins(); i++) { + if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; } + + //parse out names that are in accnos file + string binnames = list.get(i); + + string newNames = ""; + while (binnames.find_first_of(',') != -1) { + string name = binnames.substr(0,binnames.find_first_of(',')); + binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length()); + + //if that name is in the .accnos file, add it + if (names.count(name) != 0) { newNames += name + ","; } + } + + //get last name + if (names.count(binnames) != 0) { newNames += binnames + ","; } + + //if there are names in this bin add to new list + if (newNames != "") { + newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma + newList.push_back(newNames); + } + } + + //print new listvector + if (newList.getNumBins() != 0) { + wroteSomething = true; + newList.print(out); + } + + m->gobble(in); + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine(); } + outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "GetGroupsCommand", "readList"); + exit(1); + } +} +//********************************************************************************************************************** +int GetGroupsCommand::readName(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(namefile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile); + + ofstream out; + m->openOutputFile(outputFileName, out); + + ifstream in; + m->openInputFile(namefile, in); + string name, firstCol, secondCol; + + bool wroteSomething = false; + + while(!in.eof()){ + if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; } + + in >> firstCol; m->gobble(in); + in >> secondCol; + + vector parsedNames; + m->splitAtComma(secondCol, parsedNames); + + vector validSecond; validSecond.clear(); + for (int i = 0; i < parsedNames.size(); i++) { + if (names.count(parsedNames[i]) != 0) { + validSecond.push_back(parsedNames[i]); + } + } + + //if the name in the first column is in the set then print it and any other names in second column also in set + if (names.count(firstCol) != 0) { + + wroteSomething = true; + + out << firstCol << '\t'; + + //you know you have at least one valid second since first column is valid + for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; } + out << validSecond[validSecond.size()-1] << endl; + + //make first name in set you come to first column and then add the remaining names to second column + }else { + + //you want part of this row + if (validSecond.size() != 0) { + + wroteSomething = true; + + out << validSecond[0] << '\t'; + + //you know you have at least one valid second since first column is valid + for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; } + out << validSecond[validSecond.size()-1] << endl; + } + } + + m->gobble(in); + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine(); } + outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName); + + return 0; + } + catch(exception& e) { + m->errorOut(e, "GetGroupsCommand", "readName"); + exit(1); + } +} + +//********************************************************************************************************************** +int GetGroupsCommand::readGroup(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile); + + ofstream out; + m->openOutputFile(outputFileName, out); + + ifstream in; + m->openInputFile(groupfile, in); + string name, group; + + bool wroteSomething = false; + + while(!in.eof()){ + if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; } + + in >> name; //read from first column + in >> group; //read from second column + + //if this name is in the accnos file + if (names.count(name) != 0) { + wroteSomething = true; + out << name << '\t' << group << endl; + } + + m->gobble(in); + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine(); } + outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName); + + return 0; + } + catch(exception& e) { + m->errorOut(e, "GetGroupsCommand", "readGroup"); + exit(1); + } +} +//********************************************************************************************************************** +int GetGroupsCommand::readTax(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile); + ofstream out; + m->openOutputFile(outputFileName, out); + + ifstream in; + m->openInputFile(taxfile, in); + string name, tax; + + bool wroteSomething = false; + + while(!in.eof()){ + if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; } + + in >> name; //read from first column + in >> tax; //read from second column + + //if this name is in the accnos file + if (names.count(name) != 0) { + wroteSomething = true; + out << name << '\t' << tax << endl; + } + + m->gobble(in); + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine(); } + outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName); + + return 0; + } + catch(exception& e) { + m->errorOut(e, "GetGroupsCommand", "readTax"); + exit(1); + } +} +//********************************************************************************************************************** +void GetGroupsCommand::readAccnos(){ + try { + Groups.clear(); + + ifstream in; + m->openInputFile(accnosfile, in); + string name; + + while(!in.eof()){ + in >> name; + + Groups.push_back(name); + + m->gobble(in); + } + in.close(); + + } + catch(exception& e) { + m->errorOut(e, "GetGroupsCommand", "readAccnos"); + exit(1); + } +} +//********************************************************************************************************************** +int GetGroupsCommand::fillNames(){ + try { + vector seqs = groupMap->getNamesSeqs(); + + for (int i = 0; i < seqs.size(); i++) { + + if (m->control_pressed) { return 0; } + + string group = groupMap->getGroup(seqs[i]); + + if (m->inUsersGroups(group, Groups)) { + names.insert(seqs[i]); + } + } + } + catch(exception& e) { + m->errorOut(e, "GetGroupsCommand", "fillNames"); + exit(1); + } +} + +//********************************************************************************************************************** + + diff --git a/getgroupscommand.h b/getgroupscommand.h new file mode 100644 index 0000000..7c4c92f --- /dev/null +++ b/getgroupscommand.h @@ -0,0 +1,52 @@ +#ifndef GETGROUPSCOMMAND_H +#define GETGROUPSCOMMAND_H + +/* + * getgroupscommand.h + * Mothur + * + * Created by westcott on 11/10/10. + * Copyright 2010 Schloss Lab. All rights reserved. + * + */ + + +#include "command.hpp" +#include "groupmap.h" + +class GetGroupsCommand : public Command { + +public: + + GetGroupsCommand(string); + GetGroupsCommand(); + ~GetGroupsCommand(){} + vector getRequiredParameters(); + vector getValidParameters(); + vector getRequiredFiles(); + map > getOutputFiles() { return outputTypes; } + int execute(); + void help(); + +private: + set names; + string accnosfile, fastafile, namefile, groupfile, listfile, taxfile, outputDir, groups; + bool abort; + vector outputNames, Groups; + map > outputTypes; + GroupMap* groupMap; + + int readFasta(); + int readName(); + int readGroup(); + void readAccnos(); + int readList(); + int readTax(); + int fillNames(); + +}; + +#endif + + + diff --git a/getseqscommand.cpp b/getseqscommand.cpp index 9fbfb93..39c4fe0 100644 --- a/getseqscommand.cpp +++ b/getseqscommand.cpp @@ -263,6 +263,8 @@ int GetSeqsCommand::execute(){ if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; } + m->mothurOut("Selected " + toString(names.size()) + " sequences."); m->mothurOutEndLine(); + if (outputNames.size() != 0) { m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine(); diff --git a/mothur b/mothur index 09823f3..bcb2033 100755 Binary files a/mothur and b/mothur differ diff --git a/removegroupscommand.cpp b/removegroupscommand.cpp new file mode 100644 index 0000000..93646bc --- /dev/null +++ b/removegroupscommand.cpp @@ -0,0 +1,600 @@ +/* + * removegroupscommand.cpp + * Mothur + * + * Created by westcott on 11/10/10. + * Copyright 2010 Schloss Lab. All rights reserved. + * + */ + +#include "removegroupscommand.h" +#include "sequence.hpp" +#include "listvector.hpp" +#include "sharedutilities.h" + +//********************************************************************************************************************** +vector RemoveGroupsCommand::getValidParameters(){ + try { + string Array[] = {"fasta","name", "group", "accnos", "groups","list","taxonomy","outputdir","inputdir" }; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + return myArray; + } + catch(exception& e) { + m->errorOut(e, "RemoveGroupsCommand", "getValidParameters"); + exit(1); + } +} +//********************************************************************************************************************** +RemoveGroupsCommand::RemoveGroupsCommand(){ + try { + abort = true; + //initialize outputTypes + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["taxonomy"] = tempOutNames; + outputTypes["name"] = tempOutNames; + outputTypes["group"] = tempOutNames; + outputTypes["list"] = tempOutNames; + } + catch(exception& e) { + m->errorOut(e, "RemoveGroupsCommand", "RemoveGroupsCommand"); + exit(1); + } +} +//********************************************************************************************************************** +vector RemoveGroupsCommand::getRequiredParameters(){ + try { + string Array[] = {"group"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + return myArray; + } + catch(exception& e) { + m->errorOut(e, "RemoveGroupsCommand", "getRequiredParameters"); + exit(1); + } +} +//********************************************************************************************************************** +vector RemoveGroupsCommand::getRequiredFiles(){ + try { + vector myArray; + return myArray; + } + catch(exception& e) { + m->errorOut(e, "RemoveGroupsCommand", "getRequiredFiles"); + exit(1); + } +} +//********************************************************************************************************************** +RemoveGroupsCommand::RemoveGroupsCommand(string option) { + try { + abort = false; + + //allow user to run help + if(option == "help") { help(); abort = true; } + + else { + //valid paramters for this command + string Array[] = {"fasta","name", "group", "accnos", "groups", "list","taxonomy","outputdir","inputdir" }; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + + 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; } + } + + //initialize outputTypes + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["taxonomy"] = tempOutNames; + outputTypes["name"] = tempOutNames; + outputTypes["group"] = tempOutNames; + outputTypes["list"] = 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 = ""; } + + //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 { + string path; + it = parameters.find("fasta"); + //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["fasta"] = 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("list"); + //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["list"] = inputDir + it->second; } + } + + it = parameters.find("name"); + //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["name"] = inputDir + it->second; } + } + + it = parameters.find("group"); + //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["group"] = inputDir + it->second; } + } + + it = parameters.find("taxonomy"); + //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["taxonomy"] = inputDir + it->second; } + } + } + + + //check for required parameters + accnosfile = validParameter.validFile(parameters, "accnos", true); + if (accnosfile == "not open") { abort = true; } + else if (accnosfile == "not found") { accnosfile = ""; } + + fastafile = validParameter.validFile(parameters, "fasta", true); + if (fastafile == "not open") { abort = true; } + else if (fastafile == "not found") { fastafile = ""; } + + namefile = validParameter.validFile(parameters, "name", true); + if (namefile == "not open") { abort = true; } + else if (namefile == "not found") { namefile = ""; } + + groupfile = validParameter.validFile(parameters, "group", true); + if (groupfile == "not open") { abort = true; } + else if (groupfile == "not found") { groupfile = ""; m->mothurOut("You must provide a group file."); m->mothurOutEndLine(); abort = true; } + + listfile = validParameter.validFile(parameters, "list", true); + if (listfile == "not open") { abort = true; } + else if (listfile == "not found") { listfile = ""; } + + taxfile = validParameter.validFile(parameters, "taxonomy", true); + if (taxfile == "not open") { abort = true; } + else if (taxfile == "not found") { taxfile = ""; } + + groups = validParameter.validFile(parameters, "groups", false); + if (groups == "not found") { groups = ""; } + else { + m->splitAtDash(groups, Groups); + } + + if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; } + + if ((fastafile == "") && (namefile == "") && (groupfile == "") && (listfile == "") && (taxfile == "")) { m->mothurOut("You must provide at least one of the following: fasta, name, taxonomy, group or list."); m->mothurOutEndLine(); abort = true; } + } + + } + catch(exception& e) { + m->errorOut(e, "RemoveGroupsCommand", "RemoveGroupsCommand"); + exit(1); + } +} +//********************************************************************************************************************** + +void RemoveGroupsCommand::help(){ + try { + m->mothurOut("The remove.groups command removes sequences from a specfic group or set of groups from the following file types: fasta, name, group, list, taxonomy.\n"); + m->mothurOut("It outputs a file containing the sequences NOT in the those specified groups.\n"); + m->mothurOut("The remove.groups command parameters are accnos, fasta, name, group, list, taxonomy and groups. The group parameter is required.\n"); + m->mothurOut("You must also provide an accnos containing the list of groups to remove or set the groups parameter to the groups you wish to remove.\n"); + m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like removed. You can separate group names with dashes.\n"); + m->mothurOut("The remove.groups command should be in the following format: remove.groups(accnos=yourAccnos, fasta=yourFasta, group=yourGroupFile).\n"); + m->mothurOut("Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta, group=amazon.groups).\n"); + m->mothurOut("or remove.seqs(groups=pasture, fasta=amazon.fasta, amazon.groups).\n"); + m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n"); + } + catch(exception& e) { + m->errorOut(e, "RemoveGroupsCommand", "help"); + exit(1); + } +} + +//********************************************************************************************************************** + +int RemoveGroupsCommand::execute(){ + try { + + if (abort == true) { return 0; } + + groupMap = new GroupMap(groupfile); + groupMap->readMap(); + + //get groups you want to remove + if (accnosfile != "") { readAccnos(); } + + //make sure groups are valid + //takes care of user setting groupNames that are invalid or setting groups=all + SharedUtil* util = new SharedUtil(); + util->setGroups(Groups, groupMap->namesOfGroups); + delete util; + + //fill names with names of sequences that are from the groups we want to remove + fillNames(); + + if (m->control_pressed) { delete groupMap; return 0; } + + //read through the correct file and output lines you want to keep + if (namefile != "") { readName(); } + if (fastafile != "") { readFasta(); } + if (groupfile != "") { readGroup(); } + if (listfile != "") { readList(); } + if (taxfile != "") { readTax(); } + + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; } + + m->mothurOut("Removed " + toString(names.size()) + " sequences. From the groups: "); m->mothurOutEndLine(); + for (int i = 0; i < Groups.size(); i++) { m->mothurOut(Groups[i]); m->mothurOutEndLine(); } + m->mothurOutEndLine(); + + 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(); + } + + return 0; + } + + catch(exception& e) { + m->errorOut(e, "RemoveGroupsCommand", "execute"); + exit(1); + } +} + +//********************************************************************************************************************** +int RemoveGroupsCommand::readFasta(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile); + + ofstream out; + m->openOutputFile(outputFileName, out); + + ifstream in; + m->openInputFile(fastafile, in); + string name; + + bool wroteSomething = false; + + while(!in.eof()){ + if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; } + + Sequence currSeq(in); + name = currSeq.getName(); + + if (name != "") { + //if this name is in the accnos file + if (names.count(name) == 0) { + wroteSomething = true; + + currSeq.printSequence(out); + } + } + m->gobble(in); + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); } + outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "RemoveGroupsCommand", "readFasta"); + exit(1); + } +} + +//********************************************************************************************************************** +int RemoveGroupsCommand::readList(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(listfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile); + + ofstream out; + m->openOutputFile(outputFileName, out); + + ifstream in; + m->openInputFile(listfile, in); + + bool wroteSomething = false; + + while(!in.eof()){ + //read in list vector + ListVector list(in); + + //make a new list vector + ListVector newList; + newList.setLabel(list.getLabel()); + + //for each bin + for (int i = 0; i < list.getNumBins(); i++) { + if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; } + + //parse out names that are in accnos file + string binnames = list.get(i); + + string newNames = ""; + while (binnames.find_first_of(',') != -1) { + string name = binnames.substr(0,binnames.find_first_of(',')); + binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length()); + + //if that name is in the .accnos file, add it + if (names.count(name) == 0) { newNames += name + ","; } + } + + //get last name + if (names.count(binnames) == 0) { newNames += binnames + ","; } + + //if there are names in this bin add to new list + if (newNames != "") { + newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma + newList.push_back(newNames); + } + } + + //print new listvector + if (newList.getNumBins() != 0) { + wroteSomething = true; + newList.print(out); + } + + m->gobble(in); + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); } + outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "RemoveGroupsCommand", "readList"); + exit(1); + } +} +//********************************************************************************************************************** +int RemoveGroupsCommand::readName(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(namefile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile); + + ofstream out; + m->openOutputFile(outputFileName, out); + + ifstream in; + m->openInputFile(namefile, in); + string name, firstCol, secondCol; + + bool wroteSomething = false; + + while(!in.eof()){ + if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; } + + in >> firstCol; m->gobble(in); + in >> secondCol; + + vector parsedNames; + m->splitAtComma(secondCol, parsedNames); + + vector validSecond; validSecond.clear(); + for (int i = 0; i < parsedNames.size(); i++) { + if (names.count(parsedNames[i]) == 0) { + validSecond.push_back(parsedNames[i]); + } + } + + //if the name in the first column is in the set then print it and any other names in second column also in set + if (names.count(firstCol) == 0) { + + wroteSomething = true; + + out << firstCol << '\t'; + + //you know you have at least one valid second since first column is valid + for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; } + out << validSecond[validSecond.size()-1] << endl; + + //make first name in set you come to first column and then add the remaining names to second column + }else { + + //you want part of this row + if (validSecond.size() != 0) { + + wroteSomething = true; + + out << validSecond[0] << '\t'; + + //you know you have at least one valid second since first column is valid + for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; } + out << validSecond[validSecond.size()-1] << endl; + } + } + + m->gobble(in); + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); } + outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName); + + return 0; + } + catch(exception& e) { + m->errorOut(e, "RemoveGroupsCommand", "readName"); + exit(1); + } +} + +//********************************************************************************************************************** +int RemoveGroupsCommand::readGroup(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile); + + ofstream out; + m->openOutputFile(outputFileName, out); + + ifstream in; + m->openInputFile(groupfile, in); + string name, group; + + bool wroteSomething = false; + + while(!in.eof()){ + if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; } + + in >> name; //read from first column + in >> group; //read from second column + + //if this name is in the accnos file + if (names.count(name) == 0) { + wroteSomething = true; + out << name << '\t' << group << endl; + } + + m->gobble(in); + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); } + outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName); + + return 0; + } + catch(exception& e) { + m->errorOut(e, "RemoveGroupsCommand", "readGroup"); + exit(1); + } +} +//********************************************************************************************************************** +int RemoveGroupsCommand::readTax(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile); + ofstream out; + m->openOutputFile(outputFileName, out); + + ifstream in; + m->openInputFile(taxfile, in); + string name, tax; + + bool wroteSomething = false; + + while(!in.eof()){ + if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; } + + in >> name; //read from first column + in >> tax; //read from second column + + //if this name is in the accnos file + if (names.count(name) == 0) { + wroteSomething = true; + out << name << '\t' << tax << endl; + } + + m->gobble(in); + } + in.close(); + out.close(); + + if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); } + outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName); + + return 0; + } + catch(exception& e) { + m->errorOut(e, "RemoveGroupsCommand", "readTax"); + exit(1); + } +} +//********************************************************************************************************************** +void RemoveGroupsCommand::readAccnos(){ + try { + Groups.clear(); + + ifstream in; + m->openInputFile(accnosfile, in); + string name; + + while(!in.eof()){ + in >> name; + + Groups.push_back(name); + + m->gobble(in); + } + in.close(); + + } + catch(exception& e) { + m->errorOut(e, "RemoveGroupsCommand", "readAccnos"); + exit(1); + } +} +//********************************************************************************************************************** +int RemoveGroupsCommand::fillNames(){ + try { + vector seqs = groupMap->getNamesSeqs(); + + for (int i = 0; i < seqs.size(); i++) { + + if (m->control_pressed) { return 0; } + + string group = groupMap->getGroup(seqs[i]); + + if (m->inUsersGroups(group, Groups)) { + names.insert(seqs[i]); + } + } + } + catch(exception& e) { + m->errorOut(e, "RemoveGroupsCommand", "fillNames"); + exit(1); + } +} + +//********************************************************************************************************************** + + + diff --git a/removegroupscommand.h b/removegroupscommand.h new file mode 100644 index 0000000..896697a --- /dev/null +++ b/removegroupscommand.h @@ -0,0 +1,51 @@ +#ifndef REMOVEGROUPSCOMMAND_H +#define REMOVEGROUPSCOMMAND_H + +/* + * removegroupscommand.h + * Mothur + * + * Created by westcott on 11/10/10. + * Copyright 2010 Schloss Lab. All rights reserved. + * + */ + + +#include "command.hpp" +#include "groupmap.h" + +class RemoveGroupsCommand : public Command { + +public: + + RemoveGroupsCommand(string); + RemoveGroupsCommand(); + ~RemoveGroupsCommand(){} + vector getRequiredParameters(); + vector getValidParameters(); + vector getRequiredFiles(); + map > getOutputFiles() { return outputTypes; } + int execute(); + void help(); + +private: + set names; + string accnosfile, fastafile, namefile, groupfile, listfile, taxfile, outputDir, groups; + bool abort; + vector outputNames, Groups; + map > outputTypes; + GroupMap* groupMap; + + int readFasta(); + int readName(); + int readGroup(); + void readAccnos(); + int readList(); + int readTax(); + int fillNames(); + +}; + +#endif + + diff --git a/removeseqscommand.cpp b/removeseqscommand.cpp index 384a360..17de5a8 100644 --- a/removeseqscommand.cpp +++ b/removeseqscommand.cpp @@ -267,6 +267,8 @@ int RemoveSeqsCommand::execute(){ if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; } + m->mothurOut("Removed " + toString(names.size()) + " sequences."); m->mothurOutEndLine(); + if (outputNames.size() != 0) { m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine();