]> git.donarmstrong.com Git - mothur.git/blob - listotulabelscommand.cpp
93b1510d22bc04cc19fec975df447381285b7788
[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 ListOtuLabelsCommand::ListOtuLabelsCommand(){   
51         try {
52                 abort = true; calledHelp = true;
53                 setParameters();
54         vector<string> tempOutNames;
55                 outputTypes["otulabels"] = tempOutNames; 
56         }
57         catch(exception& e) {
58                 m->errorOut(e, "ListOtuLabelsCommand", "ListOtuLabelsCommand");
59                 exit(1);
60         }
61 }
62 //**********************************************************************************************************************
63 ListOtuLabelsCommand::ListOtuLabelsCommand(string option)  {
64         try {
65                 abort = false; calledHelp = false;   
66                 allLines = 1;
67         
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;}
71                 
72                 else {
73                         //valid paramters for this command
74                         vector<string> myArray = setParameters();
75                         
76                         OptionParser parser(option);
77                         map<string,string> parameters = parser.getParameters();
78                         
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;  }
84                         }
85                         
86                         
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 = "";          }
90                         else {
91                 
92                 //edit file types below to include only the types you added as parameters
93                 
94                                 string path;
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;         }
101                                 }
102                 
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;           }
109                                 }
110             }
111             
112             vector<string> tempOutNames;
113             outputTypes["otulabels"] = tempOutNames; 
114             
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); }
120                         
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); }
125             
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(); }
131                                 else { 
132                                         relabundfile = m->getRelAbundFile(); 
133                                         if (relabundfile != "") {  inputFileName = relabundfile; format="relabund"; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); }
134                                         else { 
135                                                 m->mothurOut("No valid current files. You must provide a shared or relabund."); m->mothurOutEndLine(); 
136                                                 abort = true;
137                                         }
138                                 }
139                         }
140             
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    
144                         }
145             
146             string groups = validParameter.validFile(parameters, "groups", false);                      
147                         if (groups == "not found") { groups = ""; }
148                         else { m->splitAtDash(groups, Groups); }
149                         m->setGroups(Groups);
150             
151             string label = validParameter.validFile(parameters, "label", false);                        
152                         if (label == "not found") { label = ""; }
153                         else { 
154                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
155                                 else { allLines = 1;  }
156                         }                       
157                 }
158                 
159         }
160         catch(exception& e) {
161                 m->errorOut(e, "ListOtuLabelsCommand", "ListOtuLabelsCommand");
162                 exit(1);
163         }
164 }
165 //**********************************************************************************************************************
166
167 int ListOtuLabelsCommand::execute(){
168         try {
169                 
170                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
171         
172         InputData input(inputFileName, format);
173         
174         if (format == "relabund") {
175             vector<SharedRAbundFloatVector*> lookup = input.getSharedRAbundFloatVectors();
176             string lastLabel = lookup[0]->getLabel();
177             
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;
181             
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))) {
184                 
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; }
186                 
187                 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
188                     
189                     m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
190                     
191                     createList(lookup);
192                     
193                     processedLabels.insert(lookup[0]->getLabel());
194                     userLabels.erase(lookup[0]->getLabel());
195                 }
196                 
197                 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
198                     string saveLabel = lookup[0]->getLabel();
199                     
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();
203                     
204                     createList(lookup);
205                     
206                     processedLabels.insert(lookup[0]->getLabel());
207                     userLabels.erase(lookup[0]->getLabel());
208                     
209                     //restore real lastlabel to save below
210                     lookup[0]->setLabel(saveLabel);
211                 }
212                 
213                 lastLabel = lookup[0]->getLabel();
214                 //prevent memory leak
215                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
216                 
217                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
218                 
219                 //get next line to process
220                 lookup = input.getSharedRAbundFloatVectors();                           
221             }
222             
223             if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
224             
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();
232                     needToRun = true;
233                 }else {
234                     m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
235                 }
236             }
237             
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);
242                 
243                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
244                 
245                 createList(lookup);
246                 
247                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
248             }
249         }else {
250             
251             vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
252             string lastLabel = lookup[0]->getLabel();
253             
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;
257             
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))) {
260                 
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; }
262                 
263                 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
264                     
265                     m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
266                     
267                     createList(lookup);
268                     
269                     processedLabels.insert(lookup[0]->getLabel());
270                     userLabels.erase(lookup[0]->getLabel());
271                 }
272                 
273                 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
274                     string saveLabel = lookup[0]->getLabel();
275                     
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();
279                     
280                     createList(lookup);
281                     
282                     processedLabels.insert(lookup[0]->getLabel());
283                     userLabels.erase(lookup[0]->getLabel());
284                     
285                     //restore real lastlabel to save below
286                     lookup[0]->setLabel(saveLabel);
287                 }
288                 
289                 lastLabel = lookup[0]->getLabel();
290                 //prevent memory leak
291                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
292                 
293                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
294                 
295                 //get next line to process
296                 lookup = input.getSharedRAbundVectors();                                
297             }
298             
299             if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
300             
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();
308                     needToRun = true;
309                 }else {
310                     m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
311                 }
312             }
313             
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);
318                 
319                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
320                 
321                 createList(lookup);
322                 
323                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
324             }
325         }
326         
327         if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
328         
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();
334         return 0;
335                 
336     }
337         catch(exception& e) {
338                 m->errorOut(e, "ListOtuLabelsCommand", "execute");
339                 exit(1);
340         }
341 }
342 //**********************************************************************************************************************
343
344 int ListOtuLabelsCommand::createList(vector<SharedRAbundVector*>& lookup){
345         try {
346         
347         string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + lookup[0]->getLabel() + ".otu.labels";
348         outputNames.push_back(outputFileName);  outputTypes["otulabels"].push_back(outputFileName);
349                 ofstream out;
350                 m->openOutputFile(outputFileName, out);
351         
352         for (int i = 0; i < m->currentBinLabels.size(); i++) {  out << m->currentBinLabels[i] << endl;  }
353         
354         out.close();
355         
356         return 0;
357     }
358         catch(exception& e) {
359                 m->errorOut(e, "ListOtuLabelsCommand", "createTable");
360                 exit(1);
361         }
362 }
363
364 //**********************************************************************************************************************
365
366 int ListOtuLabelsCommand::createList(vector<SharedRAbundFloatVector*>& lookup){
367         try {
368         
369         string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + lookup[0]->getLabel() + ".otu.labels";
370         outputNames.push_back(outputFileName);  outputTypes["accnos"].push_back(outputFileName);
371                 ofstream out;
372                 m->openOutputFile(outputFileName, out);
373         
374         for (int i = 0; i < m->currentBinLabels.size(); i++) {  out << m->currentBinLabels[i] << endl;  }
375         
376         out.close();
377         
378         return 0;
379     }
380         catch(exception& e) {
381                 m->errorOut(e, "ListOtuLabelsCommand", "createTable");
382                 exit(1);
383         }
384 }
385
386 //**********************************************************************************************************************
387