]> git.donarmstrong.com Git - mothur.git/blob - listotulabelscommand.cpp
Merge remote-tracking branch 'mothur/master'
[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",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);
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::getOutputFileNameTag(string type, string inputName=""){    
51         try {
52         string outputFileName = "";
53                 map<string, vector<string> >::iterator it;
54         
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"); }
58         else {
59             if (type == "otulabels")             {   outputFileName =  "otulabels";       }
60             else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
61         }
62         return outputFileName;
63         }
64         catch(exception& e) {
65                 m->errorOut(e, "ListOtuLabelsCommand", "getOutputFileNameTag");
66                 exit(1);
67         }
68 }
69
70 //**********************************************************************************************************************
71 ListOtuLabelsCommand::ListOtuLabelsCommand(){   
72         try {
73                 abort = true; calledHelp = true;
74                 setParameters();
75         vector<string> tempOutNames;
76                 outputTypes["otulabels"] = tempOutNames; 
77         }
78         catch(exception& e) {
79                 m->errorOut(e, "ListOtuLabelsCommand", "ListOtuLabelsCommand");
80                 exit(1);
81         }
82 }
83 //**********************************************************************************************************************
84 ListOtuLabelsCommand::ListOtuLabelsCommand(string option)  {
85         try {
86                 abort = false; calledHelp = false;   
87                 allLines = 1;
88         
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;}
92                 
93                 else {
94                         //valid paramters for this command
95                         vector<string> myArray = setParameters();
96                         
97                         OptionParser parser(option);
98                         map<string,string> parameters = parser.getParameters();
99                         
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;  }
105                         }
106                         
107                         
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 = "";          }
111                         else {
112                 
113                 //edit file types below to include only the types you added as parameters
114                 
115                                 string path;
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;         }
122                                 }
123                 
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;           }
130                                 }
131             }
132             
133             vector<string> tempOutNames;
134             outputTypes["otulabels"] = tempOutNames; 
135             
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); }
141                         
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); }
146             
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(); }
152                                 else { 
153                                         relabundfile = m->getRelAbundFile(); 
154                                         if (relabundfile != "") {  inputFileName = relabundfile; format="relabund"; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); }
155                                         else { 
156                                                 m->mothurOut("No valid current files. You must provide a shared or relabund."); m->mothurOutEndLine(); 
157                                                 abort = true;
158                                         }
159                                 }
160                         }
161             
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    
165                         }
166             
167             string groups = validParameter.validFile(parameters, "groups", false);                      
168                         if (groups == "not found") { groups = ""; }
169                         else { m->splitAtDash(groups, Groups); }
170                         m->setGroups(Groups);
171             
172             string label = validParameter.validFile(parameters, "label", false);                        
173                         if (label == "not found") { label = ""; }
174                         else { 
175                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
176                                 else { allLines = 1;  }
177                         }                       
178                 }
179                 
180         }
181         catch(exception& e) {
182                 m->errorOut(e, "ListOtuLabelsCommand", "ListOtuLabelsCommand");
183                 exit(1);
184         }
185 }
186 //**********************************************************************************************************************
187
188 int ListOtuLabelsCommand::execute(){
189         try {
190                 
191                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
192         
193         InputData input(inputFileName, format);
194         
195         if (format == "relabund") {
196             vector<SharedRAbundFloatVector*> lookup = input.getSharedRAbundFloatVectors();
197             string lastLabel = lookup[0]->getLabel();
198             
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;
202             
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))) {
205                 
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; }
207                 
208                 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
209                     
210                     m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
211                     
212                     createList(lookup);
213                     
214                     processedLabels.insert(lookup[0]->getLabel());
215                     userLabels.erase(lookup[0]->getLabel());
216                 }
217                 
218                 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
219                     string saveLabel = lookup[0]->getLabel();
220                     
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();
224                     
225                     createList(lookup);
226                     
227                     processedLabels.insert(lookup[0]->getLabel());
228                     userLabels.erase(lookup[0]->getLabel());
229                     
230                     //restore real lastlabel to save below
231                     lookup[0]->setLabel(saveLabel);
232                 }
233                 
234                 lastLabel = lookup[0]->getLabel();
235                 //prevent memory leak
236                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
237                 
238                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
239                 
240                 //get next line to process
241                 lookup = input.getSharedRAbundFloatVectors();                           
242             }
243             
244             if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
245             
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();
253                     needToRun = true;
254                 }else {
255                     m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
256                 }
257             }
258             
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);
263                 
264                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
265                 
266                 createList(lookup);
267                 
268                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
269             }
270         }else {
271             
272             vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
273             string lastLabel = lookup[0]->getLabel();
274             
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;
278             
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))) {
281                 
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; }
283                 
284                 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
285                     
286                     m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
287                     
288                     createList(lookup);
289                     
290                     processedLabels.insert(lookup[0]->getLabel());
291                     userLabels.erase(lookup[0]->getLabel());
292                 }
293                 
294                 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
295                     string saveLabel = lookup[0]->getLabel();
296                     
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();
300                     
301                     createList(lookup);
302                     
303                     processedLabels.insert(lookup[0]->getLabel());
304                     userLabels.erase(lookup[0]->getLabel());
305                     
306                     //restore real lastlabel to save below
307                     lookup[0]->setLabel(saveLabel);
308                 }
309                 
310                 lastLabel = lookup[0]->getLabel();
311                 //prevent memory leak
312                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
313                 
314                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
315                 
316                 //get next line to process
317                 lookup = input.getSharedRAbundVectors();                                
318             }
319             
320             if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
321             
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();
329                     needToRun = true;
330                 }else {
331                     m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
332                 }
333             }
334             
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);
339                 
340                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
341                 
342                 createList(lookup);
343                 
344                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
345             }
346         }
347         
348         if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
349         
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();
355         return 0;
356                 
357     }
358         catch(exception& e) {
359                 m->errorOut(e, "ListOtuLabelsCommand", "execute");
360                 exit(1);
361         }
362 }
363 //**********************************************************************************************************************
364
365 int ListOtuLabelsCommand::createList(vector<SharedRAbundVector*>& lookup){
366         try {
367         
368         string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + lookup[0]->getLabel() + "." + getOutputFileNameTag("otulabels");
369         outputNames.push_back(outputFileName);  outputTypes["otulabels"].push_back(outputFileName);
370                 ofstream out;
371                 m->openOutputFile(outputFileName, out);
372         
373         for (int i = 0; i < m->currentBinLabels.size(); i++) {  out << m->currentBinLabels[i] << endl;  }
374         
375         out.close();
376         
377         return 0;
378     }
379         catch(exception& e) {
380                 m->errorOut(e, "ListOtuLabelsCommand", "createTable");
381                 exit(1);
382         }
383 }
384
385 //**********************************************************************************************************************
386
387 int ListOtuLabelsCommand::createList(vector<SharedRAbundFloatVector*>& lookup){
388         try {
389         
390         string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + lookup[0]->getLabel() + "." + getOutputFileNameTag("otulabels");
391         outputNames.push_back(outputFileName);  outputTypes["accnos"].push_back(outputFileName);
392                 ofstream out;
393                 m->openOutputFile(outputFileName, out);
394         
395         for (int i = 0; i < m->currentBinLabels.size(); i++) {  out << m->currentBinLabels[i] << endl;  }
396         
397         out.close();
398         
399         return 0;
400     }
401         catch(exception& e) {
402                 m->errorOut(e, "ListOtuLabelsCommand", "createTable");
403                 exit(1);
404         }
405 }
406
407 //**********************************************************************************************************************
408