5 * Created by westcott on 11/10/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "getgroupscommand.h"
11 #include "sequence.hpp"
12 #include "listvector.hpp"
13 #include "sharedutilities.h"
14 #include "inputdata.h"
16 //**********************************************************************************************************************
17 vector<string> GetGroupsCommand::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", "", "", "NameCount", "none", "none",false,false); parameters.push_back(pname);
22 CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "none", "none",false,false); parameters.push_back(pcount);
23 CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "sharedGroup", "FNGLT",false,false); parameters.push_back(pgroup);
24 CommandParameter pdesign("design", "InputTypes", "", "", "none", "sharedGroup", "FNGLT",false,false); parameters.push_back(pdesign);
25 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "FNGLT",false,false); parameters.push_back(plist);
26 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "FNGLT",false,false); 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, "GetGroupsCommand", "setParameters");
41 //**********************************************************************************************************************
42 string GetGroupsCommand::getHelpString(){
44 string helpString = "";
45 helpString += "The get.groups command selects sequences from a specfic group or set of groups from the following file types: fasta, name, group, list, taxonomy, design or shared file.\n";
46 helpString += "It outputs a file containing the sequences in the those specified groups, or a sharedfile containing only those groups.\n";
47 helpString += "The get.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 shared file.\n";
48 helpString += "You must also provide an accnos containing the list of groups to get or set the groups parameter to the groups you wish to select.\n";
49 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like. You can separate group names with dashes.\n";
50 helpString += "The get.groups command should be in the following format: get.groups(accnos=yourAccnos, fasta=yourFasta, group=yourGroupFile).\n";
51 helpString += "Example get.groups(accnos=amazon.accnos, fasta=amazon.fasta, group=amazon.groups).\n";
52 helpString += "or get.groups(groups=pasture, fasta=amazon.fasta, group=amazon.groups).\n";
53 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
57 m->errorOut(e, "GetGroupsCommand", "getHelpString");
61 //**********************************************************************************************************************
62 string GetGroupsCommand::getOutputFileNameTag(string type, string inputName=""){
64 string outputFileName = "";
65 map<string, vector<string> >::iterator it;
67 //is this a type this command creates
68 it = outputTypes.find(type);
69 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
71 if (type == "fasta") { outputFileName = "pick" + m->getExtension(inputName); }
72 else if (type == "taxonomy") { outputFileName = "pick" + m->getExtension(inputName); }
73 else if (type == "name") { outputFileName = "pick" + m->getExtension(inputName); }
74 else if (type == "group") { outputFileName = "pick" + m->getExtension(inputName); }
75 else if (type == "count") { outputFileName = "pick.count_table"; }
76 else if (type == "list") { outputFileName = "pick" + m->getExtension(inputName); }
77 else if (type == "shared") { outputFileName = "pick" + m->getExtension(inputName); }
78 else if (type == "design") { outputFileName = "pick" + m->getExtension(inputName); }
79 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
81 return outputFileName;
84 m->errorOut(e, "GetGroupsCommand", "getOutputFileNameTag");
89 //**********************************************************************************************************************
90 GetGroupsCommand::GetGroupsCommand(){
92 abort = true; calledHelp = true;
94 vector<string> tempOutNames;
95 outputTypes["fasta"] = tempOutNames;
96 outputTypes["taxonomy"] = tempOutNames;
97 outputTypes["name"] = tempOutNames;
98 outputTypes["group"] = tempOutNames;
99 outputTypes["list"] = tempOutNames;
100 outputTypes["shared"] = tempOutNames;
101 outputTypes["design"] = tempOutNames;
102 outputTypes["count"] = tempOutNames;
104 catch(exception& e) {
105 m->errorOut(e, "GetGroupsCommand", "GetGroupsCommand");
109 //**********************************************************************************************************************
110 GetGroupsCommand::GetGroupsCommand(string option) {
112 abort = false; calledHelp = false;
114 //allow user to run help
115 if(option == "help") { help(); abort = true; calledHelp = true; }
116 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
119 vector<string> myArray = setParameters();
121 OptionParser parser(option);
122 map<string,string> parameters = parser.getParameters();
124 ValidParameters validParameter;
125 map<string,string>::iterator it;
127 //check to make sure all parameters are valid for command
128 for (it = parameters.begin(); it != parameters.end(); it++) {
129 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
132 //initialize outputTypes
133 vector<string> tempOutNames;
134 outputTypes["fasta"] = tempOutNames;
135 outputTypes["taxonomy"] = tempOutNames;
136 outputTypes["name"] = tempOutNames;
137 outputTypes["group"] = tempOutNames;
138 outputTypes["list"] = tempOutNames;
139 outputTypes["shared"] = tempOutNames;
140 outputTypes["design"] = tempOutNames;
141 outputTypes["count"] = tempOutNames;
144 //if the user changes the output directory command factory will send this info to us in the output parameter
145 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
147 //if the user changes the input directory command factory will send this info to us in the output parameter
148 string inputDir = validParameter.validFile(parameters, "inputdir", false);
149 if (inputDir == "not found"){ inputDir = ""; }
152 it = parameters.find("fasta");
153 //user has given a template file
154 if(it != parameters.end()){
155 path = m->hasPath(it->second);
156 //if the user has not given a path then, add inputdir. else leave path alone.
157 if (path == "") { parameters["fasta"] = inputDir + it->second; }
160 it = parameters.find("accnos");
161 //user has given a template file
162 if(it != parameters.end()){
163 path = m->hasPath(it->second);
164 //if the user has not given a path then, add inputdir. else leave path alone.
165 if (path == "") { parameters["accnos"] = inputDir + it->second; }
168 it = parameters.find("list");
169 //user has given a template file
170 if(it != parameters.end()){
171 path = m->hasPath(it->second);
172 //if the user has not given a path then, add inputdir. else leave path alone.
173 if (path == "") { parameters["list"] = inputDir + it->second; }
176 it = parameters.find("name");
177 //user has given a template file
178 if(it != parameters.end()){
179 path = m->hasPath(it->second);
180 //if the user has not given a path then, add inputdir. else leave path alone.
181 if (path == "") { parameters["name"] = inputDir + it->second; }
184 it = parameters.find("group");
185 //user has given a template file
186 if(it != parameters.end()){
187 path = m->hasPath(it->second);
188 //if the user has not given a path then, add inputdir. else leave path alone.
189 if (path == "") { parameters["group"] = inputDir + it->second; }
192 it = parameters.find("taxonomy");
193 //user has given a template file
194 if(it != parameters.end()){
195 path = m->hasPath(it->second);
196 //if the user has not given a path then, add inputdir. else leave path alone.
197 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
200 it = parameters.find("shared");
201 //user has given a template file
202 if(it != parameters.end()){
203 path = m->hasPath(it->second);
204 //if the user has not given a path then, add inputdir. else leave path alone.
205 if (path == "") { parameters["shared"] = inputDir + it->second; }
208 it = parameters.find("design");
209 //user has given a template file
210 if(it != parameters.end()){
211 path = m->hasPath(it->second);
212 //if the user has not given a path then, add inputdir. else leave path alone.
213 if (path == "") { parameters["design"] = inputDir + it->second; }
216 it = parameters.find("count");
217 //user has given a template file
218 if(it != parameters.end()){
219 path = m->hasPath(it->second);
220 //if the user has not given a path then, add inputdir. else leave path alone.
221 if (path == "") { parameters["count"] = inputDir + it->second; }
226 //check for required parameters
227 accnosfile = validParameter.validFile(parameters, "accnos", true);
228 if (accnosfile == "not open") { abort = true; }
229 else if (accnosfile == "not found") { accnosfile = ""; }
230 else { m->setAccnosFile(accnosfile); }
232 fastafile = validParameter.validFile(parameters, "fasta", true);
233 if (fastafile == "not open") { fastafile = ""; abort = true; }
234 else if (fastafile == "not found") { fastafile = ""; }
235 else { m->setFastaFile(fastafile); }
237 namefile = validParameter.validFile(parameters, "name", true);
238 if (namefile == "not open") { namefile = ""; abort = true; }
239 else if (namefile == "not found") { namefile = ""; }
240 else { m->setNameFile(namefile); }
242 listfile = validParameter.validFile(parameters, "list", true);
243 if (listfile == "not open") { abort = true; }
244 else if (listfile == "not found") { listfile = ""; }
245 else { m->setListFile(listfile); }
247 taxfile = validParameter.validFile(parameters, "taxonomy", true);
248 if (taxfile == "not open") { taxfile = ""; abort = true; }
249 else if (taxfile == "not found") { taxfile = ""; }
250 else { m->setTaxonomyFile(taxfile); }
252 groups = validParameter.validFile(parameters, "groups", false);
253 if (groups == "not found") { groups = ""; }
255 m->splitAtDash(groups, Groups);
256 m->setGroups(Groups);
259 sharedfile = validParameter.validFile(parameters, "shared", true);
260 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
261 else if (sharedfile == "not found") { sharedfile = ""; }
262 else { m->setSharedFile(sharedfile); }
264 groupfile = validParameter.validFile(parameters, "group", true);
265 if (groupfile == "not open") { groupfile = ""; abort = true; }
266 else if (groupfile == "not found") { groupfile = ""; }
267 else { m->setGroupFile(groupfile); }
269 designfile = validParameter.validFile(parameters, "design", true);
270 if (designfile == "not open") { designfile = ""; abort = true; }
271 else if (designfile == "not found") { designfile = ""; }
272 else { m->setDesignFile(designfile); }
274 countfile = validParameter.validFile(parameters, "count", true);
275 if (countfile == "not open") { countfile = ""; abort = true; }
276 else if (countfile == "not found") { countfile = ""; }
277 else { m->setCountTableFile(countfile); }
279 if ((namefile != "") && (countfile != "")) {
280 m->mothurOut("[ERROR]: you may only use one of the following: name or count."); m->mothurOutEndLine(); abort = true;
283 if ((groupfile != "") && (countfile != "")) {
284 m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
288 if ((sharedfile == "") && (groupfile == "") && (designfile == "") && (countfile == "")) {
289 //is there are current file available for any of these?
290 if ((namefile != "") || (fastafile != "") || (listfile != "") || (taxfile != "")) {
291 //give priority to group, then shared
292 groupfile = m->getGroupFile();
293 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
295 sharedfile = m->getSharedFile();
296 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
298 countfile = m->getCountTableFile();
299 if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
301 m->mothurOut("You have no current groupfile, countfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
306 //give priority to shared, then group
307 sharedfile = m->getSharedFile();
308 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
310 groupfile = m->getGroupFile();
311 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
313 designfile = m->getDesignFile();
314 if (designfile != "") { m->mothurOut("Using " + designfile + " as input file for the design parameter."); m->mothurOutEndLine(); }
316 countfile = m->getCountTableFile();
317 if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
319 m->mothurOut("You have no current groupfile, designfile, countfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
328 if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; }
330 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; }
331 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; }
333 if (countfile == "") {
334 if ((namefile == "") && ((fastafile != "") || (taxfile != ""))){
335 vector<string> files; files.push_back(fastafile); files.push_back(taxfile);
336 parser.getNameFile(files);
342 catch(exception& e) {
343 m->errorOut(e, "GetGroupsCommand", "GetGroupsCommand");
347 //**********************************************************************************************************************
349 int GetGroupsCommand::execute(){
352 if (abort == true) { if (calledHelp) { return 0; } return 2; }
354 //get groups you want to remove
355 if (accnosfile != "") { m->readAccnos(accnosfile, Groups); m->setGroups(Groups); }
357 if (groupfile != "") {
358 groupMap = new GroupMap(groupfile);
361 //make sure groups are valid
362 //takes care of user setting groupNames that are invalid or setting groups=all
363 SharedUtil* util = new SharedUtil();
364 vector<string> gNamesOfGroups = groupMap->getNamesOfGroups();
365 util->setGroups(Groups, gNamesOfGroups);
366 m->setGroups(Groups);
367 groupMap->setNamesOfGroups(gNamesOfGroups);
370 //fill names with names of sequences that are from the groups we want to remove
374 }else if (countfile != ""){
375 if ((fastafile != "") || (listfile != "") || (taxfile != "")) {
376 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");
379 ct.readTable(countfile);
380 if (!ct.hasGroupInfo()) { m->mothurOut("[ERROR]: your count file does not contain group info, aborting.\n"); return 0; }
382 vector<string> gNamesOfGroups = ct.getNamesOfGroups();
384 util.setGroups(Groups, gNamesOfGroups);
385 m->setGroups(Groups);
386 for (int i = 0; i < Groups.size(); i++) {
387 vector<string> thisGroupsSeqs = ct.getNamesOfSeqs(Groups[i]);
388 for (int j = 0; j < thisGroupsSeqs.size(); j++) { names.insert(thisGroupsSeqs[j]); }
392 if (m->control_pressed) { return 0; }
394 //read through the correct file and output lines you want to keep
395 if (namefile != "") { readName(); }
396 if (fastafile != "") { readFasta(); }
397 if (groupfile != "") { readGroup(); }
398 if (countfile != "") { readCount(); }
399 if (listfile != "") { readList(); }
400 if (taxfile != "") { readTax(); }
401 if (sharedfile != "") { readShared(); }
402 if (designfile != "") { readDesign(); }
404 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
407 if (outputNames.size() != 0) {
408 m->mothurOutEndLine();
409 m->mothurOut("Output File names: "); m->mothurOutEndLine();
410 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
411 m->mothurOutEndLine();
413 //set fasta file as new current fastafile
415 itTypes = outputTypes.find("fasta");
416 if (itTypes != outputTypes.end()) {
417 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
420 itTypes = outputTypes.find("name");
421 if (itTypes != outputTypes.end()) {
422 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
425 itTypes = outputTypes.find("group");
426 if (itTypes != outputTypes.end()) {
427 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
430 itTypes = outputTypes.find("list");
431 if (itTypes != outputTypes.end()) {
432 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
435 itTypes = outputTypes.find("taxonomy");
436 if (itTypes != outputTypes.end()) {
437 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
440 itTypes = outputTypes.find("shared");
441 if (itTypes != outputTypes.end()) {
442 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
445 itTypes = outputTypes.find("design");
446 if (itTypes != outputTypes.end()) {
447 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setDesignFile(current); }
450 itTypes = outputTypes.find("count");
451 if (itTypes != outputTypes.end()) {
452 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
459 catch(exception& e) {
460 m->errorOut(e, "GetGroupsCommand", "execute");
465 //**********************************************************************************************************************
466 int GetGroupsCommand::readFasta(){
468 string thisOutputDir = outputDir;
469 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
470 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + getOutputFileNameTag("fasta", fastafile);
473 m->openOutputFile(outputFileName, out);
476 m->openInputFile(fastafile, in);
479 bool wroteSomething = false;
480 int selectedCount = 0;
483 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
485 Sequence currSeq(in);
486 name = currSeq.getName();
489 //if this name is in the accnos file
490 if (names.count(name) != 0) {
491 wroteSomething = true;
493 currSeq.printSequence(out);
496 //if you are not in the accnos file check if you are a name that needs to be changed
497 map<string, string>::iterator it = uniqueToRedundant.find(name);
498 if (it != uniqueToRedundant.end()) {
499 wroteSomething = true;
500 currSeq.setName(it->second);
501 currSeq.printSequence(out);
511 if (wroteSomething == false) { m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine(); }
512 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
514 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your fasta file."); m->mothurOutEndLine();
520 catch(exception& e) {
521 m->errorOut(e, "GetGroupsCommand", "readFasta");
525 //**********************************************************************************************************************
526 int GetGroupsCommand::readShared(){
528 string thisOutputDir = outputDir;
529 if (outputDir == "") { thisOutputDir += m->hasPath(sharedfile); }
531 InputData input(sharedfile, "sharedfile");
532 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
534 bool wroteSomething = false;
536 while(lookup[0] != NULL) {
538 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + lookup[0]->getLabel() + "." + getOutputFileNameTag("shared", sharedfile);
540 m->openOutputFile(outputFileName, out);
541 outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName);
543 if (m->control_pressed) { out.close(); m->mothurRemove(outputFileName); for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
545 lookup[0]->printHeaders(out);
547 for (int i = 0; i < lookup.size(); i++) {
548 out << lookup[i]->getLabel() << '\t' << lookup[i]->getGroup() << '\t';
549 lookup[i]->print(out);
550 wroteSomething = true;
554 //get next line to process
555 //prevent memory leak
556 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
557 lookup = input.getSharedRAbundVectors();
562 if (wroteSomething == false) { m->mothurOut("Your file contains only the groups you wish to remove."); m->mothurOutEndLine(); }
564 string groupsString = "";
565 for (int i = 0; i < Groups.size()-1; i++) { groupsString += Groups[i] + ", "; }
566 groupsString += Groups[Groups.size()-1];
568 m->mothurOut("Selected groups: " + groupsString + " from your shared file."); m->mothurOutEndLine();
573 catch(exception& e) {
574 m->errorOut(e, "GetGroupsCommand", "readShared");
578 //**********************************************************************************************************************
579 int GetGroupsCommand::readList(){
581 string thisOutputDir = outputDir;
582 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
583 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + getOutputFileNameTag("list", listfile);
586 m->openOutputFile(outputFileName, out);
589 m->openInputFile(listfile, in);
591 bool wroteSomething = false;
592 int selectedCount = 0;
598 //read in list vector
601 //make a new list vector
603 newList.setLabel(list.getLabel());
606 for (int i = 0; i < list.getNumBins(); i++) {
607 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
609 //parse out names that are in accnos file
610 string binnames = list.get(i);
612 string newNames = "";
613 while (binnames.find_first_of(',') != -1) {
614 string name = binnames.substr(0,binnames.find_first_of(','));
615 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
617 //if that name is in the .accnos file, add it
618 if (names.count(name) != 0) { newNames += name + ","; selectedCount++; }
620 //if you are not in the accnos file check if you are a name that needs to be changed
621 map<string, string>::iterator it = uniqueToRedundant.find(name);
622 if (it != uniqueToRedundant.end()) {
623 newNames += it->second + ",";
630 if (names.count(binnames) != 0) { newNames += binnames + ","; selectedCount++; }
632 //if you are not in the accnos file check if you are a name that needs to be changed
633 map<string, string>::iterator it = uniqueToRedundant.find(binnames);
634 if (it != uniqueToRedundant.end()) {
635 newNames += it->second + ",";
640 //if there are names in this bin add to new list
641 if (newNames != "") {
642 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
643 newList.push_back(newNames);
647 //print new listvector
648 if (newList.getNumBins() != 0) {
649 wroteSomething = true;
658 if (wroteSomething == false) { m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine(); }
659 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
661 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your list file."); m->mothurOutEndLine();
666 catch(exception& e) {
667 m->errorOut(e, "GetGroupsCommand", "readList");
671 //**********************************************************************************************************************
672 int GetGroupsCommand::readName(){
674 string thisOutputDir = outputDir;
675 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
676 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + getOutputFileNameTag("name", namefile);
679 m->openOutputFile(outputFileName, out);
682 m->openInputFile(namefile, in);
683 string name, firstCol, secondCol;
685 bool wroteSomething = false;
686 int selectedCount = 0;
689 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
691 in >> firstCol; m->gobble(in);
694 vector<string> parsedNames;
695 m->splitAtComma(secondCol, parsedNames);
697 vector<string> validSecond; validSecond.clear();
698 for (int i = 0; i < parsedNames.size(); i++) {
699 if (names.count(parsedNames[i]) != 0) {
700 validSecond.push_back(parsedNames[i]);
704 selectedCount += validSecond.size();
706 //if the name in the first column is in the set then print it and any other names in second column also in set
707 if (names.count(firstCol) != 0) {
709 wroteSomething = true;
711 out << firstCol << '\t';
713 //you know you have at least one valid second since first column is valid
714 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
715 out << validSecond[validSecond.size()-1] << endl;
717 //make first name in set you come to first column and then add the remaining names to second column
720 //you want part of this row
721 if (validSecond.size() != 0) {
723 wroteSomething = true;
725 out << validSecond[0] << '\t';
727 //you know you have at least one valid second since first column is valid
728 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
729 out << validSecond[validSecond.size()-1] << endl;
730 uniqueToRedundant[firstCol] = validSecond[0];
739 if (wroteSomething == false) { m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine(); }
740 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
742 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your name file."); m->mothurOutEndLine();
746 catch(exception& e) {
747 m->errorOut(e, "GetGroupsCommand", "readName");
752 //**********************************************************************************************************************
753 int GetGroupsCommand::readGroup(){
755 string thisOutputDir = outputDir;
756 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
757 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + getOutputFileNameTag("group", groupfile);
760 m->openOutputFile(outputFileName, out);
763 m->openInputFile(groupfile, in);
766 bool wroteSomething = false;
767 int selectedCount = 0;
770 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
772 in >> name; //read from first column
773 in >> group; //read from second column
775 //if this name is in the accnos file
776 if (names.count(name) != 0) {
777 wroteSomething = true;
778 out << name << '\t' << group << endl;
787 if (wroteSomething == false) { m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine(); }
788 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
790 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your group file."); m->mothurOutEndLine();
794 catch(exception& e) {
795 m->errorOut(e, "GetGroupsCommand", "readGroup");
799 //**********************************************************************************************************************
800 int GetGroupsCommand::readCount(){
802 string thisOutputDir = outputDir;
803 if (outputDir == "") { thisOutputDir += m->hasPath(countfile); }
804 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(countfile)) + getOutputFileNameTag("count", countfile);
807 m->openOutputFile(outputFileName, out);
810 m->openInputFile(countfile, in);
812 bool wroteSomething = false;
813 int selectedCount = 0;
815 string headers = m->getline(in); m->gobble(in);
816 vector<string> columnHeaders = m->splitWhiteSpace(headers);
818 vector<string> groups;
819 map<int, string> originalGroupIndexes;
820 map<string, int> GroupIndexes;
821 set<int> indexOfGroupsChosen;
822 for (int i = 2; i < columnHeaders.size(); i++) { groups.push_back(columnHeaders[i]); originalGroupIndexes[i-2] = columnHeaders[i]; }
823 //sort groups to keep consistent with how we store the groups in groupmap
824 sort(groups.begin(), groups.end());
825 for (int i = 0; i < groups.size(); i++) { GroupIndexes[groups[i]] = i; }
826 sort(Groups.begin(), Groups.end());
827 out << "Representative_Sequence\ttotal\t";
828 for (int i = 0; i < Groups.size(); i++) { out << Groups[i] << '\t'; indexOfGroupsChosen.insert(GroupIndexes[Groups[i]]); }
831 string name; int oldTotal;
834 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
836 in >> name; m->gobble(in); in >> oldTotal; m->gobble(in);
837 if (m->debug) { m->mothurOut("[DEBUG]: " + name + '\t' + toString(oldTotal) + "\n"); }
839 if (names.count(name) != 0) {
840 //if group info, then read it
841 vector<int> selectedCounts; int thisTotal = 0; int temp;
842 for (int i = 0; i < groups.size(); i++) {
843 int thisIndex = GroupIndexes[originalGroupIndexes[i]];
844 in >> temp; m->gobble(in);
845 if (indexOfGroupsChosen.count(thisIndex) != 0) { //we want this group
846 selectedCounts.push_back(temp); thisTotal += temp;
850 out << name << '\t' << thisTotal << '\t';
851 for (int i = 0; i < selectedCounts.size(); i++) { out << selectedCounts[i] << '\t'; }
854 wroteSomething = true;
855 selectedCount+= thisTotal;
856 }else { m->getline(in); }
863 if (wroteSomething == false) { m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine(); }
864 outputTypes["count"].push_back(outputFileName); outputNames.push_back(outputFileName);
866 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your count file."); m->mothurOutEndLine();
870 catch(exception& e) {
871 m->errorOut(e, "GetGroupsCommand", "readCount");
875 //**********************************************************************************************************************
876 int GetGroupsCommand::readDesign(){
878 string thisOutputDir = outputDir;
879 if (outputDir == "") { thisOutputDir += m->hasPath(designfile); }
880 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(designfile)) + getOutputFileNameTag("design", designfile);
883 m->openOutputFile(outputFileName, out);
886 m->openInputFile(designfile, in);
889 bool wroteSomething = false;
890 int selectedCount = 0;
893 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
895 in >> name; //read from first column
896 in >> group; //read from second column
898 //if this name is in the accnos file
899 if (m->inUsersGroups(name, Groups)) {
900 wroteSomething = true;
901 out << name << '\t' << group << endl;
910 if (wroteSomething == false) { m->mothurOut("Your file does NOT contain groups from the groups you wish to get."); m->mothurOutEndLine(); }
911 outputTypes["design"].push_back(outputFileName); outputNames.push_back(outputFileName);
913 m->mothurOut("Selected " + toString(selectedCount) + " groups from your design file."); m->mothurOutEndLine();
918 catch(exception& e) {
919 m->errorOut(e, "GetGroupsCommand", "readDesign");
925 //**********************************************************************************************************************
926 int GetGroupsCommand::readTax(){
928 string thisOutputDir = outputDir;
929 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
930 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + getOutputFileNameTag("taxonomy", taxfile);
932 m->openOutputFile(outputFileName, out);
935 m->openInputFile(taxfile, in);
938 bool wroteSomething = false;
941 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
943 in >> name; //read from first column
944 in >> tax; //read from second column
946 //if this name is in the accnos file
947 if (names.count(name) != 0) {
948 wroteSomething = true;
949 out << name << '\t' << tax << endl;
951 //if you are not in the accnos file check if you are a name that needs to be changed
952 map<string, string>::iterator it = uniqueToRedundant.find(name);
953 if (it != uniqueToRedundant.end()) {
954 wroteSomething = true;
955 out << it->second << '\t' << tax << endl;
964 if (wroteSomething == false) { m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine(); }
965 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
969 catch(exception& e) {
970 m->errorOut(e, "GetGroupsCommand", "readTax");
974 //**********************************************************************************************************************
975 int GetGroupsCommand::fillNames(){
977 vector<string> seqs = groupMap->getNamesSeqs();
979 for (int i = 0; i < seqs.size(); i++) {
981 if (m->control_pressed) { return 0; }
983 string group = groupMap->getGroup(seqs[i]);
985 if (m->inUsersGroups(group, Groups)) {
986 names.insert(seqs[i]);
992 catch(exception& e) {
993 m->errorOut(e, "GetGroupsCommand", "fillNames");
998 //**********************************************************************************************************************