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