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