]> git.donarmstrong.com Git - mothur.git/blob - summarycommand.cpp
merged pat's trim seqs edits with sarah's major overhaul of global data; also added...
[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() == "")) { cout << "You must read a list, sabund or rabund before you can use the summary.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, "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                 cout << "Standard Error: " << e.what() << " has occurred in the SummaryCommand class Function SummaryCommand. 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 SummaryCommand class function SummaryCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
166                 exit(1);
167         }       
168 }
169 //**********************************************************************************************************************
170
171 void SummaryCommand::help(){
172         try {
173                 cout << "The summary.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION." << "\n";
174                 cout << "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";
175                 cout << "The summary.single command parameters are label, line, calc, abund.  No parameters are required, but you may not use " << "\n";
176                 cout << "both the line and label parameters at the same time. The summary.single command should be in the following format: " << "\n";
177                 cout << "summary.single(label=yourLabel, line=yourLines, calc=yourEstimators)." << "\n";
178                 cout << "Example summary.single(label=unique-.01-.03, line=0,5,10, calc=sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson)." << "\n";
179                 validCalculator->printCalc("summary", cout);
180                 cout << "The default value calc is sobs-chao-ace-jack-shannon-npshannon-simpson" << "\n";
181                 cout << "The label and line parameters are used to analyze specific lines in your input." << "\n";
182                 cout << "Note: No spaces between parameter labels (i.e. line), '=' and parameters (i.e.yourLines)." << "\n" << "\n";
183         }
184         catch(exception& e) {
185                 cout << "Standard Error: " << e.what() << " has occurred in the SummaryCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
186                 exit(1);
187         }
188         catch(...) {
189                 cout << "An unknown error has occurred in the SummaryCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
190                 exit(1);
191         }       
192 }
193
194 //**********************************************************************************************************************
195
196 SummaryCommand::~SummaryCommand(){
197         delete sabund;
198         delete input;
199         delete read;
200         delete validCalculator;
201 }
202
203 //**********************************************************************************************************************
204
205 int SummaryCommand::execute(){
206         try {
207         
208                 if (abort == true) { return 0; }
209                 
210                 int count = 1;
211                 
212                 //if the users entered no valid calculators don't execute command
213                 if (sumCalculators.size() == 0) { return 0; }
214
215                 outputFileName = ((getRootName(globaldata->inputFileName)) + "summary");
216                 openOutputFile(outputFileName, outputFileHandle);
217                 outputFileHandle << "label";
218         
219                 read = new ReadOTUFile(globaldata->inputFileName);      
220                 read->read(&*globaldata); 
221                 
222                 sabund = globaldata->sabund;
223                 SAbundVector* lastSAbund = sabund;
224                 input = globaldata->ginput;
225                 
226                 for(int i=0;i<sumCalculators.size();i++){
227                         if(sumCalculators[i]->getCols() == 1){
228                                 outputFileHandle << '\t' << sumCalculators[i]->getName();
229                         }
230                         else{
231                                 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
232                         }
233                 }
234                 outputFileHandle << endl;
235                 
236                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
237                 set<string> processedLabels;
238                 set<string> userLabels = labels;
239                 set<int> userLines = lines;
240                 
241                 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
242                         
243                         if(allLines == 1 || lines.count(count) == 1 || labels.count(sabund->getLabel()) == 1){                  
244         
245                                 cout << sabund->getLabel() << '\t' << count << endl;
246                                 processedLabels.insert(sabund->getLabel());
247                                 userLabels.erase(sabund->getLabel());
248                                 userLines.erase(count);
249
250                                 
251                                 outputFileHandle << sabund->getLabel();
252                                 for(int i=0;i<sumCalculators.size();i++){
253                                         vector<double> data = sumCalculators[i]->getValues(sabund);
254                                         outputFileHandle << '\t';
255                                         sumCalculators[i]->print(outputFileHandle);
256                                 }
257                                 outputFileHandle << endl;
258                         }
259                         
260                         if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastSAbund->getLabel()) != 1)) {
261
262                                 cout << lastSAbund->getLabel() << '\t' << count << endl;
263                                 processedLabels.insert(lastSAbund->getLabel());
264                                 userLabels.erase(lastSAbund->getLabel());
265                                 
266                                 outputFileHandle << lastSAbund->getLabel();
267                                 for(int i=0;i<sumCalculators.size();i++){
268                                         vector<double> data = sumCalculators[i]->getValues(lastSAbund);
269                                         outputFileHandle << '\t';
270                                         sumCalculators[i]->print(outputFileHandle);
271                                 }
272                                 outputFileHandle << endl;
273                         }               
274
275                         if (count != 1) { delete lastSAbund; }
276                         lastSAbund = sabund;                    
277
278                         sabund = input->getSAbundVector();
279                         count++;
280                 }
281                 
282                 //output error messages about any remaining user labels
283                 set<string>::iterator it;
284                 bool needToRun = false;
285                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
286                         cout << "Your file does not include the label "<< *it; 
287                         if (processedLabels.count(lastSAbund->getLabel()) != 1) {
288                                 cout << ". I will use " << lastSAbund->getLabel() << "." << endl;
289                                 needToRun = true;
290                         }else {
291                                 cout << ". Please refer to " << lastSAbund->getLabel() << "." << endl;
292                         }
293                 }
294                 
295                 //run last line if you need to
296                 if (needToRun == true)  {
297                         cout << lastSAbund->getLabel() << '\t' << count << endl;
298                         outputFileHandle << lastSAbund->getLabel();
299                         for(int i=0;i<sumCalculators.size();i++){
300                                 vector<double> data = sumCalculators[i]->getValues(lastSAbund);
301                                 outputFileHandle << '\t';
302                                 sumCalculators[i]->print(outputFileHandle);
303                         }
304                         outputFileHandle << endl;
305                 }
306                 
307                 delete lastSAbund;
308                 return 0;
309         }
310         catch(exception& e) {
311                 cout << "Standard Error: " << e.what() << " has occurred in the SummaryCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
312                 exit(1);
313         }
314         catch(...) {
315                 cout << "An unknown error has occurred in the SummaryCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
316                 exit(1);
317         }               
318 }
319
320 //**********************************************************************************************************************