5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "rarefactcommand.h"
15 #include "bootstrap.h"
17 #include "simpsoneven.h"
19 #include "smithwilson.h"
20 #include "invsimpson.h"
21 #include "npshannon.h"
22 #include "shannoneven.h"
24 #include "jackknife.h"
27 //**********************************************************************************************************************
28 vector<string> RareFactCommand::getValidParameters(){
30 string Array[] = {"iters","freq","label","calc","abund","processors","outputdir","inputdir"};
31 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
35 m->errorOut(e, "RareFactCommand", "getValidParameters");
39 //**********************************************************************************************************************
40 vector<string> RareFactCommand::getRequiredParameters(){
42 vector<string> myArray;
46 m->errorOut(e, "RareFactCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> RareFactCommand::getRequiredFiles(){
53 string AlignArray[] = {"shared","list","rabund","sabund","or"};
54 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
58 m->errorOut(e, "RareFactCommand", "getRequiredFiles");
62 //**********************************************************************************************************************
63 RareFactCommand::RareFactCommand(){
65 abort = true; calledHelp = true;
66 vector<string> tempOutNames;
67 outputTypes["rarefaction"] = tempOutNames;
68 outputTypes["r_chao"] = tempOutNames;
69 outputTypes["r_ace"] = tempOutNames;
70 outputTypes["r_jack"] = tempOutNames;
71 outputTypes["r_shannon"] = tempOutNames;
72 outputTypes["r_shannoneven"] = tempOutNames;
73 outputTypes["r_heip"] = tempOutNames;
74 outputTypes["r_smithwilson"] = tempOutNames;
75 outputTypes["r_npshannon"] = tempOutNames;
76 outputTypes["r_simpson"] = tempOutNames;
77 outputTypes["r_simpsoneven"] = tempOutNames;
78 outputTypes["r_invsimpson"] = tempOutNames;
79 outputTypes["r_bootstrap"] = tempOutNames;
80 outputTypes["r_coverage"] = tempOutNames;
81 outputTypes["r_nseqs"] = tempOutNames;
84 m->errorOut(e, "RareFactCommand", "RareFactCommand");
88 //**********************************************************************************************************************
89 RareFactCommand::RareFactCommand(string option) {
91 globaldata = GlobalData::getInstance();
92 abort = false; calledHelp = false;
97 //allow user to run help
98 if(option == "help") { validCalculator = new ValidCalculators(); help(); delete validCalculator; abort = true; calledHelp = true; }
101 //valid paramters for this command
102 string Array[] = {"iters","freq","label","calc","abund","processors","outputdir","inputdir"};
103 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
105 OptionParser parser(option);
106 map<string,string> parameters = parser.getParameters();
108 ValidParameters validParameter;
110 //check to make sure all parameters are valid for command
111 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
112 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
115 //initialize outputTypes
116 vector<string> tempOutNames;
117 outputTypes["rarefaction"] = tempOutNames;
118 outputTypes["r_chao"] = tempOutNames;
119 outputTypes["r_ace"] = tempOutNames;
120 outputTypes["r_jack"] = tempOutNames;
121 outputTypes["r_shannon"] = tempOutNames;
122 outputTypes["r_shannoneven"] = tempOutNames;
123 outputTypes["r_heip"] = tempOutNames;
124 outputTypes["r_smithwilson"] = tempOutNames;
125 outputTypes["r_npshannon"] = tempOutNames;
126 outputTypes["r_simpson"] = tempOutNames;
127 outputTypes["r_simpsoneven"] = tempOutNames;
128 outputTypes["r_invsimpson"] = tempOutNames;
129 outputTypes["r_bootstrap"] = tempOutNames;
130 outputTypes["r_coverage"] = tempOutNames;
131 outputTypes["r_nseqs"] = tempOutNames;
133 //if the user changes the output directory command factory will send this info to us in the output parameter
134 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
136 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
139 //make sure the user has already run the read.otu command
140 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; }
142 //check for optional parameter and set defaults
143 // ...at some point should added some additional type checking...
144 label = validParameter.validFile(parameters, "label", false);
145 if (label == "not found") { label = ""; }
147 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
148 else { allLines = 1; }
151 //if the user has not specified any labels use the ones from read.otu
153 allLines = globaldata->allLines;
154 labels = globaldata->labels;
157 calc = validParameter.validFile(parameters, "calc", false);
158 if (calc == "not found") { calc = "sobs"; }
160 if (calc == "default") { calc = "sobs"; }
162 m->splitAtDash(calc, Estimators);
165 temp = validParameter.validFile(parameters, "freq", false); if (temp == "not found") { temp = "100"; }
168 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
169 convert(temp, abund);
171 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
172 convert(temp, nIters);
174 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
175 convert(temp, processors);
179 catch(exception& e) {
180 m->errorOut(e, "RareFactCommand", "RareFactCommand");
184 //**********************************************************************************************************************
186 void RareFactCommand::help(){
188 m->mothurOut("The rarefaction.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
189 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");
190 m->mothurOut("The rarefaction.single command parameters are label, iters, freq, calc, processors and abund. No parameters are required. \n");
191 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");
192 m->mothurOut("The processors parameter allows you to specify the number of processors to use. The default is 1.\n");
193 m->mothurOut("The rarefaction.single command should be in the following format: \n");
194 m->mothurOut("rarefaction.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
195 m->mothurOut("Example rarefaction.single(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-rchao-race-rjack-rbootstrap-rshannon-rnpshannon-rsimpson).\n");
196 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");
197 validCalculator->printCalc("rarefaction", cout);
198 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
199 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
201 catch(exception& e) {
202 m->errorOut(e, "RareFactCommand", "help");
207 //**********************************************************************************************************************
209 RareFactCommand::~RareFactCommand(){}
211 //**********************************************************************************************************************
213 int RareFactCommand::execute(){
216 if (abort == true) { if (calledHelp) { return 0; } return 2; }
218 string hadShared = "";
219 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName); }
220 else { hadShared = globaldata->getSharedFile(); inputFileNames = parseSharedFile(globaldata->getSharedFile()); globaldata->setFormat("rabund"); }
222 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } return 0; }
224 for (int p = 0; p < inputFileNames.size(); p++) {
226 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p]));
227 globaldata->inputFileName = inputFileNames[p];
229 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } return 0; }
231 if (inputFileNames.size() > 1) {
232 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
235 validCalculator = new ValidCalculators();
238 for (i=0; i<Estimators.size(); i++) {
239 if (validCalculator->isValidCalculator("rarefaction", Estimators[i]) == true) {
240 if (Estimators[i] == "sobs") {
241 rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(fileNameRoot+"rarefaction")));
242 outputNames.push_back(fileNameRoot+"rarefaction"); outputTypes["rarefaction"].push_back(fileNameRoot+"rarefaction");
243 }else if (Estimators[i] == "chao") {
244 rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"r_chao")));
245 outputNames.push_back(fileNameRoot+"r_chao"); outputTypes["r_chao"].push_back(fileNameRoot+"r_chao");
246 }else if (Estimators[i] == "ace") {
249 rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"r_ace")));
250 outputNames.push_back(fileNameRoot+"r_ace"); outputTypes["r_ace"].push_back(fileNameRoot+"r_ace");
251 }else if (Estimators[i] == "jack") {
252 rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"r_jack")));
253 outputNames.push_back(fileNameRoot+"r_jack"); outputTypes["r_jack"].push_back(fileNameRoot+"r_jack");
254 }else if (Estimators[i] == "shannon") {
255 rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"r_shannon")));
256 outputNames.push_back(fileNameRoot+"r_shannon"); outputTypes["r_shannon"].push_back(fileNameRoot+"r_shannon");
257 }else if (Estimators[i] == "shannoneven") {
258 rDisplays.push_back(new RareDisplay(new ShannonEven(), new ThreeColumnFile(fileNameRoot+"r_shannoneven")));
259 outputNames.push_back(fileNameRoot+"r_shannoneven"); outputTypes["r_shannoneven"].push_back(fileNameRoot+"r_shannoneven");
260 }else if (Estimators[i] == "heip") {
261 rDisplays.push_back(new RareDisplay(new Heip(), new ThreeColumnFile(fileNameRoot+"r_heip")));
262 outputNames.push_back(fileNameRoot+"r_heip"); outputTypes["r_heip"].push_back(fileNameRoot+"r_heip");
263 }else if (Estimators[i] == "smithwilson") {
264 rDisplays.push_back(new RareDisplay(new SmithWilson(), new ThreeColumnFile(fileNameRoot+"r_smithwilson")));
265 outputNames.push_back(fileNameRoot+"r_smithwilson"); outputTypes["r_smithwilson"].push_back(fileNameRoot+"r_smithwilson");
266 }else if (Estimators[i] == "npshannon") {
267 rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(fileNameRoot+"r_npshannon")));
268 outputNames.push_back(fileNameRoot+"r_npshannon"); outputTypes["r_npshannon"].push_back(fileNameRoot+"r_npshannon");
269 }else if (Estimators[i] == "simpson") {
270 rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"r_simpson")));
271 outputNames.push_back(fileNameRoot+"r_simpson"); outputTypes["r_simpson"].push_back(fileNameRoot+"r_simpson");
272 }else if (Estimators[i] == "simpsoneven") {
273 rDisplays.push_back(new RareDisplay(new SimpsonEven(), new ThreeColumnFile(fileNameRoot+"r_simpsoneven")));
274 outputNames.push_back(fileNameRoot+"r_simpsoneven"); outputTypes["r_simpsoneven"].push_back(fileNameRoot+"r_simpsoneven");
275 }else if (Estimators[i] == "invsimpson") {
276 rDisplays.push_back(new RareDisplay(new InvSimpson(), new ThreeColumnFile(fileNameRoot+"r_invsimpson")));
277 outputNames.push_back(fileNameRoot+"r_invsimpson"); outputTypes["r_invsimpson"].push_back(fileNameRoot+"r_invsimpson");
278 }else if (Estimators[i] == "bootstrap") {
279 rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(fileNameRoot+"r_bootstrap")));
280 outputNames.push_back(fileNameRoot+"r_bootstrap"); outputTypes["r_bootstrap"].push_back(fileNameRoot+"r_bootstrap");
281 }else if (Estimators[i] == "coverage") {
282 rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(fileNameRoot+"r_coverage")));
283 outputNames.push_back(fileNameRoot+"r_coverage"); outputTypes["r_coverage"].push_back(fileNameRoot+"r_coverage");
284 }else if (Estimators[i] == "nseqs") {
285 rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(fileNameRoot+"r_nseqs")));
286 outputNames.push_back(fileNameRoot+"r_nseqs"); outputTypes["r_nseqs"].push_back(fileNameRoot+"r_nseqs");
292 //if the users entered no valid calculators don't execute command
293 if (rDisplays.size() == 0) { for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; } delete validCalculator; return 0; }
295 read = new ReadOTUFile(globaldata->inputFileName);
296 read->read(&*globaldata);
298 order = globaldata->gorder;
299 string lastLabel = order->getLabel();
300 input = globaldata->ginput;
302 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
303 set<string> processedLabels;
304 set<string> userLabels = labels;
306 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; }
308 //as long as you are not at the end of the file or done wih the lines you want
309 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
311 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; }
314 if(allLines == 1 || labels.count(order->getLabel()) == 1){
316 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
317 rCurve = new Rarefact(order, rDisplays, processors);
318 rCurve->getCurve(freq, nIters);
321 processedLabels.insert(order->getLabel());
322 userLabels.erase(order->getLabel());
325 if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
326 string saveLabel = order->getLabel();
329 order = (input->getOrderVector(lastLabel));
331 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
332 rCurve = new Rarefact(order, rDisplays, processors);
333 rCurve->getCurve(freq, nIters);
336 processedLabels.insert(order->getLabel());
337 userLabels.erase(order->getLabel());
339 //restore real lastlabel to save below
340 order->setLabel(saveLabel);
343 lastLabel = order->getLabel();
346 order = (input->getOrderVector());
349 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; }
351 //output error messages about any remaining user labels
352 set<string>::iterator it;
353 bool needToRun = false;
354 for (it = userLabels.begin(); it != userLabels.end(); it++) {
355 m->mothurOut("Your file does not include the label " + *it);
356 if (processedLabels.count(lastLabel) != 1) {
357 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
360 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
364 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; }
366 //run last label if you need to
367 if (needToRun == true) {
368 if (order != NULL) { delete order; }
369 order = (input->getOrderVector(lastLabel));
371 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
372 rCurve = new Rarefact(order, rDisplays, processors);
373 rCurve->getCurve(freq, nIters);
380 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
382 globaldata->gorder = NULL;
383 delete input; globaldata->ginput = NULL;
385 delete validCalculator;
389 if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); }
391 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
393 m->mothurOutEndLine();
394 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
395 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
396 m->mothurOutEndLine();
400 catch(exception& e) {
401 m->errorOut(e, "RareFactCommand", "execute");
405 //**********************************************************************************************************************
406 vector<string> RareFactCommand::parseSharedFile(string filename) {
408 vector<string> filenames;
410 map<string, ofstream*> filehandles;
411 map<string, ofstream*>::iterator it3;
415 read = new ReadOTUFile(filename);
416 read->read(&*globaldata);
418 input = globaldata->ginput;
419 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
421 string sharedFileRoot = m->getRootName(filename);
423 //clears file before we start to write to it below
424 for (int i=0; i<lookup.size(); i++) {
425 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
426 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
430 for (int i=0; i<lookup.size(); i++) {
432 filehandles[lookup[i]->getGroup()] = temp;
433 groups.push_back(lookup[i]->getGroup());
436 while(lookup[0] != NULL) {
438 for (int i = 0; i < lookup.size(); i++) {
439 RAbundVector rav = lookup[i]->getRAbundVector();
440 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
441 rav.print(*(filehandles[lookup[i]->getGroup()]));
442 (*(filehandles[lookup[i]->getGroup()])).close();
445 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
446 lookup = input->getSharedRAbundVectors();
450 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
455 globaldata->ginput = NULL;
459 catch(exception& e) {
460 m->errorOut(e, "RareFactCommand", "parseSharedFile");
464 //**********************************************************************************************************************