2 * removegroupscommand.cpp
5 * Created by westcott on 11/10/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "removegroupscommand.h"
11 #include "sequence.hpp"
12 #include "listvector.hpp"
13 #include "sharedutilities.h"
15 //**********************************************************************************************************************
16 vector<string> RemoveGroupsCommand::getValidParameters(){
18 string Array[] = {"fasta","name", "group", "accnos", "groups","list","taxonomy","outputdir","inputdir" };
19 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
23 m->errorOut(e, "RemoveGroupsCommand", "getValidParameters");
27 //**********************************************************************************************************************
28 RemoveGroupsCommand::RemoveGroupsCommand(){
30 abort = true; calledHelp = true;
31 vector<string> tempOutNames;
32 outputTypes["fasta"] = tempOutNames;
33 outputTypes["taxonomy"] = tempOutNames;
34 outputTypes["name"] = tempOutNames;
35 outputTypes["group"] = tempOutNames;
36 outputTypes["list"] = tempOutNames;
39 m->errorOut(e, "RemoveGroupsCommand", "RemoveGroupsCommand");
43 //**********************************************************************************************************************
44 vector<string> RemoveGroupsCommand::getRequiredParameters(){
46 string Array[] = {"group"};
47 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
51 m->errorOut(e, "RemoveGroupsCommand", "getRequiredParameters");
55 //**********************************************************************************************************************
56 vector<string> RemoveGroupsCommand::getRequiredFiles(){
58 vector<string> myArray;
62 m->errorOut(e, "RemoveGroupsCommand", "getRequiredFiles");
66 //**********************************************************************************************************************
67 RemoveGroupsCommand::RemoveGroupsCommand(string option) {
69 abort = false; calledHelp = false;
71 //allow user to run help
72 if(option == "help") { help(); abort = true; calledHelp = true; }
75 //valid paramters for this command
76 string Array[] = {"fasta","name", "group", "accnos", "groups", "list","taxonomy","outputdir","inputdir" };
77 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
79 OptionParser parser(option);
80 map<string,string> parameters = parser.getParameters();
82 ValidParameters validParameter;
83 map<string,string>::iterator it;
85 //check to make sure all parameters are valid for command
86 for (it = parameters.begin(); it != parameters.end(); it++) {
87 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
90 //initialize outputTypes
91 vector<string> tempOutNames;
92 outputTypes["fasta"] = tempOutNames;
93 outputTypes["taxonomy"] = tempOutNames;
94 outputTypes["name"] = tempOutNames;
95 outputTypes["group"] = tempOutNames;
96 outputTypes["list"] = tempOutNames;
99 //if the user changes the output directory command factory will send this info to us in the output parameter
100 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
102 //if the user changes the input directory command factory will send this info to us in the output parameter
103 string inputDir = validParameter.validFile(parameters, "inputdir", false);
104 if (inputDir == "not found"){ inputDir = ""; }
107 it = parameters.find("fasta");
108 //user has given a template file
109 if(it != parameters.end()){
110 path = m->hasPath(it->second);
111 //if the user has not given a path then, add inputdir. else leave path alone.
112 if (path == "") { parameters["fasta"] = inputDir + it->second; }
115 it = parameters.find("accnos");
116 //user has given a template file
117 if(it != parameters.end()){
118 path = m->hasPath(it->second);
119 //if the user has not given a path then, add inputdir. else leave path alone.
120 if (path == "") { parameters["accnos"] = inputDir + it->second; }
123 it = parameters.find("list");
124 //user has given a template file
125 if(it != parameters.end()){
126 path = m->hasPath(it->second);
127 //if the user has not given a path then, add inputdir. else leave path alone.
128 if (path == "") { parameters["list"] = inputDir + it->second; }
131 it = parameters.find("name");
132 //user has given a template file
133 if(it != parameters.end()){
134 path = m->hasPath(it->second);
135 //if the user has not given a path then, add inputdir. else leave path alone.
136 if (path == "") { parameters["name"] = inputDir + it->second; }
139 it = parameters.find("group");
140 //user has given a template file
141 if(it != parameters.end()){
142 path = m->hasPath(it->second);
143 //if the user has not given a path then, add inputdir. else leave path alone.
144 if (path == "") { parameters["group"] = inputDir + it->second; }
147 it = parameters.find("taxonomy");
148 //user has given a template file
149 if(it != parameters.end()){
150 path = m->hasPath(it->second);
151 //if the user has not given a path then, add inputdir. else leave path alone.
152 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
157 //check for required parameters
158 accnosfile = validParameter.validFile(parameters, "accnos", true);
159 if (accnosfile == "not open") { abort = true; }
160 else if (accnosfile == "not found") { accnosfile = ""; }
162 fastafile = validParameter.validFile(parameters, "fasta", true);
163 if (fastafile == "not open") { abort = true; }
164 else if (fastafile == "not found") { fastafile = ""; }
166 namefile = validParameter.validFile(parameters, "name", true);
167 if (namefile == "not open") { abort = true; }
168 else if (namefile == "not found") { namefile = ""; }
170 groupfile = validParameter.validFile(parameters, "group", true);
171 if (groupfile == "not open") { abort = true; }
172 else if (groupfile == "not found") { groupfile = ""; m->mothurOut("You must provide a group file."); m->mothurOutEndLine(); abort = true; }
174 listfile = validParameter.validFile(parameters, "list", true);
175 if (listfile == "not open") { abort = true; }
176 else if (listfile == "not found") { listfile = ""; }
178 taxfile = validParameter.validFile(parameters, "taxonomy", true);
179 if (taxfile == "not open") { abort = true; }
180 else if (taxfile == "not found") { taxfile = ""; }
182 groups = validParameter.validFile(parameters, "groups", false);
183 if (groups == "not found") { groups = ""; }
185 m->splitAtDash(groups, Groups);
188 if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; }
190 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; }
194 catch(exception& e) {
195 m->errorOut(e, "RemoveGroupsCommand", "RemoveGroupsCommand");
199 //**********************************************************************************************************************
201 void RemoveGroupsCommand::help(){
203 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");
204 m->mothurOut("It outputs a file containing the sequences NOT in the those specified groups.\n");
205 m->mothurOut("The remove.groups command parameters are accnos, fasta, name, group, list, taxonomy and groups. The group parameter is required.\n");
206 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");
207 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");
208 m->mothurOut("The remove.groups command should be in the following format: remove.groups(accnos=yourAccnos, fasta=yourFasta, group=yourGroupFile).\n");
209 m->mothurOut("Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta, group=amazon.groups).\n");
210 m->mothurOut("or remove.seqs(groups=pasture, fasta=amazon.fasta, amazon.groups).\n");
211 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
213 catch(exception& e) {
214 m->errorOut(e, "RemoveGroupsCommand", "help");
219 //**********************************************************************************************************************
221 int RemoveGroupsCommand::execute(){
224 if (abort == true) { if (calledHelp) { return 0; } return 2; }
226 groupMap = new GroupMap(groupfile);
229 //get groups you want to remove
230 if (accnosfile != "") { readAccnos(); }
232 //make sure groups are valid
233 //takes care of user setting groupNames that are invalid or setting groups=all
234 SharedUtil* util = new SharedUtil();
235 util->setGroups(Groups, groupMap->namesOfGroups);
238 //fill names with names of sequences that are from the groups we want to remove
241 if (m->control_pressed) { delete groupMap; return 0; }
243 //read through the correct file and output lines you want to keep
244 if (namefile != "") { readName(); }
245 if (fastafile != "") { readFasta(); }
246 if (groupfile != "") { readGroup(); }
247 if (listfile != "") { readList(); }
248 if (taxfile != "") { readTax(); }
250 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
252 m->mothurOut("Removed " + toString(names.size()) + " sequences. From the groups: "); m->mothurOutEndLine();
253 for (int i = 0; i < Groups.size(); i++) { m->mothurOut(Groups[i]); m->mothurOutEndLine(); }
254 m->mothurOutEndLine();
256 if (outputNames.size() != 0) {
257 m->mothurOutEndLine();
258 m->mothurOut("Output File names: "); m->mothurOutEndLine();
259 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
260 m->mothurOutEndLine();
266 catch(exception& e) {
267 m->errorOut(e, "RemoveGroupsCommand", "execute");
272 //**********************************************************************************************************************
273 int RemoveGroupsCommand::readFasta(){
275 string thisOutputDir = outputDir;
276 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
277 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
280 m->openOutputFile(outputFileName, out);
283 m->openInputFile(fastafile, in);
286 bool wroteSomething = false;
289 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
291 Sequence currSeq(in);
292 name = currSeq.getName();
295 //if this name is in the accnos file
296 if (names.count(name) == 0) {
297 wroteSomething = true;
299 currSeq.printSequence(out);
307 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
308 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
313 catch(exception& e) {
314 m->errorOut(e, "RemoveGroupsCommand", "readFasta");
319 //**********************************************************************************************************************
320 int RemoveGroupsCommand::readList(){
322 string thisOutputDir = outputDir;
323 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
324 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
327 m->openOutputFile(outputFileName, out);
330 m->openInputFile(listfile, in);
332 bool wroteSomething = false;
335 //read in list vector
338 //make a new list vector
340 newList.setLabel(list.getLabel());
343 for (int i = 0; i < list.getNumBins(); i++) {
344 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
346 //parse out names that are in accnos file
347 string binnames = list.get(i);
349 string newNames = "";
350 while (binnames.find_first_of(',') != -1) {
351 string name = binnames.substr(0,binnames.find_first_of(','));
352 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
354 //if that name is in the .accnos file, add it
355 if (names.count(name) == 0) { newNames += name + ","; }
359 if (names.count(binnames) == 0) { newNames += binnames + ","; }
361 //if there are names in this bin add to new list
362 if (newNames != "") {
363 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
364 newList.push_back(newNames);
368 //print new listvector
369 if (newList.getNumBins() != 0) {
370 wroteSomething = true;
379 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
380 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
385 catch(exception& e) {
386 m->errorOut(e, "RemoveGroupsCommand", "readList");
390 //**********************************************************************************************************************
391 int RemoveGroupsCommand::readName(){
393 string thisOutputDir = outputDir;
394 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
395 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
398 m->openOutputFile(outputFileName, out);
401 m->openInputFile(namefile, in);
402 string name, firstCol, secondCol;
404 bool wroteSomething = false;
407 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
409 in >> firstCol; m->gobble(in);
412 vector<string> parsedNames;
413 m->splitAtComma(secondCol, parsedNames);
415 vector<string> validSecond; validSecond.clear();
416 for (int i = 0; i < parsedNames.size(); i++) {
417 if (names.count(parsedNames[i]) == 0) {
418 validSecond.push_back(parsedNames[i]);
423 //if the name in the first column is in the set then print it and any other names in second column also in set
424 if (names.count(firstCol) == 0) {
426 wroteSomething = true;
428 out << firstCol << '\t';
430 //you know you have at least one valid second since first column is valid
431 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
432 out << validSecond[validSecond.size()-1] << endl;
434 //make first name in set you come to first column and then add the remaining names to second column
437 //you want part of this row
438 if (validSecond.size() != 0) {
440 wroteSomething = true;
442 out << validSecond[0] << '\t';
444 //you know you have at least one valid second since first column is valid
445 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
446 out << validSecond[validSecond.size()-1] << endl;
455 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
456 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
460 catch(exception& e) {
461 m->errorOut(e, "RemoveGroupsCommand", "readName");
466 //**********************************************************************************************************************
467 int RemoveGroupsCommand::readGroup(){
469 string thisOutputDir = outputDir;
470 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
471 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
474 m->openOutputFile(outputFileName, out);
477 m->openInputFile(groupfile, in);
480 bool wroteSomething = false;
483 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
485 in >> name; //read from first column
486 in >> group; //read from second column
488 //if this name is in the accnos file
489 if (names.count(name) == 0) {
490 wroteSomething = true;
491 out << name << '\t' << group << endl;
499 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
500 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
504 catch(exception& e) {
505 m->errorOut(e, "RemoveGroupsCommand", "readGroup");
509 //**********************************************************************************************************************
510 int RemoveGroupsCommand::readTax(){
512 string thisOutputDir = outputDir;
513 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
514 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
516 m->openOutputFile(outputFileName, out);
519 m->openInputFile(taxfile, in);
522 bool wroteSomething = false;
525 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
527 in >> name; //read from first column
528 in >> tax; //read from second column
530 //if this name is in the accnos file
531 if (names.count(name) == 0) {
532 wroteSomething = true;
533 out << name << '\t' << tax << endl;
541 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
542 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
546 catch(exception& e) {
547 m->errorOut(e, "RemoveGroupsCommand", "readTax");
551 //**********************************************************************************************************************
552 void RemoveGroupsCommand::readAccnos(){
557 m->openInputFile(accnosfile, in);
563 Groups.push_back(name);
570 catch(exception& e) {
571 m->errorOut(e, "RemoveGroupsCommand", "readAccnos");
575 //**********************************************************************************************************************
576 int RemoveGroupsCommand::fillNames(){
578 vector<string> seqs = groupMap->getNamesSeqs();
580 for (int i = 0; i < seqs.size(); i++) {
582 if (m->control_pressed) { return 0; }
584 string group = groupMap->getGroup(seqs[i]);
586 if (m->inUsersGroups(group, Groups)) {
587 names.insert(seqs[i]);
593 catch(exception& e) {
594 m->errorOut(e, "RemoveGroupsCommand", "fillNames");
599 //**********************************************************************************************************************