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