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 //**********************************************************************************************************************
30 RareFactCommand::RareFactCommand(string option) {
32 globaldata = GlobalData::getInstance();
38 //allow user to run help
39 if(option == "help") { validCalculator = new ValidCalculators(); help(); delete validCalculator; abort = true; }
42 //valid paramters for this command
43 string Array[] = {"iters","freq","label","calc","abund","outputdir","inputdir"};
44 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 OptionParser parser(option);
47 map<string,string> parameters = parser.getParameters();
49 ValidParameters validParameter;
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; }
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"){
59 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
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; }
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 = ""; }
70 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
71 else { allLines = 1; }
74 //if the user has not specified any labels use the ones from read.otu
76 allLines = globaldata->allLines;
77 labels = globaldata->labels;
80 calc = validParameter.validFile(parameters, "calc", false);
81 if (calc == "not found") { calc = "sobs"; }
83 if (calc == "default") { calc = "sobs"; }
85 splitAtDash(calc, Estimators);
88 temp = validParameter.validFile(parameters, "freq", false); if (temp == "not found") { temp = "100"; }
91 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
94 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
95 convert(temp, nIters);
100 m->errorOut(e, "RareFactCommand", "RareFactCommand");
104 //**********************************************************************************************************************
106 void RareFactCommand::help(){
108 m->mothurOut("The rarefaction.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
109 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");
110 m->mothurOut("The rarefaction.single command parameters are label, iters, freq, calc and abund. No parameters are required. \n");
111 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");
112 m->mothurOut("The rarefaction.single command should be in the following format: \n");
113 m->mothurOut("rarefaction.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
114 m->mothurOut("Example rarefaction.single(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-rchao-race-rjack-rbootstrap-rshannon-rnpshannon-rsimpson).\n");
115 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");
116 validCalculator->printCalc("rarefaction", cout);
117 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
118 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
120 catch(exception& e) {
121 m->errorOut(e, "RareFactCommand", "help");
126 //**********************************************************************************************************************
128 RareFactCommand::~RareFactCommand(){}
130 //**********************************************************************************************************************
132 int RareFactCommand::execute(){
135 if (abort == true) { return 0; }
137 vector<string> outputNames;
139 string hadShared = "";
140 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName); }
141 else { hadShared = globaldata->getSharedFile(); inputFileNames = parseSharedFile(globaldata->getSharedFile()); globaldata->setFormat("rabund"); }
143 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } return 0; }
145 for (int p = 0; p < inputFileNames.size(); p++) {
147 string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p]));
148 globaldata->inputFileName = inputFileNames[p];
150 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } return 0; }
152 if (inputFileNames.size() > 1) {
153 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
156 validCalculator = new ValidCalculators();
159 for (i=0; i<Estimators.size(); i++) {
160 if (validCalculator->isValidCalculator("rarefaction", Estimators[i]) == true) {
161 if (Estimators[i] == "sobs") {
162 rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(fileNameRoot+"rarefaction")));
163 outputNames.push_back(fileNameRoot+"rarefaction");
164 }else if (Estimators[i] == "chao") {
165 rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"r_chao")));
166 outputNames.push_back(fileNameRoot+"r_chao");
167 }else if (Estimators[i] == "ace") {
170 rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"r_ace")));
171 outputNames.push_back(fileNameRoot+"r_ace");
172 }else if (Estimators[i] == "jack") {
173 rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"r_jack")));
174 outputNames.push_back(fileNameRoot+"r_jack");
175 }else if (Estimators[i] == "shannon") {
176 rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"r_shannon")));
177 outputNames.push_back(fileNameRoot+"r_shannon");
178 }else if (Estimators[i] == "shannoneven") {
179 rDisplays.push_back(new RareDisplay(new ShannonEven(), new ThreeColumnFile(fileNameRoot+"r_shannoneven")));
180 outputNames.push_back(fileNameRoot+"r_shannoneven");
181 }else if (Estimators[i] == "heip") {
182 rDisplays.push_back(new RareDisplay(new Heip(), new ThreeColumnFile(fileNameRoot+"r_heip")));
183 outputNames.push_back(fileNameRoot+"r_heip");
184 }else if (Estimators[i] == "smithwilson") {
185 rDisplays.push_back(new RareDisplay(new SmithWilson(), new ThreeColumnFile(fileNameRoot+"r_smithwilson")));
186 outputNames.push_back(fileNameRoot+"r_smithwilson");
187 }else if (Estimators[i] == "npshannon") {
188 rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(fileNameRoot+"r_npshannon")));
189 outputNames.push_back(fileNameRoot+"r_npshannon");
190 }else if (Estimators[i] == "simpson") {
191 rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"r_simpson")));
192 outputNames.push_back(fileNameRoot+"r_simpson");
193 }else if (Estimators[i] == "simpsoneven") {
194 rDisplays.push_back(new RareDisplay(new SimpsonEven(), new ThreeColumnFile(fileNameRoot+"r_simpsoneven")));
195 outputNames.push_back(fileNameRoot+"r_simpsoneven");
196 }else if (Estimators[i] == "invsimpson") {
197 rDisplays.push_back(new RareDisplay(new InvSimpson(), new ThreeColumnFile(fileNameRoot+"r_invsimpson")));
198 outputNames.push_back(fileNameRoot+"r_invsimpson");
199 }else if (Estimators[i] == "bootstrap") {
200 rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(fileNameRoot+"r_bootstrap")));
201 outputNames.push_back(fileNameRoot+"r_bootstrap");
202 }else if (Estimators[i] == "coverage") {
203 rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(fileNameRoot+"r_coverage")));
204 outputNames.push_back(fileNameRoot+"r_coverage");
205 }else if (Estimators[i] == "nseqs") {
206 rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(fileNameRoot+"r_nseqs")));
207 outputNames.push_back(fileNameRoot+"r_nseqs");
213 //if the users entered no valid calculators don't execute command
214 if (rDisplays.size() == 0) { for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; } delete validCalculator; return 0; }
216 read = new ReadOTUFile(globaldata->inputFileName);
217 read->read(&*globaldata);
219 order = globaldata->gorder;
220 string lastLabel = order->getLabel();
221 input = globaldata->ginput;
223 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
224 set<string> processedLabels;
225 set<string> userLabels = labels;
227 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; }
229 //as long as you are not at the end of the file or done wih the lines you want
230 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
232 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; }
235 if(allLines == 1 || labels.count(order->getLabel()) == 1){
237 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
238 rCurve = new Rarefact(order, rDisplays);
239 rCurve->getCurve(freq, nIters);
242 processedLabels.insert(order->getLabel());
243 userLabels.erase(order->getLabel());
246 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
247 string saveLabel = order->getLabel();
250 order = (input->getOrderVector(lastLabel));
252 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
253 rCurve = new Rarefact(order, rDisplays);
254 rCurve->getCurve(freq, nIters);
257 processedLabels.insert(order->getLabel());
258 userLabels.erase(order->getLabel());
260 //restore real lastlabel to save below
261 order->setLabel(saveLabel);
264 lastLabel = order->getLabel();
267 order = (input->getOrderVector());
270 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; }
272 //output error messages about any remaining user labels
273 set<string>::iterator it;
274 bool needToRun = false;
275 for (it = userLabels.begin(); it != userLabels.end(); it++) {
276 m->mothurOut("Your file does not include the label " + *it);
277 if (processedLabels.count(lastLabel) != 1) {
278 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
281 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
285 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; }
287 //run last label if you need to
288 if (needToRun == true) {
289 if (order != NULL) { delete order; }
290 order = (input->getOrderVector(lastLabel));
292 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
293 rCurve = new Rarefact(order, rDisplays);
294 rCurve->getCurve(freq, nIters);
301 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
303 globaldata->gorder = NULL;
304 delete input; globaldata->ginput = NULL;
306 delete validCalculator;
310 if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); }
312 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
314 m->mothurOutEndLine();
315 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
316 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
317 m->mothurOutEndLine();
321 catch(exception& e) {
322 m->errorOut(e, "RareFactCommand", "execute");
326 //**********************************************************************************************************************
327 vector<string> RareFactCommand::parseSharedFile(string filename) {
329 vector<string> filenames;
331 map<string, ofstream*> filehandles;
332 map<string, ofstream*>::iterator it3;
336 read = new ReadOTUFile(filename);
337 read->read(&*globaldata);
339 input = globaldata->ginput;
340 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
342 string sharedFileRoot = getRootName(filename);
344 //clears file before we start to write to it below
345 for (int i=0; i<lookup.size(); i++) {
346 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
347 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
351 for (int i=0; i<lookup.size(); i++) {
353 filehandles[lookup[i]->getGroup()] = temp;
354 groups.push_back(lookup[i]->getGroup());
357 while(lookup[0] != NULL) {
359 for (int i = 0; i < lookup.size(); i++) {
360 RAbundVector rav = lookup[i]->getRAbundVector();
361 openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
362 rav.print(*(filehandles[lookup[i]->getGroup()]));
363 (*(filehandles[lookup[i]->getGroup()])).close();
366 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
367 lookup = input->getSharedRAbundVectors();
371 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
376 globaldata->ginput = NULL;
380 catch(exception& e) {
381 m->errorOut(e, "RareFactCommand", "parseSharedFile");
385 //**********************************************************************************************************************