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::setParameters(){
18 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pfasta);
19 CommandParameter pname("name", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pname);
20 CommandParameter pgroup("group", "InputTypes", "", "", "none", "FNGLT", "none",false,true); parameters.push_back(pgroup);
21 CommandParameter plist("list", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(plist);
22 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(ptaxonomy);
23 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(paccnos);
24 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
25 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
26 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
28 vector<string> myArray;
29 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
33 m->errorOut(e, "RemoveGroupsCommand", "setParameters");
37 //**********************************************************************************************************************
38 string RemoveGroupsCommand::getHelpString(){
40 string helpString = "";
41 helpString += "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";
42 helpString += "It outputs a file containing the sequences NOT in the those specified groups.\n";
43 helpString += "The remove.groups command parameters are accnos, fasta, name, group, list, taxonomy and groups. The group parameter is required, unless you have a current group file.\n";
44 helpString += "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";
45 helpString += "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";
46 helpString += "The remove.groups command should be in the following format: remove.groups(accnos=yourAccnos, fasta=yourFasta, group=yourGroupFile).\n";
47 helpString += "Example remove.groups(accnos=amazon.accnos, fasta=amazon.fasta, group=amazon.groups).\n";
48 helpString += "or remove.groups(groups=pasture, fasta=amazon.fasta, amazon.groups).\n";
49 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
53 m->errorOut(e, "RemoveGroupsCommand", "getHelpString");
58 //**********************************************************************************************************************
59 RemoveGroupsCommand::RemoveGroupsCommand(){
61 abort = true; calledHelp = true;
63 vector<string> tempOutNames;
64 outputTypes["fasta"] = tempOutNames;
65 outputTypes["taxonomy"] = tempOutNames;
66 outputTypes["name"] = tempOutNames;
67 outputTypes["group"] = tempOutNames;
68 outputTypes["list"] = tempOutNames;
71 m->errorOut(e, "RemoveGroupsCommand", "RemoveGroupsCommand");
75 //**********************************************************************************************************************
76 RemoveGroupsCommand::RemoveGroupsCommand(string option) {
78 abort = false; calledHelp = false;
80 //allow user to run help
81 if(option == "help") { help(); abort = true; calledHelp = true; }
84 vector<string> myArray = setParameters();
86 OptionParser parser(option);
87 map<string,string> parameters = parser.getParameters();
89 ValidParameters validParameter;
90 map<string,string>::iterator it;
92 //check to make sure all parameters are valid for command
93 for (it = parameters.begin(); it != parameters.end(); it++) {
94 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
97 //initialize outputTypes
98 vector<string> tempOutNames;
99 outputTypes["fasta"] = tempOutNames;
100 outputTypes["taxonomy"] = tempOutNames;
101 outputTypes["name"] = tempOutNames;
102 outputTypes["group"] = tempOutNames;
103 outputTypes["list"] = tempOutNames;
106 //if the user changes the output directory command factory will send this info to us in the output parameter
107 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
109 //if the user changes the input directory command factory will send this info to us in the output parameter
110 string inputDir = validParameter.validFile(parameters, "inputdir", false);
111 if (inputDir == "not found"){ inputDir = ""; }
114 it = parameters.find("fasta");
115 //user has given a template file
116 if(it != parameters.end()){
117 path = m->hasPath(it->second);
118 //if the user has not given a path then, add inputdir. else leave path alone.
119 if (path == "") { parameters["fasta"] = inputDir + it->second; }
122 it = parameters.find("accnos");
123 //user has given a template file
124 if(it != parameters.end()){
125 path = m->hasPath(it->second);
126 //if the user has not given a path then, add inputdir. else leave path alone.
127 if (path == "") { parameters["accnos"] = inputDir + it->second; }
130 it = parameters.find("list");
131 //user has given a template file
132 if(it != parameters.end()){
133 path = m->hasPath(it->second);
134 //if the user has not given a path then, add inputdir. else leave path alone.
135 if (path == "") { parameters["list"] = inputDir + it->second; }
138 it = parameters.find("name");
139 //user has given a template file
140 if(it != parameters.end()){
141 path = m->hasPath(it->second);
142 //if the user has not given a path then, add inputdir. else leave path alone.
143 if (path == "") { parameters["name"] = inputDir + it->second; }
146 it = parameters.find("group");
147 //user has given a template file
148 if(it != parameters.end()){
149 path = m->hasPath(it->second);
150 //if the user has not given a path then, add inputdir. else leave path alone.
151 if (path == "") { parameters["group"] = inputDir + it->second; }
154 it = parameters.find("taxonomy");
155 //user has given a template file
156 if(it != parameters.end()){
157 path = m->hasPath(it->second);
158 //if the user has not given a path then, add inputdir. else leave path alone.
159 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
164 //check for required parameters
165 accnosfile = validParameter.validFile(parameters, "accnos", true);
166 if (accnosfile == "not open") { abort = true; }
167 else if (accnosfile == "not found") { accnosfile = ""; }
169 fastafile = validParameter.validFile(parameters, "fasta", true);
170 if (fastafile == "not open") { abort = true; }
171 else if (fastafile == "not found") { fastafile = ""; }
173 namefile = validParameter.validFile(parameters, "name", true);
174 if (namefile == "not open") { abort = true; }
175 else if (namefile == "not found") { namefile = ""; }
177 groupfile = validParameter.validFile(parameters, "group", true);
178 if (groupfile == "not open") { abort = true; }
179 else if (groupfile == "not found") {
180 //if there is a current group file, use it
181 groupfile = m->getGroupFile();
182 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
183 else { m->mothurOut("You have no current groupfile and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
186 listfile = validParameter.validFile(parameters, "list", true);
187 if (listfile == "not open") { abort = true; }
188 else if (listfile == "not found") { listfile = ""; }
190 taxfile = validParameter.validFile(parameters, "taxonomy", true);
191 if (taxfile == "not open") { abort = true; }
192 else if (taxfile == "not found") { taxfile = ""; }
194 groups = validParameter.validFile(parameters, "groups", false);
195 if (groups == "not found") { groups = ""; }
197 m->splitAtDash(groups, Groups);
200 if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; }
202 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; }
206 catch(exception& e) {
207 m->errorOut(e, "RemoveGroupsCommand", "RemoveGroupsCommand");
211 //**********************************************************************************************************************
213 int RemoveGroupsCommand::execute(){
216 if (abort == true) { if (calledHelp) { return 0; } return 2; }
218 groupMap = new GroupMap(groupfile);
221 //get groups you want to remove
222 if (accnosfile != "") { readAccnos(); }
224 //make sure groups are valid
225 //takes care of user setting groupNames that are invalid or setting groups=all
226 SharedUtil* util = new SharedUtil();
227 util->setGroups(Groups, groupMap->namesOfGroups);
230 //fill names with names of sequences that are from the groups we want to remove
233 if (m->control_pressed) { delete groupMap; return 0; }
235 //read through the correct file and output lines you want to keep
236 if (namefile != "") { readName(); }
237 if (fastafile != "") { readFasta(); }
238 if (groupfile != "") { readGroup(); }
239 if (listfile != "") { readList(); }
240 if (taxfile != "") { readTax(); }
242 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
244 m->mothurOut("Removed " + toString(names.size()) + " sequences. From the groups: "); m->mothurOutEndLine();
245 for (int i = 0; i < Groups.size(); i++) { m->mothurOut(Groups[i]); m->mothurOut(" contains " + toString(groupMap->getNumSeqs(Groups[i])) + " sequences."); m->mothurOutEndLine(); }
246 m->mothurOutEndLine();
248 if (outputNames.size() != 0) {
249 m->mothurOutEndLine();
250 m->mothurOut("Output File names: "); m->mothurOutEndLine();
251 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
252 m->mothurOutEndLine();
254 //set fasta file as new current fastafile
256 itTypes = outputTypes.find("fasta");
257 if (itTypes != outputTypes.end()) {
258 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
261 itTypes = outputTypes.find("name");
262 if (itTypes != outputTypes.end()) {
263 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
266 itTypes = outputTypes.find("group");
267 if (itTypes != outputTypes.end()) {
268 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
271 itTypes = outputTypes.find("list");
272 if (itTypes != outputTypes.end()) {
273 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
276 itTypes = outputTypes.find("taxonomy");
277 if (itTypes != outputTypes.end()) {
278 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
285 catch(exception& e) {
286 m->errorOut(e, "RemoveGroupsCommand", "execute");
291 //**********************************************************************************************************************
292 int RemoveGroupsCommand::readFasta(){
294 string thisOutputDir = outputDir;
295 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
296 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
299 m->openOutputFile(outputFileName, out);
302 m->openInputFile(fastafile, in);
305 bool wroteSomething = false;
308 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
310 Sequence currSeq(in);
311 name = currSeq.getName();
314 //if this name is in the accnos file
315 if (names.count(name) == 0) {
316 wroteSomething = true;
318 currSeq.printSequence(out);
326 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
327 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
332 catch(exception& e) {
333 m->errorOut(e, "RemoveGroupsCommand", "readFasta");
338 //**********************************************************************************************************************
339 int RemoveGroupsCommand::readList(){
341 string thisOutputDir = outputDir;
342 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
343 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
346 m->openOutputFile(outputFileName, out);
349 m->openInputFile(listfile, in);
351 bool wroteSomething = false;
354 //read in list vector
357 //make a new list vector
359 newList.setLabel(list.getLabel());
362 for (int i = 0; i < list.getNumBins(); i++) {
363 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
365 //parse out names that are in accnos file
366 string binnames = list.get(i);
368 string newNames = "";
369 while (binnames.find_first_of(',') != -1) {
370 string name = binnames.substr(0,binnames.find_first_of(','));
371 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
373 //if that name is in the .accnos file, add it
374 if (names.count(name) == 0) { newNames += name + ","; }
378 if (names.count(binnames) == 0) { newNames += binnames + ","; }
380 //if there are names in this bin add to new list
381 if (newNames != "") {
382 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
383 newList.push_back(newNames);
387 //print new listvector
388 if (newList.getNumBins() != 0) {
389 wroteSomething = true;
398 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
399 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
404 catch(exception& e) {
405 m->errorOut(e, "RemoveGroupsCommand", "readList");
409 //**********************************************************************************************************************
410 int RemoveGroupsCommand::readName(){
412 string thisOutputDir = outputDir;
413 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
414 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
417 m->openOutputFile(outputFileName, out);
420 m->openInputFile(namefile, in);
421 string name, firstCol, secondCol;
423 bool wroteSomething = false;
426 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
428 in >> firstCol; m->gobble(in);
431 vector<string> parsedNames;
432 m->splitAtComma(secondCol, parsedNames);
434 vector<string> validSecond; validSecond.clear();
435 for (int i = 0; i < parsedNames.size(); i++) {
436 if (names.count(parsedNames[i]) == 0) {
437 validSecond.push_back(parsedNames[i]);
442 //if the name in the first column is in the set then print it and any other names in second column also in set
443 if (names.count(firstCol) == 0) {
445 wroteSomething = true;
447 out << firstCol << '\t';
449 //you know you have at least one valid second since first column is valid
450 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
451 out << validSecond[validSecond.size()-1] << endl;
453 //make first name in set you come to first column and then add the remaining names to second column
456 //you want part of this row
457 if (validSecond.size() != 0) {
459 wroteSomething = true;
461 out << validSecond[0] << '\t';
463 //you know you have at least one valid second since first column is valid
464 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
465 out << validSecond[validSecond.size()-1] << endl;
474 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
475 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
479 catch(exception& e) {
480 m->errorOut(e, "RemoveGroupsCommand", "readName");
485 //**********************************************************************************************************************
486 int RemoveGroupsCommand::readGroup(){
488 string thisOutputDir = outputDir;
489 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
490 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
493 m->openOutputFile(outputFileName, out);
496 m->openInputFile(groupfile, in);
499 bool wroteSomething = false;
502 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
504 in >> name; //read from first column
505 in >> group; //read from second column
507 //if this name is in the accnos file
508 if (names.count(name) == 0) {
509 wroteSomething = true;
510 out << name << '\t' << group << endl;
518 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
519 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
523 catch(exception& e) {
524 m->errorOut(e, "RemoveGroupsCommand", "readGroup");
528 //**********************************************************************************************************************
529 int RemoveGroupsCommand::readTax(){
531 string thisOutputDir = outputDir;
532 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
533 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
535 m->openOutputFile(outputFileName, out);
538 m->openInputFile(taxfile, in);
541 bool wroteSomething = false;
544 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
546 in >> name; //read from first column
547 in >> tax; //read from second column
549 //if this name is in the accnos file
550 if (names.count(name) == 0) {
551 wroteSomething = true;
552 out << name << '\t' << tax << endl;
560 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
561 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
565 catch(exception& e) {
566 m->errorOut(e, "RemoveGroupsCommand", "readTax");
570 //**********************************************************************************************************************
571 void RemoveGroupsCommand::readAccnos(){
576 m->openInputFile(accnosfile, in);
582 Groups.push_back(name);
589 catch(exception& e) {
590 m->errorOut(e, "RemoveGroupsCommand", "readAccnos");
594 //**********************************************************************************************************************
595 int RemoveGroupsCommand::fillNames(){
597 vector<string> seqs = groupMap->getNamesSeqs();
599 for (int i = 0; i < seqs.size(); i++) {
601 if (m->control_pressed) { return 0; }
603 string group = groupMap->getGroup(seqs[i]);
605 if (m->inUsersGroups(group, Groups)) {
606 names.insert(seqs[i]);
612 catch(exception& e) {
613 m->errorOut(e, "RemoveGroupsCommand", "fillNames");
618 //**********************************************************************************************************************