]> git.donarmstrong.com Git - mothur.git/blob - collectcommand.cpp
fac13ecab2a6ca88e8de8cc2aded29eaac0630f7
[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() == "")) { mothurOut("You must read a list, sabund or rabund before you can use the collect.single command."); mothurOutEndLine(); 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 != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); 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                 errorOut(e, "CollectCommand", "CollectCommand");
162                 exit(1);
163         }                       
164 }
165 //**********************************************************************************************************************
166
167 void CollectCommand::help(){
168         try {
169                 mothurOut("The collect.single command can only be executed after a successful read.otu command. WITH ONE EXECEPTION. \n");
170                 mothurOut("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");
171                 mothurOut("The collect.single command parameters are label, line, freq, calc and abund.  No parameters are required, but you may not use \n");
172                 mothurOut("both the line and label parameters at the same time. The collect.single command should be in the following format: \n");
173                 mothurOut("collect.single(label=yourLabel, line=yourLines, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
174                 mothurOut("Example collect(label=unique-.01-.03, line=0-5-10, iters=10000, freq=10, calc=sobs-chao-ace-jack).\n");
175                 mothurOut("The default values for freq is 100, and calc are sobs-chao-ace-jack-shannon-npshannon-simpson.\n");
176                 validCalculator->printCalc("single", cout);
177                 mothurOut("The label and line parameters are used to analyze specific lines in your input.\n");
178                 mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
179         }
180         catch(exception& e) {
181                 errorOut(e, "CollectCommand", "help");
182                 exit(1);
183         }
184 }
185
186 //**********************************************************************************************************************
187
188 CollectCommand::~CollectCommand(){
189         if (abort == false) {
190                 //delete order;
191                 delete input;  globaldata->ginput = NULL;
192                 delete read;
193                 delete validCalculator;
194                 globaldata->gorder = NULL;
195         }
196 }
197
198 //**********************************************************************************************************************
199
200 int CollectCommand::execute(){
201         try {
202                 
203                 if (abort == true) { return 0; }
204         
205                 int count = 1;
206                 
207                 //if the users entered no valid calculators don't execute command
208                 if (cDisplays.size() == 0) { return 0; }
209
210                 read = new ReadOTUFile(globaldata->inputFileName);      
211                 read->read(&*globaldata); 
212                 
213                 order = globaldata->gorder;
214                 string lastLabel = order->getLabel();
215                 input = globaldata->ginput;
216                 
217                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
218                 set<string> processedLabels;
219                 set<string> userLabels = labels;
220                 set<int> userLines = lines;
221                 
222                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
223                 
224                         if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
225                                 
226                                 cCurve = new Collect(order, cDisplays);
227                                 cCurve->getCurve(freq);
228                                 delete cCurve;
229                         
230                                 mothurOut(order->getLabel()); mothurOutEndLine();
231                                 processedLabels.insert(order->getLabel());
232                                 userLabels.erase(order->getLabel());
233                                 userLines.erase(count);
234                         
235                         //you have a label the user want that is smaller than this line and the last line has not already been processed 
236                         }
237                         
238                         if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
239                                 
240                                 delete order;
241                                 order = (input->getOrderVector(lastLabel));
242                                 
243                                 cCurve = new Collect(order, cDisplays);
244                                 cCurve->getCurve(freq);
245                                 delete cCurve;
246                         
247                                 mothurOut(order->getLabel()); mothurOutEndLine();
248                                 processedLabels.insert(order->getLabel());
249                                 userLabels.erase(order->getLabel());
250                         }
251                         
252                         lastLabel = order->getLabel();  
253                         
254                         delete order;           
255                         order = (input->getOrderVector());
256                         count++;
257                 }
258                 
259                 //output error messages about any remaining user labels
260                 set<string>::iterator it;
261                 bool needToRun = false;
262                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
263                         mothurOut("Your file does not include the label " + *it); 
264                         if (processedLabels.count(lastLabel) != 1) {
265                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
266                                 needToRun = true;
267                         }else {
268                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
269                         }
270                 }
271                 
272                 //run last line if you need to
273                 if (needToRun == true)  {
274                         delete order;
275                         order = (input->getOrderVector(lastLabel));
276                         
277                         mothurOut(order->getLabel()); mothurOutEndLine();
278                         
279                         cCurve = new Collect(order, cDisplays);
280                         cCurve->getCurve(freq);
281                         delete cCurve;
282                         delete order;
283                 }
284                 
285                 
286                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
287                 return 0;
288         }
289         catch(exception& e) {
290                 errorOut(e, "CollectCommand", "execute");
291                 exit(1);
292         }
293 }
294
295 //**********************************************************************************************************************