]> git.donarmstrong.com Git - mothur.git/blob - getsabundcommand.cpp
added load.logfile command. changed summary.single output for subsample=t.
[mothur.git] / getsabundcommand.cpp
1 /*
2  *  getsabundcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 6/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "getsabundcommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> GetSAbundCommand::setParameters(){       
14         try {
15                 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(plist);
16                 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(prabund);            
17                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
18                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
19                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
20                 
21                 vector<string> myArray;
22                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
23                 return myArray;
24         }
25         catch(exception& e) {
26                 m->errorOut(e, "GetSAbundCommand", "setParameters");
27                 exit(1);
28         }
29 }
30 //**********************************************************************************************************************
31 string GetSAbundCommand::getHelpString(){       
32         try {
33                 string helpString = "";
34                 helpString += "The get.sabund command parameters is list, rabund and label.  list or rabund is required unless a valid current file exists.\n";
35                 helpString += "The label parameter allows you to select what distance levels you would like included in your .sabund file, and are separated by dashes.\n";
36                 helpString += "The get.sabund command should be in the following format: get.sabund(label=yourLabels).\n";
37                 helpString += "Example get.sabund().\n";
38                 helpString += "The default value for label is all labels in your inputfile.\n";
39                 helpString += "The get.sabund command outputs a .sabund file containing the labels you selected.\n";
40                 helpString += "Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n";
41                 return helpString;
42         }
43         catch(exception& e) {
44                 m->errorOut(e, "GetSAbundCommand", "getHelpString");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49 string GetSAbundCommand::getOutputFileNameTag(string type, string inputName=""){        
50         try {
51         string outputFileName = "";
52                 map<string, vector<string> >::iterator it;
53         
54         //is this a type this command creates
55         it = outputTypes.find(type);
56         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
57         else {
58             if (type == "sabund")            {   outputFileName = "sabund";  }
59             else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
60         }
61         return outputFileName;
62         }
63         catch(exception& e) {
64                 m->errorOut(e, "GetSAbundCommand", "getOutputFileNameTag");
65                 exit(1);
66         }
67 }
68 //**********************************************************************************************************************
69 GetSAbundCommand::GetSAbundCommand(){   
70         try {
71                 abort = true; calledHelp = true; 
72                 setParameters();
73                 vector<string> tempOutNames;
74                 outputTypes["sabund"] = tempOutNames;
75         }
76         catch(exception& e) {
77                 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
78                 exit(1);
79         }
80 }
81 //**********************************************************************************************************************
82 GetSAbundCommand::GetSAbundCommand(string option)  {
83         try {
84                 abort = false; calledHelp = false;   
85                 allLines = 1;
86                 
87                 //allow user to run help
88                 if(option == "help") { help(); abort = true; calledHelp = true; }
89                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
90                 
91                 else {
92                         vector<string> myArray = setParameters();
93                         
94                         OptionParser parser(option);
95                         map<string,string> parameters = parser.getParameters();
96                         map<string,string>::iterator it;
97                         
98                         ValidParameters validParameter;
99                         
100                         //check to make sure all parameters are valid for command
101                         for (it = parameters.begin(); it != parameters.end(); it++) { 
102                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
103                         }
104                         
105                         //initialize outputTypes
106                         vector<string> tempOutNames;
107                         outputTypes["sabund"] = tempOutNames;
108                         
109                         //if the user changes the input directory command factory will send this info to us in the output parameter 
110                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
111                         if (inputDir == "not found"){   inputDir = "";          }
112                         else {
113                                 string path;
114                                 it = parameters.find("list");
115                                 //user has given a template file
116                                 if(it != parameters.end()){ 
117                                         path = m->hasPath(it->second);
118                                         //if the user has not given a path then, add inputdir. else leave path alone.
119                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
120                                 }
121                                 
122                                 it = parameters.find("rabund");
123                                 //user has given a template file
124                                 if(it != parameters.end()){ 
125                                         path = m->hasPath(it->second);
126                                         //if the user has not given a path then, add inputdir. else leave path alone.
127                                         if (path == "") {       parameters["rabund"] = inputDir + it->second;           }
128                                 }
129                         }
130                         
131                         
132                         //check for required parameters
133                         listfile = validParameter.validFile(parameters, "list", true);
134                         if (listfile == "not open") { listfile = ""; abort = true; }
135                         else if (listfile == "not found") { listfile = ""; }
136                         else {  format = "list"; inputfile = listfile; m->setListFile(listfile); }
137                         
138                         rabundfile = validParameter.validFile(parameters, "rabund", true);
139                         if (rabundfile == "not open") { rabundfile = ""; abort = true; }        
140                         else if (rabundfile == "not found") { rabundfile = ""; }
141                         else {  format = "rabund"; inputfile = rabundfile; m->setRabundFile(rabundfile); }
142                         
143                 
144                                                 //check for optional parameter and set defaults
145                         // ...at some point should added some additional type checking...
146                         label = validParameter.validFile(parameters, "label", false);                   
147                         if (label == "not found") { label = ""; }
148                         else { 
149                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
150                                 else { allLines = 1;  }
151                         }
152                         
153                         if ((listfile == "") && (rabundfile == "")) { 
154                                 //is there are current file available for any of these?
155                                 //give priority to shared, then list, then rabund, then sabund
156                                 //if there is a current shared file, use it
157                                 listfile = m->getListFile(); 
158                                 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
159                                 else { 
160                                         rabundfile = m->getRabundFile(); 
161                                         if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
162                                         else { 
163                                                 m->mothurOut("No valid current files. You must provide a list or rabund file."); m->mothurOutEndLine(); 
164                                                 abort = true;
165                                         }
166                                 }
167                         }
168                         
169                         
170                         //if the user changes the output directory command factory will send this info to us in the output parameter 
171                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(inputfile);      }                       
172                         
173                         
174                 }
175
176         }
177         catch(exception& e) {
178                 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
179                 exit(1);
180         }
181 }
182 //**********************************************************************************************************************
183
184 int GetSAbundCommand::execute(){
185         try {
186                 
187                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
188                 filename = outputDir + m->getRootName(m->getSimpleName(inputfile)) + getOutputFileNameTag("sabund");
189                 m->openOutputFile(filename, out);
190                 
191                 input = new InputData(inputfile, format);
192                 sabund = input->getSAbundVector();
193                 string lastLabel = sabund->getLabel();
194                 
195                                                 
196                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
197                 set<string> processedLabels;
198                 set<string> userLabels = labels;
199                 
200                 if (m->control_pressed) {  outputTypes.clear(); out.close(); m->mothurRemove(filename);  delete sabund; delete input; return 0; }
201
202                 
203                 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
204                         
205                         if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
206                                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
207                                         
208                                         sabund->print(out);
209                                         
210                                 if (m->control_pressed) { outputTypes.clear();  out.close(); m->mothurRemove(filename);  delete sabund; delete input;  return 0; }
211
212                                         processedLabels.insert(sabund->getLabel());
213                                         userLabels.erase(sabund->getLabel());
214                         }
215                         
216                         if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
217                                         string saveLabel = sabund->getLabel();
218                                         
219                                         delete sabund;          
220                                         sabund = (input->getSAbundVector(lastLabel));
221                                         
222                                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
223                                         sabund->print(out);
224                                         
225                                         if (m->control_pressed) {  outputTypes.clear(); out.close(); m->mothurRemove(filename);  delete sabund; delete input;  return 0; }
226
227                                         processedLabels.insert(sabund->getLabel());
228                                         userLabels.erase(sabund->getLabel());
229                                         
230                                         //restore real lastlabel to save below
231                                         sabund->setLabel(saveLabel);
232                         }
233                         
234                         
235                         lastLabel = sabund->getLabel(); 
236                         
237                         delete sabund;          
238                         sabund = (input->getSAbundVector());
239                 }
240                 
241                 //output error messages about any remaining user labels
242                 set<string>::iterator it;
243                 bool needToRun = false;
244                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
245                         m->mothurOut("Your file does not include the label " + *it); 
246                         if (processedLabels.count(lastLabel) != 1) {
247                                 m->mothurOut(". I will use " + lastLabel + ".");  m->mothurOutEndLine();
248                                 needToRun = true;
249                         }else {
250                                 m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
251                         }
252                 }
253                 
254                 //run last label if you need to
255                 if (needToRun == true)  {
256                         if (sabund != NULL) {   delete sabund;  }
257                         sabund = (input->getSAbundVector(lastLabel));
258                         
259                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
260                         sabund->print(out);
261                         delete sabund;
262                         
263                         if (m->control_pressed) {  outputTypes.clear(); out.close(); m->mothurRemove(filename);  delete input; return 0; }
264                         
265                 }
266                 
267                 out.close();
268                 delete input;
269                 
270                 m->mothurOutEndLine();
271                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
272                 m->mothurOut(filename); m->mothurOutEndLine();  outputNames.push_back(filename); outputTypes["sabund"].push_back(filename);
273                 m->mothurOutEndLine();
274                 
275                 //set sabund file as new current sabundfile
276                 string current = "";
277                 itTypes = outputTypes.find("sabund");
278                 if (itTypes != outputTypes.end()) {
279                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
280                 }
281                 
282                 return 0;               
283         }
284
285         catch(exception& e) {
286                 m->errorOut(e, "GetSAbundCommand", "execute");
287                 exit(1);
288         }
289 }
290
291 //**********************************************************************************************************************
292
293