]> git.donarmstrong.com Git - mothur.git/blob - summarycommand.cpp
added checks for ^C to quit command instead of program
[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(); delete validCalculator; abort = true; }
44                 
45                 else {
46                         //valid paramters for this command
47                         string Array[] =  {"label","calc","abund","size","outputdir","inputdir"};
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->getSharedFile() == "") && (globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { m->mothurOut("You must read a list, sabund, rabund or shared file before you can use the summary.single command."); m->mothurOutEndLine(); abort = true; }
62                         
63                         //if the user changes the output directory command factory will send this info to us in the output parameter 
64                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
65                                 outputDir = ""; 
66                                 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it  
67                         }
68
69                         //check for optional parameter and set defaults
70                         // ...at some point should added some additional type checking...
71                         label = validParameter.validFile(parameters, "label", false);                   
72                         if (label == "not found") { label = ""; }
73                         else { 
74                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
75                                 else { allLines = 1;  }
76                         }
77                         
78                         //if the user has not specified any labels use the ones from read.otu
79                         if(label == "") {  
80                                 allLines = globaldata->allLines; 
81                                 labels = globaldata->labels; 
82                         }
83                                 
84                         calc = validParameter.validFile(parameters, "calc", false);                     
85                         if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson";  }
86                         else { 
87                                  if (calc == "default")  {  calc = "sobs-chao-ace-jack-shannon-npshannon-simpson";  }
88                         }
89                         splitAtDash(calc, Estimators);
90
91                         string temp;
92                         temp = validParameter.validFile(parameters, "abund", false);            if (temp == "not found") { temp = "10"; }
93                         convert(temp, abund); 
94                         
95                         temp = validParameter.validFile(parameters, "size", false);                     if (temp == "not found") { temp = "0"; }
96                         convert(temp, size); 
97         
98                 }
99         }
100         catch(exception& e) {
101                 m->errorOut(e, "SummaryCommand", "SummaryCommand");
102                 exit(1);
103         }
104 }
105 //**********************************************************************************************************************
106
107 void SummaryCommand::help(){
108         try {
109                 m->mothurOut("The summary.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
110                 m->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");
111                 m->mothurOut("The summary.single command parameters are label, calc, abund.  No parameters are required.\n");
112                 m->mothurOut("The summary.single command should be in the following format: \n");
113                 m->mothurOut("summary.single(label=yourLabel, calc=yourEstimators).\n");
114                 m->mothurOut("Example summary.single(label=unique-.01-.03, calc=sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson).\n");
115                 validCalculator->printCalc("summary", cout);
116                 m->mothurOut("The default value calc is sobs-chao-ace-jack-shannon-npshannon-simpson\n");
117                 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
118                 m->mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabels).\n\n");
119         }
120         catch(exception& e) {
121                 m->errorOut(e, "SummaryCommand", "help");
122                 exit(1);
123         }
124 }
125
126 //**********************************************************************************************************************
127
128 SummaryCommand::~SummaryCommand(){}
129
130 //**********************************************************************************************************************
131
132 int SummaryCommand::execute(){
133         try {
134         
135                 if (abort == true) { return 0; }
136                 
137                 vector<string> outputNames;
138                 
139                 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName);  }
140                 else {  inputFileNames = parseSharedFile(globaldata->getSharedFile());  globaldata->setFormat("rabund");  }
141                 
142                 if (m->control_pressed) { return 0; }
143                 
144                 for (int p = 0; p < inputFileNames.size(); p++) {
145                         
146                         string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p])) + "summary";
147                         globaldata->inputFileName = inputFileNames[p];
148                         outputNames.push_back(fileNameRoot);
149                         
150                         if (inputFileNames.size() > 1) {
151                                 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
152                         }
153                         
154                         sumCalculators.clear();
155                         
156                         validCalculator = new ValidCalculators();
157                         
158                         for (int i=0; i<Estimators.size(); i++) {
159                                 if (validCalculator->isValidCalculator("summary", Estimators[i]) == true) { 
160                                         if(Estimators[i] == "sobs"){
161                                                 sumCalculators.push_back(new Sobs());
162                                         }else if(Estimators[i] == "chao"){
163                                                 sumCalculators.push_back(new Chao1());
164                                         }else if(Estimators[i] == "coverage"){
165                                                 sumCalculators.push_back(new Coverage());
166                                         }else if(Estimators[i] == "geometric"){
167                                                 sumCalculators.push_back(new Geom());
168                                         }else if(Estimators[i] == "logseries"){
169                                                 sumCalculators.push_back(new LogSD());
170                                         }else if(Estimators[i] == "qstat"){
171                                                 sumCalculators.push_back(new QStat());
172                                         }else if(Estimators[i] == "bergerparker"){
173                                                 sumCalculators.push_back(new BergerParker());
174                                         }else if(Estimators[i] == "bstick"){
175                                                 sumCalculators.push_back(new BStick());
176                                         }else if(Estimators[i] == "ace"){
177                                                 if(abund < 5)
178                                                         abund = 10;
179                                                 sumCalculators.push_back(new Ace(abund));
180                                         }else if(Estimators[i] == "jack"){
181                                                 sumCalculators.push_back(new Jackknife());
182                                         }else if(Estimators[i] == "shannon"){
183                                                 sumCalculators.push_back(new Shannon());
184                                         }else if(Estimators[i] == "npshannon"){
185                                                 sumCalculators.push_back(new NPShannon());
186                                         }else if(Estimators[i] == "simpson"){
187                                                 sumCalculators.push_back(new Simpson());
188                                         }else if(Estimators[i] == "bootstrap"){
189                                                 sumCalculators.push_back(new Bootstrap());
190                                         }else if (Estimators[i] == "nseqs") { 
191                                                 sumCalculators.push_back(new NSeqs());
192                                         }else if (Estimators[i] == "goodscoverage") { 
193                                                 sumCalculators.push_back(new GoodsCoverage());
194                                         }else if (Estimators[i] == "efron") { 
195                                                 sumCalculators.push_back(new Efron(size));
196                                         }else if (Estimators[i] == "boneh") { 
197                                                 sumCalculators.push_back(new Boneh(size));
198                                         }else if (Estimators[i] == "solow") { 
199                                                 sumCalculators.push_back(new Solow(size));
200                                         }else if (Estimators[i] == "shen") { 
201                                                 sumCalculators.push_back(new Shen(size, abund));
202                                         }
203                                 }
204                         }
205                         
206                         //if the users entered no valid calculators don't execute command
207                         if (sumCalculators.size() == 0) { return 0; }
208                         
209                         ofstream outputFileHandle;
210                         openOutputFile(fileNameRoot, 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                         
234                         if (m->control_pressed) { outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) {      remove(outputNames[i].c_str());  } for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; } delete validCalculator; delete read; delete sabund; globaldata->sabund = NULL; delete input; globaldata->ginput = NULL; return 0;  }
235                         
236                         while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
237                                 
238                                 if (m->control_pressed) { outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) {      remove(outputNames[i].c_str());  } for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; } delete validCalculator; delete read; delete sabund; globaldata->sabund = NULL; delete input; globaldata->ginput = NULL; return 0;  }
239                                 
240                                 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){                     
241                                         
242                                         m->mothurOut(sabund->getLabel()); m->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                                                 
250                                                 if (m->control_pressed) { outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) {      remove(outputNames[i].c_str());  } for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; } delete validCalculator; delete read; delete sabund; globaldata->sabund = NULL; delete input; globaldata->ginput = NULL; return 0;  }
251
252                                                 outputFileHandle << '\t';
253                                                 sumCalculators[i]->print(outputFileHandle);
254                                         }
255                                         outputFileHandle << endl;
256                                 }
257                                 
258                                 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
259                                         string saveLabel = sabund->getLabel();
260                                         
261                                         delete sabund;
262                                         sabund = input->getSAbundVector(lastLabel);
263                                         
264                                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
265                                         processedLabels.insert(sabund->getLabel());
266                                         userLabels.erase(sabund->getLabel());
267                                         
268                                         outputFileHandle << sabund->getLabel();
269                                         for(int i=0;i<sumCalculators.size();i++){
270                                                 vector<double> data = sumCalculators[i]->getValues(sabund);
271                                                 
272                                                 if (m->control_pressed) { outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) {      remove(outputNames[i].c_str());  } for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; } delete validCalculator; delete read; delete sabund; globaldata->sabund = NULL; delete input; globaldata->ginput = NULL; return 0;  }
273                                                 
274                                                 outputFileHandle << '\t';
275                                                 sumCalculators[i]->print(outputFileHandle);
276                                         }
277                                         outputFileHandle << endl;
278                                         
279                                         //restore real lastlabel to save below
280                                         sabund->setLabel(saveLabel);
281                                 }               
282                                 
283                                 lastLabel = sabund->getLabel();                 
284                                 
285                                 delete sabund;
286                                 sabund = input->getSAbundVector();
287                         }
288                         
289                         if (m->control_pressed) { outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) {      remove(outputNames[i].c_str());  } for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; } delete validCalculator; delete read;  delete input; globaldata->ginput = NULL; return 0;  }
290
291                         //output error messages about any remaining user labels
292                         set<string>::iterator it;
293                         bool needToRun = false;
294                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
295                                 m->mothurOut("Your file does not include the label " + *it); 
296                                 if (processedLabels.count(lastLabel) != 1) {
297                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
298                                         needToRun = true;
299                                 }else {
300                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
301                                 }
302                         }
303                         
304                         //run last label if you need to
305                         if (needToRun == true)  {
306                                 if (sabund != NULL) {   delete sabund;  }
307                                 sabund = input->getSAbundVector(lastLabel);
308                                 
309                                 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
310                                 outputFileHandle << sabund->getLabel();
311                                 for(int i=0;i<sumCalculators.size();i++){
312                                         vector<double> data = sumCalculators[i]->getValues(sabund);
313                                         
314                                         if (m->control_pressed) { outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) {      remove(outputNames[i].c_str());  } for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; } delete validCalculator; delete read; delete sabund; globaldata->sabund = NULL; delete input; globaldata->ginput = NULL; return 0;  }
315
316                                         outputFileHandle << '\t';
317                                         sumCalculators[i]->print(outputFileHandle);
318                                 }
319                                 outputFileHandle << endl;
320                                 delete sabund;
321                         }
322                         
323                         outputFileHandle.close();
324                         
325                         if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; } delete validCalculator; delete read;  delete input; globaldata->ginput = NULL; return 0;  }
326
327                         
328                         delete input;  globaldata->ginput = NULL;
329                         delete read;
330                         delete validCalculator;
331                         globaldata->sabund = NULL;
332                         for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; }
333                 }
334                 
335                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  }  return 0;  }
336                 
337                 m->mothurOutEndLine();
338                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
339                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
340                 m->mothurOutEndLine();
341                 
342                 return 0;
343         }
344         catch(exception& e) {
345                 m->errorOut(e, "SummaryCommand", "execute");
346                 exit(1);
347         }
348 }
349 //**********************************************************************************************************************
350 vector<string> SummaryCommand::parseSharedFile(string filename) {
351         try {
352                 vector<string> filenames;
353                 
354                 map<string, ofstream*> filehandles;
355                 map<string, ofstream*>::iterator it3;
356                 
357                                 
358                 //read first line
359                 read = new ReadOTUFile(filename);       
360                 read->read(&*globaldata); 
361                         
362                 input = globaldata->ginput;
363                 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
364                 
365                 string sharedFileRoot = getRootName(filename);
366                 
367                 //clears file before we start to write to it below
368                 for (int i=0; i<lookup.size(); i++) {
369                         remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
370                         filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
371                 }
372                 
373                 ofstream* temp;
374                 for (int i=0; i<lookup.size(); i++) {
375                         temp = new ofstream;
376                         filehandles[lookup[i]->getGroup()] = temp;
377                         groups.push_back(lookup[i]->getGroup());
378                 }
379
380                 while(lookup[0] != NULL) {
381                 
382                         for (int i = 0; i < lookup.size(); i++) {
383                                 RAbundVector rav = lookup[i]->getRAbundVector();
384                                 openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
385                                 rav.print(*(filehandles[lookup[i]->getGroup()]));
386                                 (*(filehandles[lookup[i]->getGroup()])).close();
387                         }
388                 
389                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
390                         lookup = input->getSharedRAbundVectors();
391                 }
392                 
393                 //free memory
394                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
395                         delete it3->second;
396                 }
397                 delete read;
398                 delete input;
399                 globaldata->ginput = NULL;
400
401                 return filenames;
402         }
403         catch(exception& e) {
404                 m->errorOut(e, "SummaryCommand", "parseSharedFile");
405                 exit(1);
406         }
407 }
408 //**********************************************************************************************************************