]> git.donarmstrong.com Git - mothur.git/blob - rarefactcommand.cpp
created mothurOut class to handle logfiles
[mothur.git] / rarefactcommand.cpp
1 /*
2  *  rarefactcommand.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 "rarefactcommand.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 "coverage.h"
21
22 //**********************************************************************************************************************
23
24
25 RareFactCommand::RareFactCommand(string option)  {
26         try {
27                 globaldata = GlobalData::getInstance();
28                 abort = false;
29                 allLines = 1;
30                 labels.clear();
31                 Estimators.clear();
32                                 
33                 //allow user to run help
34                 if(option == "help") { validCalculator = new ValidCalculators(); help(); delete validCalculator; abort = true; }
35                 
36                 else {
37                         //valid paramters for this command
38                         string Array[] =  {"iters","freq","label","calc","abund","outputdir","inputdir"};
39                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
40                         
41                         OptionParser parser(option);
42                         map<string,string> parameters = parser.getParameters();
43                         
44                         ValidParameters validParameter;
45                 
46                         //check to make sure all parameters are valid for command
47                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
48                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
49                         }
50                         
51                         //if the user changes the output directory command factory will send this info to us in the output parameter 
52                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
53                                 outputDir = ""; 
54                                 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it  
55                         }
56
57                         //make sure the user has already run the read.otu command
58                         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 rarefact.single command."); m->mothurOutEndLine(); abort = true; }
59                         
60                         //check for optional parameter and set defaults
61                         // ...at some point should added some additional type checking...
62                         label = validParameter.validFile(parameters, "label", false);                   
63                         if (label == "not found") { label = ""; }
64                         else { 
65                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
66                                 else { allLines = 1;  }
67                         }
68                         
69                         //if the user has not specified any labels use the ones from read.otu
70                         if(label == "") {  
71                                 allLines = globaldata->allLines; 
72                                 labels = globaldata->labels; 
73                         }
74                                 
75                         calc = validParameter.validFile(parameters, "calc", false);                     
76                         if (calc == "not found") { calc = "sobs";  }
77                         else { 
78                                  if (calc == "default")  {  calc = "sobs";  }
79                         }
80                         splitAtDash(calc, Estimators);
81
82                         string temp;
83                         temp = validParameter.validFile(parameters, "freq", false);                     if (temp == "not found") { temp = "100"; }
84                         convert(temp, freq); 
85                         
86                         temp = validParameter.validFile(parameters, "abund", false);                    if (temp == "not found") { temp = "10"; }
87                         convert(temp, abund); 
88                         
89                         temp = validParameter.validFile(parameters, "iters", false);                    if (temp == "not found") { temp = "1000"; }
90                         convert(temp, nIters); 
91                 }
92                 
93         }
94         catch(exception& e) {
95                 m->errorOut(e, "RareFactCommand", "RareFactCommand");
96                 exit(1);
97         }
98 }
99 //**********************************************************************************************************************
100
101 void RareFactCommand::help(){
102         try {
103                 m->mothurOut("The rarefaction.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
104                 m->mothurOut("The rarefaction.single command can be executed after a successful cluster command.  It will use the .list file from the output of the cluster.\n");
105                 m->mothurOut("The rarefaction.single command parameters are label, iters, freq, calc and abund.  No parameters are required. \n");
106                 m->mothurOut("The rarefaction.single command should be in the following format: \n");
107                 m->mothurOut("rarefaction.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
108                 m->mothurOut("Example rarefaction.single(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-rchao-race-rjack-rbootstrap-rshannon-rnpshannon-rsimpson).\n");
109                 m->mothurOut("The default values for iters is 1000, freq is 100, and calc is rarefaction which calculates the rarefaction curve for the observed richness.\n");
110                 validCalculator->printCalc("rarefaction", cout);
111                 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
112                 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
113         }
114         catch(exception& e) {
115                 m->errorOut(e, "RareFactCommand", "help");
116                 exit(1);
117         }
118 }
119
120 //**********************************************************************************************************************
121
122 RareFactCommand::~RareFactCommand(){}
123
124 //**********************************************************************************************************************
125
126 int RareFactCommand::execute(){
127         try {
128         
129                 if (abort == true) { return 0; }
130                 
131                 vector<string> outputNames;
132                 
133                 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName);  }
134                 else {  inputFileNames = parseSharedFile(globaldata->getSharedFile());  globaldata->setFormat("rabund");  }
135                 
136                 for (int p = 0; p < inputFileNames.size(); p++) {
137                         
138                         string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p]));
139                         globaldata->inputFileName = inputFileNames[p];
140                         
141                         if (inputFileNames.size() > 1) {
142                                 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
143                         }
144                         int i;
145                         validCalculator = new ValidCalculators();
146                         
147                         
148                         for (i=0; i<Estimators.size(); i++) {
149                                 if (validCalculator->isValidCalculator("rarefaction", Estimators[i]) == true) { 
150                                         if (Estimators[i] == "sobs") { 
151                                                 rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(fileNameRoot+"rarefaction")));
152                                                 outputNames.push_back(fileNameRoot+"rarefaction");
153                                         }else if (Estimators[i] == "chao") { 
154                                                 rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"r_chao")));
155                                                 outputNames.push_back(fileNameRoot+"r_chao");
156                                         }else if (Estimators[i] == "ace") { 
157                                                 if(abund < 5)
158                                                         abund = 10;
159                                                 rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"r_ace")));
160                                                 outputNames.push_back(fileNameRoot+"r_ace");
161                                         }else if (Estimators[i] == "jack") { 
162                                                 rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"r_jack")));
163                                                 outputNames.push_back(fileNameRoot+"r_jack");
164                                         }else if (Estimators[i] == "shannon") { 
165                                                 rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"r_shannon")));
166                                                 outputNames.push_back(fileNameRoot+"r_shannon");
167                                         }else if (Estimators[i] == "npshannon") { 
168                                                 rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(fileNameRoot+"r_npshannon")));
169                                                 outputNames.push_back(fileNameRoot+"r_npshannon");
170                                         }else if (Estimators[i] == "simpson") { 
171                                                 rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"r_simpson")));
172                                                 outputNames.push_back(fileNameRoot+"r_simpson");
173                                         }else if (Estimators[i] == "bootstrap") { 
174                                                 rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(fileNameRoot+"r_bootstrap")));
175                                                 outputNames.push_back(fileNameRoot+"r_bootstrap");
176                                         }else if (Estimators[i] == "coverage") { 
177                                                 rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(fileNameRoot+"r_coverage")));
178                                                 outputNames.push_back(fileNameRoot+"r_coverage");
179                                         }else if (Estimators[i] == "nseqs") { 
180                                                 rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(fileNameRoot+"r_nseqs")));
181                                                 outputNames.push_back(fileNameRoot+"r_nseqs");
182                                         }
183                                 }
184                         }
185                         
186                         
187                         //if the users entered no valid calculators don't execute command
188                         if (rDisplays.size() == 0) { return 0; }
189                         
190                         read = new ReadOTUFile(globaldata->inputFileName);      
191                         read->read(&*globaldata); 
192                         
193                         order = globaldata->gorder;
194                         string lastLabel = order->getLabel();
195                         input = globaldata->ginput;
196                         
197                         //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
198                         set<string> processedLabels;
199                         set<string> userLabels = labels;
200                         
201                         //as long as you are not at the end of the file or done wih the lines you want
202                         while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
203                                 
204                                 if(allLines == 1 || labels.count(order->getLabel()) == 1){
205                                         
206                                         rCurve = new Rarefact(order, rDisplays);
207                                         rCurve->getCurve(freq, nIters);
208                                         delete rCurve;
209                                         
210                                         m->mothurOut(order->getLabel()); m->mothurOutEndLine();
211                                         processedLabels.insert(order->getLabel());
212                                         userLabels.erase(order->getLabel());
213                                 }
214                                 
215                                 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
216                                         string saveLabel = order->getLabel();
217                                         
218                                         delete order;
219                                         order = (input->getOrderVector(lastLabel));
220                                         
221                                         rCurve = new Rarefact(order, rDisplays);
222                                         rCurve->getCurve(freq, nIters);
223                                         delete rCurve;
224                                         
225                                         m->mothurOut(order->getLabel()); m->mothurOutEndLine();
226                                         processedLabels.insert(order->getLabel());
227                                         userLabels.erase(order->getLabel());
228                                         
229                                         //restore real lastlabel to save below
230                                         order->setLabel(saveLabel);
231                                 }
232                                 
233                                 lastLabel = order->getLabel();          
234                                 
235                                 delete order;
236                                 order = (input->getOrderVector());
237                         }
238                         
239                         //output error messages about any remaining user labels
240                         set<string>::iterator it;
241                         bool needToRun = false;
242                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
243                                 m->mothurOut("Your file does not include the label " + *it);
244                                 if (processedLabels.count(lastLabel) != 1) {
245                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
246                                         needToRun = true;
247                                 }else {
248                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
249                                 }
250                         }
251                         
252                         //run last label if you need to
253                         if (needToRun == true)  {
254                                 if (order != NULL) {    delete order;   }
255                                 order = (input->getOrderVector(lastLabel));
256                                 
257                                 rCurve = new Rarefact(order, rDisplays);
258                                 rCurve->getCurve(freq, nIters);
259                                 delete rCurve;
260                                 
261                                 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
262                                 delete order;
263                         }
264                         
265                         
266                         for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
267                         rDisplays.clear();
268                         globaldata->gorder = NULL;
269                         delete input;  globaldata->ginput = NULL;
270                         delete read;
271                         delete validCalculator;
272                         
273                 }
274                 
275                 
276                 m->mothurOutEndLine();
277                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
278                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
279                 m->mothurOutEndLine();
280
281                 return 0;
282         }
283         catch(exception& e) {
284                 m->errorOut(e, "RareFactCommand", "execute");
285                 exit(1);
286         }
287 }
288 //**********************************************************************************************************************
289 vector<string> RareFactCommand::parseSharedFile(string filename) {
290         try {
291                 vector<string> filenames;
292                 
293                 map<string, ofstream*> filehandles;
294                 map<string, ofstream*>::iterator it3;
295                 
296                                 
297                 //read first line
298                 read = new ReadOTUFile(filename);       
299                 read->read(&*globaldata); 
300                         
301                 input = globaldata->ginput;
302                 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
303                 
304                 string sharedFileRoot = getRootName(filename);
305                 
306                 //clears file before we start to write to it below
307                 for (int i=0; i<lookup.size(); i++) {
308                         remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
309                         filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
310                 }
311                 
312                 ofstream* temp;
313                 for (int i=0; i<lookup.size(); i++) {
314                         temp = new ofstream;
315                         filehandles[lookup[i]->getGroup()] = temp;
316                         groups.push_back(lookup[i]->getGroup());
317                 }
318
319                 while(lookup[0] != NULL) {
320                 
321                         for (int i = 0; i < lookup.size(); i++) {
322                                 RAbundVector rav = lookup[i]->getRAbundVector();
323                                 openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
324                                 rav.print(*(filehandles[lookup[i]->getGroup()]));
325                                 (*(filehandles[lookup[i]->getGroup()])).close();
326                         }
327                 
328                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
329                         lookup = input->getSharedRAbundVectors();
330                 }
331                 
332                 //free memory
333                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
334                         delete it3->second;
335                 }
336                 delete read;
337                 delete input;
338                 globaldata->ginput = NULL;
339
340                 return filenames;
341         }
342         catch(exception& e) {
343                 m->errorOut(e, "RareFactCommand", "parseSharedFile");
344                 exit(1);
345         }
346 }
347 //**********************************************************************************************************************
348
349
350