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(){
66 //initialize outputTypes
67 vector<string> tempOutNames;
68 outputTypes["rarefaction"] = tempOutNames;
69 outputTypes["r_chao"] = tempOutNames;
70 outputTypes["r_ace"] = tempOutNames;
71 outputTypes["r_jack"] = tempOutNames;
72 outputTypes["r_shannon"] = tempOutNames;
73 outputTypes["r_shannoneven"] = tempOutNames;
74 outputTypes["r_heip"] = tempOutNames;
75 outputTypes["r_smithwilson"] = tempOutNames;
76 outputTypes["r_npshannon"] = tempOutNames;
77 outputTypes["r_simpson"] = tempOutNames;
78 outputTypes["r_simpsoneven"] = tempOutNames;
79 outputTypes["r_invsimpson"] = tempOutNames;
80 outputTypes["r_bootstrap"] = tempOutNames;
81 outputTypes["r_coverage"] = tempOutNames;
82 outputTypes["r_nseqs"] = tempOutNames;
85 m->errorOut(e, "RareFactCommand", "RareFactCommand");
89 //**********************************************************************************************************************
90 RareFactCommand::RareFactCommand(string option) {
92 globaldata = GlobalData::getInstance();
98 //allow user to run help
99 if(option == "help") { validCalculator = new ValidCalculators(); help(); delete validCalculator; abort = true; }
102 //valid paramters for this command
103 string Array[] = {"iters","freq","label","calc","abund","processors","outputdir","inputdir"};
104 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
106 OptionParser parser(option);
107 map<string,string> parameters = parser.getParameters();
109 ValidParameters validParameter;
111 //check to make sure all parameters are valid for command
112 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
113 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
116 //initialize outputTypes
117 vector<string> tempOutNames;
118 outputTypes["rarefaction"] = tempOutNames;
119 outputTypes["r_chao"] = tempOutNames;
120 outputTypes["r_ace"] = tempOutNames;
121 outputTypes["r_jack"] = tempOutNames;
122 outputTypes["r_shannon"] = tempOutNames;
123 outputTypes["r_shannoneven"] = tempOutNames;
124 outputTypes["r_heip"] = tempOutNames;
125 outputTypes["r_smithwilson"] = tempOutNames;
126 outputTypes["r_npshannon"] = tempOutNames;
127 outputTypes["r_simpson"] = tempOutNames;
128 outputTypes["r_simpsoneven"] = tempOutNames;
129 outputTypes["r_invsimpson"] = tempOutNames;
130 outputTypes["r_bootstrap"] = tempOutNames;
131 outputTypes["r_coverage"] = tempOutNames;
132 outputTypes["r_nseqs"] = tempOutNames;
134 //if the user changes the output directory command factory will send this info to us in the output parameter
135 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
137 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
140 //make sure the user has already run the read.otu command
141 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; }
143 //check for optional parameter and set defaults
144 // ...at some point should added some additional type checking...
145 label = validParameter.validFile(parameters, "label", false);
146 if (label == "not found") { label = ""; }
148 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
149 else { allLines = 1; }
152 //if the user has not specified any labels use the ones from read.otu
154 allLines = globaldata->allLines;
155 labels = globaldata->labels;
158 calc = validParameter.validFile(parameters, "calc", false);
159 if (calc == "not found") { calc = "sobs"; }
161 if (calc == "default") { calc = "sobs"; }
163 m->splitAtDash(calc, Estimators);
166 temp = validParameter.validFile(parameters, "freq", false); if (temp == "not found") { temp = "100"; }
169 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
170 convert(temp, abund);
172 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
173 convert(temp, nIters);
175 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
176 convert(temp, processors);
180 catch(exception& e) {
181 m->errorOut(e, "RareFactCommand", "RareFactCommand");
185 //**********************************************************************************************************************
187 void RareFactCommand::help(){
189 m->mothurOut("The rarefaction.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
190 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");
191 m->mothurOut("The rarefaction.single command parameters are label, iters, freq, calc, processors and abund. No parameters are required. \n");
192 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");
193 m->mothurOut("The processors parameter allows you to specify the number of processors to use. The default is 1.\n");
194 m->mothurOut("The rarefaction.single command should be in the following format: \n");
195 m->mothurOut("rarefaction.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
196 m->mothurOut("Example rarefaction.single(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-rchao-race-rjack-rbootstrap-rshannon-rnpshannon-rsimpson).\n");
197 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");
198 validCalculator->printCalc("rarefaction", cout);
199 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
200 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
202 catch(exception& e) {
203 m->errorOut(e, "RareFactCommand", "help");
208 //**********************************************************************************************************************
210 RareFactCommand::~RareFactCommand(){}
212 //**********************************************************************************************************************
214 int RareFactCommand::execute(){
217 if (abort == true) { return 0; }
219 string hadShared = "";
220 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName); }
221 else { hadShared = globaldata->getSharedFile(); inputFileNames = parseSharedFile(globaldata->getSharedFile()); globaldata->setFormat("rabund"); }
223 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } return 0; }
225 for (int p = 0; p < inputFileNames.size(); p++) {
227 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p]));
228 globaldata->inputFileName = inputFileNames[p];
230 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } return 0; }
232 if (inputFileNames.size() > 1) {
233 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
236 validCalculator = new ValidCalculators();
239 for (i=0; i<Estimators.size(); i++) {
240 if (validCalculator->isValidCalculator("rarefaction", Estimators[i]) == true) {
241 if (Estimators[i] == "sobs") {
242 rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(fileNameRoot+"rarefaction")));
243 outputNames.push_back(fileNameRoot+"rarefaction"); outputTypes["rarefaction"].push_back(fileNameRoot+"rarefaction");
244 }else if (Estimators[i] == "chao") {
245 rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"r_chao")));
246 outputNames.push_back(fileNameRoot+"r_chao"); outputTypes["r_chao"].push_back(fileNameRoot+"r_chao");
247 }else if (Estimators[i] == "ace") {
250 rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"r_ace")));
251 outputNames.push_back(fileNameRoot+"r_ace"); outputTypes["r_ace"].push_back(fileNameRoot+"r_ace");
252 }else if (Estimators[i] == "jack") {
253 rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"r_jack")));
254 outputNames.push_back(fileNameRoot+"r_jack"); outputTypes["r_jack"].push_back(fileNameRoot+"r_jack");
255 }else if (Estimators[i] == "shannon") {
256 rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"r_shannon")));
257 outputNames.push_back(fileNameRoot+"r_shannon"); outputTypes["r_shannon"].push_back(fileNameRoot+"r_shannon");
258 }else if (Estimators[i] == "shannoneven") {
259 rDisplays.push_back(new RareDisplay(new ShannonEven(), new ThreeColumnFile(fileNameRoot+"r_shannoneven")));
260 outputNames.push_back(fileNameRoot+"r_shannoneven"); outputTypes["r_shannoneven"].push_back(fileNameRoot+"r_shannoneven");
261 }else if (Estimators[i] == "heip") {
262 rDisplays.push_back(new RareDisplay(new Heip(), new ThreeColumnFile(fileNameRoot+"r_heip")));
263 outputNames.push_back(fileNameRoot+"r_heip"); outputTypes["r_heip"].push_back(fileNameRoot+"r_heip");
264 }else if (Estimators[i] == "smithwilson") {
265 rDisplays.push_back(new RareDisplay(new SmithWilson(), new ThreeColumnFile(fileNameRoot+"r_smithwilson")));
266 outputNames.push_back(fileNameRoot+"r_smithwilson"); outputTypes["r_smithwilson"].push_back(fileNameRoot+"r_smithwilson");
267 }else if (Estimators[i] == "npshannon") {
268 rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(fileNameRoot+"r_npshannon")));
269 outputNames.push_back(fileNameRoot+"r_npshannon"); outputTypes["r_npshannon"].push_back(fileNameRoot+"r_npshannon");
270 }else if (Estimators[i] == "simpson") {
271 rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"r_simpson")));
272 outputNames.push_back(fileNameRoot+"r_simpson"); outputTypes["r_simpson"].push_back(fileNameRoot+"r_simpson");
273 }else if (Estimators[i] == "simpsoneven") {
274 rDisplays.push_back(new RareDisplay(new SimpsonEven(), new ThreeColumnFile(fileNameRoot+"r_simpsoneven")));
275 outputNames.push_back(fileNameRoot+"r_simpsoneven"); outputTypes["r_simpsoneven"].push_back(fileNameRoot+"r_simpsoneven");
276 }else if (Estimators[i] == "invsimpson") {
277 rDisplays.push_back(new RareDisplay(new InvSimpson(), new ThreeColumnFile(fileNameRoot+"r_invsimpson")));
278 outputNames.push_back(fileNameRoot+"r_invsimpson"); outputTypes["r_invsimpson"].push_back(fileNameRoot+"r_invsimpson");
279 }else if (Estimators[i] == "bootstrap") {
280 rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(fileNameRoot+"r_bootstrap")));
281 outputNames.push_back(fileNameRoot+"r_bootstrap"); outputTypes["r_bootstrap"].push_back(fileNameRoot+"r_bootstrap");
282 }else if (Estimators[i] == "coverage") {
283 rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(fileNameRoot+"r_coverage")));
284 outputNames.push_back(fileNameRoot+"r_coverage"); outputTypes["r_coverage"].push_back(fileNameRoot+"r_coverage");
285 }else if (Estimators[i] == "nseqs") {
286 rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(fileNameRoot+"r_nseqs")));
287 outputNames.push_back(fileNameRoot+"r_nseqs"); outputTypes["r_nseqs"].push_back(fileNameRoot+"r_nseqs");
293 //if the users entered no valid calculators don't execute command
294 if (rDisplays.size() == 0) { for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; } delete validCalculator; return 0; }
296 read = new ReadOTUFile(globaldata->inputFileName);
297 read->read(&*globaldata);
299 order = globaldata->gorder;
300 string lastLabel = order->getLabel();
301 input = globaldata->ginput;
303 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
304 set<string> processedLabels;
305 set<string> userLabels = labels;
307 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; }
309 //as long as you are not at the end of the file or done wih the lines you want
310 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
312 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; }
315 if(allLines == 1 || labels.count(order->getLabel()) == 1){
317 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
318 rCurve = new Rarefact(order, rDisplays, processors);
319 rCurve->getCurve(freq, nIters);
322 processedLabels.insert(order->getLabel());
323 userLabels.erase(order->getLabel());
326 if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
327 string saveLabel = order->getLabel();
330 order = (input->getOrderVector(lastLabel));
332 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
333 rCurve = new Rarefact(order, rDisplays, processors);
334 rCurve->getCurve(freq, nIters);
337 processedLabels.insert(order->getLabel());
338 userLabels.erase(order->getLabel());
340 //restore real lastlabel to save below
341 order->setLabel(saveLabel);
344 lastLabel = order->getLabel();
347 order = (input->getOrderVector());
350 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; }
352 //output error messages about any remaining user labels
353 set<string>::iterator it;
354 bool needToRun = false;
355 for (it = userLabels.begin(); it != userLabels.end(); it++) {
356 m->mothurOut("Your file does not include the label " + *it);
357 if (processedLabels.count(lastLabel) != 1) {
358 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
361 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
365 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; }
367 //run last label if you need to
368 if (needToRun == true) {
369 if (order != NULL) { delete order; }
370 order = (input->getOrderVector(lastLabel));
372 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
373 rCurve = new Rarefact(order, rDisplays, processors);
374 rCurve->getCurve(freq, nIters);
381 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
383 globaldata->gorder = NULL;
384 delete input; globaldata->ginput = NULL;
386 delete validCalculator;
390 if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); }
392 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
394 m->mothurOutEndLine();
395 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
396 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
397 m->mothurOutEndLine();
401 catch(exception& e) {
402 m->errorOut(e, "RareFactCommand", "execute");
406 //**********************************************************************************************************************
407 vector<string> RareFactCommand::parseSharedFile(string filename) {
409 vector<string> filenames;
411 map<string, ofstream*> filehandles;
412 map<string, ofstream*>::iterator it3;
416 read = new ReadOTUFile(filename);
417 read->read(&*globaldata);
419 input = globaldata->ginput;
420 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
422 string sharedFileRoot = m->getRootName(filename);
424 //clears file before we start to write to it below
425 for (int i=0; i<lookup.size(); i++) {
426 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
427 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
431 for (int i=0; i<lookup.size(); i++) {
433 filehandles[lookup[i]->getGroup()] = temp;
434 groups.push_back(lookup[i]->getGroup());
437 while(lookup[0] != NULL) {
439 for (int i = 0; i < lookup.size(); i++) {
440 RAbundVector rav = lookup[i]->getRAbundVector();
441 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
442 rav.print(*(filehandles[lookup[i]->getGroup()]));
443 (*(filehandles[lookup[i]->getGroup()])).close();
446 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
447 lookup = input->getSharedRAbundVectors();
451 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
456 globaldata->ginput = NULL;
460 catch(exception& e) {
461 m->errorOut(e, "RareFactCommand", "parseSharedFile");
465 //**********************************************************************************************************************