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 "npshannon.h"
19 #include "jackknife.h"
22 //**********************************************************************************************************************
25 RareFactCommand::RareFactCommand(string option) {
27 globaldata = GlobalData::getInstance();
33 //allow user to run help
34 if(option == "help") { validCalculator = new ValidCalculators(); help(); delete validCalculator; abort = true; }
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)));
41 OptionParser parser(option);
42 map<string,string> parameters = parser.getParameters();
44 ValidParameters validParameter;
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; }
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"){
54 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
57 //make sure the user has already run the read.otu command
58 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; }
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 = ""; }
65 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
66 else { allLines = 1; }
69 //if the user has not specified any labels use the ones from read.otu
71 allLines = globaldata->allLines;
72 labels = globaldata->labels;
75 calc = validParameter.validFile(parameters, "calc", false);
76 if (calc == "not found") { calc = "sobs"; }
78 if (calc == "default") { calc = "sobs"; }
80 splitAtDash(calc, Estimators);
83 temp = validParameter.validFile(parameters, "freq", false); if (temp == "not found") { temp = "100"; }
86 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
89 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
90 convert(temp, nIters);
95 m->errorOut(e, "RareFactCommand", "RareFactCommand");
99 //**********************************************************************************************************************
101 void RareFactCommand::help(){
103 m->mothurOut("The rarefaction.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
104 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");
105 m->mothurOut("The rarefaction.single command parameters are label, iters, freq, calc and abund. No parameters are required. \n");
106 m->mothurOut("The rarefaction.single command should be in the following format: \n");
107 m->mothurOut("rarefaction.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
108 m->mothurOut("Example rarefaction.single(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-rchao-race-rjack-rbootstrap-rshannon-rnpshannon-rsimpson).\n");
109 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");
110 validCalculator->printCalc("rarefaction", cout);
111 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
112 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
114 catch(exception& e) {
115 m->errorOut(e, "RareFactCommand", "help");
120 //**********************************************************************************************************************
122 RareFactCommand::~RareFactCommand(){}
124 //**********************************************************************************************************************
126 int RareFactCommand::execute(){
129 if (abort == true) { return 0; }
131 vector<string> outputNames;
133 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName); }
134 else { inputFileNames = parseSharedFile(globaldata->getSharedFile()); globaldata->setFormat("rabund"); }
136 if (m->control_pressed) { return 0; }
138 for (int p = 0; p < inputFileNames.size(); p++) {
140 string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p]));
141 globaldata->inputFileName = inputFileNames[p];
143 if (m->control_pressed) { return 0; }
145 if (inputFileNames.size() > 1) {
146 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
149 validCalculator = new ValidCalculators();
152 for (i=0; i<Estimators.size(); i++) {
153 if (validCalculator->isValidCalculator("rarefaction", Estimators[i]) == true) {
154 if (Estimators[i] == "sobs") {
155 rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(fileNameRoot+"rarefaction")));
156 outputNames.push_back(fileNameRoot+"rarefaction");
157 }else if (Estimators[i] == "chao") {
158 rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"r_chao")));
159 outputNames.push_back(fileNameRoot+"r_chao");
160 }else if (Estimators[i] == "ace") {
163 rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"r_ace")));
164 outputNames.push_back(fileNameRoot+"r_ace");
165 }else if (Estimators[i] == "jack") {
166 rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"r_jack")));
167 outputNames.push_back(fileNameRoot+"r_jack");
168 }else if (Estimators[i] == "shannon") {
169 rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"r_shannon")));
170 outputNames.push_back(fileNameRoot+"r_shannon");
171 }else if (Estimators[i] == "npshannon") {
172 rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(fileNameRoot+"r_npshannon")));
173 outputNames.push_back(fileNameRoot+"r_npshannon");
174 }else if (Estimators[i] == "simpson") {
175 rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"r_simpson")));
176 outputNames.push_back(fileNameRoot+"r_simpson");
177 }else if (Estimators[i] == "bootstrap") {
178 rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(fileNameRoot+"r_bootstrap")));
179 outputNames.push_back(fileNameRoot+"r_bootstrap");
180 }else if (Estimators[i] == "coverage") {
181 rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(fileNameRoot+"r_coverage")));
182 outputNames.push_back(fileNameRoot+"r_coverage");
183 }else if (Estimators[i] == "nseqs") {
184 rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(fileNameRoot+"r_nseqs")));
185 outputNames.push_back(fileNameRoot+"r_nseqs");
191 //if the users entered no valid calculators don't execute command
192 if (rDisplays.size() == 0) { for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; } delete validCalculator; return 0; }
194 read = new ReadOTUFile(globaldata->inputFileName);
195 read->read(&*globaldata);
197 order = globaldata->gorder;
198 string lastLabel = order->getLabel();
199 input = globaldata->ginput;
201 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
202 set<string> processedLabels;
203 set<string> userLabels = labels;
205 if (m->control_pressed) { 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; }
207 //as long as you are not at the end of the file or done wih the lines you want
208 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
210 if (m->control_pressed) { 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; }
213 if(allLines == 1 || labels.count(order->getLabel()) == 1){
215 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
216 rCurve = new Rarefact(order, rDisplays);
217 rCurve->getCurve(freq, nIters);
220 processedLabels.insert(order->getLabel());
221 userLabels.erase(order->getLabel());
224 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
225 string saveLabel = order->getLabel();
228 order = (input->getOrderVector(lastLabel));
230 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
231 rCurve = new Rarefact(order, rDisplays);
232 rCurve->getCurve(freq, nIters);
235 processedLabels.insert(order->getLabel());
236 userLabels.erase(order->getLabel());
238 //restore real lastlabel to save below
239 order->setLabel(saveLabel);
242 lastLabel = order->getLabel();
245 order = (input->getOrderVector());
248 if (m->control_pressed) { 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; }
250 //output error messages about any remaining user labels
251 set<string>::iterator it;
252 bool needToRun = false;
253 for (it = userLabels.begin(); it != userLabels.end(); it++) {
254 m->mothurOut("Your file does not include the label " + *it);
255 if (processedLabels.count(lastLabel) != 1) {
256 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
259 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
263 if (m->control_pressed) { 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; }
265 //run last label if you need to
266 if (needToRun == true) {
267 if (order != NULL) { delete order; }
268 order = (input->getOrderVector(lastLabel));
270 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
271 rCurve = new Rarefact(order, rDisplays);
272 rCurve->getCurve(freq, nIters);
279 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
281 globaldata->gorder = NULL;
282 delete input; globaldata->ginput = NULL;
284 delete validCalculator;
288 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
290 m->mothurOutEndLine();
291 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
292 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
293 m->mothurOutEndLine();
297 catch(exception& e) {
298 m->errorOut(e, "RareFactCommand", "execute");
302 //**********************************************************************************************************************
303 vector<string> RareFactCommand::parseSharedFile(string filename) {
305 vector<string> filenames;
307 map<string, ofstream*> filehandles;
308 map<string, ofstream*>::iterator it3;
312 read = new ReadOTUFile(filename);
313 read->read(&*globaldata);
315 input = globaldata->ginput;
316 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
318 string sharedFileRoot = getRootName(filename);
320 //clears file before we start to write to it below
321 for (int i=0; i<lookup.size(); i++) {
322 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
323 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
327 for (int i=0; i<lookup.size(); i++) {
329 filehandles[lookup[i]->getGroup()] = temp;
330 groups.push_back(lookup[i]->getGroup());
333 while(lookup[0] != NULL) {
335 for (int i = 0; i < lookup.size(); i++) {
336 RAbundVector rav = lookup[i]->getRAbundVector();
337 openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
338 rav.print(*(filehandles[lookup[i]->getGroup()]));
339 (*(filehandles[lookup[i]->getGroup()])).close();
342 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
343 lookup = input->getSharedRAbundVectors();
347 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
352 globaldata->ginput = NULL;
356 catch(exception& e) {
357 m->errorOut(e, "RareFactCommand", "parseSharedFile");
361 //**********************************************************************************************************************