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",false,false); parameters.push_back(pshared);
16 CommandParameter prelabund("relabund", "InputTypes", "", "", "SharedRel", "SharedRel", "none",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::getOutputFileNameTag(string type, string inputName=""){
52 string outputFileName = "";
53 map<string, vector<string> >::iterator it;
55 //is this a type this command creates
56 it = outputTypes.find(type);
57 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
59 if (type == "otulabels") { outputFileName = "otulabels"; }
60 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
62 return outputFileName;
65 m->errorOut(e, "ListOtuLabelsCommand", "getOutputFileNameTag");
70 //**********************************************************************************************************************
71 ListOtuLabelsCommand::ListOtuLabelsCommand(){
73 abort = true; calledHelp = true;
75 vector<string> tempOutNames;
76 outputTypes["otulabels"] = tempOutNames;
79 m->errorOut(e, "ListOtuLabelsCommand", "ListOtuLabelsCommand");
83 //**********************************************************************************************************************
84 ListOtuLabelsCommand::ListOtuLabelsCommand(string option) {
86 abort = false; calledHelp = false;
89 //allow user to run help
90 if(option == "help") { help(); abort = true; calledHelp = true; }
91 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
94 //valid paramters for this command
95 vector<string> myArray = setParameters();
97 OptionParser parser(option);
98 map<string,string> parameters = parser.getParameters();
100 ValidParameters validParameter;
101 map<string,string>::iterator it;
102 //check to make sure all parameters are valid for command
103 for (it = parameters.begin(); it != parameters.end(); it++) {
104 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
108 //if the user changes the input directory command factory will send this info to us in the output parameter
109 string inputDir = validParameter.validFile(parameters, "inputdir", false);
110 if (inputDir == "not found"){ inputDir = ""; }
113 //edit file types below to include only the types you added as parameters
116 it = parameters.find("relabund");
117 //user has given a template file
118 if(it != parameters.end()){
119 path = m->hasPath(it->second);
120 //if the user has not given a path then, add inputdir. else leave path alone.
121 if (path == "") { parameters["relabund"] = inputDir + it->second; }
124 it = parameters.find("shared");
125 //user has given a template file
126 if(it != parameters.end()){
127 path = m->hasPath(it->second);
128 //if the user has not given a path then, add inputdir. else leave path alone.
129 if (path == "") { parameters["shared"] = inputDir + it->second; }
133 vector<string> tempOutNames;
134 outputTypes["otulabels"] = tempOutNames;
136 //check for parameters
137 sharedfile = validParameter.validFile(parameters, "shared", true);
138 if (sharedfile == "not open") { abort = true; }
139 else if (sharedfile == "not found") { sharedfile = ""; }
140 else { inputFileName = sharedfile; format = "sharedfile"; m->setSharedFile(sharedfile); }
142 relabundfile = validParameter.validFile(parameters, "relabund", true);
143 if (relabundfile == "not open") { abort = true; }
144 else if (relabundfile == "not found") { relabundfile = ""; }
145 else { inputFileName = relabundfile; format = "relabund"; m->setRelAbundFile(relabundfile); }
147 if ((relabundfile == "") && (sharedfile == "")) {
148 //is there are current file available for either of these?
149 //give priority to shared, then relabund
150 sharedfile = m->getSharedFile();
151 if (sharedfile != "") { inputFileName = sharedfile; format="sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
153 relabundfile = m->getRelAbundFile();
154 if (relabundfile != "") { inputFileName = relabundfile; format="relabund"; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); }
156 m->mothurOut("No valid current files. You must provide a shared or relabund."); m->mothurOutEndLine();
162 //if the user changes the output directory command factory will send this info to us in the output parameter
163 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
164 outputDir = m->hasPath(inputFileName); //if user entered a file with a path then preserve it
167 string groups = validParameter.validFile(parameters, "groups", false);
168 if (groups == "not found") { groups = ""; }
169 else { m->splitAtDash(groups, Groups); }
170 m->setGroups(Groups);
172 string label = validParameter.validFile(parameters, "label", false);
173 if (label == "not found") { label = ""; }
175 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
176 else { allLines = 1; }
181 catch(exception& e) {
182 m->errorOut(e, "ListOtuLabelsCommand", "ListOtuLabelsCommand");
186 //**********************************************************************************************************************
188 int ListOtuLabelsCommand::execute(){
191 if (abort == true) { if (calledHelp) { return 0; } return 2; }
193 InputData input(inputFileName, format);
195 if (format == "relabund") {
196 vector<SharedRAbundFloatVector*> lookup = input.getSharedRAbundFloatVectors();
197 string lastLabel = lookup[0]->getLabel();
199 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
200 set<string> processedLabels;
201 set<string> userLabels = labels;
203 //as long as you are not at the end of the file or done wih the lines you want
204 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
206 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; }
208 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
210 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
214 processedLabels.insert(lookup[0]->getLabel());
215 userLabels.erase(lookup[0]->getLabel());
218 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
219 string saveLabel = lookup[0]->getLabel();
221 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
222 lookup = input.getSharedRAbundFloatVectors(lastLabel);
223 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
227 processedLabels.insert(lookup[0]->getLabel());
228 userLabels.erase(lookup[0]->getLabel());
230 //restore real lastlabel to save below
231 lookup[0]->setLabel(saveLabel);
234 lastLabel = lookup[0]->getLabel();
235 //prevent memory leak
236 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
238 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
240 //get next line to process
241 lookup = input.getSharedRAbundFloatVectors();
244 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
246 //output error messages about any remaining user labels
247 set<string>::iterator it;
248 bool needToRun = false;
249 for (it = userLabels.begin(); it != userLabels.end(); it++) {
250 m->mothurOut("Your file does not include the label " + *it);
251 if (processedLabels.count(lastLabel) != 1) {
252 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
255 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
259 //run last label if you need to
260 if (needToRun == true) {
261 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
262 lookup = input.getSharedRAbundFloatVectors(lastLabel);
264 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
268 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
272 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
273 string lastLabel = lookup[0]->getLabel();
275 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
276 set<string> processedLabels;
277 set<string> userLabels = labels;
279 //as long as you are not at the end of the file or done wih the lines you want
280 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
282 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; }
284 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
286 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
290 processedLabels.insert(lookup[0]->getLabel());
291 userLabels.erase(lookup[0]->getLabel());
294 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
295 string saveLabel = lookup[0]->getLabel();
297 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
298 lookup = input.getSharedRAbundVectors(lastLabel);
299 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
303 processedLabels.insert(lookup[0]->getLabel());
304 userLabels.erase(lookup[0]->getLabel());
306 //restore real lastlabel to save below
307 lookup[0]->setLabel(saveLabel);
310 lastLabel = lookup[0]->getLabel();
311 //prevent memory leak
312 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
314 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
316 //get next line to process
317 lookup = input.getSharedRAbundVectors();
320 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
322 //output error messages about any remaining user labels
323 set<string>::iterator it;
324 bool needToRun = false;
325 for (it = userLabels.begin(); it != userLabels.end(); it++) {
326 m->mothurOut("Your file does not include the label " + *it);
327 if (processedLabels.count(lastLabel) != 1) {
328 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
331 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
335 //run last label if you need to
336 if (needToRun == true) {
337 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
338 lookup = input.getSharedRAbundVectors(lastLabel);
340 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
344 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
348 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
350 //output files created by command
351 m->mothurOutEndLine();
352 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
353 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
354 m->mothurOutEndLine();
358 catch(exception& e) {
359 m->errorOut(e, "ListOtuLabelsCommand", "execute");
363 //**********************************************************************************************************************
365 int ListOtuLabelsCommand::createList(vector<SharedRAbundVector*>& lookup){
368 string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + lookup[0]->getLabel() + "." + getOutputFileNameTag("otulabels");
369 outputNames.push_back(outputFileName); outputTypes["otulabels"].push_back(outputFileName);
371 m->openOutputFile(outputFileName, out);
373 for (int i = 0; i < m->currentBinLabels.size(); i++) { out << m->currentBinLabels[i] << endl; }
379 catch(exception& e) {
380 m->errorOut(e, "ListOtuLabelsCommand", "createTable");
385 //**********************************************************************************************************************
387 int ListOtuLabelsCommand::createList(vector<SharedRAbundFloatVector*>& lookup){
390 string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + lookup[0]->getLabel() + "." + getOutputFileNameTag("otulabels");
391 outputNames.push_back(outputFileName); outputTypes["accnos"].push_back(outputFileName);
393 m->openOutputFile(outputFileName, out);
395 for (int i = 0; i < m->currentBinLabels.size(); i++) { out << m->currentBinLabels[i] << endl; }
401 catch(exception& e) {
402 m->errorOut(e, "ListOtuLabelsCommand", "createTable");
407 //**********************************************************************************************************************