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