]> git.donarmstrong.com Git - mothur.git/blob - rarefactcommand.cpp
1.10.0
[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 freq parameter is used indicate when to output your data, by default it is set to 100. But you can set it to a percentage of the number of sequence. For example freq=0.10, means 10%. \n");
107                 m->mothurOut("The rarefaction.single command should be in the following format: \n");
108                 m->mothurOut("rarefaction.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
109                 m->mothurOut("Example rarefaction.single(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-rchao-race-rjack-rbootstrap-rshannon-rnpshannon-rsimpson).\n");
110                 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");
111                 validCalculator->printCalc("rarefaction", cout);
112                 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
113                 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
114         }
115         catch(exception& e) {
116                 m->errorOut(e, "RareFactCommand", "help");
117                 exit(1);
118         }
119 }
120
121 //**********************************************************************************************************************
122
123 RareFactCommand::~RareFactCommand(){}
124
125 //**********************************************************************************************************************
126
127 int RareFactCommand::execute(){
128         try {
129         
130                 if (abort == true) { return 0; }
131                 
132                 vector<string> outputNames;
133                 
134                 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName);  }
135                 else {  inputFileNames = parseSharedFile(globaldata->getSharedFile());  globaldata->setFormat("rabund");  }
136                 
137                 if (m->control_pressed) { return 0; }
138                 
139                 for (int p = 0; p < inputFileNames.size(); p++) {
140                         
141                         string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p]));
142                         globaldata->inputFileName = inputFileNames[p];
143                         
144                         if (m->control_pressed) { return 0; }
145                         
146                         if (inputFileNames.size() > 1) {
147                                 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
148                         }
149                         int i;
150                         validCalculator = new ValidCalculators();
151                         
152                         
153                         for (i=0; i<Estimators.size(); i++) {
154                                 if (validCalculator->isValidCalculator("rarefaction", Estimators[i]) == true) { 
155                                         if (Estimators[i] == "sobs") { 
156                                                 rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(fileNameRoot+"rarefaction")));
157                                                 outputNames.push_back(fileNameRoot+"rarefaction");
158                                         }else if (Estimators[i] == "chao") { 
159                                                 rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"r_chao")));
160                                                 outputNames.push_back(fileNameRoot+"r_chao");
161                                         }else if (Estimators[i] == "ace") { 
162                                                 if(abund < 5)
163                                                         abund = 10;
164                                                 rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"r_ace")));
165                                                 outputNames.push_back(fileNameRoot+"r_ace");
166                                         }else if (Estimators[i] == "jack") { 
167                                                 rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"r_jack")));
168                                                 outputNames.push_back(fileNameRoot+"r_jack");
169                                         }else if (Estimators[i] == "shannon") { 
170                                                 rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"r_shannon")));
171                                                 outputNames.push_back(fileNameRoot+"r_shannon");
172                                         }else if (Estimators[i] == "npshannon") { 
173                                                 rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(fileNameRoot+"r_npshannon")));
174                                                 outputNames.push_back(fileNameRoot+"r_npshannon");
175                                         }else if (Estimators[i] == "simpson") { 
176                                                 rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"r_simpson")));
177                                                 outputNames.push_back(fileNameRoot+"r_simpson");
178                                         }else if (Estimators[i] == "bootstrap") { 
179                                                 rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(fileNameRoot+"r_bootstrap")));
180                                                 outputNames.push_back(fileNameRoot+"r_bootstrap");
181                                         }else if (Estimators[i] == "coverage") { 
182                                                 rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(fileNameRoot+"r_coverage")));
183                                                 outputNames.push_back(fileNameRoot+"r_coverage");
184                                         }else if (Estimators[i] == "nseqs") { 
185                                                 rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(fileNameRoot+"r_nseqs")));
186                                                 outputNames.push_back(fileNameRoot+"r_nseqs");
187                                         }
188                                 }
189                         }
190                         
191                         
192                         //if the users entered no valid calculators don't execute command
193                         if (rDisplays.size() == 0) { for(int i=0;i<rDisplays.size();i++){       delete rDisplays[i];    } delete validCalculator; return 0; }
194                         
195                         read = new ReadOTUFile(globaldata->inputFileName);      
196                         read->read(&*globaldata); 
197                         
198                         order = globaldata->gorder;
199                         string lastLabel = order->getLabel();
200                         input = globaldata->ginput;
201                         
202                         //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
203                         set<string> processedLabels;
204                         set<string> userLabels = labels;
205                         
206                         if (m->control_pressed) { for(int i=0;i<rDisplays.size();i++){  delete rDisplays[i];    } delete validCalculator; delete read; delete input; globaldata->ginput = NULL; delete order; globaldata->gorder = NULL; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
207                         
208                         //as long as you are not at the end of the file or done wih the lines you want
209                         while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
210                                 
211                                 if (m->control_pressed) { for(int i=0;i<rDisplays.size();i++){  delete rDisplays[i];    } delete validCalculator; delete read; delete input; globaldata->ginput = NULL; delete order; globaldata->gorder = NULL; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
212
213                                 
214                                 if(allLines == 1 || labels.count(order->getLabel()) == 1){
215                                         
216                                         m->mothurOut(order->getLabel()); m->mothurOutEndLine();
217                                         rCurve = new Rarefact(order, rDisplays);
218                                         rCurve->getCurve(freq, nIters);
219                                         delete rCurve;
220                                         
221                                         processedLabels.insert(order->getLabel());
222                                         userLabels.erase(order->getLabel());
223                                 }
224                                 
225                                 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
226                                         string saveLabel = order->getLabel();
227                                         
228                                         delete order;
229                                         order = (input->getOrderVector(lastLabel));
230                                         
231                                         m->mothurOut(order->getLabel()); m->mothurOutEndLine();
232                                         rCurve = new Rarefact(order, rDisplays);
233                                         rCurve->getCurve(freq, nIters);
234                                         delete rCurve;
235                                         
236                                         processedLabels.insert(order->getLabel());
237                                         userLabels.erase(order->getLabel());
238                                         
239                                         //restore real lastlabel to save below
240                                         order->setLabel(saveLabel);
241                                 }
242                                 
243                                 lastLabel = order->getLabel();          
244                                 
245                                 delete order;
246                                 order = (input->getOrderVector());
247                         }
248                         
249                         if (m->control_pressed) { for(int i=0;i<rDisplays.size();i++){  delete rDisplays[i];    } delete validCalculator; delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); }  return 0; }
250
251                         //output error messages about any remaining user labels
252                         set<string>::iterator it;
253                         bool needToRun = false;
254                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
255                                 m->mothurOut("Your file does not include the label " + *it);
256                                 if (processedLabels.count(lastLabel) != 1) {
257                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
258                                         needToRun = true;
259                                 }else {
260                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
261                                 }
262                         }
263                         
264                         if (m->control_pressed) { for(int i=0;i<rDisplays.size();i++){  delete rDisplays[i];    } delete validCalculator; delete read; delete input; globaldata->ginput = NULL;  for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
265
266                         //run last label if you need to
267                         if (needToRun == true)  {
268                                 if (order != NULL) {    delete order;   }
269                                 order = (input->getOrderVector(lastLabel));
270                                 
271                                 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
272                                 rCurve = new Rarefact(order, rDisplays);
273                                 rCurve->getCurve(freq, nIters);
274                                 delete rCurve;
275                                 
276                                 delete order;
277                         }
278                         
279                         
280                         for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
281                         rDisplays.clear();
282                         globaldata->gorder = NULL;
283                         delete input;  globaldata->ginput = NULL;
284                         delete read;
285                         delete validCalculator;
286                         
287                 }
288                 
289                 if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str()); } return 0; }
290
291                 m->mothurOutEndLine();
292                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
293                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
294                 m->mothurOutEndLine();
295
296                 return 0;
297         }
298         catch(exception& e) {
299                 m->errorOut(e, "RareFactCommand", "execute");
300                 exit(1);
301         }
302 }
303 //**********************************************************************************************************************
304 vector<string> RareFactCommand::parseSharedFile(string filename) {
305         try {
306                 vector<string> filenames;
307                 
308                 map<string, ofstream*> filehandles;
309                 map<string, ofstream*>::iterator it3;
310                 
311                                 
312                 //read first line
313                 read = new ReadOTUFile(filename);       
314                 read->read(&*globaldata); 
315                         
316                 input = globaldata->ginput;
317                 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
318                 
319                 string sharedFileRoot = getRootName(filename);
320                 
321                 //clears file before we start to write to it below
322                 for (int i=0; i<lookup.size(); i++) {
323                         remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
324                         filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
325                 }
326                 
327                 ofstream* temp;
328                 for (int i=0; i<lookup.size(); i++) {
329                         temp = new ofstream;
330                         filehandles[lookup[i]->getGroup()] = temp;
331                         groups.push_back(lookup[i]->getGroup());
332                 }
333
334                 while(lookup[0] != NULL) {
335                 
336                         for (int i = 0; i < lookup.size(); i++) {
337                                 RAbundVector rav = lookup[i]->getRAbundVector();
338                                 openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
339                                 rav.print(*(filehandles[lookup[i]->getGroup()]));
340                                 (*(filehandles[lookup[i]->getGroup()])).close();
341                         }
342                 
343                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
344                         lookup = input->getSharedRAbundVectors();
345                 }
346                 
347                 //free memory
348                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
349                         delete it3->second;
350                 }
351                 delete read;
352                 delete input;
353                 globaldata->ginput = NULL;
354
355                 return filenames;
356         }
357         catch(exception& e) {
358                 m->errorOut(e, "RareFactCommand", "parseSharedFile");
359                 exit(1);
360         }
361 }
362 //**********************************************************************************************************************
363
364
365