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"
14 #include "inputdata.h"
16 //**********************************************************************************************************************
17 vector<string> RemoveGroupsCommand::setParameters(){
19 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "FNGLT",false,false); parameters.push_back(pfasta);
20 CommandParameter pshared("shared", "InputTypes", "", "", "none", "sharedGroup", "none",false,false); parameters.push_back(pshared);
21 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
22 CommandParameter pgroup("group", "InputTypes", "", "", "none", "sharedGroup", "FNGLT",false,false); parameters.push_back(pgroup);
23 CommandParameter pdesign("design", "InputTypes", "", "", "none", "sharedGroup", "FNGLT",false,false); parameters.push_back(pdesign);
24 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "FNGLT",false,false); parameters.push_back(plist);
25 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "FNGLT",false,false); parameters.push_back(ptaxonomy);
26 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(paccnos);
27 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
28 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
29 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
31 vector<string> myArray;
32 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
36 m->errorOut(e, "RemoveGroupsCommand", "setParameters");
40 //**********************************************************************************************************************
41 string RemoveGroupsCommand::getHelpString(){
43 string helpString = "";
44 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, design or sharedfile.\n";
45 helpString += "It outputs a file containing the sequences NOT in the those specified groups, or with a sharedfile eliminates the groups you selected.\n";
46 helpString += "The remove.groups command parameters are accnos, fasta, name, group, list, taxonomy, shared, design and groups. The group parameter is required, unless you have a current group file or are using a sharedfile.\n";
47 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";
48 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";
49 helpString += "The remove.groups command should be in the following format: remove.groups(accnos=yourAccnos, fasta=yourFasta, group=yourGroupFile).\n";
50 helpString += "Example remove.groups(accnos=amazon.accnos, fasta=amazon.fasta, group=amazon.groups).\n";
51 helpString += "or remove.groups(groups=pasture, fasta=amazon.fasta, amazon.groups).\n";
52 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
56 m->errorOut(e, "RemoveGroupsCommand", "getHelpString");
61 //**********************************************************************************************************************
62 RemoveGroupsCommand::RemoveGroupsCommand(){
64 abort = true; calledHelp = true;
66 vector<string> tempOutNames;
67 outputTypes["fasta"] = tempOutNames;
68 outputTypes["taxonomy"] = tempOutNames;
69 outputTypes["name"] = tempOutNames;
70 outputTypes["group"] = tempOutNames;
71 outputTypes["list"] = tempOutNames;
72 outputTypes["shared"] = tempOutNames;
73 outputTypes["design"] = tempOutNames;
76 m->errorOut(e, "RemoveGroupsCommand", "RemoveGroupsCommand");
80 //**********************************************************************************************************************
81 RemoveGroupsCommand::RemoveGroupsCommand(string option) {
83 abort = false; calledHelp = false;
85 //allow user to run help
86 if(option == "help") { help(); abort = true; calledHelp = true; }
87 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
90 vector<string> myArray = setParameters();
92 OptionParser parser(option);
93 map<string,string> parameters = parser.getParameters();
95 ValidParameters validParameter;
96 map<string,string>::iterator it;
98 //check to make sure all parameters are valid for command
99 for (it = parameters.begin(); it != parameters.end(); it++) {
100 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
103 //initialize outputTypes
104 vector<string> tempOutNames;
105 outputTypes["fasta"] = tempOutNames;
106 outputTypes["taxonomy"] = tempOutNames;
107 outputTypes["name"] = tempOutNames;
108 outputTypes["group"] = tempOutNames;
109 outputTypes["list"] = tempOutNames;
110 outputTypes["shared"] = tempOutNames;
111 outputTypes["design"] = tempOutNames;
114 //if the user changes the output directory command factory will send this info to us in the output parameter
115 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
117 //if the user changes the input directory command factory will send this info to us in the output parameter
118 string inputDir = validParameter.validFile(parameters, "inputdir", false);
119 if (inputDir == "not found"){ inputDir = ""; }
122 it = parameters.find("fasta");
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["fasta"] = inputDir + it->second; }
130 it = parameters.find("accnos");
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["accnos"] = inputDir + it->second; }
138 it = parameters.find("list");
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["list"] = inputDir + it->second; }
146 it = parameters.find("name");
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["name"] = inputDir + it->second; }
154 it = parameters.find("group");
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["group"] = inputDir + it->second; }
162 it = parameters.find("taxonomy");
163 //user has given a template file
164 if(it != parameters.end()){
165 path = m->hasPath(it->second);
166 //if the user has not given a path then, add inputdir. else leave path alone.
167 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
170 it = parameters.find("shared");
171 //user has given a template file
172 if(it != parameters.end()){
173 path = m->hasPath(it->second);
174 //if the user has not given a path then, add inputdir. else leave path alone.
175 if (path == "") { parameters["shared"] = inputDir + it->second; }
178 it = parameters.find("design");
179 //user has given a template file
180 if(it != parameters.end()){
181 path = m->hasPath(it->second);
182 //if the user has not given a path then, add inputdir. else leave path alone.
183 if (path == "") { parameters["design"] = inputDir + it->second; }
188 //check for required parameters
189 accnosfile = validParameter.validFile(parameters, "accnos", true);
190 if (accnosfile == "not open") { accnosfile = ""; abort = true; }
191 else if (accnosfile == "not found") { accnosfile = ""; }
192 else { m->setAccnosFile(accnosfile); }
194 fastafile = validParameter.validFile(parameters, "fasta", true);
195 if (fastafile == "not open") { fastafile = ""; abort = true; }
196 else if (fastafile == "not found") { fastafile = ""; }
197 else { m->setFastaFile(fastafile); }
199 namefile = validParameter.validFile(parameters, "name", true);
200 if (namefile == "not open") { namefile = ""; abort = true; }
201 else if (namefile == "not found") { namefile = ""; }
202 else { m->setNameFile(namefile); }
204 groupfile = validParameter.validFile(parameters, "group", true);
205 if (groupfile == "not open") { groupfile = ""; abort = true; }
206 else if (groupfile == "not found") { groupfile = ""; }
207 else { m->setGroupFile(groupfile); }
209 listfile = validParameter.validFile(parameters, "list", true);
210 if (listfile == "not open") { listfile = ""; abort = true; }
211 else if (listfile == "not found") { listfile = ""; }
212 else { m->setListFile(listfile); }
214 taxfile = validParameter.validFile(parameters, "taxonomy", true);
215 if (taxfile == "not open") { taxfile = ""; abort = true; }
216 else if (taxfile == "not found") { taxfile = ""; }
217 else { m->setTaxonomyFile(taxfile); }
219 designfile = validParameter.validFile(parameters, "design", true);
220 if (designfile == "not open") { designfile = ""; abort = true; }
221 else if (designfile == "not found") { designfile = ""; }
222 else { m->setDesignFile(designfile); }
224 groups = validParameter.validFile(parameters, "groups", false);
225 if (groups == "not found") { groups = ""; }
227 m->splitAtDash(groups, Groups);
228 m->setGroups(Groups);
231 sharedfile = validParameter.validFile(parameters, "shared", true);
232 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
233 else if (sharedfile == "not found") { sharedfile = ""; }
234 else { m->setSharedFile(sharedfile); }
236 groupfile = validParameter.validFile(parameters, "group", true);
237 if (groupfile == "not open") { groupfile = ""; abort = true; }
238 else if (groupfile == "not found") { groupfile = ""; }
239 else { m->setGroupFile(groupfile); }
241 if ((sharedfile == "") && (groupfile == "") && (designfile == "")) {
242 //is there are current file available for any of these?
243 if ((namefile != "") || (fastafile != "") || (listfile != "") || (taxfile != "")) {
244 //give priority to group, then shared
245 groupfile = m->getGroupFile();
246 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
248 sharedfile = m->getSharedFile();
249 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
251 m->mothurOut("You have no current groupfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
255 //give priority to shared, then group
256 sharedfile = m->getSharedFile();
257 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
259 groupfile = m->getGroupFile();
260 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
262 designfile = m->getDesignFile();
263 if (designfile != "") { m->mothurOut("Using " + designfile + " as input file for the design parameter."); m->mothurOutEndLine(); }
265 m->mothurOut("You have no current groupfile or sharedfile or designfile and one is required."); m->mothurOutEndLine(); abort = true;
272 if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file containing group names or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; }
274 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (sharedfile == "") && (designfile == "") && (listfile == "") && (taxfile == "")) { m->mothurOut("You must provide at least one of the following: fasta, name, taxonomy, group, shared, design or list."); m->mothurOutEndLine(); abort = true; }
275 if ((groupfile == "") && ((namefile != "") || (fastafile != "") || (listfile != "") || (taxfile != ""))) { m->mothurOut("If using a fasta, name, taxonomy, group or list, then you must provide a group file."); m->mothurOutEndLine(); abort = true; }
277 if ((namefile == "") && ((fastafile != "") || (taxfile != ""))){
278 vector<string> files; files.push_back(fastafile); files.push_back(taxfile);
279 parser.getNameFile(files);
285 catch(exception& e) {
286 m->errorOut(e, "RemoveGroupsCommand", "RemoveGroupsCommand");
290 //**********************************************************************************************************************
292 int RemoveGroupsCommand::execute(){
295 if (abort == true) { if (calledHelp) { return 0; } return 2; }
297 //get groups you want to remove
298 if (accnosfile != "") { readAccnos(); }
300 if (groupfile != "") {
301 groupMap = new GroupMap(groupfile);
304 //make sure groups are valid
305 //takes care of user setting groupNames that are invalid or setting groups=all
306 SharedUtil* util = new SharedUtil();
307 vector<string> namesGroups = groupMap->getNamesOfGroups();
308 util->setGroups(Groups, namesGroups);
311 //fill names with names of sequences that are from the groups we want to remove
317 if (m->control_pressed) { return 0; }
319 //read through the correct file and output lines you want to keep
320 if (namefile != "") { readName(); }
321 if (fastafile != "") { readFasta(); }
322 if (groupfile != "") { readGroup(); }
323 if (listfile != "") { readList(); }
324 if (taxfile != "") { readTax(); }
325 if (sharedfile != "") { readShared(); }
326 if (designfile != "") { readDesign(); }
328 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
330 if (outputNames.size() != 0) {
331 m->mothurOutEndLine();
332 m->mothurOut("Output File names: "); m->mothurOutEndLine();
333 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
334 m->mothurOutEndLine();
336 //set fasta file as new current fastafile
338 itTypes = outputTypes.find("fasta");
339 if (itTypes != outputTypes.end()) {
340 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
343 itTypes = outputTypes.find("name");
344 if (itTypes != outputTypes.end()) {
345 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
348 itTypes = outputTypes.find("group");
349 if (itTypes != outputTypes.end()) {
350 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
353 itTypes = outputTypes.find("list");
354 if (itTypes != outputTypes.end()) {
355 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
358 itTypes = outputTypes.find("taxonomy");
359 if (itTypes != outputTypes.end()) {
360 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
363 itTypes = outputTypes.find("shared");
364 if (itTypes != outputTypes.end()) {
365 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
368 itTypes = outputTypes.find("design");
369 if (itTypes != outputTypes.end()) {
370 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setDesignFile(current); }
377 catch(exception& e) {
378 m->errorOut(e, "RemoveGroupsCommand", "execute");
383 //**********************************************************************************************************************
384 int RemoveGroupsCommand::readFasta(){
386 string thisOutputDir = outputDir;
387 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
388 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
391 m->openOutputFile(outputFileName, out);
394 m->openInputFile(fastafile, in);
397 bool wroteSomething = false;
398 int removedCount = 0;
401 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
403 Sequence currSeq(in);
404 name = currSeq.getName();
407 //if this name is in the accnos file
408 if (names.count(name) == 0) {
409 wroteSomething = true;
410 currSeq.printSequence(out);
412 //if you are not in the accnos file check if you are a name that needs to be changed
413 map<string, string>::iterator it = uniqueToRedundant.find(name);
414 if (it != uniqueToRedundant.end()) {
415 wroteSomething = true;
416 currSeq.setName(it->second);
417 currSeq.printSequence(out);
418 }else { removedCount++; }
426 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
427 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
429 m->mothurOut("Removed " + toString(removedCount) + " sequences from your fasta file."); m->mothurOutEndLine();
434 catch(exception& e) {
435 m->errorOut(e, "RemoveGroupsCommand", "readFasta");
439 //**********************************************************************************************************************
440 int RemoveGroupsCommand::readShared(){
442 string thisOutputDir = outputDir;
443 if (outputDir == "") { thisOutputDir += m->hasPath(sharedfile); }
445 //get group names from sharedfile so we can set Groups to the groupNames we want to keep
446 //that way we can take advantage of the reads in inputdata and sharedRabundVector
447 InputData* tempInput = new InputData(sharedfile, "sharedfile");
448 vector<SharedRAbundVector*> lookup = tempInput->getSharedRAbundVectors();
451 vector<string> allGroupsNames = m->getAllGroups();
452 vector<string> mothurOutGroups = m->getGroups();
454 vector<string> groupsToKeep;
455 for (int i = 0; i < allGroupsNames.size(); i++) {
456 if (!m->inUsersGroups(allGroupsNames[i], m->getGroups())) {
457 groupsToKeep.push_back(allGroupsNames[i]);
461 if (allGroupsNames.size() == groupsToKeep.size()) { m->mothurOut("Your file does not contain any groups you wish to remove."); m->mothurOutEndLine(); m->setGroups(mothurOutGroups); delete tempInput; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
464 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
466 m->setGroups(groupsToKeep);
468 m->saveNextLabel = "";
469 m->printedHeaders = false;
470 m->currentBinLabels.clear();
471 m->binLabelsInFile.clear();
473 InputData input(sharedfile, "sharedfile");
474 lookup = input.getSharedRAbundVectors();
476 bool wroteSomething = false;
478 while(lookup[0] != NULL) {
480 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + lookup[0]->getLabel() + ".pick" + m->getExtension(sharedfile);
482 m->openOutputFile(outputFileName, out);
483 outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName);
485 if (m->control_pressed) { out.close(); m->mothurRemove(outputFileName); for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
487 lookup[0]->printHeaders(out);
489 for (int i = 0; i < lookup.size(); i++) {
490 out << lookup[i]->getLabel() << '\t' << lookup[i]->getGroup() << '\t';
491 lookup[i]->print(out);
492 wroteSomething = true;
496 //get next line to process
497 //prevent memory leak
498 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
499 lookup = input.getSharedRAbundVectors();
505 m->setGroups(mothurOutGroups);
507 if (wroteSomething == false) { m->mothurOut("Your file contains only the groups you wish to remove."); m->mothurOutEndLine(); }
509 string groupsString = "";
510 for (int i = 0; i < Groups.size()-1; i++) { groupsString += Groups[i] + ", "; }
511 groupsString += Groups[Groups.size()-1];
513 m->mothurOut("Removed groups: " + groupsString + " from your shared file."); m->mothurOutEndLine();
518 catch(exception& e) {
519 m->errorOut(e, "RemoveGroupsCommand", "readShared");
523 //**********************************************************************************************************************
524 int RemoveGroupsCommand::readList(){
526 string thisOutputDir = outputDir;
527 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
528 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
531 m->openOutputFile(outputFileName, out);
534 m->openInputFile(listfile, in);
536 bool wroteSomething = false;
537 int removedCount = 0;
543 //read in list vector
546 //make a new list vector
548 newList.setLabel(list.getLabel());
551 for (int i = 0; i < list.getNumBins(); i++) {
552 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
554 //parse out names that are in accnos file
555 string binnames = list.get(i);
557 string newNames = "";
558 while (binnames.find_first_of(',') != -1) {
559 string name = binnames.substr(0,binnames.find_first_of(','));
560 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
562 //if that name is in the .accnos file, add it
563 if (names.count(name) == 0) { newNames += name + ","; }
565 //if you are not in the accnos file check if you are a name that needs to be changed
566 map<string, string>::iterator it = uniqueToRedundant.find(name);
567 if (it != uniqueToRedundant.end()) {
568 newNames += it->second + ",";
569 }else { removedCount++; }
574 if (names.count(binnames) == 0) { newNames += binnames + ","; }
575 else { //if you are not in the accnos file check if you are a name that needs to be changed
576 map<string, string>::iterator it = uniqueToRedundant.find(binnames);
577 if (it != uniqueToRedundant.end()) {
578 newNames += it->second + ",";
579 }else { removedCount++; }
582 //if there are names in this bin add to new list
583 if (newNames != "") {
584 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
585 newList.push_back(newNames);
589 //print new listvector
590 if (newList.getNumBins() != 0) {
591 wroteSomething = true;
600 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
601 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
603 m->mothurOut("Removed " + toString(removedCount) + " sequences from your list file."); m->mothurOutEndLine();
608 catch(exception& e) {
609 m->errorOut(e, "RemoveGroupsCommand", "readList");
613 //**********************************************************************************************************************
614 int RemoveGroupsCommand::readName(){
616 string thisOutputDir = outputDir;
617 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
618 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
621 m->openOutputFile(outputFileName, out);
624 m->openInputFile(namefile, in);
625 string name, firstCol, secondCol;
627 bool wroteSomething = false;
628 int removedCount = 0;
631 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
633 in >> firstCol; m->gobble(in);
636 vector<string> parsedNames;
637 m->splitAtComma(secondCol, parsedNames);
639 vector<string> validSecond; validSecond.clear();
640 for (int i = 0; i < parsedNames.size(); i++) {
641 if (names.count(parsedNames[i]) == 0) {
642 validSecond.push_back(parsedNames[i]);
646 removedCount += parsedNames.size()-validSecond.size();
648 //if the name in the first column is in the set then print it and any other names in second column also in set
649 if (names.count(firstCol) == 0) {
651 wroteSomething = true;
653 out << firstCol << '\t';
655 //you know you have at least one valid second since first column is valid
656 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
657 out << validSecond[validSecond.size()-1] << endl;
659 //make first name in set you come to first column and then add the remaining names to second column
662 //you want part of this row
663 if (validSecond.size() != 0) {
665 wroteSomething = true;
667 out << validSecond[0] << '\t';
669 //you know you have at least one valid second since first column is valid
670 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
671 out << validSecond[validSecond.size()-1] << endl;
672 uniqueToRedundant[firstCol] = validSecond[0];
681 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
682 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
684 m->mothurOut("Removed " + toString(removedCount) + " sequences from your name file."); m->mothurOutEndLine();
688 catch(exception& e) {
689 m->errorOut(e, "RemoveGroupsCommand", "readName");
694 //**********************************************************************************************************************
695 int RemoveGroupsCommand::readGroup(){
697 string thisOutputDir = outputDir;
698 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
699 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
702 m->openOutputFile(outputFileName, out);
705 m->openInputFile(groupfile, in);
708 bool wroteSomething = false;
709 int removedCount = 0;
712 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
714 in >> name; //read from first column
715 in >> group; //read from second column
717 //if this name is in the accnos file
718 if (names.count(name) == 0) {
719 wroteSomething = true;
720 out << name << '\t' << group << endl;
721 }else { removedCount++; }
728 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
729 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
731 m->mothurOut("Removed " + toString(removedCount) + " sequences from your group file."); m->mothurOutEndLine();
736 catch(exception& e) {
737 m->errorOut(e, "RemoveGroupsCommand", "readGroup");
741 //**********************************************************************************************************************
742 int RemoveGroupsCommand::readDesign(){
744 string thisOutputDir = outputDir;
745 if (outputDir == "") { thisOutputDir += m->hasPath(designfile); }
746 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(designfile)) + "pick" + m->getExtension(designfile);
749 m->openOutputFile(outputFileName, out);
752 m->openInputFile(designfile, in);
755 bool wroteSomething = false;
756 int removedCount = 0;
759 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
761 in >> name; //read from first column
762 in >> group; //read from second column
764 //if this name is in the accnos file
765 if (!(m->inUsersGroups(name, Groups))) {
766 wroteSomething = true;
767 out << name << '\t' << group << endl;
768 }else { removedCount++; }
775 if (wroteSomething == false) { m->mothurOut("Your file contains only groups from the groups you wish to remove."); m->mothurOutEndLine(); }
776 outputTypes["design"].push_back(outputFileName); outputNames.push_back(outputFileName);
778 m->mothurOut("Removed " + toString(removedCount) + " groups from your design file."); m->mothurOutEndLine();
783 catch(exception& e) {
784 m->errorOut(e, "RemoveGroupsCommand", "readDesign");
789 //**********************************************************************************************************************
790 int RemoveGroupsCommand::readTax(){
792 string thisOutputDir = outputDir;
793 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
794 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
796 m->openOutputFile(outputFileName, out);
799 m->openInputFile(taxfile, in);
802 bool wroteSomething = false;
803 int removedCount = 0;
806 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
808 in >> name; //read from first column
809 in >> tax; //read from second column
811 //if this name is in the accnos file
812 if (names.count(name) == 0) {
813 wroteSomething = true;
814 out << name << '\t' << tax << endl;
815 }else { //if you are not in the accnos file check if you are a name that needs to be changed
816 map<string, string>::iterator it = uniqueToRedundant.find(name);
817 if (it != uniqueToRedundant.end()) {
818 wroteSomething = true;
819 out << it->second << '\t' << tax << endl;
820 }else { removedCount++; } }
827 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
828 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
830 m->mothurOut("Removed " + toString(removedCount) + " sequences from your taxonomy file."); m->mothurOutEndLine();
834 catch(exception& e) {
835 m->errorOut(e, "RemoveGroupsCommand", "readTax");
839 //**********************************************************************************************************************
840 void RemoveGroupsCommand::readAccnos(){
845 m->openInputFile(accnosfile, in);
851 Groups.push_back(name);
857 m->setGroups(Groups);
860 catch(exception& e) {
861 m->errorOut(e, "RemoveGroupsCommand", "readAccnos");
865 //**********************************************************************************************************************
866 int RemoveGroupsCommand::fillNames(){
868 vector<string> seqs = groupMap->getNamesSeqs();
870 for (int i = 0; i < seqs.size(); i++) {
872 if (m->control_pressed) { return 0; }
874 string group = groupMap->getGroup(seqs[i]);
876 if (m->inUsersGroups(group, Groups)) {
877 names.insert(seqs[i]);
883 catch(exception& e) {
884 m->errorOut(e, "RemoveGroupsCommand", "fillNames");
889 //**********************************************************************************************************************