]> git.donarmstrong.com Git - mothur.git/blob - summarycommand.cpp
fixed some issues while testing 1.6
[mothur.git] / summarycommand.cpp
1 /*
2  *  summarycommand.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 "summarycommand.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 "logsd.h"
22 #include "qstat.h"
23 #include "bergerparker.h"
24 #include "bstick.h"
25 #include "goodscoverage.h"
26 #include "coverage.h"
27 #include "efron.h"
28 #include "boneh.h"
29 #include "solow.h"
30 #include "shen.h"
31
32 //**********************************************************************************************************************
33
34 SummaryCommand::SummaryCommand(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[] =  {"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 summary.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, "abund", false);            if (temp == "not found") { temp = "10"; }
98                         convert(temp, abund); 
99                         
100                         temp = validParameter.validFile(parameters, "size", false);                     if (temp == "not found") { temp = "0"; }
101                         convert(temp, size); 
102         
103                         if (abort == false) {
104                         
105                                 validCalculator = new ValidCalculators();
106                                 int i;
107                                 
108                                 for (i=0; i<Estimators.size(); i++) {
109                                         if (validCalculator->isValidCalculator("summary", Estimators[i]) == true) { 
110                                                 if(Estimators[i] == "sobs"){
111                                                         sumCalculators.push_back(new Sobs());
112                                                 }else if(Estimators[i] == "chao"){
113                                                         sumCalculators.push_back(new Chao1());
114                                                 }else if(Estimators[i] == "coverage"){
115                                                         sumCalculators.push_back(new Coverage());
116                                                 }else if(Estimators[i] == "geometric"){
117                                                         sumCalculators.push_back(new Geom());
118                                                 }else if(Estimators[i] == "logseries"){
119                                                         sumCalculators.push_back(new LogSD());
120                                                 }else if(Estimators[i] == "qstat"){
121                                                         sumCalculators.push_back(new QStat());
122                                                 }else if(Estimators[i] == "bergerparker"){
123                                                         sumCalculators.push_back(new BergerParker());
124                                                 }else if(Estimators[i] == "bstick"){
125                                                         sumCalculators.push_back(new BStick());
126                                                 }else if(Estimators[i] == "ace"){
127                                                         if(abund < 5)
128                                                                 abund = 10;
129                                                         sumCalculators.push_back(new Ace(abund));
130                                                 }else if(Estimators[i] == "jack"){
131                                                         sumCalculators.push_back(new Jackknife());
132                                                 }else if(Estimators[i] == "shannon"){
133                                                         sumCalculators.push_back(new Shannon());
134                                                 }else if(Estimators[i] == "npshannon"){
135                                                         sumCalculators.push_back(new NPShannon());
136                                                 }else if(Estimators[i] == "simpson"){
137                                                         sumCalculators.push_back(new Simpson());
138                                                 }else if(Estimators[i] == "bootstrap"){
139                                                         sumCalculators.push_back(new Bootstrap());
140                                                 }else if (Estimators[i] == "nseqs") { 
141                                                         sumCalculators.push_back(new NSeqs());
142                                                 }else if (Estimators[i] == "goodscoverage") { 
143                                                         sumCalculators.push_back(new GoodsCoverage());
144                                                 }else if (Estimators[i] == "efron") { 
145                                                         sumCalculators.push_back(new Efron(size));
146                                                 }else if (Estimators[i] == "boneh") { 
147                                                         sumCalculators.push_back(new Boneh(size));
148                                                 }else if (Estimators[i] == "solow") { 
149                                                         sumCalculators.push_back(new Solow(size));
150                                                 }else if (Estimators[i] == "shen") { 
151                                                         sumCalculators.push_back(new Shen(size, abund));
152                                                 }
153                                         }
154                                 }
155                         }
156                 }
157
158                                 
159         }
160         catch(exception& e) {
161                 errorOut(e, "SummaryCommand", "SummaryCommand");
162                 exit(1);
163         }
164 }
165 //**********************************************************************************************************************
166
167 void SummaryCommand::help(){
168         try {
169                 mothurOut("The summary.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
170                 mothurOut("The summary.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 summary.single command parameters are label, line, calc, abund.  No parameters are required, but you may not use \n");
172                 mothurOut("both the line and label parameters at the same time. The summary.single command should be in the following format: \n");
173                 mothurOut("summary.single(label=yourLabel, line=yourLines, calc=yourEstimators).\n");
174                 mothurOut("Example summary.single(label=unique-.01-.03, line=0,5,10, calc=sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson).\n");
175                 validCalculator->printCalc("summary", cout);
176                 mothurOut("The default value calc is sobs-chao-ace-jack-shannon-npshannon-simpson\n");
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. line), '=' and parameters (i.e.yourLines).\n\n");
179         }
180         catch(exception& e) {
181                 errorOut(e, "SummaryCommand", "help");
182                 exit(1);
183         }
184 }
185
186 //**********************************************************************************************************************
187
188 SummaryCommand::~SummaryCommand(){
189         if (abort == false) {
190                 delete input;  globaldata->ginput = NULL;
191                 delete read;
192                 delete validCalculator;
193                 globaldata->sabund = NULL;
194         }
195 }
196
197 //**********************************************************************************************************************
198
199 int SummaryCommand::execute(){
200         try {
201         
202                 if (abort == true) { return 0; }
203                 
204                 int count = 1;
205                 
206                 //if the users entered no valid calculators don't execute command
207                 if (sumCalculators.size() == 0) { return 0; }
208
209                 outputFileName = ((getRootName(globaldata->inputFileName)) + "summary");
210                 openOutputFile(outputFileName, outputFileHandle);
211                 outputFileHandle << "label";
212         
213                 read = new ReadOTUFile(globaldata->inputFileName);      
214                 read->read(&*globaldata); 
215                 
216                 sabund = globaldata->sabund;
217                 string lastLabel = sabund->getLabel();
218                 input = globaldata->ginput;
219                 
220                 for(int i=0;i<sumCalculators.size();i++){
221                         if(sumCalculators[i]->getCols() == 1){
222                                 outputFileHandle << '\t' << sumCalculators[i]->getName();
223                         }
224                         else{
225                                 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
226                         }
227                 }
228                 outputFileHandle << endl;
229                 
230                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
231                 set<string> processedLabels;
232                 set<string> userLabels = labels;
233                 set<int> userLines = lines;
234                 
235                 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
236                         
237                         if(allLines == 1 || lines.count(count) == 1 || labels.count(sabund->getLabel()) == 1){                  
238         
239                                 mothurOut(sabund->getLabel()); mothurOutEndLine();
240                                 processedLabels.insert(sabund->getLabel());
241                                 userLabels.erase(sabund->getLabel());
242                                 userLines.erase(count);
243
244                                 
245                                 outputFileHandle << sabund->getLabel();
246                                 for(int i=0;i<sumCalculators.size();i++){
247                                         vector<double> data = sumCalculators[i]->getValues(sabund);
248                                         outputFileHandle << '\t';
249                                         sumCalculators[i]->print(outputFileHandle);
250                                 }
251                                 outputFileHandle << endl;
252                         }
253                         
254                         if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
255                                 delete sabund;
256                                 sabund = input->getSAbundVector(lastLabel);
257                                 
258                                 mothurOut(sabund->getLabel()); mothurOutEndLine();
259                                 processedLabels.insert(sabund->getLabel());
260                                 userLabels.erase(sabund->getLabel());
261                                 
262                                 outputFileHandle << sabund->getLabel();
263                                 for(int i=0;i<sumCalculators.size();i++){
264                                         vector<double> data = sumCalculators[i]->getValues(sabund);
265                                         outputFileHandle << '\t';
266                                         sumCalculators[i]->print(outputFileHandle);
267                                 }
268                                 outputFileHandle << endl;
269                         }               
270
271                         lastLabel = sabund->getLabel();                 
272                         
273                         delete sabund;
274                         sabund = input->getSAbundVector();
275                         count++;
276                 }
277                 
278                 //output error messages about any remaining user labels
279                 set<string>::iterator it;
280                 bool needToRun = false;
281                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
282                         mothurOut("Your file does not include the label " + *it); 
283                         if (processedLabels.count(lastLabel) != 1) {
284                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
285                                 needToRun = true;
286                         }else {
287                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
288                         }
289                 }
290                 
291                 //run last line if you need to
292                 if (needToRun == true)  {
293                         if (sabund != NULL) {   delete sabund;  }
294                         sabund = input->getSAbundVector(lastLabel);
295                         
296                         mothurOut(sabund->getLabel()); mothurOutEndLine();
297                         outputFileHandle << sabund->getLabel();
298                         for(int i=0;i<sumCalculators.size();i++){
299                                 vector<double> data = sumCalculators[i]->getValues(sabund);
300                                 outputFileHandle << '\t';
301                                 sumCalculators[i]->print(outputFileHandle);
302                         }
303                         outputFileHandle << endl;
304                         delete sabund;
305                 }
306                 
307                 outputFileHandle.close();
308                 
309                 return 0;
310         }
311         catch(exception& e) {
312                 errorOut(e, "SummaryCommand", "execute");
313                 exit(1);
314         }
315 }
316
317 //**********************************************************************************************************************