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"
28 //**********************************************************************************************************************
29 vector<string> RareFactCommand::setParameters(){
31 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(plist);
32 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(prabund);
33 CommandParameter psabund("sabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(psabund);
34 CommandParameter pshared("shared", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(pshared);
35 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
36 CommandParameter pfreq("freq", "Number", "", "100", "", "", "",false,false); parameters.push_back(pfreq);
37 CommandParameter piters("iters", "Number", "", "1000", "", "", "",false,false); parameters.push_back(piters);
38 CommandParameter pcalc("calc", "Multiple", "sobs-chao-nseqs-coverage-ace-jack-shannon-shannoneven-npshannon-heip-smithwilson-simpson-simpsoneven-invsimpson-bootstrap", "sobs", "", "", "",true,false); parameters.push_back(pcalc);
39 CommandParameter pabund("abund", "Number", "", "10", "", "", "",false,false); parameters.push_back(pabund);
40 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
41 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
42 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
44 vector<string> myArray;
45 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
49 m->errorOut(e, "RareFactCommand", "setParameters");
53 //**********************************************************************************************************************
54 string RareFactCommand::getHelpString(){
56 ValidCalculators validCalculator;
57 string helpString = "";
58 helpString += "The rarefaction.single command parameters are list, sabund, rabund, shared, label, iters, freq, calc, processors and abund. list, sabund, rabund or shared is required unless you have a valid current file. \n";
59 helpString += "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";
60 helpString += "The processors parameter allows you to specify the number of processors to use. The default is 1.\n";
61 helpString += "The rarefaction.single command should be in the following format: \n";
62 helpString += "rarefaction.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n";
63 helpString += "Example rarefaction.single(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-rchao-race-rjack-rbootstrap-rshannon-rnpshannon-rsimpson).\n";
64 helpString += "The default values for iters is 1000, freq is 100, and calc is rarefaction which calculates the rarefaction curve for the observed richness.\n";
65 validCalculator.printCalc("rarefaction");
66 helpString += "The label parameter is used to analyze specific labels in your input.\n";
67 helpString += "Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n";
71 m->errorOut(e, "RareFactCommand", "getHelpString");
76 //**********************************************************************************************************************
77 RareFactCommand::RareFactCommand(){
79 abort = true; calledHelp = true;
81 vector<string> tempOutNames;
82 outputTypes["rarefaction"] = tempOutNames;
83 outputTypes["r_chao"] = tempOutNames;
84 outputTypes["r_ace"] = tempOutNames;
85 outputTypes["r_jack"] = tempOutNames;
86 outputTypes["r_shannon"] = tempOutNames;
87 outputTypes["r_shannoneven"] = tempOutNames;
88 outputTypes["r_heip"] = tempOutNames;
89 outputTypes["r_smithwilson"] = tempOutNames;
90 outputTypes["r_npshannon"] = tempOutNames;
91 outputTypes["r_simpson"] = tempOutNames;
92 outputTypes["r_simpsoneven"] = tempOutNames;
93 outputTypes["r_invsimpson"] = tempOutNames;
94 outputTypes["r_bootstrap"] = tempOutNames;
95 outputTypes["r_coverage"] = tempOutNames;
96 outputTypes["r_nseqs"] = tempOutNames;
99 m->errorOut(e, "RareFactCommand", "RareFactCommand");
103 //**********************************************************************************************************************
104 RareFactCommand::RareFactCommand(string option) {
106 abort = false; calledHelp = false;
109 //allow user to run help
110 if(option == "help") { help(); abort = true; calledHelp = true; }
111 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
114 vector<string> myArray = setParameters();
116 OptionParser parser(option);
117 map<string,string> parameters = parser.getParameters();
118 map<string,string>::iterator it;
120 ValidParameters validParameter;
122 //check to make sure all parameters are valid for command
123 for (it = parameters.begin(); it != parameters.end(); it++) {
124 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
127 //initialize outputTypes
128 vector<string> tempOutNames;
129 outputTypes["rarefaction"] = tempOutNames;
130 outputTypes["r_chao"] = tempOutNames;
131 outputTypes["r_ace"] = tempOutNames;
132 outputTypes["r_jack"] = tempOutNames;
133 outputTypes["r_shannon"] = tempOutNames;
134 outputTypes["r_shannoneven"] = tempOutNames;
135 outputTypes["r_heip"] = tempOutNames;
136 outputTypes["r_smithwilson"] = tempOutNames;
137 outputTypes["r_npshannon"] = tempOutNames;
138 outputTypes["r_simpson"] = tempOutNames;
139 outputTypes["r_simpsoneven"] = tempOutNames;
140 outputTypes["r_invsimpson"] = tempOutNames;
141 outputTypes["r_bootstrap"] = tempOutNames;
142 outputTypes["r_coverage"] = tempOutNames;
143 outputTypes["r_nseqs"] = tempOutNames;
145 //if the user changes the input directory command factory will send this info to us in the output parameter
146 string inputDir = validParameter.validFile(parameters, "inputdir", false);
147 if (inputDir == "not found"){ inputDir = ""; }
150 it = parameters.find("shared");
151 //user has given a template file
152 if(it != parameters.end()){
153 path = m->hasPath(it->second);
154 //if the user has not given a path then, add inputdir. else leave path alone.
155 if (path == "") { parameters["shared"] = inputDir + it->second; }
158 it = parameters.find("rabund");
159 //user has given a template file
160 if(it != parameters.end()){
161 path = m->hasPath(it->second);
162 //if the user has not given a path then, add inputdir. else leave path alone.
163 if (path == "") { parameters["rabund"] = inputDir + it->second; }
166 it = parameters.find("sabund");
167 //user has given a template file
168 if(it != parameters.end()){
169 path = m->hasPath(it->second);
170 //if the user has not given a path then, add inputdir. else leave path alone.
171 if (path == "") { parameters["sabund"] = inputDir + it->second; }
174 it = parameters.find("list");
175 //user has given a template file
176 if(it != parameters.end()){
177 path = m->hasPath(it->second);
178 //if the user has not given a path then, add inputdir. else leave path alone.
179 if (path == "") { parameters["list"] = inputDir + it->second; }
183 //check for required parameters
184 listfile = validParameter.validFile(parameters, "list", true);
185 if (listfile == "not open") { listfile = ""; abort = true; }
186 else if (listfile == "not found") { listfile = ""; }
187 else { format = "list"; inputfile = listfile; m->setListFile(listfile); }
189 sabundfile = validParameter.validFile(parameters, "sabund", true);
190 if (sabundfile == "not open") { sabundfile = ""; abort = true; }
191 else if (sabundfile == "not found") { sabundfile = ""; }
192 else { format = "sabund"; inputfile = sabundfile; m->setSabundFile(sabundfile); }
194 rabundfile = validParameter.validFile(parameters, "rabund", true);
195 if (rabundfile == "not open") { rabundfile = ""; abort = true; }
196 else if (rabundfile == "not found") { rabundfile = ""; }
197 else { format = "rabund"; inputfile = rabundfile; m->setRabundFile(rabundfile); }
199 sharedfile = validParameter.validFile(parameters, "shared", true);
200 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
201 else if (sharedfile == "not found") { sharedfile = ""; }
202 else { format = "sharedfile"; inputfile = sharedfile; m->setSharedFile(sharedfile); }
204 if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) {
205 //is there are current file available for any of these?
206 //give priority to shared, then list, then rabund, then sabund
207 //if there is a current shared file, use it
208 sharedfile = m->getSharedFile();
209 if (sharedfile != "") { inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
211 listfile = m->getListFile();
212 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
214 rabundfile = m->getRabundFile();
215 if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
217 sabundfile = m->getSabundFile();
218 if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
220 m->mothurOut("No valid current files. You must provide a list, sabund, rabund or shared file before you can use the collect.single command."); m->mothurOutEndLine();
228 //if the user changes the output directory command factory will send this info to us in the output parameter
229 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
231 //check for optional parameter and set defaults
232 // ...at some point should added some additional type checking...
233 label = validParameter.validFile(parameters, "label", false);
234 if (label == "not found") { label = ""; }
236 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
237 else { allLines = 1; }
240 calc = validParameter.validFile(parameters, "calc", false);
241 if (calc == "not found") { calc = "sobs"; }
243 if (calc == "default") { calc = "sobs"; }
245 m->splitAtDash(calc, Estimators);
246 if (m->inUsersGroups("citation", Estimators)) {
247 ValidCalculators validCalc; validCalc.printCitations(Estimators);
248 //remove citation from list of calcs
249 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") { Estimators.erase(Estimators.begin()+i); break; } }
253 temp = validParameter.validFile(parameters, "freq", false); if (temp == "not found") { temp = "100"; }
256 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
257 convert(temp, abund);
259 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
260 convert(temp, nIters);
262 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
263 m->setProcessors(temp);
264 convert(temp, processors);
268 catch(exception& e) {
269 m->errorOut(e, "RareFactCommand", "RareFactCommand");
273 //**********************************************************************************************************************
275 int RareFactCommand::execute(){
278 if (abort == true) { if (calledHelp) { return 0; } return 2; }
280 if ((format != "sharedfile")) { inputFileNames.push_back(inputfile); }
281 else { inputFileNames = parseSharedFile(sharedfile); format = "rabund"; }
283 if (m->control_pressed) { return 0; }
285 for (int p = 0; p < inputFileNames.size(); p++) {
287 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p]));
289 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } m->Groups.clear(); return 0; }
291 if (inputFileNames.size() > 1) {
292 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
295 ValidCalculators validCalculator;
298 for (i=0; i<Estimators.size(); i++) {
299 if (validCalculator.isValidCalculator("rarefaction", Estimators[i]) == true) {
300 if (Estimators[i] == "sobs") {
301 rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(fileNameRoot+"rarefaction")));
302 outputNames.push_back(fileNameRoot+"rarefaction"); outputTypes["rarefaction"].push_back(fileNameRoot+"rarefaction");
303 }else if (Estimators[i] == "chao") {
304 rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"r_chao")));
305 outputNames.push_back(fileNameRoot+"r_chao"); outputTypes["r_chao"].push_back(fileNameRoot+"r_chao");
306 }else if (Estimators[i] == "ace") {
309 rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"r_ace")));
310 outputNames.push_back(fileNameRoot+"r_ace"); outputTypes["r_ace"].push_back(fileNameRoot+"r_ace");
311 }else if (Estimators[i] == "jack") {
312 rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"r_jack")));
313 outputNames.push_back(fileNameRoot+"r_jack"); outputTypes["r_jack"].push_back(fileNameRoot+"r_jack");
314 }else if (Estimators[i] == "shannon") {
315 rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"r_shannon")));
316 outputNames.push_back(fileNameRoot+"r_shannon"); outputTypes["r_shannon"].push_back(fileNameRoot+"r_shannon");
317 }else if (Estimators[i] == "shannoneven") {
318 rDisplays.push_back(new RareDisplay(new ShannonEven(), new ThreeColumnFile(fileNameRoot+"r_shannoneven")));
319 outputNames.push_back(fileNameRoot+"r_shannoneven"); outputTypes["r_shannoneven"].push_back(fileNameRoot+"r_shannoneven");
320 }else if (Estimators[i] == "heip") {
321 rDisplays.push_back(new RareDisplay(new Heip(), new ThreeColumnFile(fileNameRoot+"r_heip")));
322 outputNames.push_back(fileNameRoot+"r_heip"); outputTypes["r_heip"].push_back(fileNameRoot+"r_heip");
323 }else if (Estimators[i] == "smithwilson") {
324 rDisplays.push_back(new RareDisplay(new SmithWilson(), new ThreeColumnFile(fileNameRoot+"r_smithwilson")));
325 outputNames.push_back(fileNameRoot+"r_smithwilson"); outputTypes["r_smithwilson"].push_back(fileNameRoot+"r_smithwilson");
326 }else if (Estimators[i] == "npshannon") {
327 rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(fileNameRoot+"r_npshannon")));
328 outputNames.push_back(fileNameRoot+"r_npshannon"); outputTypes["r_npshannon"].push_back(fileNameRoot+"r_npshannon");
329 }else if (Estimators[i] == "simpson") {
330 rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"r_simpson")));
331 outputNames.push_back(fileNameRoot+"r_simpson"); outputTypes["r_simpson"].push_back(fileNameRoot+"r_simpson");
332 }else if (Estimators[i] == "simpsoneven") {
333 rDisplays.push_back(new RareDisplay(new SimpsonEven(), new ThreeColumnFile(fileNameRoot+"r_simpsoneven")));
334 outputNames.push_back(fileNameRoot+"r_simpsoneven"); outputTypes["r_simpsoneven"].push_back(fileNameRoot+"r_simpsoneven");
335 }else if (Estimators[i] == "invsimpson") {
336 rDisplays.push_back(new RareDisplay(new InvSimpson(), new ThreeColumnFile(fileNameRoot+"r_invsimpson")));
337 outputNames.push_back(fileNameRoot+"r_invsimpson"); outputTypes["r_invsimpson"].push_back(fileNameRoot+"r_invsimpson");
338 }else if (Estimators[i] == "bootstrap") {
339 rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(fileNameRoot+"r_bootstrap")));
340 outputNames.push_back(fileNameRoot+"r_bootstrap"); outputTypes["r_bootstrap"].push_back(fileNameRoot+"r_bootstrap");
341 }else if (Estimators[i] == "coverage") {
342 rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(fileNameRoot+"r_coverage")));
343 outputNames.push_back(fileNameRoot+"r_coverage"); outputTypes["r_coverage"].push_back(fileNameRoot+"r_coverage");
344 }else if (Estimators[i] == "nseqs") {
345 rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(fileNameRoot+"r_nseqs")));
346 outputNames.push_back(fileNameRoot+"r_nseqs"); outputTypes["r_nseqs"].push_back(fileNameRoot+"r_nseqs");
352 //if the users entered no valid calculators don't execute command
353 if (rDisplays.size() == 0) { for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; } return 0; }
355 input = new InputData(inputFileNames[p], format);
356 order = input->getOrderVector();
357 string lastLabel = order->getLabel();
359 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
360 set<string> processedLabels;
361 set<string> userLabels = labels;
363 if (m->control_pressed) { for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; } delete input; delete order; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
365 //as long as you are not at the end of the file or done wih the lines you want
366 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
368 if (m->control_pressed) { for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; } delete input; delete order; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
371 if(allLines == 1 || labels.count(order->getLabel()) == 1){
373 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
374 rCurve = new Rarefact(order, rDisplays, processors);
375 rCurve->getCurve(freq, nIters);
378 processedLabels.insert(order->getLabel());
379 userLabels.erase(order->getLabel());
382 if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
383 string saveLabel = order->getLabel();
386 order = (input->getOrderVector(lastLabel));
388 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
389 rCurve = new Rarefact(order, rDisplays, processors);
390 rCurve->getCurve(freq, nIters);
393 processedLabels.insert(order->getLabel());
394 userLabels.erase(order->getLabel());
396 //restore real lastlabel to save below
397 order->setLabel(saveLabel);
400 lastLabel = order->getLabel();
403 order = (input->getOrderVector());
406 if (m->control_pressed) { for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; } delete input; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
408 //output error messages about any remaining user labels
409 set<string>::iterator it;
410 bool needToRun = false;
411 for (it = userLabels.begin(); it != userLabels.end(); it++) {
412 m->mothurOut("Your file does not include the label " + *it);
413 if (processedLabels.count(lastLabel) != 1) {
414 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
417 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
421 if (m->control_pressed) { for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; } delete input; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
423 //run last label if you need to
424 if (needToRun == true) {
425 if (order != NULL) { delete order; }
426 order = (input->getOrderVector(lastLabel));
428 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
429 rCurve = new Rarefact(order, rDisplays, processors);
430 rCurve->getCurve(freq, nIters);
437 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
443 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
445 m->mothurOutEndLine();
446 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
447 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
448 m->mothurOutEndLine();
452 catch(exception& e) {
453 m->errorOut(e, "RareFactCommand", "execute");
457 //**********************************************************************************************************************
458 vector<string> RareFactCommand::parseSharedFile(string filename) {
460 vector<string> filenames;
462 map<string, ofstream*> filehandles;
463 map<string, ofstream*>::iterator it3;
465 input = new InputData(filename, "sharedfile");
466 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
468 string sharedFileRoot = m->getRootName(filename);
470 //clears file before we start to write to it below
471 for (int i=0; i<lookup.size(); i++) {
472 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
473 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
477 for (int i=0; i<lookup.size(); i++) {
479 filehandles[lookup[i]->getGroup()] = temp;
480 groups.push_back(lookup[i]->getGroup());
483 while(lookup[0] != NULL) {
485 for (int i = 0; i < lookup.size(); i++) {
486 RAbundVector rav = lookup[i]->getRAbundVector();
487 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
488 rav.print(*(filehandles[lookup[i]->getGroup()]));
489 (*(filehandles[lookup[i]->getGroup()])).close();
492 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
493 lookup = input->getSharedRAbundVectors();
497 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
506 catch(exception& e) {
507 m->errorOut(e, "RareFactCommand", "parseSharedFile");
511 //**********************************************************************************************************************