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