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.labels lists otu labels from shared or relabund file. The results can be used by the get.labels to select specific otus with the output from classify.otu, otu.association, or corr.axes.\n";
37 helpString += "The list.labels 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.labels 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 ListOtuLabelsCommand::ListOtuLabelsCommand(){
52 abort = true; calledHelp = true;
54 vector<string> tempOutNames;
55 outputTypes["otulabels"] = tempOutNames;
58 m->errorOut(e, "ListOtuLabelsCommand", "ListOtuLabelsCommand");
62 //**********************************************************************************************************************
63 ListOtuLabelsCommand::ListOtuLabelsCommand(string option) {
65 abort = false; calledHelp = false;
68 //allow user to run help
69 if(option == "help") { help(); abort = true; calledHelp = true; }
70 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
73 //valid paramters for this command
74 vector<string> myArray = setParameters();
76 OptionParser parser(option);
77 map<string,string> parameters = parser.getParameters();
79 ValidParameters validParameter;
80 map<string,string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 //if the user changes the input directory command factory will send this info to us in the output parameter
88 string inputDir = validParameter.validFile(parameters, "inputdir", false);
89 if (inputDir == "not found"){ inputDir = ""; }
92 //edit file types below to include only the types you added as parameters
95 it = parameters.find("relabund");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["relabund"] = inputDir + it->second; }
103 it = parameters.find("shared");
104 //user has given a template file
105 if(it != parameters.end()){
106 path = m->hasPath(it->second);
107 //if the user has not given a path then, add inputdir. else leave path alone.
108 if (path == "") { parameters["shared"] = inputDir + it->second; }
112 vector<string> tempOutNames;
113 outputTypes["otulabels"] = tempOutNames;
115 //check for parameters
116 sharedfile = validParameter.validFile(parameters, "shared", true);
117 if (sharedfile == "not open") { abort = true; }
118 else if (sharedfile == "not found") { sharedfile = ""; }
119 else { inputFileName = sharedfile; format = "sharedfile"; m->setSharedFile(sharedfile); }
121 relabundfile = validParameter.validFile(parameters, "relabund", true);
122 if (relabundfile == "not open") { abort = true; }
123 else if (relabundfile == "not found") { relabundfile = ""; }
124 else { inputFileName = relabundfile; format = "relabund"; m->setRelAbundFile(relabundfile); }
126 if ((relabundfile == "") && (sharedfile == "")) {
127 //is there are current file available for either of these?
128 //give priority to shared, then relabund
129 sharedfile = m->getSharedFile();
130 if (sharedfile != "") { inputFileName = sharedfile; format="sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
132 relabundfile = m->getRelAbundFile();
133 if (relabundfile != "") { inputFileName = relabundfile; format="relabund"; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); }
135 m->mothurOut("No valid current files. You must provide a shared or relabund."); m->mothurOutEndLine();
141 //if the user changes the output directory command factory will send this info to us in the output parameter
142 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
143 outputDir = m->hasPath(inputFileName); //if user entered a file with a path then preserve it
146 string groups = validParameter.validFile(parameters, "groups", false);
147 if (groups == "not found") { groups = ""; }
148 else { m->splitAtDash(groups, Groups); }
149 m->setGroups(Groups);
151 string label = validParameter.validFile(parameters, "label", false);
152 if (label == "not found") { label = ""; }
154 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
155 else { allLines = 1; }
160 catch(exception& e) {
161 m->errorOut(e, "ListOtuLabelsCommand", "ListOtuLabelsCommand");
165 //**********************************************************************************************************************
167 int ListOtuLabelsCommand::execute(){
170 if (abort == true) { if (calledHelp) { return 0; } return 2; }
172 InputData input(inputFileName, format);
174 if (format == "relabund") {
175 vector<SharedRAbundFloatVector*> lookup = input.getSharedRAbundFloatVectors();
176 string lastLabel = lookup[0]->getLabel();
178 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
179 set<string> processedLabels;
180 set<string> userLabels = labels;
182 //as long as you are not at the end of the file or done wih the lines you want
183 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
185 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; }
187 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
189 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
193 processedLabels.insert(lookup[0]->getLabel());
194 userLabels.erase(lookup[0]->getLabel());
197 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
198 string saveLabel = lookup[0]->getLabel();
200 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
201 lookup = input.getSharedRAbundFloatVectors(lastLabel);
202 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
206 processedLabels.insert(lookup[0]->getLabel());
207 userLabels.erase(lookup[0]->getLabel());
209 //restore real lastlabel to save below
210 lookup[0]->setLabel(saveLabel);
213 lastLabel = lookup[0]->getLabel();
214 //prevent memory leak
215 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
217 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
219 //get next line to process
220 lookup = input.getSharedRAbundFloatVectors();
223 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
225 //output error messages about any remaining user labels
226 set<string>::iterator it;
227 bool needToRun = false;
228 for (it = userLabels.begin(); it != userLabels.end(); it++) {
229 m->mothurOut("Your file does not include the label " + *it);
230 if (processedLabels.count(lastLabel) != 1) {
231 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
234 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
238 //run last label if you need to
239 if (needToRun == true) {
240 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
241 lookup = input.getSharedRAbundFloatVectors(lastLabel);
243 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
247 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
251 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
252 string lastLabel = lookup[0]->getLabel();
254 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
255 set<string> processedLabels;
256 set<string> userLabels = labels;
258 //as long as you are not at the end of the file or done wih the lines you want
259 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
261 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; }
263 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
265 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
269 processedLabels.insert(lookup[0]->getLabel());
270 userLabels.erase(lookup[0]->getLabel());
273 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
274 string saveLabel = lookup[0]->getLabel();
276 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
277 lookup = input.getSharedRAbundVectors(lastLabel);
278 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
282 processedLabels.insert(lookup[0]->getLabel());
283 userLabels.erase(lookup[0]->getLabel());
285 //restore real lastlabel to save below
286 lookup[0]->setLabel(saveLabel);
289 lastLabel = lookup[0]->getLabel();
290 //prevent memory leak
291 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
293 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
295 //get next line to process
296 lookup = input.getSharedRAbundVectors();
299 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
301 //output error messages about any remaining user labels
302 set<string>::iterator it;
303 bool needToRun = false;
304 for (it = userLabels.begin(); it != userLabels.end(); it++) {
305 m->mothurOut("Your file does not include the label " + *it);
306 if (processedLabels.count(lastLabel) != 1) {
307 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
310 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
314 //run last label if you need to
315 if (needToRun == true) {
316 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
317 lookup = input.getSharedRAbundVectors(lastLabel);
319 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
323 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
327 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
329 //output files created by command
330 m->mothurOutEndLine();
331 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
332 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
333 m->mothurOutEndLine();
337 catch(exception& e) {
338 m->errorOut(e, "ListOtuLabelsCommand", "execute");
342 //**********************************************************************************************************************
344 int ListOtuLabelsCommand::createList(vector<SharedRAbundVector*>& lookup){
347 string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + lookup[0]->getLabel() + ".otu.labels";
348 outputNames.push_back(outputFileName); outputTypes["otulabels"].push_back(outputFileName);
350 m->openOutputFile(outputFileName, out);
352 for (int i = 0; i < m->currentBinLabels.size(); i++) { out << m->currentBinLabels[i] << endl; }
358 catch(exception& e) {
359 m->errorOut(e, "ListOtuLabelsCommand", "createTable");
364 //**********************************************************************************************************************
366 int ListOtuLabelsCommand::createList(vector<SharedRAbundFloatVector*>& lookup){
369 string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + lookup[0]->getLabel() + ".otu.labels";
370 outputNames.push_back(outputFileName); outputTypes["accnos"].push_back(outputFileName);
372 m->openOutputFile(outputFileName, out);
374 for (int i = 0; i < m->currentBinLabels.size(); i++) { out << m->currentBinLabels[i] << endl; }
380 catch(exception& e) {
381 m->errorOut(e, "ListOtuLabelsCommand", "createTable");
386 //**********************************************************************************************************************