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 = ""; }
169 else { m->setAccnosFile(accnosfile); }
171 fastafile = validParameter.validFile(parameters, "fasta", true);
172 if (fastafile == "not open") { abort = true; }
173 else if (fastafile == "not found") { fastafile = ""; }
174 else { m->setFastaFile(fastafile); }
176 namefile = validParameter.validFile(parameters, "name", true);
177 if (namefile == "not open") { abort = true; }
178 else if (namefile == "not found") { namefile = ""; }
179 else { m->setNameFile(namefile); }
181 groupfile = validParameter.validFile(parameters, "group", true);
182 if (groupfile == "not open") { abort = true; }
183 else if (groupfile == "not found") {
184 //if there is a current group file, use it
185 groupfile = m->getGroupFile();
186 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
187 else { m->mothurOut("You have no current groupfile and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
188 }else { m->setGroupFile(groupfile); }
190 listfile = validParameter.validFile(parameters, "list", true);
191 if (listfile == "not open") { abort = true; }
192 else if (listfile == "not found") { listfile = ""; }
193 else { m->setListFile(listfile); }
195 taxfile = validParameter.validFile(parameters, "taxonomy", true);
196 if (taxfile == "not open") { abort = true; }
197 else if (taxfile == "not found") { taxfile = ""; }
198 else { m->setTaxonomyFile(taxfile); }
200 groups = validParameter.validFile(parameters, "groups", false);
201 if (groups == "not found") { groups = ""; }
203 m->splitAtDash(groups, Groups);
206 if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; }
208 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; }
212 catch(exception& e) {
213 m->errorOut(e, "RemoveGroupsCommand", "RemoveGroupsCommand");
217 //**********************************************************************************************************************
219 int RemoveGroupsCommand::execute(){
222 if (abort == true) { if (calledHelp) { return 0; } return 2; }
224 groupMap = new GroupMap(groupfile);
227 //get groups you want to remove
228 if (accnosfile != "") { readAccnos(); }
230 //make sure groups are valid
231 //takes care of user setting groupNames that are invalid or setting groups=all
232 SharedUtil* util = new SharedUtil();
233 util->setGroups(Groups, groupMap->namesOfGroups);
236 //fill names with names of sequences that are from the groups we want to remove
239 if (m->control_pressed) { delete groupMap; return 0; }
241 //read through the correct file and output lines you want to keep
242 if (namefile != "") { readName(); }
243 if (fastafile != "") { readFasta(); }
244 if (groupfile != "") { readGroup(); }
245 if (listfile != "") { readList(); }
246 if (taxfile != "") { readTax(); }
248 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
250 m->mothurOut("Removed " + toString(names.size()) + " sequences. From the groups: "); m->mothurOutEndLine();
251 for (int i = 0; i < Groups.size(); i++) { m->mothurOut(Groups[i]); m->mothurOut("\t" + toString(groupMap->getNumSeqs(Groups[i]))); m->mothurOutEndLine(); }
252 m->mothurOutEndLine();
254 if (outputNames.size() != 0) {
255 m->mothurOutEndLine();
256 m->mothurOut("Output File names: "); m->mothurOutEndLine();
257 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
258 m->mothurOutEndLine();
260 //set fasta file as new current fastafile
262 itTypes = outputTypes.find("fasta");
263 if (itTypes != outputTypes.end()) {
264 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
267 itTypes = outputTypes.find("name");
268 if (itTypes != outputTypes.end()) {
269 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
272 itTypes = outputTypes.find("group");
273 if (itTypes != outputTypes.end()) {
274 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
277 itTypes = outputTypes.find("list");
278 if (itTypes != outputTypes.end()) {
279 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
282 itTypes = outputTypes.find("taxonomy");
283 if (itTypes != outputTypes.end()) {
284 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
291 catch(exception& e) {
292 m->errorOut(e, "RemoveGroupsCommand", "execute");
297 //**********************************************************************************************************************
298 int RemoveGroupsCommand::readFasta(){
300 string thisOutputDir = outputDir;
301 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
302 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
305 m->openOutputFile(outputFileName, out);
308 m->openInputFile(fastafile, in);
311 bool wroteSomething = false;
314 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
316 Sequence currSeq(in);
317 name = currSeq.getName();
320 //if this name is in the accnos file
321 if (names.count(name) == 0) {
322 wroteSomething = true;
324 currSeq.printSequence(out);
332 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
333 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
338 catch(exception& e) {
339 m->errorOut(e, "RemoveGroupsCommand", "readFasta");
344 //**********************************************************************************************************************
345 int RemoveGroupsCommand::readList(){
347 string thisOutputDir = outputDir;
348 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
349 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
352 m->openOutputFile(outputFileName, out);
355 m->openInputFile(listfile, in);
357 bool wroteSomething = false;
360 //read in list vector
363 //make a new list vector
365 newList.setLabel(list.getLabel());
368 for (int i = 0; i < list.getNumBins(); i++) {
369 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
371 //parse out names that are in accnos file
372 string binnames = list.get(i);
374 string newNames = "";
375 while (binnames.find_first_of(',') != -1) {
376 string name = binnames.substr(0,binnames.find_first_of(','));
377 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
379 //if that name is in the .accnos file, add it
380 if (names.count(name) == 0) { newNames += name + ","; }
384 if (names.count(binnames) == 0) { newNames += binnames + ","; }
386 //if there are names in this bin add to new list
387 if (newNames != "") {
388 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
389 newList.push_back(newNames);
393 //print new listvector
394 if (newList.getNumBins() != 0) {
395 wroteSomething = true;
404 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
405 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
410 catch(exception& e) {
411 m->errorOut(e, "RemoveGroupsCommand", "readList");
415 //**********************************************************************************************************************
416 int RemoveGroupsCommand::readName(){
418 string thisOutputDir = outputDir;
419 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
420 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
423 m->openOutputFile(outputFileName, out);
426 m->openInputFile(namefile, in);
427 string name, firstCol, secondCol;
429 bool wroteSomething = false;
432 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
434 in >> firstCol; m->gobble(in);
437 vector<string> parsedNames;
438 m->splitAtComma(secondCol, parsedNames);
440 vector<string> validSecond; validSecond.clear();
441 for (int i = 0; i < parsedNames.size(); i++) {
442 if (names.count(parsedNames[i]) == 0) {
443 validSecond.push_back(parsedNames[i]);
448 //if the name in the first column is in the set then print it and any other names in second column also in set
449 if (names.count(firstCol) == 0) {
451 wroteSomething = true;
453 out << firstCol << '\t';
455 //you know you have at least one valid second since first column is valid
456 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
457 out << validSecond[validSecond.size()-1] << endl;
459 //make first name in set you come to first column and then add the remaining names to second column
462 //you want part of this row
463 if (validSecond.size() != 0) {
465 wroteSomething = true;
467 out << validSecond[0] << '\t';
469 //you know you have at least one valid second since first column is valid
470 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
471 out << validSecond[validSecond.size()-1] << endl;
480 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
481 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
485 catch(exception& e) {
486 m->errorOut(e, "RemoveGroupsCommand", "readName");
491 //**********************************************************************************************************************
492 int RemoveGroupsCommand::readGroup(){
494 string thisOutputDir = outputDir;
495 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
496 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
499 m->openOutputFile(outputFileName, out);
502 m->openInputFile(groupfile, in);
505 bool wroteSomething = false;
508 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
510 in >> name; //read from first column
511 in >> group; //read from second column
513 //if this name is in the accnos file
514 if (names.count(name) == 0) {
515 wroteSomething = true;
516 out << name << '\t' << group << endl;
524 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
525 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
529 catch(exception& e) {
530 m->errorOut(e, "RemoveGroupsCommand", "readGroup");
534 //**********************************************************************************************************************
535 int RemoveGroupsCommand::readTax(){
537 string thisOutputDir = outputDir;
538 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
539 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
541 m->openOutputFile(outputFileName, out);
544 m->openInputFile(taxfile, in);
547 bool wroteSomething = false;
550 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
552 in >> name; //read from first column
553 in >> tax; //read from second column
555 //if this name is in the accnos file
556 if (names.count(name) == 0) {
557 wroteSomething = true;
558 out << name << '\t' << tax << endl;
566 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
567 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
571 catch(exception& e) {
572 m->errorOut(e, "RemoveGroupsCommand", "readTax");
576 //**********************************************************************************************************************
577 void RemoveGroupsCommand::readAccnos(){
582 m->openInputFile(accnosfile, in);
588 Groups.push_back(name);
595 catch(exception& e) {
596 m->errorOut(e, "RemoveGroupsCommand", "readAccnos");
600 //**********************************************************************************************************************
601 int RemoveGroupsCommand::fillNames(){
603 vector<string> seqs = groupMap->getNamesSeqs();
605 for (int i = 0; i < seqs.size(); i++) {
607 if (m->control_pressed) { return 0; }
609 string group = groupMap->getGroup(seqs[i]);
611 if (m->inUsersGroups(group, Groups)) {
612 names.insert(seqs[i]);
618 catch(exception& e) {
619 m->errorOut(e, "RemoveGroupsCommand", "fillNames");
624 //**********************************************************************************************************************