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