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 pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
18 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
19 //every command must have inputdir and outputdir. This allows mothur users to redirect input and output files.
20 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
21 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
23 vector<string> myArray;
24 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
28 m->errorOut(e, "ListOtuLabelsCommand", "setParameters");
32 //**********************************************************************************************************************
33 string ListOtuLabelsCommand::getHelpString(){
35 string helpString = "";
36 helpString += "The list.otulabels lists otu labels from shared or relabund 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";
37 helpString += "The list.otulabels parameters are: shared, relabund, label and groups.\n";
38 helpString += "The label parameter is used to analyze specific labels in your input.\n";
39 helpString += "The groups parameter allows you to specify which of the groups you would like analyzed.\n";
40 helpString += "The list.otulabels commmand should be in the following format: \n";
41 helpString += "list.otulabels(shared=yourSharedFile, groups=yourGroup1-yourGroup2)\n";
45 m->errorOut(e, "ListOtuLabelsCommand", "getHelpString");
49 //**********************************************************************************************************************
50 string ListOtuLabelsCommand::getOutputPattern(string type) {
54 if (type == "otulabels") { pattern = "[filename],[distance],otulabels"; }
55 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
60 m->errorOut(e, "ListOtuLabelsCommand", "getOutputPattern");
64 //**********************************************************************************************************************
65 ListOtuLabelsCommand::ListOtuLabelsCommand(){
67 abort = true; calledHelp = true;
69 vector<string> tempOutNames;
70 outputTypes["otulabels"] = tempOutNames;
73 m->errorOut(e, "ListOtuLabelsCommand", "ListOtuLabelsCommand");
77 //**********************************************************************************************************************
78 ListOtuLabelsCommand::ListOtuLabelsCommand(string option) {
80 abort = false; calledHelp = false;
83 //allow user to run help
84 if(option == "help") { help(); abort = true; calledHelp = true; }
85 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
88 //valid paramters for this command
89 vector<string> myArray = setParameters();
91 OptionParser parser(option);
92 map<string,string> parameters = parser.getParameters();
94 ValidParameters validParameter;
95 map<string,string>::iterator it;
96 //check to make sure all parameters are valid for command
97 for (it = parameters.begin(); it != parameters.end(); it++) {
98 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
102 //if the user changes the input directory command factory will send this info to us in the output parameter
103 string inputDir = validParameter.validFile(parameters, "inputdir", false);
104 if (inputDir == "not found"){ inputDir = ""; }
107 //edit file types below to include only the types you added as parameters
110 it = parameters.find("relabund");
111 //user has given a template file
112 if(it != parameters.end()){
113 path = m->hasPath(it->second);
114 //if the user has not given a path then, add inputdir. else leave path alone.
115 if (path == "") { parameters["relabund"] = inputDir + it->second; }
118 it = parameters.find("shared");
119 //user has given a template file
120 if(it != parameters.end()){
121 path = m->hasPath(it->second);
122 //if the user has not given a path then, add inputdir. else leave path alone.
123 if (path == "") { parameters["shared"] = inputDir + it->second; }
127 vector<string> tempOutNames;
128 outputTypes["otulabels"] = tempOutNames;
130 //check for parameters
131 sharedfile = validParameter.validFile(parameters, "shared", true);
132 if (sharedfile == "not open") { abort = true; }
133 else if (sharedfile == "not found") { sharedfile = ""; }
134 else { inputFileName = sharedfile; format = "sharedfile"; m->setSharedFile(sharedfile); }
136 relabundfile = validParameter.validFile(parameters, "relabund", true);
137 if (relabundfile == "not open") { abort = true; }
138 else if (relabundfile == "not found") { relabundfile = ""; }
139 else { inputFileName = relabundfile; format = "relabund"; m->setRelAbundFile(relabundfile); }
141 if ((relabundfile == "") && (sharedfile == "")) {
142 //is there are current file available for either of these?
143 //give priority to shared, then relabund
144 sharedfile = m->getSharedFile();
145 if (sharedfile != "") { inputFileName = sharedfile; format="sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
147 relabundfile = m->getRelAbundFile();
148 if (relabundfile != "") { inputFileName = relabundfile; format="relabund"; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); }
150 m->mothurOut("No valid current files. You must provide a shared or relabund."); m->mothurOutEndLine();
156 //if the user changes the output directory command factory will send this info to us in the output parameter
157 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
158 outputDir = m->hasPath(inputFileName); //if user entered a file with a path then preserve it
161 string groups = validParameter.validFile(parameters, "groups", false);
162 if (groups == "not found") { groups = ""; }
163 else { m->splitAtDash(groups, Groups); }
164 m->setGroups(Groups);
166 string label = validParameter.validFile(parameters, "label", false);
167 if (label == "not found") { label = ""; }
169 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
170 else { allLines = 1; }
175 catch(exception& e) {
176 m->errorOut(e, "ListOtuLabelsCommand", "ListOtuLabelsCommand");
180 //**********************************************************************************************************************
182 int ListOtuLabelsCommand::execute(){
185 if (abort == true) { if (calledHelp) { return 0; } return 2; }
187 InputData input(inputFileName, format);
189 if (format == "relabund") {
190 vector<SharedRAbundFloatVector*> lookup = input.getSharedRAbundFloatVectors();
191 string lastLabel = lookup[0]->getLabel();
193 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
194 set<string> processedLabels;
195 set<string> userLabels = labels;
197 //as long as you are not at the end of the file or done wih the lines you want
198 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
200 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; }
202 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
204 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
208 processedLabels.insert(lookup[0]->getLabel());
209 userLabels.erase(lookup[0]->getLabel());
212 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
213 string saveLabel = lookup[0]->getLabel();
215 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
216 lookup = input.getSharedRAbundFloatVectors(lastLabel);
217 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
221 processedLabels.insert(lookup[0]->getLabel());
222 userLabels.erase(lookup[0]->getLabel());
224 //restore real lastlabel to save below
225 lookup[0]->setLabel(saveLabel);
228 lastLabel = lookup[0]->getLabel();
229 //prevent memory leak
230 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
232 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
234 //get next line to process
235 lookup = input.getSharedRAbundFloatVectors();
238 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
240 //output error messages about any remaining user labels
241 set<string>::iterator it;
242 bool needToRun = false;
243 for (it = userLabels.begin(); it != userLabels.end(); it++) {
244 m->mothurOut("Your file does not include the label " + *it);
245 if (processedLabels.count(lastLabel) != 1) {
246 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
249 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
253 //run last label if you need to
254 if (needToRun == true) {
255 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
256 lookup = input.getSharedRAbundFloatVectors(lastLabel);
258 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
262 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
266 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
267 string lastLabel = lookup[0]->getLabel();
269 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
270 set<string> processedLabels;
271 set<string> userLabels = labels;
273 //as long as you are not at the end of the file or done wih the lines you want
274 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
276 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; }
278 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
280 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
284 processedLabels.insert(lookup[0]->getLabel());
285 userLabels.erase(lookup[0]->getLabel());
288 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
289 string saveLabel = lookup[0]->getLabel();
291 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
292 lookup = input.getSharedRAbundVectors(lastLabel);
293 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
297 processedLabels.insert(lookup[0]->getLabel());
298 userLabels.erase(lookup[0]->getLabel());
300 //restore real lastlabel to save below
301 lookup[0]->setLabel(saveLabel);
304 lastLabel = lookup[0]->getLabel();
305 //prevent memory leak
306 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
308 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
310 //get next line to process
311 lookup = input.getSharedRAbundVectors();
314 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
316 //output error messages about any remaining user labels
317 set<string>::iterator it;
318 bool needToRun = false;
319 for (it = userLabels.begin(); it != userLabels.end(); it++) {
320 m->mothurOut("Your file does not include the label " + *it);
321 if (processedLabels.count(lastLabel) != 1) {
322 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
325 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
329 //run last label if you need to
330 if (needToRun == true) {
331 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
332 lookup = input.getSharedRAbundVectors(lastLabel);
334 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
338 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
342 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
344 //output files created by command
345 m->mothurOutEndLine();
346 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
347 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
348 m->mothurOutEndLine();
352 catch(exception& e) {
353 m->errorOut(e, "ListOtuLabelsCommand", "execute");
357 //**********************************************************************************************************************
359 int ListOtuLabelsCommand::createList(vector<SharedRAbundVector*>& lookup){
362 map<string, string> variables;
363 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputFileName));
364 variables["[distance]"] = lookup[0]->getLabel();
365 string outputFileName = getOutputFileName("otulabels",variables);
366 outputNames.push_back(outputFileName); outputTypes["otulabels"].push_back(outputFileName);
368 m->openOutputFile(outputFileName, out);
370 for (int i = 0; i < m->currentBinLabels.size(); i++) { out << m->currentBinLabels[i] << endl; }
376 catch(exception& e) {
377 m->errorOut(e, "ListOtuLabelsCommand", "createTable");
382 //**********************************************************************************************************************
384 int ListOtuLabelsCommand::createList(vector<SharedRAbundFloatVector*>& lookup){
386 map<string, string> variables;
387 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputFileName));
388 variables["[distance]"] = lookup[0]->getLabel();
389 string outputFileName = getOutputFileName("otulabels",variables);
390 outputNames.push_back(outputFileName); outputTypes["accnos"].push_back(outputFileName);
392 m->openOutputFile(outputFileName, out);
394 for (int i = 0; i < m->currentBinLabels.size(); i++) { out << m->currentBinLabels[i] << endl; }
400 catch(exception& e) {
401 m->errorOut(e, "ListOtuLabelsCommand", "createTable");
406 //**********************************************************************************************************************