]> git.donarmstrong.com Git - mothur.git/blob - summarycommand.cpp
some bug fixes
[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                 string hadShared = "";
140                 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName);  }
141                 else { hadShared = globaldata->getSharedFile(); inputFileNames = parseSharedFile(globaldata->getSharedFile());  globaldata->setFormat("rabund");  }
142                 
143                 if (m->control_pressed) { if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  } return 0; }
144                 
145                 for (int p = 0; p < inputFileNames.size(); p++) {
146                         
147                         string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p])) + "summary";
148                         globaldata->inputFileName = inputFileNames[p];
149                         outputNames.push_back(fileNameRoot);
150                         
151                         if (inputFileNames.size() > 1) {
152                                 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
153                         }
154                         
155                         sumCalculators.clear();
156                         
157                         validCalculator = new ValidCalculators();
158                         
159                         for (int i=0; i<Estimators.size(); i++) {
160                                 if (validCalculator->isValidCalculator("summary", Estimators[i]) == true) { 
161                                         if(Estimators[i] == "sobs"){
162                                                 sumCalculators.push_back(new Sobs());
163                                         }else if(Estimators[i] == "chao"){
164                                                 sumCalculators.push_back(new Chao1());
165                                         }else if(Estimators[i] == "coverage"){
166                                                 sumCalculators.push_back(new Coverage());
167                                         }else if(Estimators[i] == "geometric"){
168                                                 sumCalculators.push_back(new Geom());
169                                         }else if(Estimators[i] == "logseries"){
170                                                 sumCalculators.push_back(new LogSD());
171                                         }else if(Estimators[i] == "qstat"){
172                                                 sumCalculators.push_back(new QStat());
173                                         }else if(Estimators[i] == "bergerparker"){
174                                                 sumCalculators.push_back(new BergerParker());
175                                         }else if(Estimators[i] == "bstick"){
176                                                 sumCalculators.push_back(new BStick());
177                                         }else if(Estimators[i] == "ace"){
178                                                 if(abund < 5)
179                                                         abund = 10;
180                                                 sumCalculators.push_back(new Ace(abund));
181                                         }else if(Estimators[i] == "jack"){
182                                                 sumCalculators.push_back(new Jackknife());
183                                         }else if(Estimators[i] == "shannon"){
184                                                 sumCalculators.push_back(new Shannon());
185                                         }else if(Estimators[i] == "npshannon"){
186                                                 sumCalculators.push_back(new NPShannon());
187                                         }else if(Estimators[i] == "simpson"){
188                                                 sumCalculators.push_back(new Simpson());
189                                         }else if(Estimators[i] == "bootstrap"){
190                                                 sumCalculators.push_back(new Bootstrap());
191                                         }else if (Estimators[i] == "nseqs") { 
192                                                 sumCalculators.push_back(new NSeqs());
193                                         }else if (Estimators[i] == "goodscoverage") { 
194                                                 sumCalculators.push_back(new GoodsCoverage());
195                                         }else if (Estimators[i] == "efron") { 
196                                                 sumCalculators.push_back(new Efron(size));
197                                         }else if (Estimators[i] == "boneh") { 
198                                                 sumCalculators.push_back(new Boneh(size));
199                                         }else if (Estimators[i] == "solow") { 
200                                                 sumCalculators.push_back(new Solow(size));
201                                         }else if (Estimators[i] == "shen") { 
202                                                 sumCalculators.push_back(new Shen(size, abund));
203                                         }
204                                 }
205                         }
206                         
207                         //if the users entered no valid calculators don't execute command
208                         if (sumCalculators.size() == 0) { if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  } return 0; }
209                         
210                         ofstream outputFileHandle;
211                         openOutputFile(fileNameRoot, outputFileHandle);
212                         outputFileHandle << "label";
213                         
214                         read = new ReadOTUFile(globaldata->inputFileName);      
215                         read->read(&*globaldata); 
216                         
217                         sabund = globaldata->sabund;
218                         string lastLabel = sabund->getLabel();
219                         input = globaldata->ginput;
220                         
221                         for(int i=0;i<sumCalculators.size();i++){
222                                 if(sumCalculators[i]->getCols() == 1){
223                                         outputFileHandle << '\t' << sumCalculators[i]->getName();
224                                 }
225                                 else{
226                                         outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
227                                 }
228                         }
229                         outputFileHandle << endl;
230                         
231                         //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
232                         set<string> processedLabels;
233                         set<string> userLabels = labels;
234                         
235                         if (m->control_pressed) { if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  }  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;  }
236                         
237                         while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
238                                 
239                                 if (m->control_pressed) { if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  } 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;  }
240                                 
241                                 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){                     
242                                         
243                                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
244                                         processedLabels.insert(sabund->getLabel());
245                                         userLabels.erase(sabund->getLabel());
246                                         
247                                         outputFileHandle << sabund->getLabel();
248                                         for(int i=0;i<sumCalculators.size();i++){
249                                                 vector<double> data = sumCalculators[i]->getValues(sabund);
250                                                 
251                                                 if (m->control_pressed) { if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  } 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;  }
252
253                                                 outputFileHandle << '\t';
254                                                 sumCalculators[i]->print(outputFileHandle);
255                                         }
256                                         outputFileHandle << endl;
257                                 }
258                                 
259                                 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
260                                         string saveLabel = sabund->getLabel();
261                                         
262                                         delete sabund;
263                                         sabund = input->getSAbundVector(lastLabel);
264                                         
265                                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
266                                         processedLabels.insert(sabund->getLabel());
267                                         userLabels.erase(sabund->getLabel());
268                                         
269                                         outputFileHandle << sabund->getLabel();
270                                         for(int i=0;i<sumCalculators.size();i++){
271                                                 vector<double> data = sumCalculators[i]->getValues(sabund);
272                                                 
273                                                 if (m->control_pressed) { if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  } 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;  }
274                                                 
275                                                 outputFileHandle << '\t';
276                                                 sumCalculators[i]->print(outputFileHandle);
277                                         }
278                                         outputFileHandle << endl;
279                                         
280                                         //restore real lastlabel to save below
281                                         sabund->setLabel(saveLabel);
282                                 }               
283                                 
284                                 lastLabel = sabund->getLabel();                 
285                                 
286                                 delete sabund;
287                                 sabund = input->getSAbundVector();
288                         }
289                         
290                         if (m->control_pressed) { if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  } 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;  }
291
292                         //output error messages about any remaining user labels
293                         set<string>::iterator it;
294                         bool needToRun = false;
295                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
296                                 m->mothurOut("Your file does not include the label " + *it); 
297                                 if (processedLabels.count(lastLabel) != 1) {
298                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
299                                         needToRun = true;
300                                 }else {
301                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
302                                 }
303                         }
304                         
305                         //run last label if you need to
306                         if (needToRun == true)  {
307                                 if (sabund != NULL) {   delete sabund;  }
308                                 sabund = input->getSAbundVector(lastLabel);
309                                 
310                                 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
311                                 outputFileHandle << sabund->getLabel();
312                                 for(int i=0;i<sumCalculators.size();i++){
313                                         vector<double> data = sumCalculators[i]->getValues(sabund);
314                                         
315                                         if (m->control_pressed) { if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  } 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;  }
316
317                                         outputFileHandle << '\t';
318                                         sumCalculators[i]->print(outputFileHandle);
319                                 }
320                                 outputFileHandle << endl;
321                                 delete sabund;
322                         }
323                         
324                         outputFileHandle.close();
325                         
326                         if (m->control_pressed) { if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  } 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;  }
327
328                         
329                         delete input;  globaldata->ginput = NULL;
330                         delete read;
331                         delete validCalculator;
332                         globaldata->sabund = NULL;
333                         for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; }
334                 }
335                 
336                 if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  }
337                 
338                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  }  return 0;  }
339                 
340                 m->mothurOutEndLine();
341                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
342                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
343                 m->mothurOutEndLine();
344                 
345                 return 0;
346         }
347         catch(exception& e) {
348                 m->errorOut(e, "SummaryCommand", "execute");
349                 exit(1);
350         }
351 }
352 //**********************************************************************************************************************
353 vector<string> SummaryCommand::parseSharedFile(string filename) {
354         try {
355                 vector<string> filenames;
356                 
357                 map<string, ofstream*> filehandles;
358                 map<string, ofstream*>::iterator it3;
359                 
360                                 
361                 //read first line
362                 read = new ReadOTUFile(filename);       
363                 read->read(&*globaldata); 
364                         
365                 input = globaldata->ginput;
366                 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
367                 
368                 string sharedFileRoot = getRootName(filename);
369                 
370                 //clears file before we start to write to it below
371                 for (int i=0; i<lookup.size(); i++) {
372                         remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
373                         filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
374                 }
375                 
376                 ofstream* temp;
377                 for (int i=0; i<lookup.size(); i++) {
378                         temp = new ofstream;
379                         filehandles[lookup[i]->getGroup()] = temp;
380                         groups.push_back(lookup[i]->getGroup());
381                 }
382
383                 while(lookup[0] != NULL) {
384                 
385                         for (int i = 0; i < lookup.size(); i++) {
386                                 RAbundVector rav = lookup[i]->getRAbundVector();
387                                 openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
388                                 rav.print(*(filehandles[lookup[i]->getGroup()]));
389                                 (*(filehandles[lookup[i]->getGroup()])).close();
390                         }
391                 
392                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
393                         lookup = input->getSharedRAbundVectors();
394                 }
395                 
396                 //free memory
397                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
398                         delete it3->second;
399                 }
400                 delete read;
401                 delete input;
402                 globaldata->ginput = NULL;
403
404                 return filenames;
405         }
406         catch(exception& e) {
407                 m->errorOut(e, "SummaryCommand", "parseSharedFile");
408                 exit(1);
409         }
410 }
411 //**********************************************************************************************************************