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","processors","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 += m->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") { m->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 m->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);
97 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
98 convert(temp, processors);
102 catch(exception& e) {
103 m->errorOut(e, "RareFactCommand", "RareFactCommand");
107 //**********************************************************************************************************************
109 void RareFactCommand::help(){
111 m->mothurOut("The rarefaction.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
112 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");
113 m->mothurOut("The rarefaction.single command parameters are label, iters, freq, calc, processors and abund. No parameters are required. \n");
114 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");
115 m->mothurOut("The processors parameter allows you to specify the number of processors to use. The default is 1.\n");
116 m->mothurOut("The rarefaction.single command should be in the following format: \n");
117 m->mothurOut("rarefaction.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
118 m->mothurOut("Example rarefaction.single(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-rchao-race-rjack-rbootstrap-rshannon-rnpshannon-rsimpson).\n");
119 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");
120 validCalculator->printCalc("rarefaction", cout);
121 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
122 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
124 catch(exception& e) {
125 m->errorOut(e, "RareFactCommand", "help");
130 //**********************************************************************************************************************
132 RareFactCommand::~RareFactCommand(){}
134 //**********************************************************************************************************************
136 int RareFactCommand::execute(){
139 if (abort == true) { return 0; }
141 vector<string> outputNames;
143 string hadShared = "";
144 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName); }
145 else { hadShared = globaldata->getSharedFile(); inputFileNames = parseSharedFile(globaldata->getSharedFile()); globaldata->setFormat("rabund"); }
147 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } return 0; }
149 for (int p = 0; p < inputFileNames.size(); p++) {
151 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p]));
152 globaldata->inputFileName = inputFileNames[p];
154 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } return 0; }
156 if (inputFileNames.size() > 1) {
157 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
160 validCalculator = new ValidCalculators();
163 for (i=0; i<Estimators.size(); i++) {
164 if (validCalculator->isValidCalculator("rarefaction", Estimators[i]) == true) {
165 if (Estimators[i] == "sobs") {
166 rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(fileNameRoot+"rarefaction")));
167 outputNames.push_back(fileNameRoot+"rarefaction");
168 }else if (Estimators[i] == "chao") {
169 rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"r_chao")));
170 outputNames.push_back(fileNameRoot+"r_chao");
171 }else if (Estimators[i] == "ace") {
174 rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"r_ace")));
175 outputNames.push_back(fileNameRoot+"r_ace");
176 }else if (Estimators[i] == "jack") {
177 rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"r_jack")));
178 outputNames.push_back(fileNameRoot+"r_jack");
179 }else if (Estimators[i] == "shannon") {
180 rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"r_shannon")));
181 outputNames.push_back(fileNameRoot+"r_shannon");
182 }else if (Estimators[i] == "shannoneven") {
183 rDisplays.push_back(new RareDisplay(new ShannonEven(), new ThreeColumnFile(fileNameRoot+"r_shannoneven")));
184 outputNames.push_back(fileNameRoot+"r_shannoneven");
185 }else if (Estimators[i] == "heip") {
186 rDisplays.push_back(new RareDisplay(new Heip(), new ThreeColumnFile(fileNameRoot+"r_heip")));
187 outputNames.push_back(fileNameRoot+"r_heip");
188 }else if (Estimators[i] == "smithwilson") {
189 rDisplays.push_back(new RareDisplay(new SmithWilson(), new ThreeColumnFile(fileNameRoot+"r_smithwilson")));
190 outputNames.push_back(fileNameRoot+"r_smithwilson");
191 }else if (Estimators[i] == "npshannon") {
192 rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(fileNameRoot+"r_npshannon")));
193 outputNames.push_back(fileNameRoot+"r_npshannon");
194 }else if (Estimators[i] == "simpson") {
195 rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"r_simpson")));
196 outputNames.push_back(fileNameRoot+"r_simpson");
197 }else if (Estimators[i] == "simpsoneven") {
198 rDisplays.push_back(new RareDisplay(new SimpsonEven(), new ThreeColumnFile(fileNameRoot+"r_simpsoneven")));
199 outputNames.push_back(fileNameRoot+"r_simpsoneven");
200 }else if (Estimators[i] == "invsimpson") {
201 rDisplays.push_back(new RareDisplay(new InvSimpson(), new ThreeColumnFile(fileNameRoot+"r_invsimpson")));
202 outputNames.push_back(fileNameRoot+"r_invsimpson");
203 }else if (Estimators[i] == "bootstrap") {
204 rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(fileNameRoot+"r_bootstrap")));
205 outputNames.push_back(fileNameRoot+"r_bootstrap");
206 }else if (Estimators[i] == "coverage") {
207 rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(fileNameRoot+"r_coverage")));
208 outputNames.push_back(fileNameRoot+"r_coverage");
209 }else if (Estimators[i] == "nseqs") {
210 rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(fileNameRoot+"r_nseqs")));
211 outputNames.push_back(fileNameRoot+"r_nseqs");
217 //if the users entered no valid calculators don't execute command
218 if (rDisplays.size() == 0) { for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; } delete validCalculator; return 0; }
220 read = new ReadOTUFile(globaldata->inputFileName);
221 read->read(&*globaldata);
223 order = globaldata->gorder;
224 string lastLabel = order->getLabel();
225 input = globaldata->ginput;
227 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
228 set<string> processedLabels;
229 set<string> userLabels = labels;
231 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; }
233 //as long as you are not at the end of the file or done wih the lines you want
234 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
236 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; }
239 if(allLines == 1 || labels.count(order->getLabel()) == 1){
241 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
242 rCurve = new Rarefact(order, rDisplays, processors);
243 rCurve->getCurve(freq, nIters);
246 processedLabels.insert(order->getLabel());
247 userLabels.erase(order->getLabel());
250 if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
251 string saveLabel = order->getLabel();
254 order = (input->getOrderVector(lastLabel));
256 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
257 rCurve = new Rarefact(order, rDisplays, processors);
258 rCurve->getCurve(freq, nIters);
261 processedLabels.insert(order->getLabel());
262 userLabels.erase(order->getLabel());
264 //restore real lastlabel to save below
265 order->setLabel(saveLabel);
268 lastLabel = order->getLabel();
271 order = (input->getOrderVector());
274 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; }
276 //output error messages about any remaining user labels
277 set<string>::iterator it;
278 bool needToRun = false;
279 for (it = userLabels.begin(); it != userLabels.end(); it++) {
280 m->mothurOut("Your file does not include the label " + *it);
281 if (processedLabels.count(lastLabel) != 1) {
282 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
285 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
289 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; }
291 //run last label if you need to
292 if (needToRun == true) {
293 if (order != NULL) { delete order; }
294 order = (input->getOrderVector(lastLabel));
296 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
297 rCurve = new Rarefact(order, rDisplays, processors);
298 rCurve->getCurve(freq, nIters);
305 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
307 globaldata->gorder = NULL;
308 delete input; globaldata->ginput = NULL;
310 delete validCalculator;
314 if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); }
316 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
318 m->mothurOutEndLine();
319 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
320 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
321 m->mothurOutEndLine();
325 catch(exception& e) {
326 m->errorOut(e, "RareFactCommand", "execute");
330 //**********************************************************************************************************************
331 vector<string> RareFactCommand::parseSharedFile(string filename) {
333 vector<string> filenames;
335 map<string, ofstream*> filehandles;
336 map<string, ofstream*>::iterator it3;
340 read = new ReadOTUFile(filename);
341 read->read(&*globaldata);
343 input = globaldata->ginput;
344 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
346 string sharedFileRoot = m->getRootName(filename);
348 //clears file before we start to write to it below
349 for (int i=0; i<lookup.size(); i++) {
350 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
351 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
355 for (int i=0; i<lookup.size(); i++) {
357 filehandles[lookup[i]->getGroup()] = temp;
358 groups.push_back(lookup[i]->getGroup());
361 while(lookup[0] != NULL) {
363 for (int i = 0; i < lookup.size(); i++) {
364 RAbundVector rav = lookup[i]->getRAbundVector();
365 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
366 rav.print(*(filehandles[lookup[i]->getGroup()]));
367 (*(filehandles[lookup[i]->getGroup()])).close();
370 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
371 lookup = input->getSharedRAbundVectors();
375 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
380 globaldata->ginput = NULL;
384 catch(exception& e) {
385 m->errorOut(e, "RareFactCommand", "parseSharedFile");
389 //**********************************************************************************************************************