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