5 // Created by Sarah Westcott on 5/15/12.
6 // Copyright (c) 2012 Schloss Lab. All rights reserved.
9 #include "listotulabelscommand.h"
10 #include "inputdata.h"
12 //**********************************************************************************************************************
13 vector<string> ListOtuLabelsCommand::setParameters(){
15 CommandParameter pshared("shared", "InputTypes", "", "", "SharedRel", "SharedRel", "none","otulabels",false,false,true); parameters.push_back(pshared);
16 CommandParameter prelabund("relabund", "InputTypes", "", "", "SharedRel", "SharedRel", "none","otulabels",false,false); parameters.push_back(prelabund);
17 CommandParameter plist("list", "InputTypes", "", "", "SharedRel", "SharedRel", "none","otulabels",false,false); parameters.push_back(plist);
18 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
19 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
20 //every command must have inputdir and outputdir. This allows mothur users to redirect input and output files.
21 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
22 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
24 vector<string> myArray;
25 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
29 m->errorOut(e, "ListOtuLabelsCommand", "setParameters");
33 //**********************************************************************************************************************
34 string ListOtuLabelsCommand::getHelpString(){
36 string helpString = "";
37 helpString += "The list.otulabels lists otu labels from shared, relabund or list file. The results can be used by the get.otulabels to select specific otus with the output from classify.otu, otu.association, or corr.axes.\n";
38 helpString += "The list.otulabels parameters are: shared, relabund, label and groups.\n";
39 helpString += "The label parameter is used to analyze specific labels in your input.\n";
40 helpString += "The groups parameter allows you to specify which of the groups you would like analyzed.\n";
41 helpString += "The list.otulabels commmand should be in the following format: \n";
42 helpString += "list.otulabels(shared=yourSharedFile, groups=yourGroup1-yourGroup2)\n";
46 m->errorOut(e, "ListOtuLabelsCommand", "getHelpString");
50 //**********************************************************************************************************************
51 string ListOtuLabelsCommand::getOutputPattern(string type) {
55 if (type == "otulabels") { pattern = "[filename],[distance],otulabels"; }
56 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
61 m->errorOut(e, "ListOtuLabelsCommand", "getOutputPattern");
65 //**********************************************************************************************************************
66 ListOtuLabelsCommand::ListOtuLabelsCommand(){
68 abort = true; calledHelp = true;
70 vector<string> tempOutNames;
71 outputTypes["otulabels"] = tempOutNames;
74 m->errorOut(e, "ListOtuLabelsCommand", "ListOtuLabelsCommand");
78 //**********************************************************************************************************************
79 ListOtuLabelsCommand::ListOtuLabelsCommand(string option) {
81 abort = false; calledHelp = false;
84 //allow user to run help
85 if(option == "help") { help(); abort = true; calledHelp = true; }
86 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
89 //valid paramters for this command
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;
97 //check to make sure all parameters are valid for command
98 for (it = parameters.begin(); it != parameters.end(); it++) {
99 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
103 //if the user changes the input directory command factory will send this info to us in the output parameter
104 string inputDir = validParameter.validFile(parameters, "inputdir", false);
105 if (inputDir == "not found"){ inputDir = ""; }
108 //edit file types below to include only the types you added as parameters
111 it = parameters.find("relabund");
112 //user has given a template file
113 if(it != parameters.end()){
114 path = m->hasPath(it->second);
115 //if the user has not given a path then, add inputdir. else leave path alone.
116 if (path == "") { parameters["relabund"] = inputDir + it->second; }
119 it = parameters.find("shared");
120 //user has given a template file
121 if(it != parameters.end()){
122 path = m->hasPath(it->second);
123 //if the user has not given a path then, add inputdir. else leave path alone.
124 if (path == "") { parameters["shared"] = inputDir + it->second; }
127 it = parameters.find("list");
128 //user has given a template file
129 if(it != parameters.end()){
130 path = m->hasPath(it->second);
131 //if the user has not given a path then, add inputdir. else leave path alone.
132 if (path == "") { parameters["list"] = inputDir + it->second; }
136 vector<string> tempOutNames;
137 outputTypes["otulabels"] = tempOutNames;
139 //check for parameters
140 sharedfile = validParameter.validFile(parameters, "shared", true);
141 if (sharedfile == "not open") { abort = true; }
142 else if (sharedfile == "not found") { sharedfile = ""; }
143 else { inputFileName = sharedfile; format = "sharedfile"; m->setSharedFile(sharedfile); }
145 relabundfile = validParameter.validFile(parameters, "relabund", true);
146 if (relabundfile == "not open") { abort = true; }
147 else if (relabundfile == "not found") { relabundfile = ""; }
148 else { inputFileName = relabundfile; format = "relabund"; m->setRelAbundFile(relabundfile); }
150 listfile = validParameter.validFile(parameters, "list", true);
151 if (listfile == "not open") { abort = true; }
152 else if (listfile == "not found") { listfile = ""; }
153 else { inputFileName = listfile; format = "list"; m->setListFile(listfile); }
156 if ((relabundfile == "") && (sharedfile == "") && (listfile== "")) {
157 //is there are current file available for either of these?
158 //give priority to shared, then relabund
159 sharedfile = m->getSharedFile();
160 if (sharedfile != "") { inputFileName = sharedfile; format="sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
162 relabundfile = m->getRelAbundFile();
163 if (relabundfile != "") { inputFileName = relabundfile; format="relabund"; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); }
165 listfile = m->getListFile();
166 if (listfile != "") { inputFileName = listfile; format="list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
168 m->mothurOut("No valid current files. You must provide a shared, list or relabund."); m->mothurOutEndLine();
175 //if the user changes the output directory command factory will send this info to us in the output parameter
176 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
177 outputDir = m->hasPath(inputFileName); //if user entered a file with a path then preserve it
180 string groups = validParameter.validFile(parameters, "groups", false);
181 if (groups == "not found") { groups = ""; }
182 else { m->splitAtDash(groups, Groups); }
183 m->setGroups(Groups);
185 string label = validParameter.validFile(parameters, "label", false);
186 if (label == "not found") { label = ""; }
188 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
189 else { allLines = 1; }
194 catch(exception& e) {
195 m->errorOut(e, "ListOtuLabelsCommand", "ListOtuLabelsCommand");
199 //**********************************************************************************************************************
201 int ListOtuLabelsCommand::execute(){
204 if (abort == true) { if (calledHelp) { return 0; } return 2; }
206 InputData input(inputFileName, format);
208 if (format == "relabund") {
209 vector<SharedRAbundFloatVector*> lookup = input.getSharedRAbundFloatVectors();
210 string lastLabel = lookup[0]->getLabel();
212 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
213 set<string> processedLabels;
214 set<string> userLabels = labels;
216 //as long as you are not at the end of the file or done wih the lines you want
217 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
219 if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
221 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
223 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
227 processedLabels.insert(lookup[0]->getLabel());
228 userLabels.erase(lookup[0]->getLabel());
231 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
232 string saveLabel = lookup[0]->getLabel();
234 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
235 lookup = input.getSharedRAbundFloatVectors(lastLabel);
236 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
240 processedLabels.insert(lookup[0]->getLabel());
241 userLabels.erase(lookup[0]->getLabel());
243 //restore real lastlabel to save below
244 lookup[0]->setLabel(saveLabel);
247 lastLabel = lookup[0]->getLabel();
248 //prevent memory leak
249 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
251 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
253 //get next line to process
254 lookup = input.getSharedRAbundFloatVectors();
257 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
259 //output error messages about any remaining user labels
260 set<string>::iterator it;
261 bool needToRun = false;
262 for (it = userLabels.begin(); it != userLabels.end(); it++) {
263 m->mothurOut("Your file does not include the label " + *it);
264 if (processedLabels.count(lastLabel) != 1) {
265 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
268 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
272 //run last label if you need to
273 if (needToRun == true) {
274 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
275 lookup = input.getSharedRAbundFloatVectors(lastLabel);
277 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
281 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
283 }else if (format == "sharedfile") {
285 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
286 string lastLabel = lookup[0]->getLabel();
288 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
289 set<string> processedLabels;
290 set<string> userLabels = labels;
292 //as long as you are not at the end of the file or done wih the lines you want
293 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
295 if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
297 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
299 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
303 processedLabels.insert(lookup[0]->getLabel());
304 userLabels.erase(lookup[0]->getLabel());
307 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
308 string saveLabel = lookup[0]->getLabel();
310 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
311 lookup = input.getSharedRAbundVectors(lastLabel);
312 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
316 processedLabels.insert(lookup[0]->getLabel());
317 userLabels.erase(lookup[0]->getLabel());
319 //restore real lastlabel to save below
320 lookup[0]->setLabel(saveLabel);
323 lastLabel = lookup[0]->getLabel();
324 //prevent memory leak
325 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
327 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
329 //get next line to process
330 lookup = input.getSharedRAbundVectors();
333 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
335 //output error messages about any remaining user labels
336 set<string>::iterator it;
337 bool needToRun = false;
338 for (it = userLabels.begin(); it != userLabels.end(); it++) {
339 m->mothurOut("Your file does not include the label " + *it);
340 if (processedLabels.count(lastLabel) != 1) {
341 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
344 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
348 //run last label if you need to
349 if (needToRun == true) {
350 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
351 lookup = input.getSharedRAbundVectors(lastLabel);
353 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
357 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
360 ListVector* list = input.getListVector();
361 string lastLabel = list->getLabel();
363 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
364 set<string> processedLabels;
365 set<string> userLabels = labels;
367 //as long as you are not at the end of the file or done wih the lines you want
368 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
370 if (m->control_pressed) { delete list; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
372 if(allLines == 1 || labels.count(list->getLabel()) == 1){
374 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
378 processedLabels.insert(list->getLabel());
379 userLabels.erase(list->getLabel());
382 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
383 string saveLabel = list->getLabel();
386 list = input.getListVector(lastLabel);
387 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
391 processedLabels.insert(list->getLabel());
392 userLabels.erase(list->getLabel());
394 //restore real lastlabel to save below
395 list->setLabel(saveLabel);
398 lastLabel = list->getLabel();
399 //prevent memory leak
400 delete list; list = NULL;
402 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
404 //get next line to process
405 list = input.getListVector();
408 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
410 //output error messages about any remaining user labels
411 set<string>::iterator it;
412 bool needToRun = false;
413 for (it = userLabels.begin(); it != userLabels.end(); it++) {
414 m->mothurOut("Your file does not include the label " + *it);
415 if (processedLabels.count(lastLabel) != 1) {
416 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
419 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
423 //run last label if you need to
424 if (needToRun == true) {
426 list = input.getListVector(lastLabel);
428 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
436 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
438 //output files created by command
439 m->mothurOutEndLine();
440 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
441 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
442 m->mothurOutEndLine();
446 catch(exception& e) {
447 m->errorOut(e, "ListOtuLabelsCommand", "execute");
451 //**********************************************************************************************************************
453 int ListOtuLabelsCommand::createList(vector<SharedRAbundVector*>& lookup){
456 map<string, string> variables;
457 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputFileName));
458 variables["[distance]"] = lookup[0]->getLabel();
459 string outputFileName = getOutputFileName("otulabels",variables);
460 outputNames.push_back(outputFileName); outputTypes["otulabels"].push_back(outputFileName);
462 m->openOutputFile(outputFileName, out);
464 for (int i = 0; i < m->currentBinLabels.size(); i++) { out << m->currentBinLabels[i] << endl; }
470 catch(exception& e) {
471 m->errorOut(e, "ListOtuLabelsCommand", "createList");
476 //**********************************************************************************************************************
478 int ListOtuLabelsCommand::createList(vector<SharedRAbundFloatVector*>& lookup){
480 map<string, string> variables;
481 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputFileName));
482 variables["[distance]"] = lookup[0]->getLabel();
483 string outputFileName = getOutputFileName("otulabels",variables);
484 outputNames.push_back(outputFileName); outputTypes["accnos"].push_back(outputFileName);
486 m->openOutputFile(outputFileName, out);
488 for (int i = 0; i < m->currentBinLabels.size(); i++) { out << m->currentBinLabels[i] << endl; }
494 catch(exception& e) {
495 m->errorOut(e, "ListOtuLabelsCommand", "createList");
499 //**********************************************************************************************************************
500 int ListOtuLabelsCommand::createList(ListVector*& list){
502 map<string, string> variables;
503 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputFileName));
504 variables["[distance]"] = list->getLabel();
505 string outputFileName = getOutputFileName("otulabels",variables);
506 outputNames.push_back(outputFileName); outputTypes["accnos"].push_back(outputFileName);
508 m->openOutputFile(outputFileName, out);
510 string snumBins = toString(list->getNumBins());
511 for (int i = 0; i < list->getNumBins(); i++) {
512 if (m->control_pressed) { break; }
514 string otuLabel = "Otu";
515 string sbinNumber = toString(i+1);
516 if (sbinNumber.length() < snumBins.length()) {
517 int diff = snumBins.length() - sbinNumber.length();
518 for (int h = 0; h < diff; h++) { otuLabel += "0"; }
520 otuLabel += sbinNumber;
522 out << otuLabel << endl;
529 catch(exception& e) {
530 m->errorOut(e, "ListOtuLabelsCommand", "createList");
535 //**********************************************************************************************************************