]> git.donarmstrong.com Git - mothur.git/blob - collectcommand.cpp
broke up globaldata and moved error checking and help into commands
[mothur.git] / collectcommand.cpp
1 /*
2  *  collectcommand.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 "collectcommand.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 "geom.h"
21 #include "qstat.h"
22 #include "logsd.h"
23 #include "bergerparker.h"
24 #include "bstick.h"
25 #include "goodscoverage.h"
26 #include "efron.h"
27 #include "boneh.h"
28 #include "solow.h"
29 #include "shen.h"
30 #include "coverage.h"
31
32
33 //**********************************************************************************************************************
34 CollectCommand::CollectCommand(string option){
35         try {
36                 globaldata = GlobalData::getInstance();
37                 abort = false;
38                 allLines = 1;
39                 lines.clear();
40                 labels.clear();
41                 Estimators.clear();
42                 
43                 //allow user to run help
44                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
45                 
46                 else {
47                         //valid paramters for this command
48                         string Array[] =  {"freq","line","label","calc","abund","size"};
49                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
50                         
51                         parser = new OptionParser();
52                         parser->parse(option, parameters);  delete parser;
53                         
54                         ValidParameters* validParameter = new ValidParameters();
55                 
56                         //check to make sure all parameters are valid for command
57                         for (it = parameters.begin(); it != parameters.end(); it++) { 
58                                 if (validParameter->isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
59                         }
60                         
61                         //make sure the user has already run the read.otu command
62                         if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { cout << "You must read a list, sabund or rabund before you can use the collect.single command." << endl; abort = true; }
63                         
64                         //check for optional parameter and set defaults
65                         // ...at some point should added some additional type checking...
66                         line = validParameter->validFile(parameters, "line", false);                            
67                         if (line == "not found") { line = "";  }
68                         else { 
69                                 if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
70                                 else { allLines = 1;  }
71                         }
72                         
73                         label = validParameter->validFile(parameters, "label", false);                  
74                         if (label == "not found") { label = ""; }
75                         else { 
76                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
77                                 else { allLines = 1;  }
78                         }
79                         
80                         //make sure user did not use both the line and label parameters
81                         if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
82                         //if the user has not specified any line or labels use the ones from read.otu
83                         else if((line == "") && (label == "")) {  
84                                 allLines = globaldata->allLines; 
85                                 labels = globaldata->labels; 
86                                 lines = globaldata->lines;
87                         }
88                                 
89                         calc = validParameter->validFile(parameters, "calc", false);                    
90                         if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson";  }
91                         else { 
92                                  if (calc == "default")  {  calc = "sobs-chao-ace-jack-shannon-npshannon-simpson";  }
93                         }
94                         splitAtDash(calc, Estimators);
95
96                         string temp;
97                         temp = validParameter->validFile(parameters, "freq", false);                    if (temp == "not found") { temp = "100"; }
98                         convert(temp, freq); 
99                         
100                         temp = validParameter->validFile(parameters, "abund", false);           if (temp == "not found") { temp = "10"; }
101                         convert(temp, abund); 
102                         
103                         temp = validParameter->validFile(parameters, "size", false);                    if (temp == "not found") { temp = "0"; }
104                         convert(temp, size); 
105         
106                         delete validParameter;
107                         
108                         
109                         if (abort == false) {
110                                 string fileNameRoot = getRootName(globaldata->inputFileName);
111                                 int i;
112                                 validCalculator = new ValidCalculators();
113                                 
114                                 for (i=0; i<Estimators.size(); i++) {
115                                         if (validCalculator->isValidCalculator("single", Estimators[i]) == true) { 
116                                                 if (Estimators[i] == "sobs") { 
117                                                         cDisplays.push_back(new CollectDisplay(new Sobs(), new OneColumnFile(fileNameRoot+"sobs")));
118                                                 }else if (Estimators[i] == "chao") { 
119                                                         cDisplays.push_back(new CollectDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"chao")));
120                                                 }else if (Estimators[i] == "nseqs") { 
121                                                         cDisplays.push_back(new CollectDisplay(new NSeqs(), new OneColumnFile(fileNameRoot+"nseqs")));
122                                                 }else if (Estimators[i] == "coverage") { 
123                                                         cDisplays.push_back(new CollectDisplay(new Coverage(), new OneColumnFile(fileNameRoot+"coverage")));
124                                                 }else if (Estimators[i] == "ace") { 
125                                                         cDisplays.push_back(new CollectDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"ace")));
126                                                 }else if (Estimators[i] == "jack") { 
127                                                         cDisplays.push_back(new CollectDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"jack")));
128                                                 }else if (Estimators[i] == "shannon") { 
129                                                         cDisplays.push_back(new CollectDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"shannon")));
130                                                 }else if (Estimators[i] == "npshannon") { 
131                                                         cDisplays.push_back(new CollectDisplay(new NPShannon(), new OneColumnFile(fileNameRoot+"np_shannon")));
132                                                 }else if (Estimators[i] == "simpson") { 
133                                                         cDisplays.push_back(new CollectDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"simpson")));
134                                                 }else if (Estimators[i] == "bootstrap") { 
135                                                         cDisplays.push_back(new CollectDisplay(new Bootstrap(), new OneColumnFile(fileNameRoot+"bootstrap")));
136                                                 }else if (Estimators[i] == "geometric") { 
137                                                         cDisplays.push_back(new CollectDisplay(new Geom(), new OneColumnFile(fileNameRoot+"geometric")));
138                                                 }else if (Estimators[i] == "qstat") { 
139                                                         cDisplays.push_back(new CollectDisplay(new QStat(), new OneColumnFile(fileNameRoot+"qstat")));
140                                                 }else if (Estimators[i] == "logseries") { 
141                                                         cDisplays.push_back(new CollectDisplay(new LogSD(), new OneColumnFile(fileNameRoot+"logseries")));
142                                                 }else if (Estimators[i] == "bergerparker") { 
143                                                         cDisplays.push_back(new CollectDisplay(new BergerParker(), new OneColumnFile(fileNameRoot+"bergerparker")));
144                                                 }else if (Estimators[i] == "bstick") { 
145                                                         cDisplays.push_back(new CollectDisplay(new BStick(), new ThreeColumnFile(fileNameRoot+"bstick")));
146                                                 }else if (Estimators[i] == "goodscoverage") { 
147                                                         cDisplays.push_back(new CollectDisplay(new GoodsCoverage(), new OneColumnFile(fileNameRoot+"goodscoverage")));
148                                                 }else if (Estimators[i] == "efron") {
149                                                         cDisplays.push_back(new CollectDisplay(new Efron(size), new OneColumnFile(fileNameRoot+"efron")));
150                                                 }else if (Estimators[i] == "boneh") {
151                                                         cDisplays.push_back(new CollectDisplay(new Boneh(size), new OneColumnFile(fileNameRoot+"boneh")));
152                                                 }else if (Estimators[i] == "solow") {
153                                                         cDisplays.push_back(new CollectDisplay(new Solow(size), new OneColumnFile(fileNameRoot+"solow")));
154                                                 }else if (Estimators[i] == "shen") {
155                                                         cDisplays.push_back(new CollectDisplay(new Shen(size, abund), new OneColumnFile(fileNameRoot+"shen")));
156                                                 }
157                                         }
158                                 }
159                         }
160                 }
161                 
162         }
163         catch(exception& e) {
164                 cout << "Standard Error: " << e.what() << " has occurred in the CollectCommand class Function CollectCommand. 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 CollectCommand class function CollectCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
169                 exit(1);
170         }       
171                         
172 }
173 //**********************************************************************************************************************
174
175 void CollectCommand::help(){
176         try {
177                 cout << "The collect.single command can only be executed after a successful read.otu command. WITH ONE EXECEPTION. " << "\n";
178                 cout << "The collect.single command can be executed after a successful cluster command.  It will use the .list file from the output of the cluster." << "\n";
179                 cout << "The collect.single command parameters are label, line, freq, calc and abund.  No parameters are required, but you may not use " << "\n";
180                 cout << "both the line and label parameters at the same time. The collect.single command should be in the following format: " << "\n";
181                 cout << "collect.single(label=yourLabel, line=yourLines, iters=yourIters, freq=yourFreq, calc=yourEstimators)." << "\n";
182                 cout << "Example collect(label=unique-.01-.03, line=0-5-10, iters=10000, freq=10, calc=sobs-chao-ace-jack)." << "\n";
183                 cout << "The default values for freq is 100, and calc are sobs-chao-ace-jack-shannon-npshannon-simpson." << "\n";
184                 validCalculator->printCalc("single", cout);
185                 cout << "The label and line parameters are used to analyze specific lines in your input." << "\n";
186                 cout << "Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq)." << "\n" << "\n";
187         }
188         catch(exception& e) {
189                 cout << "Standard Error: " << e.what() << " has occurred in the CollectCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
190                 exit(1);
191         }
192         catch(...) {
193                 cout << "An unknown error has occurred in the CollectCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
194                 exit(1);
195         }       
196 }
197
198 //**********************************************************************************************************************
199
200 CollectCommand::~CollectCommand(){
201         delete order;
202         delete input;
203         delete cCurve;
204         delete read;
205         delete validCalculator;
206 }
207
208 //**********************************************************************************************************************
209
210 int CollectCommand::execute(){
211         try {
212                 
213                 if (abort == true) { return 0; }
214         
215                 int count = 1;
216                 
217                 //if the users entered no valid calculators don't execute command
218                 if (cDisplays.size() == 0) { return 0; }
219
220                 read = new ReadOTUFile(globaldata->inputFileName);      
221                 read->read(&*globaldata); 
222                 
223                 order = globaldata->gorder;
224                 lastOrder = order;
225                 input = globaldata->ginput;
226                 
227                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
228                 set<string> processedLabels;
229                 set<string> userLabels = labels;
230                 set<int> userLines = lines;
231                 
232                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
233                 
234                         if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
235                                 
236                                 cCurve = new Collect(order, cDisplays);
237                                 cCurve->getCurve(freq);
238                                 delete cCurve;
239                         
240                                 cout << order->getLabel() << '\t' << count << endl;
241                                 processedLabels.insert(order->getLabel());
242                                 userLabels.erase(order->getLabel());
243                                 userLines.erase(count);
244                         
245                         //you have a label the user want that is smaller than this line and the last line has not already been processed 
246                         }
247                         
248                         if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastOrder->getLabel()) != 1)) {
249                                 cCurve = new Collect(lastOrder, cDisplays);
250                                 cCurve->getCurve(freq);
251                                 delete cCurve;
252                         
253                                 cout << lastOrder->getLabel() << '\t' << count << endl;
254                                 processedLabels.insert(lastOrder->getLabel());
255                                 userLabels.erase(lastOrder->getLabel());
256                         }
257                         
258                         if (count != 1) { delete lastOrder; }
259                         lastOrder = order;                      
260                         order = (input->getOrderVector());
261                         count++;
262                 }
263                 
264                 //output error messages about any remaining user labels
265                 set<string>::iterator it;
266                 bool needToRun = false;
267                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
268                         cout << "Your file does not include the label "<< *it; 
269                         if (processedLabels.count(lastOrder->getLabel()) != 1) {
270                                 cout << ". I will use " << lastOrder->getLabel() << "." << endl;
271                                 needToRun = true;
272                         }else {
273                                 cout << ". Please refer to " << lastOrder->getLabel() << "." << endl;
274                         }
275                 }
276                 
277                 //run last line if you need to
278                 if (needToRun == true)  {
279                         cCurve = new Collect(lastOrder, cDisplays);
280                         cCurve->getCurve(freq);
281                         delete cCurve;
282                         
283                         cout << lastOrder->getLabel() << '\t' << count << endl;
284                 }
285                 
286                 delete lastOrder;
287                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
288                 return 0;
289         }
290         catch(exception& e) {
291                 cout << "Standard Error: " << e.what() << " has occurred in the CollectCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
292                 exit(1);
293         }
294         catch(...) {
295                 cout << "An unknown error has occurred in the CollectCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
296                 exit(1);
297         }       
298 }
299
300 //**********************************************************************************************************************