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","fasta",false,false,true); parameters.push_back(pfasta);
20 CommandParameter pshared("shared", "InputTypes", "", "", "none", "sharedGroup", "none","shared",false,false,true); parameters.push_back(pshared);
21 CommandParameter pname("name", "InputTypes", "", "", "NameCount", "none", "none","name",false,false,true); parameters.push_back(pname);
22 CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "none", "none","count",false,false,true); parameters.push_back(pcount);
23 CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "sharedGroup", "FNGLT","group",false,false,true); parameters.push_back(pgroup);
24 CommandParameter pdesign("design", "InputTypes", "", "", "none", "sharedGroup", "FNGLT","design",false,false); parameters.push_back(pdesign);
25 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "FNGLT","list",false,false,true); parameters.push_back(plist);
26 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "FNGLT","taxonomy",false,false,true); parameters.push_back(ptaxonomy);
27 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(paccnos);
28 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
29 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
30 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
32 vector<string> myArray;
33 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
37 m->errorOut(e, "RemoveGroupsCommand", "setParameters");
41 //**********************************************************************************************************************
42 string RemoveGroupsCommand::getHelpString(){
44 string helpString = "";
45 helpString += "The remove.groups command removes sequences from a specfic group or set of groups from the following file types: fasta, name, group, count, list, taxonomy, design or sharedfile.\n";
46 helpString += "It outputs a file containing the sequences NOT in the those specified groups, or with a sharedfile eliminates the groups you selected.\n";
47 helpString += "The remove.groups command parameters are accnos, fasta, name, group, list, taxonomy, shared, design and groups. The group or count parameter is required, unless you have a current group or count file or are using a sharedfile.\n";
48 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";
49 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";
50 helpString += "The remove.groups command should be in the following format: remove.groups(accnos=yourAccnos, fasta=yourFasta, group=yourGroupFile).\n";
51 helpString += "Example remove.groups(accnos=amazon.accnos, fasta=amazon.fasta, group=amazon.groups).\n";
52 helpString += "or remove.groups(groups=pasture, fasta=amazon.fasta, amazon.groups).\n";
53 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
57 m->errorOut(e, "RemoveGroupsCommand", "getHelpString");
61 //**********************************************************************************************************************
62 string RemoveGroupsCommand::getOutputPattern(string type) {
66 if (type == "fasta") { pattern = "[filename],pick,[extension]"; }
67 else if (type == "taxonomy") { pattern = "[filename],pick,[extension]"; }
68 else if (type == "name") { pattern = "[filename],pick,[extension]"; }
69 else if (type == "group") { pattern = "[filename],pick,[extension]"; }
70 else if (type == "count") { pattern = "[filename],pick,[extension]"; }
71 else if (type == "list") { pattern = "[filename],pick,[extension]"; }
72 else if (type == "shared") { pattern = "[filename],[tag],pick,[extension]"; }
73 else if (type == "design") { pattern = "[filename],[tag],pick,[extension]"; }
74 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
79 m->errorOut(e, "RemoveGroupsCommand", "getOutputPattern");
83 //**********************************************************************************************************************
84 RemoveGroupsCommand::RemoveGroupsCommand(){
86 abort = true; calledHelp = true;
88 vector<string> tempOutNames;
89 outputTypes["fasta"] = tempOutNames;
90 outputTypes["taxonomy"] = tempOutNames;
91 outputTypes["name"] = tempOutNames;
92 outputTypes["group"] = tempOutNames;
93 outputTypes["list"] = tempOutNames;
94 outputTypes["shared"] = tempOutNames;
95 outputTypes["design"] = tempOutNames;
96 outputTypes["count"] = tempOutNames;
99 m->errorOut(e, "RemoveGroupsCommand", "RemoveGroupsCommand");
103 //**********************************************************************************************************************
104 RemoveGroupsCommand::RemoveGroupsCommand(string option) {
106 abort = false; calledHelp = false;
108 //allow user to run help
109 if(option == "help") { help(); abort = true; calledHelp = true; }
110 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
113 vector<string> myArray = setParameters();
115 OptionParser parser(option);
116 map<string,string> parameters = parser.getParameters();
118 ValidParameters validParameter;
119 map<string,string>::iterator it;
121 //check to make sure all parameters are valid for command
122 for (it = parameters.begin(); it != parameters.end(); it++) {
123 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
126 //initialize outputTypes
127 vector<string> tempOutNames;
128 outputTypes["fasta"] = tempOutNames;
129 outputTypes["taxonomy"] = tempOutNames;
130 outputTypes["name"] = tempOutNames;
131 outputTypes["group"] = tempOutNames;
132 outputTypes["list"] = tempOutNames;
133 outputTypes["shared"] = tempOutNames;
134 outputTypes["design"] = tempOutNames;
135 outputTypes["count"] = tempOutNames;
138 //if the user changes the output directory command factory will send this info to us in the output parameter
139 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
141 //if the user changes the input directory command factory will send this info to us in the output parameter
142 string inputDir = validParameter.validFile(parameters, "inputdir", false);
143 if (inputDir == "not found"){ inputDir = ""; }
146 it = parameters.find("fasta");
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["fasta"] = inputDir + it->second; }
154 it = parameters.find("accnos");
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["accnos"] = inputDir + it->second; }
162 it = parameters.find("list");
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["list"] = inputDir + it->second; }
170 it = parameters.find("name");
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["name"] = inputDir + it->second; }
178 it = parameters.find("group");
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["group"] = inputDir + it->second; }
186 it = parameters.find("taxonomy");
187 //user has given a template file
188 if(it != parameters.end()){
189 path = m->hasPath(it->second);
190 //if the user has not given a path then, add inputdir. else leave path alone.
191 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
194 it = parameters.find("shared");
195 //user has given a template file
196 if(it != parameters.end()){
197 path = m->hasPath(it->second);
198 //if the user has not given a path then, add inputdir. else leave path alone.
199 if (path == "") { parameters["shared"] = inputDir + it->second; }
202 it = parameters.find("design");
203 //user has given a template file
204 if(it != parameters.end()){
205 path = m->hasPath(it->second);
206 //if the user has not given a path then, add inputdir. else leave path alone.
207 if (path == "") { parameters["design"] = inputDir + it->second; }
210 it = parameters.find("count");
211 //user has given a template file
212 if(it != parameters.end()){
213 path = m->hasPath(it->second);
214 //if the user has not given a path then, add inputdir. else leave path alone.
215 if (path == "") { parameters["count"] = inputDir + it->second; }
220 //check for required parameters
221 accnosfile = validParameter.validFile(parameters, "accnos", true);
222 if (accnosfile == "not open") { accnosfile = ""; abort = true; }
223 else if (accnosfile == "not found") { accnosfile = ""; }
224 else { m->setAccnosFile(accnosfile); }
226 fastafile = validParameter.validFile(parameters, "fasta", true);
227 if (fastafile == "not open") { fastafile = ""; abort = true; }
228 else if (fastafile == "not found") { fastafile = ""; }
229 else { m->setFastaFile(fastafile); }
231 namefile = validParameter.validFile(parameters, "name", true);
232 if (namefile == "not open") { namefile = ""; abort = true; }
233 else if (namefile == "not found") { namefile = ""; }
234 else { m->setNameFile(namefile); }
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 listfile = validParameter.validFile(parameters, "list", true);
242 if (listfile == "not open") { listfile = ""; abort = true; }
243 else if (listfile == "not found") { listfile = ""; }
244 else { m->setListFile(listfile); }
246 taxfile = validParameter.validFile(parameters, "taxonomy", true);
247 if (taxfile == "not open") { taxfile = ""; abort = true; }
248 else if (taxfile == "not found") { taxfile = ""; }
249 else { m->setTaxonomyFile(taxfile); }
251 designfile = validParameter.validFile(parameters, "design", true);
252 if (designfile == "not open") { designfile = ""; abort = true; }
253 else if (designfile == "not found") { designfile = ""; }
254 else { m->setDesignFile(designfile); }
256 groups = validParameter.validFile(parameters, "groups", false);
257 if (groups == "not found") { groups = ""; }
259 m->splitAtDash(groups, Groups);
260 m->setGroups(Groups);
263 sharedfile = validParameter.validFile(parameters, "shared", true);
264 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
265 else if (sharedfile == "not found") { sharedfile = ""; }
266 else { m->setSharedFile(sharedfile); }
269 countfile = validParameter.validFile(parameters, "count", true);
270 if (countfile == "not open") { countfile = ""; abort = true; }
271 else if (countfile == "not found") { countfile = ""; }
272 else { m->setCountTableFile(countfile); }
274 if ((namefile != "") && (countfile != "")) {
275 m->mothurOut("[ERROR]: you may only use one of the following: name or count."); m->mothurOutEndLine(); abort = true;
278 if ((groupfile != "") && (countfile != "")) {
279 m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
283 if ((sharedfile == "") && (groupfile == "") && (designfile == "") && (countfile == "")) {
284 //is there are current file available for any of these?
285 if ((namefile != "") || (fastafile != "") || (listfile != "") || (taxfile != "")) {
286 //give priority to group, then shared
287 groupfile = m->getGroupFile();
288 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
290 sharedfile = m->getSharedFile();
291 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
293 countfile = m->getCountTableFile();
294 if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
296 m->mothurOut("You have no current groupfile, countfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
301 //give priority to shared, then group
302 sharedfile = m->getSharedFile();
303 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
305 groupfile = m->getGroupFile();
306 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
308 designfile = m->getDesignFile();
309 if (designfile != "") { m->mothurOut("Using " + designfile + " as input file for the design parameter."); m->mothurOutEndLine(); }
311 countfile = m->getCountTableFile();
312 if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
314 m->mothurOut("You have no current groupfile, designfile, countfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
323 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; }
325 if ((fastafile == "") && (namefile == "") && (countfile == "") && (groupfile == "") && (designfile == "") && (sharedfile == "") && (listfile == "") && (taxfile == "")) { m->mothurOut("You must provide at least one of the following: fasta, name, taxonomy, group, shared, design, count or list."); m->mothurOutEndLine(); abort = true; }
326 if (((groupfile == "") && (countfile == "")) && ((namefile != "") || (fastafile != "") || (listfile != "") || (taxfile != ""))) { m->mothurOut("If using a fasta, name, taxonomy, group or list, then you must provide a group or count file."); m->mothurOutEndLine(); abort = true; }
328 if (countfile == "") {
329 if ((namefile == "") && ((fastafile != "") || (taxfile != ""))){
330 vector<string> files; files.push_back(fastafile); files.push_back(taxfile);
331 parser.getNameFile(files);
337 catch(exception& e) {
338 m->errorOut(e, "RemoveGroupsCommand", "RemoveGroupsCommand");
342 //**********************************************************************************************************************
344 int RemoveGroupsCommand::execute(){
347 if (abort == true) { if (calledHelp) { return 0; } return 2; }
349 //get groups you want to remove
350 if (accnosfile != "") { m->readAccnos(accnosfile, Groups); m->setGroups(Groups); }
352 if (groupfile != "") {
353 groupMap = new GroupMap(groupfile);
356 //make sure groups are valid
357 //takes care of user setting groupNames that are invalid or setting groups=all
358 vector<string> namesGroups = groupMap->getNamesOfGroups();
359 vector<string> checkedGroups;
360 for (int i = 0; i < Groups.size(); i++) {
361 if (m->inUsersGroups(Groups[i], namesGroups)) { checkedGroups.push_back(Groups[i]); }
362 else { m->mothurOut("[WARNING]: " + Groups[i] + " is not a valid group in your groupfile, ignoring.\n"); }
365 if (checkedGroups.size() == 0) { m->mothurOut("[ERROR]: no valid groups, aborting.\n"); delete groupMap; return 0; }
367 Groups = checkedGroups;
368 m->setGroups(Groups);
371 //fill names with names of sequences that are from the groups we want to remove
375 }else if (countfile != ""){
376 if ((fastafile != "") || (listfile != "") || (taxfile != "")) {
377 m->mothurOut("\n[NOTE]: The count file should contain only unique names, so mothur assumes your fasta, list and taxonomy files also contain only uniques.\n\n");
380 ct.readTable(countfile, true, false);
381 if (!ct.hasGroupInfo()) { m->mothurOut("[ERROR]: your count file does not contain group info, aborting.\n"); return 0; }
383 vector<string> gNamesOfGroups = ct.getNamesOfGroups();
385 util.setGroups(Groups, gNamesOfGroups);
386 vector<string> namesOfSeqs = ct.getNamesOfSeqs();
387 sort(Groups.begin(), Groups.end());
389 for (int i = 0; i < namesOfSeqs.size(); i++) {
390 vector<string> thisSeqsGroups = ct.getGroups(namesOfSeqs[i]);
391 if (m->isSubset(Groups, thisSeqsGroups)) { //you only have seqs from these groups so remove you
392 names.insert(namesOfSeqs[i]);
398 if (m->control_pressed) { return 0; }
400 //read through the correct file and output lines you want to keep
401 if (namefile != "") { readName(); }
402 if (fastafile != "") { readFasta(); }
403 if (groupfile != "") { readGroup(); }
404 if (countfile != "") { readCount(); }
405 if (listfile != "") { readList(); }
406 if (taxfile != "") { readTax(); }
407 if (sharedfile != "") { readShared(); }
408 if (designfile != "") { readDesign(); }
410 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
412 if (outputNames.size() != 0) {
413 m->mothurOutEndLine();
414 m->mothurOut("Output File names: "); m->mothurOutEndLine();
415 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
416 m->mothurOutEndLine();
418 //set fasta file as new current fastafile
420 itTypes = outputTypes.find("fasta");
421 if (itTypes != outputTypes.end()) {
422 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
425 itTypes = outputTypes.find("name");
426 if (itTypes != outputTypes.end()) {
427 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
430 itTypes = outputTypes.find("group");
431 if (itTypes != outputTypes.end()) {
432 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
435 itTypes = outputTypes.find("list");
436 if (itTypes != outputTypes.end()) {
437 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
440 itTypes = outputTypes.find("taxonomy");
441 if (itTypes != outputTypes.end()) {
442 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
445 itTypes = outputTypes.find("shared");
446 if (itTypes != outputTypes.end()) {
447 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
450 itTypes = outputTypes.find("design");
451 if (itTypes != outputTypes.end()) {
452 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setDesignFile(current); }
455 itTypes = outputTypes.find("count");
456 if (itTypes != outputTypes.end()) {
457 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
464 catch(exception& e) {
465 m->errorOut(e, "RemoveGroupsCommand", "execute");
470 //**********************************************************************************************************************
471 int RemoveGroupsCommand::readFasta(){
473 string thisOutputDir = outputDir;
474 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
475 map<string, string> variables;
476 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(fastafile));
477 variables["[extension]"] = m->getExtension(fastafile);
478 string outputFileName = getOutputFileName("fasta", variables);
481 m->openOutputFile(outputFileName, out);
484 m->openInputFile(fastafile, in);
487 bool wroteSomething = false;
488 int removedCount = 0;
491 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
493 Sequence currSeq(in);
494 name = currSeq.getName();
497 //if this name is in the accnos file
498 if (names.count(name) == 0) {
499 wroteSomething = true;
500 currSeq.printSequence(out);
502 //if you are not in the accnos file check if you are a name that needs to be changed
503 map<string, string>::iterator it = uniqueToRedundant.find(name);
504 if (it != uniqueToRedundant.end()) {
505 wroteSomething = true;
506 currSeq.setName(it->second);
507 currSeq.printSequence(out);
508 }else { removedCount++; }
516 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
517 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
519 m->mothurOut("Removed " + toString(removedCount) + " sequences from your fasta file."); m->mothurOutEndLine();
524 catch(exception& e) {
525 m->errorOut(e, "RemoveGroupsCommand", "readFasta");
529 //**********************************************************************************************************************
530 int RemoveGroupsCommand::readShared(){
532 string thisOutputDir = outputDir;
533 if (outputDir == "") { thisOutputDir += m->hasPath(sharedfile); }
535 //get group names from sharedfile so we can set Groups to the groupNames we want to keep
536 //that way we can take advantage of the reads in inputdata and sharedRabundVector
537 InputData* tempInput = new InputData(sharedfile, "sharedfile");
538 vector<SharedRAbundVector*> lookup = tempInput->getSharedRAbundVectors();
540 map<string, string> variables;
541 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile));
542 variables["[extension]"] = m->getExtension(sharedfile);
545 vector<string> allGroupsNames = m->getAllGroups();
546 vector<string> mothurOutGroups = m->getGroups();
548 vector<string> groupsToKeep;
549 for (int i = 0; i < allGroupsNames.size(); i++) {
550 if (!m->inUsersGroups(allGroupsNames[i], m->getGroups())) {
551 groupsToKeep.push_back(allGroupsNames[i]);
555 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; }
558 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
560 m->setGroups(groupsToKeep);
562 m->saveNextLabel = "";
563 m->printedSharedHeaders = false;
564 m->currentSharedBinLabels.clear();
565 m->sharedBinLabelsInFile.clear();
567 InputData input(sharedfile, "sharedfile");
568 lookup = input.getSharedRAbundVectors();
570 bool wroteSomething = false;
572 while(lookup[0] != NULL) {
574 variables["[tag]"] = lookup[0]->getLabel();
575 string outputFileName = getOutputFileName("shared", variables);
577 m->openOutputFile(outputFileName, out);
578 outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName);
580 if (m->control_pressed) { out.close(); m->mothurRemove(outputFileName); for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
582 lookup[0]->printHeaders(out);
584 for (int i = 0; i < lookup.size(); i++) {
585 out << lookup[i]->getLabel() << '\t' << lookup[i]->getGroup() << '\t';
586 lookup[i]->print(out);
587 wroteSomething = true;
591 //get next line to process
592 //prevent memory leak
593 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
594 lookup = input.getSharedRAbundVectors();
600 m->setGroups(mothurOutGroups);
602 if (wroteSomething == false) { m->mothurOut("Your file contains only the groups you wish to remove."); m->mothurOutEndLine(); }
604 string groupsString = "";
605 for (int i = 0; i < Groups.size()-1; i++) { groupsString += Groups[i] + ", "; }
606 groupsString += Groups[Groups.size()-1];
608 m->mothurOut("Removed groups: " + groupsString + " from your shared file."); m->mothurOutEndLine();
613 catch(exception& e) {
614 m->errorOut(e, "RemoveGroupsCommand", "readShared");
618 //**********************************************************************************************************************
619 int RemoveGroupsCommand::readList(){
621 string thisOutputDir = outputDir;
622 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
623 map<string, string> variables;
624 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(listfile));
625 variables["[extension]"] = m->getExtension(listfile);
628 m->openInputFile(listfile, in);
630 bool wroteSomething = false;
631 int removedCount = 0;
637 //read in list vector
640 variables["[tag]"] = list.getLabel();
641 string outputFileName = getOutputFileName("list", variables);
644 m->openOutputFile(outputFileName, out);
645 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
647 vector<string> binLabels = list.getLabels();
648 vector<string> newBinLabels;
650 //make a new list vector
652 newList.setLabel(list.getLabel());
655 for (int i = 0; i < list.getNumBins(); i++) {
656 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
658 //parse out names that are in accnos file
659 string binnames = list.get(i);
661 string newNames = "";
662 while (binnames.find_first_of(',') != -1) {
663 string name = binnames.substr(0,binnames.find_first_of(','));
664 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
666 //if that name is in the .accnos file, add it
667 if (names.count(name) == 0) { newNames += name + ","; }
669 //if you are not in the accnos file check if you are a name that needs to be changed
670 map<string, string>::iterator it = uniqueToRedundant.find(name);
671 if (it != uniqueToRedundant.end()) {
672 newNames += it->second + ",";
673 }else { removedCount++; }
678 if (names.count(binnames) == 0) { newNames += binnames + ","; }
679 else { //if you are not in the accnos file check if you are a name that needs to be changed
680 map<string, string>::iterator it = uniqueToRedundant.find(binnames);
681 if (it != uniqueToRedundant.end()) {
682 newNames += it->second + ",";
683 }else { removedCount++; }
686 //if there are names in this bin add to new list
687 if (newNames != "") {
688 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
689 newList.push_back(newNames);
690 newBinLabels.push_back(binLabels[i]);
694 //print new listvector
695 if (newList.getNumBins() != 0) {
696 wroteSomething = true;
697 newList.setLabels(newBinLabels);
698 newList.printHeaders(out);
708 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
709 m->mothurOut("Removed " + toString(removedCount) + " sequences from your list file."); m->mothurOutEndLine();
714 catch(exception& e) {
715 m->errorOut(e, "RemoveGroupsCommand", "readList");
719 //**********************************************************************************************************************
720 int RemoveGroupsCommand::readName(){
722 string thisOutputDir = outputDir;
723 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
724 map<string, string> variables;
725 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(namefile));
726 variables["[extension]"] = m->getExtension(namefile);
727 string outputFileName = getOutputFileName("name", variables);
729 m->openOutputFile(outputFileName, out);
732 m->openInputFile(namefile, in);
733 string name, firstCol, secondCol;
735 bool wroteSomething = false;
736 int removedCount = 0;
739 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
741 in >> firstCol; m->gobble(in);
744 vector<string> parsedNames;
745 m->splitAtComma(secondCol, parsedNames);
747 vector<string> validSecond; validSecond.clear();
748 for (int i = 0; i < parsedNames.size(); i++) {
749 if (names.count(parsedNames[i]) == 0) {
750 validSecond.push_back(parsedNames[i]);
754 removedCount += parsedNames.size()-validSecond.size();
756 //if the name in the first column is in the set then print it and any other names in second column also in set
757 if (names.count(firstCol) == 0) {
759 wroteSomething = true;
761 out << firstCol << '\t';
763 //you know you have at least one valid second since first column is valid
764 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
765 out << validSecond[validSecond.size()-1] << endl;
767 //make first name in set you come to first column and then add the remaining names to second column
770 //you want part of this row
771 if (validSecond.size() != 0) {
773 wroteSomething = true;
775 out << validSecond[0] << '\t';
777 //you know you have at least one valid second since first column is valid
778 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
779 out << validSecond[validSecond.size()-1] << endl;
780 uniqueToRedundant[firstCol] = validSecond[0];
789 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
790 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
792 m->mothurOut("Removed " + toString(removedCount) + " sequences from your name file."); m->mothurOutEndLine();
796 catch(exception& e) {
797 m->errorOut(e, "RemoveGroupsCommand", "readName");
802 //**********************************************************************************************************************
803 int RemoveGroupsCommand::readGroup(){
805 string thisOutputDir = outputDir;
806 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
807 map<string, string> variables;
808 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
809 variables["[extension]"] = m->getExtension(groupfile);
810 string outputFileName = getOutputFileName("group", variables);
812 m->openOutputFile(outputFileName, out);
815 m->openInputFile(groupfile, in);
818 bool wroteSomething = false;
819 int removedCount = 0;
822 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
824 in >> name; //read from first column
825 in >> group; //read from second column
827 //if this name is in the accnos file
828 if (names.count(name) == 0) {
829 wroteSomething = true;
830 out << name << '\t' << group << endl;
831 }else { removedCount++; }
838 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
839 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
841 m->mothurOut("Removed " + toString(removedCount) + " sequences from your group file."); m->mothurOutEndLine();
846 catch(exception& e) {
847 m->errorOut(e, "RemoveGroupsCommand", "readGroup");
851 //**********************************************************************************************************************
852 int RemoveGroupsCommand::readCount(){
854 string thisOutputDir = outputDir;
855 if (outputDir == "") { thisOutputDir += m->hasPath(countfile); }
856 map<string, string> variables;
857 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
858 variables["[extension]"] = m->getExtension(countfile);
859 string outputFileName = getOutputFileName("count", variables);
862 m->openOutputFile(outputFileName, out);
865 m->openInputFile(countfile, in);
867 bool wroteSomething = false;
868 int removedCount = 0;
870 string headers = m->getline(in); m->gobble(in);
871 vector<string> columnHeaders = m->splitWhiteSpace(headers);
873 vector<string> groups;
874 map<int, string> originalGroupIndexes;
875 map<string, int> GroupIndexes;
876 set<int> indexOfGroupsChosen;
877 for (int i = 2; i < columnHeaders.size(); i++) { groups.push_back(columnHeaders[i]); originalGroupIndexes[i-2] = columnHeaders[i]; }
878 //sort groups to keep consistent with how we store the groups in groupmap
879 sort(groups.begin(), groups.end());
880 for (int i = 0; i < groups.size(); i++) { GroupIndexes[groups[i]] = i; }
882 vector<string> groupsToKeep;
883 for (int i = 0; i < groups.size(); i++) {
884 if (!m->inUsersGroups(groups[i], Groups)) { groupsToKeep.push_back(groups[i]); }
886 sort(groupsToKeep.begin(), groupsToKeep.end());
887 out << "Representative_Sequence\ttotal\t";
888 for (int i = 0; i < groupsToKeep.size(); i++) { out << groupsToKeep[i] << '\t'; indexOfGroupsChosen.insert(GroupIndexes[groupsToKeep[i]]); }
891 string name; int oldTotal;
894 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
896 in >> name; m->gobble(in); in >> oldTotal; m->gobble(in);
897 if (m->debug) { m->mothurOut("[DEBUG]: " + name + '\t' + toString(oldTotal) + "\n"); }
899 if (names.count(name) == 0) {
900 //if group info, then read it
901 vector<int> selectedCounts; int thisTotal = 0; int temp;
902 for (int i = 0; i < groups.size(); i++) {
903 int thisIndex = GroupIndexes[originalGroupIndexes[i]];
904 in >> temp; m->gobble(in);
905 if (indexOfGroupsChosen.count(thisIndex) != 0) { //we want this group
906 selectedCounts.push_back(temp); thisTotal += temp;
910 out << name << '\t' << thisTotal << '\t';
911 for (int i = 0; i < selectedCounts.size(); i++) { out << selectedCounts[i] << '\t'; }
914 wroteSomething = true;
915 removedCount+= (oldTotal - thisTotal);
916 }else { m->getline(in); removedCount += oldTotal; }
923 if (wroteSomething == false) { m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine(); }
924 outputTypes["count"].push_back(outputFileName); outputNames.push_back(outputFileName);
926 m->mothurOut("Removed " + toString(removedCount) + " sequences from your count file."); m->mothurOutEndLine();
930 catch(exception& e) {
931 m->errorOut(e, "RemoveGroupsCommand", "readCount");
935 //**********************************************************************************************************************
936 int RemoveGroupsCommand::readDesign(){
938 string thisOutputDir = outputDir;
939 if (outputDir == "") { thisOutputDir += m->hasPath(designfile); }
940 map<string, string> variables;
941 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(designfile));
942 variables["[extension]"] = m->getExtension(designfile);
943 string outputFileName = getOutputFileName("design", variables);
946 m->openOutputFile(outputFileName, out);
949 m->openInputFile(designfile, in);
952 bool wroteSomething = false;
953 int removedCount = 0;
956 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
958 in >> name; //read from first column
959 in >> group; //read from second column
961 //if this name is in the accnos file
962 if (!(m->inUsersGroups(name, Groups))) {
963 wroteSomething = true;
964 out << name << '\t' << group << endl;
965 }else { removedCount++; }
972 if (wroteSomething == false) { m->mothurOut("Your file contains only groups from the groups you wish to remove."); m->mothurOutEndLine(); }
973 outputTypes["design"].push_back(outputFileName); outputNames.push_back(outputFileName);
975 m->mothurOut("Removed " + toString(removedCount) + " groups from your design file."); m->mothurOutEndLine();
980 catch(exception& e) {
981 m->errorOut(e, "RemoveGroupsCommand", "readDesign");
986 //**********************************************************************************************************************
987 int RemoveGroupsCommand::readTax(){
989 string thisOutputDir = outputDir;
990 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
991 map<string, string> variables;
992 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(taxfile));
993 variables["[extension]"] = m->getExtension(taxfile);
994 string outputFileName = getOutputFileName("taxonomy", variables);
996 m->openOutputFile(outputFileName, out);
999 m->openInputFile(taxfile, in);
1002 bool wroteSomething = false;
1003 int removedCount = 0;
1006 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
1008 in >> name; //read from first column
1009 in >> tax; //read from second column
1011 //if this name is in the accnos file
1012 if (names.count(name) == 0) {
1013 wroteSomething = true;
1014 out << name << '\t' << tax << endl;
1015 }else { //if you are not in the accnos file check if you are a name that needs to be changed
1016 map<string, string>::iterator it = uniqueToRedundant.find(name);
1017 if (it != uniqueToRedundant.end()) {
1018 wroteSomething = true;
1019 out << it->second << '\t' << tax << endl;
1020 }else { removedCount++; } }
1027 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
1028 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
1030 m->mothurOut("Removed " + toString(removedCount) + " sequences from your taxonomy file."); m->mothurOutEndLine();
1034 catch(exception& e) {
1035 m->errorOut(e, "RemoveGroupsCommand", "readTax");
1039 //**********************************************************************************************************************
1040 int RemoveGroupsCommand::fillNames(){
1042 vector<string> seqs = groupMap->getNamesSeqs();
1044 for (int i = 0; i < seqs.size(); i++) {
1046 if (m->control_pressed) { return 0; }
1048 string group = groupMap->getGroup(seqs[i]);
1050 if (m->inUsersGroups(group, Groups)) {
1051 names.insert(seqs[i]);
1057 catch(exception& e) {
1058 m->errorOut(e, "RemoveGroupsCommand", "fillNames");
1063 //**********************************************************************************************************************