]> git.donarmstrong.com Git - mothur.git/blob - rarefactcommand.cpp
broke up globaldata and moved error checking and help into commands
[mothur.git] / rarefactcommand.cpp
1 /*
2  *  rarefactcommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "rarefactcommand.h"
11 #include "ace.h"
12 #include "sobs.h"
13 #include "nseqs.h"
14 #include "chao1.h"
15 #include "bootstrap.h"
16 #include "simpson.h"
17 #include "npshannon.h"
18 #include "shannon.h"
19 #include "jackknife.h"
20 #include "coverage.h"
21
22 //**********************************************************************************************************************
23
24
25 RareFactCommand::RareFactCommand(string option){
26         try {
27                 globaldata = GlobalData::getInstance();
28                 abort = false;
29                 allLines = 1;
30                 lines.clear();
31                 labels.clear();
32                 Estimators.clear();
33                 
34                 //allow user to run help
35                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
36                 
37                 else {
38                         //valid paramters for this command
39                         string Array[] =  {"iters","freq","line","label","calc","abund"};
40                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
41                         
42                         parser = new OptionParser();
43                         parser->parse(option, parameters);  delete parser;
44                         
45                         ValidParameters* validParameter = new ValidParameters();
46                 
47                         //check to make sure all parameters are valid for command
48                         for (it = parameters.begin(); it != parameters.end(); it++) { 
49                                 if (validParameter->isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
50                         }
51                         
52                         //make sure the user has already run the read.otu command
53                         if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { cout << "You must read a list, sabund or rabund before you can use the rarefaction.single command." << endl; abort = true; }
54                         
55                         //check for optional parameter and set defaults
56                         // ...at some point should added some additional type checking...
57                         line = validParameter->validFile(parameters, "line", false);                            
58                         if (line == "not found") { line = "";  }
59                         else { 
60                                 if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
61                                 else { allLines = 1;  }
62                         }
63                         
64                         label = validParameter->validFile(parameters, "label", false);                  
65                         if (label == "not found") { label = ""; }
66                         else { 
67                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
68                                 else { allLines = 1;  }
69                         }
70                         
71                         //make sure user did not use both the line and label parameters
72                         if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
73                         //if the user has not specified any line or labels use the ones from read.otu
74                         else if((line == "") && (label == "")) {  
75                                 allLines = globaldata->allLines; 
76                                 labels = globaldata->labels; 
77                                 lines = globaldata->lines;
78                         }
79                                 
80                         calc = validParameter->validFile(parameters, "calc", false);                    
81                         if (calc == "not found") { calc = "sobs";  }
82                         else { 
83                                  if (calc == "default")  {  calc = "sobs";  }
84                         }
85                         splitAtDash(calc, Estimators);
86
87                         string temp;
88                         temp = validParameter->validFile(parameters, "freq", false);                    if (temp == "not found") { temp = "100"; }
89                         convert(temp, freq); 
90                         
91                         temp = validParameter->validFile(parameters, "abund", false);                   if (temp == "not found") { temp = "10"; }
92                         convert(temp, abund); 
93                         
94                         temp = validParameter->validFile(parameters, "iters", false);                   if (temp == "not found") { temp = "1000"; }
95                         convert(temp, nIters); 
96         
97                         delete validParameter;
98                         
99                         if (abort == false) {
100                         
101                                 string fileNameRoot = getRootName(globaldata->inputFileName);
102                                 int i;
103                                 validCalculator = new ValidCalculators();
104                                 
105                                 
106                                 for (i=0; i<Estimators.size(); i++) {
107                                         if (validCalculator->isValidCalculator("rarefaction", Estimators[i]) == true) { 
108                                                 if (Estimators[i] == "sobs") { 
109                                                         rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(fileNameRoot+"rarefaction")));
110                                                 }else if (Estimators[i] == "chao") { 
111                                                         rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"r_chao")));
112                                                 }else if (Estimators[i] == "ace") { 
113                                                         if(abund < 5)
114                                                                 abund = 10;
115                                                         rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"r_ace")));
116                                                 }else if (Estimators[i] == "jack") { 
117                                                         rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"r_jack")));
118                                                 }else if (Estimators[i] == "shannon") { 
119                                                         rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"r_shannon")));
120                                                 }else if (Estimators[i] == "npshannon") { 
121                                                         rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(fileNameRoot+"r_npshannon")));
122                                                 }else if (Estimators[i] == "simpson") { 
123                                                         rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"r_simpson")));
124                                                 }else if (Estimators[i] == "bootstrap") { 
125                                                         rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(fileNameRoot+"r_bootstrap")));
126                                                 }else if (Estimators[i] == "coverage") { 
127                                                         rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(fileNameRoot+"r_coverage")));
128                                                 }else if (Estimators[i] == "nseqs") { 
129                                                         rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(fileNameRoot+"r_nseqs")));
130                                                 }
131                                         }
132                                 }
133                         }
134                                 
135                 }
136                 
137         }
138         catch(exception& e) {
139                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactCommand class Function RareFactCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
140                 exit(1);
141         }
142         catch(...) {
143                 cout << "An unknown error has occurred in the RareFactCommand class function RareFactCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
144                 exit(1);
145         }       
146                         
147 }
148 //**********************************************************************************************************************
149
150 void RareFactCommand::help(){
151         try {
152                 cout << "The rarefaction.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION." << "\n";
153                 cout << "The rarefaction.single command can be executed after a successful cluster command.  It will use the .list file from the output of the cluster." << "\n";
154                 cout << "The rarefaction.single command parameters are label, line, iters, freq, calc and abund.  No parameters are required, but you may not use " << "\n";
155                 cout << "both the line and label parameters at the same time. The rarefaction.single command should be in the following format: " << "\n";
156                 cout << "rarefaction.single(label=yourLabel, line=yourLines, iters=yourIters, freq=yourFreq, calc=yourEstimators)." << "\n";
157                 cout << "Example rarefaction.single(label=unique-.01-.03, line=0-5-10, iters=10000, freq=10, calc=sobs-rchao-race-rjack-rbootstrap-rshannon-rnpshannon-rsimpson)." << "\n";
158                 cout << "The default values for iters is 1000, freq is 100, and calc is rarefaction which calculates the rarefaction curve for the observed richness." << "\n";
159                 validCalculator->printCalc("rarefaction", cout);
160                 cout << "The label and line parameters are used to analyze specific lines in your input." << "\n";
161                 cout << "Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq)." << "\n" << "\n";
162         }
163         catch(exception& e) {
164                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
165                 exit(1);
166         }
167         catch(...) {
168                 cout << "An unknown error has occurred in the RareFactCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
169                 exit(1);
170         }       
171 }
172
173 //**********************************************************************************************************************
174
175 RareFactCommand::~RareFactCommand(){
176         delete order;
177         delete input;
178         delete rCurve;
179         delete read;
180         delete validCalculator;
181 }
182
183 //**********************************************************************************************************************
184
185 int RareFactCommand::execute(){
186         try {
187         
188                 if (abort == true) { return 0; }
189                 
190                 int count = 1;
191                 
192                 //if the users entered no valid calculators don't execute command
193                 if (rDisplays.size() == 0) { return 0; }
194
195                 read = new ReadOTUFile(globaldata->inputFileName);      
196                 read->read(&*globaldata); 
197
198                 order = globaldata->gorder;
199                 OrderVector* lastOrder = order;
200                 input = globaldata->ginput;
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                 set<int> userLines = lines;
206         
207                 //as long as you are not at the end of the file or done wih the lines you want
208                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
209                         
210                         if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
211                         
212                                 rCurve = new Rarefact(order, rDisplays);
213                                 rCurve->getCurve(freq, nIters);
214                                 delete rCurve;
215                         
216                                 cout << order->getLabel() << '\t' << count << endl;
217                                 processedLabels.insert(order->getLabel());
218                                 userLabels.erase(order->getLabel());
219                                 userLines.erase(count);
220                         }
221                         
222                         if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastOrder->getLabel()) != 1)) {
223                                 rCurve = new Rarefact(lastOrder, rDisplays);
224                                 rCurve->getCurve(freq, nIters);
225                                 delete rCurve;
226                         
227                                 cout << lastOrder->getLabel() << '\t' << count << endl;
228                                 processedLabels.insert(lastOrder->getLabel());
229                                 userLabels.erase(lastOrder->getLabel());
230                         }
231                         
232                         if (count != 1) { delete lastOrder; }
233                         lastOrder = order;                      
234
235                         order = (input->getOrderVector());
236                         count++;
237                 }
238                 
239                 //output error messages about any remaining user labels
240                 set<string>::iterator it;
241                 bool needToRun = false;
242                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
243                         cout << "Your file does not include the label "<< *it; 
244                         if (processedLabels.count(lastOrder->getLabel()) != 1) {
245                                 cout << ". I will use " << lastOrder->getLabel() << "." << endl;
246                                 needToRun = true;
247                         }else {
248                                 cout << ". Please refer to " << lastOrder->getLabel() << "." << endl;
249                         }
250                 }
251                 
252                 //run last line if you need to
253                 if (needToRun == true)  {
254                         rCurve = new Rarefact(lastOrder, rDisplays);
255                         rCurve->getCurve(freq, nIters);
256                         delete rCurve;
257                         
258                         cout << lastOrder->getLabel() << '\t' << count << endl;
259                 }
260                 
261                 delete lastOrder;
262
263                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
264                 return 0;
265         }
266         catch(exception& e) {
267                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
268                 exit(1);
269         }
270         catch(...) {
271                 cout << "An unknown error has occurred in the RareFactCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
272                 exit(1);
273         }       
274 }
275
276 //**********************************************************************************************************************