]> git.donarmstrong.com Git - mothur.git/blob - collectcommand.cpp
merged pat's trim seqs edits with sarah's major overhaul of global data; also added...
[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                         OptionParser parser(option);
52                         map<string,string> parameters = parser.getParameters();
53                         
54                         ValidParameters validParameter;
55                 
56                         //check to make sure all parameters are valid for command
57                         for (map<string,string>::iterator 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                         if (abort == false) {
107                                 string fileNameRoot = getRootName(globaldata->inputFileName);
108                                 int i;
109                                 validCalculator = new ValidCalculators();
110                                 
111                                 for (i=0; i<Estimators.size(); i++) {
112                                         if (validCalculator->isValidCalculator("single", Estimators[i]) == true) { 
113                                                 if (Estimators[i] == "sobs") { 
114                                                         cDisplays.push_back(new CollectDisplay(new Sobs(), new OneColumnFile(fileNameRoot+"sobs")));
115                                                 }else if (Estimators[i] == "chao") { 
116                                                         cDisplays.push_back(new CollectDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"chao")));
117                                                 }else if (Estimators[i] == "nseqs") { 
118                                                         cDisplays.push_back(new CollectDisplay(new NSeqs(), new OneColumnFile(fileNameRoot+"nseqs")));
119                                                 }else if (Estimators[i] == "coverage") { 
120                                                         cDisplays.push_back(new CollectDisplay(new Coverage(), new OneColumnFile(fileNameRoot+"coverage")));
121                                                 }else if (Estimators[i] == "ace") { 
122                                                         cDisplays.push_back(new CollectDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"ace")));
123                                                 }else if (Estimators[i] == "jack") { 
124                                                         cDisplays.push_back(new CollectDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"jack")));
125                                                 }else if (Estimators[i] == "shannon") { 
126                                                         cDisplays.push_back(new CollectDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"shannon")));
127                                                 }else if (Estimators[i] == "npshannon") { 
128                                                         cDisplays.push_back(new CollectDisplay(new NPShannon(), new OneColumnFile(fileNameRoot+"np_shannon")));
129                                                 }else if (Estimators[i] == "simpson") { 
130                                                         cDisplays.push_back(new CollectDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"simpson")));
131                                                 }else if (Estimators[i] == "bootstrap") { 
132                                                         cDisplays.push_back(new CollectDisplay(new Bootstrap(), new OneColumnFile(fileNameRoot+"bootstrap")));
133                                                 }else if (Estimators[i] == "geometric") { 
134                                                         cDisplays.push_back(new CollectDisplay(new Geom(), new OneColumnFile(fileNameRoot+"geometric")));
135                                                 }else if (Estimators[i] == "qstat") { 
136                                                         cDisplays.push_back(new CollectDisplay(new QStat(), new OneColumnFile(fileNameRoot+"qstat")));
137                                                 }else if (Estimators[i] == "logseries") { 
138                                                         cDisplays.push_back(new CollectDisplay(new LogSD(), new OneColumnFile(fileNameRoot+"logseries")));
139                                                 }else if (Estimators[i] == "bergerparker") { 
140                                                         cDisplays.push_back(new CollectDisplay(new BergerParker(), new OneColumnFile(fileNameRoot+"bergerparker")));
141                                                 }else if (Estimators[i] == "bstick") { 
142                                                         cDisplays.push_back(new CollectDisplay(new BStick(), new ThreeColumnFile(fileNameRoot+"bstick")));
143                                                 }else if (Estimators[i] == "goodscoverage") { 
144                                                         cDisplays.push_back(new CollectDisplay(new GoodsCoverage(), new OneColumnFile(fileNameRoot+"goodscoverage")));
145                                                 }else if (Estimators[i] == "efron") {
146                                                         cDisplays.push_back(new CollectDisplay(new Efron(size), new OneColumnFile(fileNameRoot+"efron")));
147                                                 }else if (Estimators[i] == "boneh") {
148                                                         cDisplays.push_back(new CollectDisplay(new Boneh(size), new OneColumnFile(fileNameRoot+"boneh")));
149                                                 }else if (Estimators[i] == "solow") {
150                                                         cDisplays.push_back(new CollectDisplay(new Solow(size), new OneColumnFile(fileNameRoot+"solow")));
151                                                 }else if (Estimators[i] == "shen") {
152                                                         cDisplays.push_back(new CollectDisplay(new Shen(size, abund), new OneColumnFile(fileNameRoot+"shen")));
153                                                 }
154                                         }
155                                 }
156                         }
157                 }
158                 
159         }
160         catch(exception& e) {
161                 cout << "Standard Error: " << e.what() << " has occurred in the CollectCommand class Function CollectCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
162                 exit(1);
163         }
164         catch(...) {
165                 cout << "An unknown error has occurred in the CollectCommand class function CollectCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
166                 exit(1);
167         }       
168                         
169 }
170 //**********************************************************************************************************************
171
172 void CollectCommand::help(){
173         try {
174                 cout << "The collect.single command can only be executed after a successful read.otu command. WITH ONE EXECEPTION. " << "\n";
175                 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";
176                 cout << "The collect.single command parameters are label, line, freq, calc and abund.  No parameters are required, but you may not use " << "\n";
177                 cout << "both the line and label parameters at the same time. The collect.single command should be in the following format: " << "\n";
178                 cout << "collect.single(label=yourLabel, line=yourLines, iters=yourIters, freq=yourFreq, calc=yourEstimators)." << "\n";
179                 cout << "Example collect(label=unique-.01-.03, line=0-5-10, iters=10000, freq=10, calc=sobs-chao-ace-jack)." << "\n";
180                 cout << "The default values for freq is 100, and calc are sobs-chao-ace-jack-shannon-npshannon-simpson." << "\n";
181                 validCalculator->printCalc("single", cout);
182                 cout << "The label and line parameters are used to analyze specific lines in your input." << "\n";
183                 cout << "Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq)." << "\n" << "\n";
184         }
185         catch(exception& e) {
186                 cout << "Standard Error: " << e.what() << " has occurred in the CollectCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
187                 exit(1);
188         }
189         catch(...) {
190                 cout << "An unknown error has occurred in the CollectCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
191                 exit(1);
192         }       
193 }
194
195 //**********************************************************************************************************************
196
197 CollectCommand::~CollectCommand(){
198         delete order;
199         delete input;
200         delete cCurve;
201         delete read;
202         delete validCalculator;
203 }
204
205 //**********************************************************************************************************************
206
207 int CollectCommand::execute(){
208         try {
209                 
210                 if (abort == true) { return 0; }
211         
212                 int count = 1;
213                 
214                 //if the users entered no valid calculators don't execute command
215                 if (cDisplays.size() == 0) { return 0; }
216
217                 read = new ReadOTUFile(globaldata->inputFileName);      
218                 read->read(&*globaldata); 
219                 
220                 order = globaldata->gorder;
221                 lastOrder = order;
222                 input = globaldata->ginput;
223                 
224                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
225                 set<string> processedLabels;
226                 set<string> userLabels = labels;
227                 set<int> userLines = lines;
228                 
229                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
230                 
231                         if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
232                                 
233                                 cCurve = new Collect(order, cDisplays);
234                                 cCurve->getCurve(freq);
235                                 delete cCurve;
236                         
237                                 cout << order->getLabel() << '\t' << count << endl;
238                                 processedLabels.insert(order->getLabel());
239                                 userLabels.erase(order->getLabel());
240                                 userLines.erase(count);
241                         
242                         //you have a label the user want that is smaller than this line and the last line has not already been processed 
243                         }
244                         
245                         if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastOrder->getLabel()) != 1)) {
246                                 cCurve = new Collect(lastOrder, cDisplays);
247                                 cCurve->getCurve(freq);
248                                 delete cCurve;
249                         
250                                 cout << lastOrder->getLabel() << '\t' << count << endl;
251                                 processedLabels.insert(lastOrder->getLabel());
252                                 userLabels.erase(lastOrder->getLabel());
253                         }
254                         
255                         if (count != 1) { delete lastOrder; }
256                         lastOrder = order;                      
257                         order = (input->getOrderVector());
258                         count++;
259                 }
260                 
261                 //output error messages about any remaining user labels
262                 set<string>::iterator it;
263                 bool needToRun = false;
264                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
265                         cout << "Your file does not include the label "<< *it; 
266                         if (processedLabels.count(lastOrder->getLabel()) != 1) {
267                                 cout << ". I will use " << lastOrder->getLabel() << "." << endl;
268                                 needToRun = true;
269                         }else {
270                                 cout << ". Please refer to " << lastOrder->getLabel() << "." << endl;
271                         }
272                 }
273                 
274                 //run last line if you need to
275                 if (needToRun == true)  {
276                         cCurve = new Collect(lastOrder, cDisplays);
277                         cCurve->getCurve(freq);
278                         delete cCurve;
279                         
280                         cout << lastOrder->getLabel() << '\t' << count << endl;
281                 }
282                 
283                 delete lastOrder;
284                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
285                 return 0;
286         }
287         catch(exception& e) {
288                 cout << "Standard Error: " << e.what() << " has occurred in the CollectCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
289                 exit(1);
290         }
291         catch(...) {
292                 cout << "An unknown error has occurred in the CollectCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
293                 exit(1);
294         }       
295 }
296
297 //**********************************************************************************************************************