]> git.donarmstrong.com Git - mothur.git/blob - listotulabelscommand.cpp
fixed bug in phylo.diversity rooting. added filename patterns and create filename...
[mothur.git] / listotulabelscommand.cpp
1 //
2 //  listotucommand.cpp
3 //  Mothur
4 //
5 //  Created by Sarah Westcott on 5/15/12.
6 //  Copyright (c) 2012 Schloss Lab. All rights reserved.
7 //
8
9 #include "listotulabelscommand.h"
10 #include "inputdata.h"
11
12 //**********************************************************************************************************************
13 vector<string> ListOtuLabelsCommand::setParameters(){   
14         try {
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);
22                 
23                 vector<string> myArray;
24                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
25                 return myArray;
26         }
27         catch(exception& e) {
28                 m->errorOut(e, "ListOtuLabelsCommand", "setParameters");
29                 exit(1);
30         }
31 }
32 //**********************************************************************************************************************
33 string ListOtuLabelsCommand::getHelpString(){   
34         try {
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";
42                 return helpString;
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "ListOtuLabelsCommand", "getHelpString");
46                 exit(1);
47         }
48 }
49 //**********************************************************************************************************************
50 string ListOtuLabelsCommand::getOutputPattern(string type) {
51     try {
52         string pattern = "";
53         
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;  }
56         
57         return pattern;
58     }
59     catch(exception& e) {
60         m->errorOut(e, "ListOtuLabelsCommand", "getOutputPattern");
61         exit(1);
62     }
63 }
64 //**********************************************************************************************************************
65 ListOtuLabelsCommand::ListOtuLabelsCommand(){   
66         try {
67                 abort = true; calledHelp = true;
68                 setParameters();
69         vector<string> tempOutNames;
70                 outputTypes["otulabels"] = tempOutNames; 
71         }
72         catch(exception& e) {
73                 m->errorOut(e, "ListOtuLabelsCommand", "ListOtuLabelsCommand");
74                 exit(1);
75         }
76 }
77 //**********************************************************************************************************************
78 ListOtuLabelsCommand::ListOtuLabelsCommand(string option)  {
79         try {
80                 abort = false; calledHelp = false;   
81                 allLines = 1;
82         
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;}
86                 
87                 else {
88                         //valid paramters for this command
89                         vector<string> myArray = setParameters();
90                         
91                         OptionParser parser(option);
92                         map<string,string> parameters = parser.getParameters();
93                         
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;  }
99                         }
100                         
101                         
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 = "";          }
105                         else {
106                 
107                 //edit file types below to include only the types you added as parameters
108                 
109                                 string path;
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;         }
116                                 }
117                 
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;           }
124                                 }
125             }
126             
127             vector<string> tempOutNames;
128             outputTypes["otulabels"] = tempOutNames; 
129             
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); }
135                         
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); }
140             
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(); }
146                                 else { 
147                                         relabundfile = m->getRelAbundFile(); 
148                                         if (relabundfile != "") {  inputFileName = relabundfile; format="relabund"; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); }
149                                         else { 
150                                                 m->mothurOut("No valid current files. You must provide a shared or relabund."); m->mothurOutEndLine(); 
151                                                 abort = true;
152                                         }
153                                 }
154                         }
155             
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    
159                         }
160             
161             string groups = validParameter.validFile(parameters, "groups", false);                      
162                         if (groups == "not found") { groups = ""; }
163                         else { m->splitAtDash(groups, Groups); }
164                         m->setGroups(Groups);
165             
166             string label = validParameter.validFile(parameters, "label", false);                        
167                         if (label == "not found") { label = ""; }
168                         else { 
169                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
170                                 else { allLines = 1;  }
171                         }                       
172                 }
173                 
174         }
175         catch(exception& e) {
176                 m->errorOut(e, "ListOtuLabelsCommand", "ListOtuLabelsCommand");
177                 exit(1);
178         }
179 }
180 //**********************************************************************************************************************
181
182 int ListOtuLabelsCommand::execute(){
183         try {
184                 
185                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
186         
187         InputData input(inputFileName, format);
188         
189         if (format == "relabund") {
190             vector<SharedRAbundFloatVector*> lookup = input.getSharedRAbundFloatVectors();
191             string lastLabel = lookup[0]->getLabel();
192             
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;
196             
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))) {
199                 
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; }
201                 
202                 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
203                     
204                     m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
205                     
206                     createList(lookup);
207                     
208                     processedLabels.insert(lookup[0]->getLabel());
209                     userLabels.erase(lookup[0]->getLabel());
210                 }
211                 
212                 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
213                     string saveLabel = lookup[0]->getLabel();
214                     
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();
218                     
219                     createList(lookup);
220                     
221                     processedLabels.insert(lookup[0]->getLabel());
222                     userLabels.erase(lookup[0]->getLabel());
223                     
224                     //restore real lastlabel to save below
225                     lookup[0]->setLabel(saveLabel);
226                 }
227                 
228                 lastLabel = lookup[0]->getLabel();
229                 //prevent memory leak
230                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
231                 
232                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
233                 
234                 //get next line to process
235                 lookup = input.getSharedRAbundFloatVectors();                           
236             }
237             
238             if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
239             
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();
247                     needToRun = true;
248                 }else {
249                     m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
250                 }
251             }
252             
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);
257                 
258                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
259                 
260                 createList(lookup);
261                 
262                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
263             }
264         }else {
265             
266             vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
267             string lastLabel = lookup[0]->getLabel();
268             
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;
272             
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))) {
275                 
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; }
277                 
278                 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
279                     
280                     m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
281                     
282                     createList(lookup);
283                     
284                     processedLabels.insert(lookup[0]->getLabel());
285                     userLabels.erase(lookup[0]->getLabel());
286                 }
287                 
288                 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
289                     string saveLabel = lookup[0]->getLabel();
290                     
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();
294                     
295                     createList(lookup);
296                     
297                     processedLabels.insert(lookup[0]->getLabel());
298                     userLabels.erase(lookup[0]->getLabel());
299                     
300                     //restore real lastlabel to save below
301                     lookup[0]->setLabel(saveLabel);
302                 }
303                 
304                 lastLabel = lookup[0]->getLabel();
305                 //prevent memory leak
306                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
307                 
308                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
309                 
310                 //get next line to process
311                 lookup = input.getSharedRAbundVectors();                                
312             }
313             
314             if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
315             
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();
323                     needToRun = true;
324                 }else {
325                     m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
326                 }
327             }
328             
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);
333                 
334                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
335                 
336                 createList(lookup);
337                 
338                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
339             }
340         }
341         
342         if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
343         
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();
349         return 0;
350                 
351     }
352         catch(exception& e) {
353                 m->errorOut(e, "ListOtuLabelsCommand", "execute");
354                 exit(1);
355         }
356 }
357 //**********************************************************************************************************************
358
359 int ListOtuLabelsCommand::createList(vector<SharedRAbundVector*>& lookup){
360         try {
361         
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);
367                 ofstream out;
368                 m->openOutputFile(outputFileName, out);
369         
370         for (int i = 0; i < m->currentBinLabels.size(); i++) {  out << m->currentBinLabels[i] << endl;  }
371         
372         out.close();
373         
374         return 0;
375     }
376         catch(exception& e) {
377                 m->errorOut(e, "ListOtuLabelsCommand", "createTable");
378                 exit(1);
379         }
380 }
381
382 //**********************************************************************************************************************
383
384 int ListOtuLabelsCommand::createList(vector<SharedRAbundFloatVector*>& lookup){
385         try {
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);
391                 ofstream out;
392                 m->openOutputFile(outputFileName, out);
393         
394         for (int i = 0; i < m->currentBinLabels.size(); i++) {  out << m->currentBinLabels[i] << endl;  }
395         
396         out.close();
397         
398         return 0;
399     }
400         catch(exception& e) {
401                 m->errorOut(e, "ListOtuLabelsCommand", "createTable");
402                 exit(1);
403         }
404 }
405
406 //**********************************************************************************************************************
407