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; }
82 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
85 vector<string> myArray = setParameters();
87 OptionParser parser(option);
88 map<string,string> parameters = parser.getParameters();
90 ValidParameters validParameter;
91 map<string,string>::iterator it;
93 //check to make sure all parameters are valid for command
94 for (it = parameters.begin(); it != parameters.end(); it++) {
95 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
98 //initialize outputTypes
99 vector<string> tempOutNames;
100 outputTypes["fasta"] = tempOutNames;
101 outputTypes["taxonomy"] = tempOutNames;
102 outputTypes["name"] = tempOutNames;
103 outputTypes["group"] = tempOutNames;
104 outputTypes["list"] = tempOutNames;
107 //if the user changes the output directory command factory will send this info to us in the output parameter
108 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
110 //if the user changes the input directory command factory will send this info to us in the output parameter
111 string inputDir = validParameter.validFile(parameters, "inputdir", false);
112 if (inputDir == "not found"){ inputDir = ""; }
115 it = parameters.find("fasta");
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["fasta"] = inputDir + it->second; }
123 it = parameters.find("accnos");
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["accnos"] = inputDir + it->second; }
131 it = parameters.find("list");
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["list"] = inputDir + it->second; }
139 it = parameters.find("name");
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["name"] = inputDir + it->second; }
147 it = parameters.find("group");
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["group"] = inputDir + it->second; }
155 it = parameters.find("taxonomy");
156 //user has given a template file
157 if(it != parameters.end()){
158 path = m->hasPath(it->second);
159 //if the user has not given a path then, add inputdir. else leave path alone.
160 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
165 //check for required parameters
166 accnosfile = validParameter.validFile(parameters, "accnos", true);
167 if (accnosfile == "not open") { abort = true; }
168 else if (accnosfile == "not found") { accnosfile = ""; }
170 fastafile = validParameter.validFile(parameters, "fasta", true);
171 if (fastafile == "not open") { abort = true; }
172 else if (fastafile == "not found") { fastafile = ""; }
174 namefile = validParameter.validFile(parameters, "name", true);
175 if (namefile == "not open") { abort = true; }
176 else if (namefile == "not found") { namefile = ""; }
178 groupfile = validParameter.validFile(parameters, "group", true);
179 if (groupfile == "not open") { abort = true; }
180 else if (groupfile == "not found") {
181 //if there is a current group file, use it
182 groupfile = m->getGroupFile();
183 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
184 else { m->mothurOut("You have no current groupfile and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
187 listfile = validParameter.validFile(parameters, "list", true);
188 if (listfile == "not open") { abort = true; }
189 else if (listfile == "not found") { listfile = ""; }
191 taxfile = validParameter.validFile(parameters, "taxonomy", true);
192 if (taxfile == "not open") { abort = true; }
193 else if (taxfile == "not found") { taxfile = ""; }
195 groups = validParameter.validFile(parameters, "groups", false);
196 if (groups == "not found") { groups = ""; }
198 m->splitAtDash(groups, Groups);
201 if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; }
203 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; }
207 catch(exception& e) {
208 m->errorOut(e, "RemoveGroupsCommand", "RemoveGroupsCommand");
212 //**********************************************************************************************************************
214 int RemoveGroupsCommand::execute(){
217 if (abort == true) { if (calledHelp) { return 0; } return 2; }
219 groupMap = new GroupMap(groupfile);
222 //get groups you want to remove
223 if (accnosfile != "") { readAccnos(); }
225 //make sure groups are valid
226 //takes care of user setting groupNames that are invalid or setting groups=all
227 SharedUtil* util = new SharedUtil();
228 util->setGroups(Groups, groupMap->namesOfGroups);
231 //fill names with names of sequences that are from the groups we want to remove
234 if (m->control_pressed) { delete groupMap; return 0; }
236 //read through the correct file and output lines you want to keep
237 if (namefile != "") { readName(); }
238 if (fastafile != "") { readFasta(); }
239 if (groupfile != "") { readGroup(); }
240 if (listfile != "") { readList(); }
241 if (taxfile != "") { readTax(); }
243 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
245 m->mothurOut("Removed " + toString(names.size()) + " sequences. From the groups: "); m->mothurOutEndLine();
246 for (int i = 0; i < Groups.size(); i++) { m->mothurOut(Groups[i]); m->mothurOut(" contains " + toString(groupMap->getNumSeqs(Groups[i])) + " sequences."); m->mothurOutEndLine(); }
247 m->mothurOutEndLine();
249 if (outputNames.size() != 0) {
250 m->mothurOutEndLine();
251 m->mothurOut("Output File names: "); m->mothurOutEndLine();
252 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
253 m->mothurOutEndLine();
255 //set fasta file as new current fastafile
257 itTypes = outputTypes.find("fasta");
258 if (itTypes != outputTypes.end()) {
259 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
262 itTypes = outputTypes.find("name");
263 if (itTypes != outputTypes.end()) {
264 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
267 itTypes = outputTypes.find("group");
268 if (itTypes != outputTypes.end()) {
269 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
272 itTypes = outputTypes.find("list");
273 if (itTypes != outputTypes.end()) {
274 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
277 itTypes = outputTypes.find("taxonomy");
278 if (itTypes != outputTypes.end()) {
279 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
286 catch(exception& e) {
287 m->errorOut(e, "RemoveGroupsCommand", "execute");
292 //**********************************************************************************************************************
293 int RemoveGroupsCommand::readFasta(){
295 string thisOutputDir = outputDir;
296 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
297 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
300 m->openOutputFile(outputFileName, out);
303 m->openInputFile(fastafile, in);
306 bool wroteSomething = false;
309 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
311 Sequence currSeq(in);
312 name = currSeq.getName();
315 //if this name is in the accnos file
316 if (names.count(name) == 0) {
317 wroteSomething = true;
319 currSeq.printSequence(out);
327 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
328 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
333 catch(exception& e) {
334 m->errorOut(e, "RemoveGroupsCommand", "readFasta");
339 //**********************************************************************************************************************
340 int RemoveGroupsCommand::readList(){
342 string thisOutputDir = outputDir;
343 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
344 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
347 m->openOutputFile(outputFileName, out);
350 m->openInputFile(listfile, in);
352 bool wroteSomething = false;
355 //read in list vector
358 //make a new list vector
360 newList.setLabel(list.getLabel());
363 for (int i = 0; i < list.getNumBins(); i++) {
364 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
366 //parse out names that are in accnos file
367 string binnames = list.get(i);
369 string newNames = "";
370 while (binnames.find_first_of(',') != -1) {
371 string name = binnames.substr(0,binnames.find_first_of(','));
372 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
374 //if that name is in the .accnos file, add it
375 if (names.count(name) == 0) { newNames += name + ","; }
379 if (names.count(binnames) == 0) { newNames += binnames + ","; }
381 //if there are names in this bin add to new list
382 if (newNames != "") {
383 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
384 newList.push_back(newNames);
388 //print new listvector
389 if (newList.getNumBins() != 0) {
390 wroteSomething = true;
399 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
400 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
405 catch(exception& e) {
406 m->errorOut(e, "RemoveGroupsCommand", "readList");
410 //**********************************************************************************************************************
411 int RemoveGroupsCommand::readName(){
413 string thisOutputDir = outputDir;
414 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
415 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
418 m->openOutputFile(outputFileName, out);
421 m->openInputFile(namefile, in);
422 string name, firstCol, secondCol;
424 bool wroteSomething = false;
427 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
429 in >> firstCol; m->gobble(in);
432 vector<string> parsedNames;
433 m->splitAtComma(secondCol, parsedNames);
435 vector<string> validSecond; validSecond.clear();
436 for (int i = 0; i < parsedNames.size(); i++) {
437 if (names.count(parsedNames[i]) == 0) {
438 validSecond.push_back(parsedNames[i]);
443 //if the name in the first column is in the set then print it and any other names in second column also in set
444 if (names.count(firstCol) == 0) {
446 wroteSomething = true;
448 out << firstCol << '\t';
450 //you know you have at least one valid second since first column is valid
451 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
452 out << validSecond[validSecond.size()-1] << endl;
454 //make first name in set you come to first column and then add the remaining names to second column
457 //you want part of this row
458 if (validSecond.size() != 0) {
460 wroteSomething = true;
462 out << validSecond[0] << '\t';
464 //you know you have at least one valid second since first column is valid
465 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
466 out << validSecond[validSecond.size()-1] << endl;
475 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
476 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
480 catch(exception& e) {
481 m->errorOut(e, "RemoveGroupsCommand", "readName");
486 //**********************************************************************************************************************
487 int RemoveGroupsCommand::readGroup(){
489 string thisOutputDir = outputDir;
490 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
491 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
494 m->openOutputFile(outputFileName, out);
497 m->openInputFile(groupfile, in);
500 bool wroteSomething = false;
503 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
505 in >> name; //read from first column
506 in >> group; //read from second column
508 //if this name is in the accnos file
509 if (names.count(name) == 0) {
510 wroteSomething = true;
511 out << name << '\t' << group << endl;
519 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
520 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
524 catch(exception& e) {
525 m->errorOut(e, "RemoveGroupsCommand", "readGroup");
529 //**********************************************************************************************************************
530 int RemoveGroupsCommand::readTax(){
532 string thisOutputDir = outputDir;
533 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
534 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
536 m->openOutputFile(outputFileName, out);
539 m->openInputFile(taxfile, in);
542 bool wroteSomething = false;
545 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
547 in >> name; //read from first column
548 in >> tax; //read from second column
550 //if this name is in the accnos file
551 if (names.count(name) == 0) {
552 wroteSomething = true;
553 out << name << '\t' << tax << endl;
561 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
562 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
566 catch(exception& e) {
567 m->errorOut(e, "RemoveGroupsCommand", "readTax");
571 //**********************************************************************************************************************
572 void RemoveGroupsCommand::readAccnos(){
577 m->openInputFile(accnosfile, in);
583 Groups.push_back(name);
590 catch(exception& e) {
591 m->errorOut(e, "RemoveGroupsCommand", "readAccnos");
595 //**********************************************************************************************************************
596 int RemoveGroupsCommand::fillNames(){
598 vector<string> seqs = groupMap->getNamesSeqs();
600 for (int i = 0; i < seqs.size(); i++) {
602 if (m->control_pressed) { return 0; }
604 string group = groupMap->getGroup(seqs[i]);
606 if (m->inUsersGroups(group, Groups)) {
607 names.insert(seqs[i]);
613 catch(exception& e) {
614 m->errorOut(e, "RemoveGroupsCommand", "fillNames");
619 //**********************************************************************************************************************