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() == "")) { mothurOut("You must read a list, sabund, rabund or shared file before you can use the rarefact.single command."); 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 errorOut(e, "RareFactCommand", "RareFactCommand");
99 //**********************************************************************************************************************
101 void RareFactCommand::help(){
103 mothurOut("The rarefaction.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
104 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 mothurOut("The rarefaction.single command parameters are label, iters, freq, calc and abund. No parameters are required. \n");
106 mothurOut("The rarefaction.single command should be in the following format: \n");
107 mothurOut("rarefaction.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
108 mothurOut("Example rarefaction.single(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-rchao-race-rjack-rbootstrap-rshannon-rnpshannon-rsimpson).\n");
109 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 mothurOut("The label parameter is used to analyze specific labels in your input.\n");
112 mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
114 catch(exception& e) {
115 errorOut(e, "RareFactCommand", "help");
120 //**********************************************************************************************************************
122 RareFactCommand::~RareFactCommand(){}
124 //**********************************************************************************************************************
126 int RareFactCommand::execute(){
129 if (abort == true) { return 0; }
131 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName); }
132 else { inputFileNames = parseSharedFile(globaldata->getSharedFile()); globaldata->setFormat("rabund"); }
134 for (int p = 0; p < inputFileNames.size(); p++) {
136 string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p]));
137 globaldata->inputFileName = inputFileNames[p];
139 if (inputFileNames.size() > 1) {
140 mothurOutEndLine(); mothurOut("Processing group " + groups[p]); mothurOutEndLine(); mothurOutEndLine();
143 validCalculator = new ValidCalculators();
146 for (i=0; i<Estimators.size(); i++) {
147 if (validCalculator->isValidCalculator("rarefaction", Estimators[i]) == true) {
148 if (Estimators[i] == "sobs") {
149 rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(fileNameRoot+"rarefaction")));
150 }else if (Estimators[i] == "chao") {
151 rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"r_chao")));
152 }else if (Estimators[i] == "ace") {
155 rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"r_ace")));
156 }else if (Estimators[i] == "jack") {
157 rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"r_jack")));
158 }else if (Estimators[i] == "shannon") {
159 rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"r_shannon")));
160 }else if (Estimators[i] == "npshannon") {
161 rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(fileNameRoot+"r_npshannon")));
162 }else if (Estimators[i] == "simpson") {
163 rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"r_simpson")));
164 }else if (Estimators[i] == "bootstrap") {
165 rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(fileNameRoot+"r_bootstrap")));
166 }else if (Estimators[i] == "coverage") {
167 rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(fileNameRoot+"r_coverage")));
168 }else if (Estimators[i] == "nseqs") {
169 rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(fileNameRoot+"r_nseqs")));
175 //if the users entered no valid calculators don't execute command
176 if (rDisplays.size() == 0) { return 0; }
178 read = new ReadOTUFile(globaldata->inputFileName);
179 read->read(&*globaldata);
181 order = globaldata->gorder;
182 string lastLabel = order->getLabel();
183 input = globaldata->ginput;
185 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
186 set<string> processedLabels;
187 set<string> userLabels = labels;
189 //as long as you are not at the end of the file or done wih the lines you want
190 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
192 if(allLines == 1 || labels.count(order->getLabel()) == 1){
194 rCurve = new Rarefact(order, rDisplays);
195 rCurve->getCurve(freq, nIters);
198 mothurOut(order->getLabel()); mothurOutEndLine();
199 processedLabels.insert(order->getLabel());
200 userLabels.erase(order->getLabel());
203 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
204 string saveLabel = order->getLabel();
207 order = (input->getOrderVector(lastLabel));
209 rCurve = new Rarefact(order, rDisplays);
210 rCurve->getCurve(freq, nIters);
213 mothurOut(order->getLabel()); mothurOutEndLine();
214 processedLabels.insert(order->getLabel());
215 userLabels.erase(order->getLabel());
217 //restore real lastlabel to save below
218 order->setLabel(saveLabel);
221 lastLabel = order->getLabel();
224 order = (input->getOrderVector());
227 //output error messages about any remaining user labels
228 set<string>::iterator it;
229 bool needToRun = false;
230 for (it = userLabels.begin(); it != userLabels.end(); it++) {
231 mothurOut("Your file does not include the label " + *it);
232 if (processedLabels.count(lastLabel) != 1) {
233 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
236 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
240 //run last label if you need to
241 if (needToRun == true) {
242 if (order != NULL) { delete order; }
243 order = (input->getOrderVector(lastLabel));
245 rCurve = new Rarefact(order, rDisplays);
246 rCurve->getCurve(freq, nIters);
249 mothurOut(order->getLabel()); mothurOutEndLine();
254 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
256 globaldata->gorder = NULL;
257 delete input; globaldata->ginput = NULL;
259 delete validCalculator;
265 catch(exception& e) {
266 errorOut(e, "RareFactCommand", "execute");
270 //**********************************************************************************************************************
271 vector<string> RareFactCommand::parseSharedFile(string filename) {
273 vector<string> filenames;
275 map<string, ofstream*> filehandles;
276 map<string, ofstream*>::iterator it3;
280 read = new ReadOTUFile(filename);
281 read->read(&*globaldata);
283 input = globaldata->ginput;
284 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
286 string sharedFileRoot = getRootName(filename);
288 //clears file before we start to write to it below
289 for (int i=0; i<lookup.size(); i++) {
290 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
291 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
295 for (int i=0; i<lookup.size(); i++) {
297 filehandles[lookup[i]->getGroup()] = temp;
298 groups.push_back(lookup[i]->getGroup());
301 while(lookup[0] != NULL) {
303 for (int i = 0; i < lookup.size(); i++) {
304 RAbundVector rav = lookup[i]->getRAbundVector();
305 openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
306 rav.print(*(filehandles[lookup[i]->getGroup()]));
307 (*(filehandles[lookup[i]->getGroup()])).close();
310 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
311 lookup = input->getSharedRAbundVectors();
315 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
320 globaldata->ginput = NULL;
324 catch(exception& e) {
325 errorOut(e, "RareFactCommand", "parseSharedFile");
329 //**********************************************************************************************************************