]> git.donarmstrong.com Git - mothur.git/blob - getsabundcommand.cpp
fixed bug in phylo.diversity rooting. added filename patterns and create filename...
[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","sabund",false,false, true); parameters.push_back(plist);
16                 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none","sabund",false,false, true); 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::getOutputPattern(string type) {
50     try {
51         string pattern = "";
52         
53         if (type == "sabund")      {   pattern = "[filename],sabund";    }
54         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
55         
56         return pattern;
57     }
58     catch(exception& e) {
59         m->errorOut(e, "GetRAbundCommand", "getOutputPattern");
60         exit(1);
61     }
62 }
63 //**********************************************************************************************************************
64 GetSAbundCommand::GetSAbundCommand(){   
65         try {
66                 abort = true; calledHelp = true; 
67                 setParameters();
68                 vector<string> tempOutNames;
69                 outputTypes["sabund"] = tempOutNames;
70         }
71         catch(exception& e) {
72                 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
73                 exit(1);
74         }
75 }
76 //**********************************************************************************************************************
77 GetSAbundCommand::GetSAbundCommand(string option)  {
78         try {
79                 abort = false; calledHelp = false;   
80                 allLines = 1;
81                 
82                 //allow user to run help
83                 if(option == "help") { help(); abort = true; calledHelp = true; }
84                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
85                 
86                 else {
87                         vector<string> myArray = setParameters();
88                         
89                         OptionParser parser(option);
90                         map<string,string> parameters = parser.getParameters();
91                         map<string,string>::iterator it;
92                         
93                         ValidParameters validParameter;
94                         
95                         //check to make sure all parameters are valid for command
96                         for (it = parameters.begin(); it != parameters.end(); it++) { 
97                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
98                         }
99                         
100                         //initialize outputTypes
101                         vector<string> tempOutNames;
102                         outputTypes["sabund"] = tempOutNames;
103                         
104                         //if the user changes the input directory command factory will send this info to us in the output parameter 
105                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
106                         if (inputDir == "not found"){   inputDir = "";          }
107                         else {
108                                 string path;
109                                 it = parameters.find("list");
110                                 //user has given a template file
111                                 if(it != parameters.end()){ 
112                                         path = m->hasPath(it->second);
113                                         //if the user has not given a path then, add inputdir. else leave path alone.
114                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
115                                 }
116                                 
117                                 it = parameters.find("rabund");
118                                 //user has given a template file
119                                 if(it != parameters.end()){ 
120                                         path = m->hasPath(it->second);
121                                         //if the user has not given a path then, add inputdir. else leave path alone.
122                                         if (path == "") {       parameters["rabund"] = inputDir + it->second;           }
123                                 }
124                         }
125                         
126                         
127                         //check for required parameters
128                         listfile = validParameter.validFile(parameters, "list", true);
129                         if (listfile == "not open") { listfile = ""; abort = true; }
130                         else if (listfile == "not found") { listfile = ""; }
131                         else {  format = "list"; inputfile = listfile; m->setListFile(listfile); }
132                         
133                         rabundfile = validParameter.validFile(parameters, "rabund", true);
134                         if (rabundfile == "not open") { rabundfile = ""; abort = true; }        
135                         else if (rabundfile == "not found") { rabundfile = ""; }
136                         else {  format = "rabund"; inputfile = rabundfile; m->setRabundFile(rabundfile); }
137                         
138                 
139                                                 //check for optional parameter and set defaults
140                         // ...at some point should added some additional type checking...
141                         label = validParameter.validFile(parameters, "label", false);                   
142                         if (label == "not found") { label = ""; }
143                         else { 
144                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
145                                 else { allLines = 1;  }
146                         }
147                         
148                         if ((listfile == "") && (rabundfile == "")) { 
149                                 //is there are current file available for any of these?
150                                 //give priority to shared, then list, then rabund, then sabund
151                                 //if there is a current shared file, use it
152                                 listfile = m->getListFile(); 
153                                 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
154                                 else { 
155                                         rabundfile = m->getRabundFile(); 
156                                         if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
157                                         else { 
158                                                 m->mothurOut("No valid current files. You must provide a list or rabund file."); m->mothurOutEndLine(); 
159                                                 abort = true;
160                                         }
161                                 }
162                         }
163                         
164                         
165                         //if the user changes the output directory command factory will send this info to us in the output parameter 
166                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(inputfile);      }                       
167                         
168                         
169                 }
170
171         }
172         catch(exception& e) {
173                 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
174                 exit(1);
175         }
176 }
177 //**********************************************************************************************************************
178
179 int GetSAbundCommand::execute(){
180         try {
181                 
182                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
183         map<string, string> variables; 
184                 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputfile));
185                 filename = getOutputFileName("sabund", variables);
186                 m->openOutputFile(filename, out);
187                 
188                 input = new InputData(inputfile, format);
189                 sabund = input->getSAbundVector();
190                 string lastLabel = sabund->getLabel();
191                 
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                 if (m->control_pressed) {  outputTypes.clear(); out.close(); m->mothurRemove(filename);  delete sabund; delete input; return 0; }
198
199                 
200                 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
201                         
202                         if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
203                                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
204                                         
205                                         sabund->print(out);
206                                         
207                                 if (m->control_pressed) { outputTypes.clear();  out.close(); m->mothurRemove(filename);  delete sabund; delete input;  return 0; }
208
209                                         processedLabels.insert(sabund->getLabel());
210                                         userLabels.erase(sabund->getLabel());
211                         }
212                         
213                         if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
214                                         string saveLabel = sabund->getLabel();
215                                         
216                                         delete sabund;          
217                                         sabund = (input->getSAbundVector(lastLabel));
218                                         
219                                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
220                                         sabund->print(out);
221                                         
222                                         if (m->control_pressed) {  outputTypes.clear(); out.close(); m->mothurRemove(filename);  delete sabund; delete input;  return 0; }
223
224                                         processedLabels.insert(sabund->getLabel());
225                                         userLabels.erase(sabund->getLabel());
226                                         
227                                         //restore real lastlabel to save below
228                                         sabund->setLabel(saveLabel);
229                         }
230                         
231                         
232                         lastLabel = sabund->getLabel(); 
233                         
234                         delete sabund;          
235                         sabund = (input->getSAbundVector());
236                 }
237                 
238                 //output error messages about any remaining user labels
239                 set<string>::iterator it;
240                 bool needToRun = false;
241                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
242                         m->mothurOut("Your file does not include the label " + *it); 
243                         if (processedLabels.count(lastLabel) != 1) {
244                                 m->mothurOut(". I will use " + lastLabel + ".");  m->mothurOutEndLine();
245                                 needToRun = true;
246                         }else {
247                                 m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
248                         }
249                 }
250                 
251                 //run last label if you need to
252                 if (needToRun == true)  {
253                         if (sabund != NULL) {   delete sabund;  }
254                         sabund = (input->getSAbundVector(lastLabel));
255                         
256                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
257                         sabund->print(out);
258                         delete sabund;
259                         
260                         if (m->control_pressed) {  outputTypes.clear(); out.close(); m->mothurRemove(filename);  delete input; return 0; }
261                         
262                 }
263                 
264                 out.close();
265                 delete input;
266                 
267                 m->mothurOutEndLine();
268                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
269                 m->mothurOut(filename); m->mothurOutEndLine();  outputNames.push_back(filename); outputTypes["sabund"].push_back(filename);
270                 m->mothurOutEndLine();
271                 
272                 //set sabund file as new current sabundfile
273                 string current = "";
274                 itTypes = outputTypes.find("sabund");
275                 if (itTypes != outputTypes.end()) {
276                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
277                 }
278                 
279                 return 0;               
280         }
281
282         catch(exception& e) {
283                 m->errorOut(e, "GetSAbundCommand", "execute");
284                 exit(1);
285         }
286 }
287
288 //**********************************************************************************************************************
289
290