]> git.donarmstrong.com Git - mothur.git/blob - summarycommand.cpp
removed line option
[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                 labels.clear();
40                 Estimators.clear();
41                 
42                 //allow user to run help
43                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
44                 
45                 else {
46                         //valid paramters for this command
47                         string Array[] =  {"label","calc","abund","size"};
48                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
49                         
50                         OptionParser parser(option);
51                         map<string,string> parameters = parser.getParameters();
52                         
53                         ValidParameters validParameter;
54                         
55                         //check to make sure all parameters are valid for command
56                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
57                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
58                         }
59                         
60                         //make sure the user has already run the read.otu command
61                         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; }
62                         
63                         //check for optional parameter and set defaults
64                         // ...at some point should added some additional type checking...
65                         label = validParameter.validFile(parameters, "label", false);                   
66                         if (label == "not found") { label = ""; }
67                         else { 
68                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
69                                 else { allLines = 1;  }
70                         }
71                         
72                         //if the user has not specified any labels use the ones from read.otu
73                         if(label == "") {  
74                                 allLines = globaldata->allLines; 
75                                 labels = globaldata->labels; 
76                         }
77                                 
78                         calc = validParameter.validFile(parameters, "calc", false);                     
79                         if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson";  }
80                         else { 
81                                  if (calc == "default")  {  calc = "sobs-chao-ace-jack-shannon-npshannon-simpson";  }
82                         }
83                         splitAtDash(calc, Estimators);
84
85                         string temp;
86                         temp = validParameter.validFile(parameters, "abund", false);            if (temp == "not found") { temp = "10"; }
87                         convert(temp, abund); 
88                         
89                         temp = validParameter.validFile(parameters, "size", false);                     if (temp == "not found") { temp = "0"; }
90                         convert(temp, size); 
91         
92                         if (abort == false) {
93                         
94                                 validCalculator = new ValidCalculators();
95                                 int i;
96                                 
97                                 for (i=0; i<Estimators.size(); i++) {
98                                         if (validCalculator->isValidCalculator("summary", Estimators[i]) == true) { 
99                                                 if(Estimators[i] == "sobs"){
100                                                         sumCalculators.push_back(new Sobs());
101                                                 }else if(Estimators[i] == "chao"){
102                                                         sumCalculators.push_back(new Chao1());
103                                                 }else if(Estimators[i] == "coverage"){
104                                                         sumCalculators.push_back(new Coverage());
105                                                 }else if(Estimators[i] == "geometric"){
106                                                         sumCalculators.push_back(new Geom());
107                                                 }else if(Estimators[i] == "logseries"){
108                                                         sumCalculators.push_back(new LogSD());
109                                                 }else if(Estimators[i] == "qstat"){
110                                                         sumCalculators.push_back(new QStat());
111                                                 }else if(Estimators[i] == "bergerparker"){
112                                                         sumCalculators.push_back(new BergerParker());
113                                                 }else if(Estimators[i] == "bstick"){
114                                                         sumCalculators.push_back(new BStick());
115                                                 }else if(Estimators[i] == "ace"){
116                                                         if(abund < 5)
117                                                                 abund = 10;
118                                                         sumCalculators.push_back(new Ace(abund));
119                                                 }else if(Estimators[i] == "jack"){
120                                                         sumCalculators.push_back(new Jackknife());
121                                                 }else if(Estimators[i] == "shannon"){
122                                                         sumCalculators.push_back(new Shannon());
123                                                 }else if(Estimators[i] == "npshannon"){
124                                                         sumCalculators.push_back(new NPShannon());
125                                                 }else if(Estimators[i] == "simpson"){
126                                                         sumCalculators.push_back(new Simpson());
127                                                 }else if(Estimators[i] == "bootstrap"){
128                                                         sumCalculators.push_back(new Bootstrap());
129                                                 }else if (Estimators[i] == "nseqs") { 
130                                                         sumCalculators.push_back(new NSeqs());
131                                                 }else if (Estimators[i] == "goodscoverage") { 
132                                                         sumCalculators.push_back(new GoodsCoverage());
133                                                 }else if (Estimators[i] == "efron") { 
134                                                         sumCalculators.push_back(new Efron(size));
135                                                 }else if (Estimators[i] == "boneh") { 
136                                                         sumCalculators.push_back(new Boneh(size));
137                                                 }else if (Estimators[i] == "solow") { 
138                                                         sumCalculators.push_back(new Solow(size));
139                                                 }else if (Estimators[i] == "shen") { 
140                                                         sumCalculators.push_back(new Shen(size, abund));
141                                                 }
142                                         }
143                                 }
144                         }
145                 }
146
147                                 
148         }
149         catch(exception& e) {
150                 errorOut(e, "SummaryCommand", "SummaryCommand");
151                 exit(1);
152         }
153 }
154 //**********************************************************************************************************************
155
156 void SummaryCommand::help(){
157         try {
158                 mothurOut("The summary.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
159                 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");
160                 mothurOut("The summary.single command parameters are label, calc, abund.  No parameters are required.\n");
161                 mothurOut("The summary.single command should be in the following format: \n");
162                 mothurOut("summary.single(label=yourLabel, calc=yourEstimators).\n");
163                 mothurOut("Example summary.single(label=unique-.01-.03, calc=sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson).\n");
164                 validCalculator->printCalc("summary", cout);
165                 mothurOut("The default value calc is sobs-chao-ace-jack-shannon-npshannon-simpson\n");
166                 mothurOut("The label parameter is used to analyze specific labels in your input.\n");
167                 mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabels).\n\n");
168         }
169         catch(exception& e) {
170                 errorOut(e, "SummaryCommand", "help");
171                 exit(1);
172         }
173 }
174
175 //**********************************************************************************************************************
176
177 SummaryCommand::~SummaryCommand(){
178         if (abort == false) {
179                 delete input;  globaldata->ginput = NULL;
180                 delete read;
181                 delete validCalculator;
182                 globaldata->sabund = NULL;
183         }
184 }
185
186 //**********************************************************************************************************************
187
188 int SummaryCommand::execute(){
189         try {
190         
191                 if (abort == true) { return 0; }
192                 
193                 //if the users entered no valid calculators don't execute command
194                 if (sumCalculators.size() == 0) { return 0; }
195
196                 outputFileName = ((getRootName(globaldata->inputFileName)) + "summary");
197                 openOutputFile(outputFileName, outputFileHandle);
198                 outputFileHandle << "label";
199         
200                 read = new ReadOTUFile(globaldata->inputFileName);      
201                 read->read(&*globaldata); 
202                 
203                 sabund = globaldata->sabund;
204                 string lastLabel = sabund->getLabel();
205                 input = globaldata->ginput;
206                 
207                 for(int i=0;i<sumCalculators.size();i++){
208                         if(sumCalculators[i]->getCols() == 1){
209                                 outputFileHandle << '\t' << sumCalculators[i]->getName();
210                         }
211                         else{
212                                 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
213                         }
214                 }
215                 outputFileHandle << endl;
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                         
221                 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
222                         
223                         if(allLines == 1 || labels.count(sabund->getLabel()) == 1){                     
224         
225                                 mothurOut(sabund->getLabel()); mothurOutEndLine();
226                                 processedLabels.insert(sabund->getLabel());
227                                 userLabels.erase(sabund->getLabel());
228                                                                 
229                                 outputFileHandle << sabund->getLabel();
230                                 for(int i=0;i<sumCalculators.size();i++){
231                                         vector<double> data = sumCalculators[i]->getValues(sabund);
232                                         outputFileHandle << '\t';
233                                         sumCalculators[i]->print(outputFileHandle);
234                                 }
235                                 outputFileHandle << endl;
236                         }
237                         
238                         if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
239                                 delete sabund;
240                                 sabund = input->getSAbundVector(lastLabel);
241                                 
242                                 mothurOut(sabund->getLabel()); mothurOutEndLine();
243                                 processedLabels.insert(sabund->getLabel());
244                                 userLabels.erase(sabund->getLabel());
245                                 
246                                 outputFileHandle << sabund->getLabel();
247                                 for(int i=0;i<sumCalculators.size();i++){
248                                         vector<double> data = sumCalculators[i]->getValues(sabund);
249                                         outputFileHandle << '\t';
250                                         sumCalculators[i]->print(outputFileHandle);
251                                 }
252                                 outputFileHandle << endl;
253                         }               
254
255                         lastLabel = sabund->getLabel();                 
256                         
257                         delete sabund;
258                         sabund = input->getSAbundVector();
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                         mothurOut("Your file does not include the label " + *it); 
266                         if (processedLabels.count(lastLabel) != 1) {
267                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
268                                 needToRun = true;
269                         }else {
270                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
271                         }
272                 }
273                 
274                 //run last label if you need to
275                 if (needToRun == true)  {
276                         if (sabund != NULL) {   delete sabund;  }
277                         sabund = input->getSAbundVector(lastLabel);
278                         
279                         mothurOut(sabund->getLabel()); mothurOutEndLine();
280                         outputFileHandle << sabund->getLabel();
281                         for(int i=0;i<sumCalculators.size();i++){
282                                 vector<double> data = sumCalculators[i]->getValues(sabund);
283                                 outputFileHandle << '\t';
284                                 sumCalculators[i]->print(outputFileHandle);
285                         }
286                         outputFileHandle << endl;
287                         delete sabund;
288                 }
289                 
290                 outputFileHandle.close();
291                 
292                 return 0;
293         }
294         catch(exception& e) {
295                 errorOut(e, "SummaryCommand", "execute");
296                 exit(1);
297         }
298 }
299
300 //**********************************************************************************************************************