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"
26 #include "shannonrange.h"
29 //**********************************************************************************************************************
30 vector<string> RareFactCommand::setParameters(){
32 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none","",false,false,true); parameters.push_back(plist);
33 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none","",false,false); parameters.push_back(prabund);
34 CommandParameter psabund("sabund", "InputTypes", "", "", "LRSS", "LRSS", "none","",false,false); parameters.push_back(psabund);
35 CommandParameter pshared("shared", "InputTypes", "", "", "LRSS", "LRSS", "none","",false,false,true); parameters.push_back(pshared);
36 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
37 CommandParameter pfreq("freq", "Number", "", "100", "", "", "","",false,false); parameters.push_back(pfreq);
38 CommandParameter piters("iters", "Number", "", "1000", "", "", "","",false,false); parameters.push_back(piters);
39 CommandParameter pcalc("calc", "Multiple", "sobs-chao-nseqs-coverage-ace-jack-shannon-shannoneven-npshannon-heip-smithwilson-simpson-simpsoneven-invsimpson-bootstrap-shannonrange", "sobs", "", "", "","",true,false,true); parameters.push_back(pcalc);
40 CommandParameter pabund("abund", "Number", "", "10", "", "", "","",false,false); parameters.push_back(pabund);
41 CommandParameter palpha("alpha", "Multiple", "0-1-2", "1", "", "", "","",false,false,true); parameters.push_back(palpha);
42 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
43 CommandParameter pgroupmode("groupmode", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(pgroupmode);
44 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
45 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
47 vector<string> myArray;
48 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
52 m->errorOut(e, "RareFactCommand", "setParameters");
56 //**********************************************************************************************************************
57 string RareFactCommand::getHelpString(){
59 ValidCalculators validCalculator;
60 string helpString = "";
61 helpString += "The rarefaction.single command parameters are list, sabund, rabund, shared, label, iters, freq, calc, processors, groupmode and abund. list, sabund, rabund or shared is required unless you have a valid current file. \n";
62 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";
63 helpString += "The processors parameter allows you to specify the number of processors to use. The default is 1.\n";
64 helpString += "The rarefaction.single command should be in the following format: \n";
65 helpString += "rarefaction.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n";
66 helpString += "Example rarefaction.single(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-rchao-race-rjack-rbootstrap-rshannon-rnpshannon-rsimpson).\n";
67 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";
68 helpString += "The alpha parameter is used to set the alpha value for the shannonrange calculator.\n";
69 validCalculator.printCalc("rarefaction");
70 helpString += "If you are running rarefaction.single with a shared file and would like your results collated in one file, set groupmode=t. (Default=true).\n";
71 helpString += "The label parameter is used to analyze specific labels in your input.\n";
72 helpString += "Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n";
76 m->errorOut(e, "RareFactCommand", "getHelpString");
80 //**********************************************************************************************************************
81 string RareFactCommand::getOutputPattern(string type) {
84 if (type == "rarefaction") { pattern = "[filename],rarefaction"; }
85 else if (type == "r_chao") { pattern = "[filename],r_chao"; }
86 else if (type == "r_ace") { pattern = "[filename],r_ace"; }
87 else if (type == "r_jack") { pattern = "[filename],r_jack"; }
88 else if (type == "r_shannon") { pattern = "[filename],r_shannon"; }
89 else if (type == "r_shannoneven") { pattern = "[filename],r_shannoneven"; }
90 else if (type == "r_smithwilson") { pattern = "[filename],r_smithwilson"; }
91 else if (type == "r_npshannon") { pattern = "[filename],r_npshannon"; }
92 else if (type == "r_shannonrange"){ pattern = "[filename],r_shannonrange"; }
93 else if (type == "r_simpson") { pattern = "[filename],r_simpson"; }
94 else if (type == "r_simpsoneven") { pattern = "[filename],r_simpsoneven"; }
95 else if (type == "r_invsimpson") { pattern = "[filename],r_invsimpson"; }
96 else if (type == "r_bootstrap") { pattern = "[filename],r_bootstrap"; }
97 else if (type == "r_coverage") { pattern = "[filename],r_coverage"; }
98 else if (type == "r_nseqs") { pattern = "[filename],r_nseqs"; }
99 else if (type == "r_heip") { pattern = "[filename],r_heip"; }
100 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
104 catch(exception& e) {
105 m->errorOut(e, "RareFactCommand", "getOutputPattern");
109 //**********************************************************************************************************************
110 RareFactCommand::RareFactCommand(){
112 abort = true; calledHelp = true;
114 vector<string> tempOutNames;
115 outputTypes["rarefaction"] = tempOutNames;
116 outputTypes["r_chao"] = tempOutNames;
117 outputTypes["r_ace"] = tempOutNames;
118 outputTypes["r_jack"] = tempOutNames;
119 outputTypes["r_shannon"] = tempOutNames;
120 outputTypes["r_shannoneven"] = tempOutNames;
121 outputTypes["r_shannonrange"] = tempOutNames;
122 outputTypes["r_heip"] = tempOutNames;
123 outputTypes["r_smithwilson"] = tempOutNames;
124 outputTypes["r_npshannon"] = tempOutNames;
125 outputTypes["r_simpson"] = tempOutNames;
126 outputTypes["r_simpsoneven"] = tempOutNames;
127 outputTypes["r_invsimpson"] = tempOutNames;
128 outputTypes["r_bootstrap"] = tempOutNames;
129 outputTypes["r_coverage"] = tempOutNames;
130 outputTypes["r_nseqs"] = tempOutNames;
132 catch(exception& e) {
133 m->errorOut(e, "RareFactCommand", "RareFactCommand");
137 //**********************************************************************************************************************
138 RareFactCommand::RareFactCommand(string option) {
140 abort = false; calledHelp = false;
143 //allow user to run help
144 if(option == "help") { help(); abort = true; calledHelp = true; }
145 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
148 vector<string> myArray = setParameters();
150 OptionParser parser(option);
151 map<string,string> parameters = parser.getParameters();
152 map<string,string>::iterator it;
154 ValidParameters validParameter;
156 //check to make sure all parameters are valid for command
157 for (it = parameters.begin(); it != parameters.end(); it++) {
158 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
161 //initialize outputTypes
162 vector<string> tempOutNames;
163 outputTypes["rarefaction"] = tempOutNames;
164 outputTypes["r_chao"] = tempOutNames;
165 outputTypes["r_ace"] = tempOutNames;
166 outputTypes["r_jack"] = tempOutNames;
167 outputTypes["r_shannon"] = tempOutNames;
168 outputTypes["r_shannoneven"] = tempOutNames;
169 outputTypes["r_shannonrange"] = tempOutNames;
170 outputTypes["r_heip"] = tempOutNames;
171 outputTypes["r_smithwilson"] = tempOutNames;
172 outputTypes["r_npshannon"] = tempOutNames;
173 outputTypes["r_simpson"] = tempOutNames;
174 outputTypes["r_simpsoneven"] = tempOutNames;
175 outputTypes["r_invsimpson"] = tempOutNames;
176 outputTypes["r_bootstrap"] = tempOutNames;
177 outputTypes["r_coverage"] = tempOutNames;
178 outputTypes["r_nseqs"] = tempOutNames;
180 //if the user changes the input directory command factory will send this info to us in the output parameter
181 string inputDir = validParameter.validFile(parameters, "inputdir", false);
182 if (inputDir == "not found"){ inputDir = ""; }
185 it = parameters.find("shared");
186 //user has given a template file
187 if(it != parameters.end()){
188 path = m->hasPath(it->second);
189 //if the user has not given a path then, add inputdir. else leave path alone.
190 if (path == "") { parameters["shared"] = inputDir + it->second; }
193 it = parameters.find("rabund");
194 //user has given a template file
195 if(it != parameters.end()){
196 path = m->hasPath(it->second);
197 //if the user has not given a path then, add inputdir. else leave path alone.
198 if (path == "") { parameters["rabund"] = inputDir + it->second; }
201 it = parameters.find("sabund");
202 //user has given a template file
203 if(it != parameters.end()){
204 path = m->hasPath(it->second);
205 //if the user has not given a path then, add inputdir. else leave path alone.
206 if (path == "") { parameters["sabund"] = inputDir + it->second; }
209 it = parameters.find("list");
210 //user has given a template file
211 if(it != parameters.end()){
212 path = m->hasPath(it->second);
213 //if the user has not given a path then, add inputdir. else leave path alone.
214 if (path == "") { parameters["list"] = inputDir + it->second; }
218 //check for required parameters
219 listfile = validParameter.validFile(parameters, "list", true);
220 if (listfile == "not open") { listfile = ""; abort = true; }
221 else if (listfile == "not found") { listfile = ""; }
222 else { format = "list"; inputfile = listfile; m->setListFile(listfile); }
224 sabundfile = validParameter.validFile(parameters, "sabund", true);
225 if (sabundfile == "not open") { sabundfile = ""; abort = true; }
226 else if (sabundfile == "not found") { sabundfile = ""; }
227 else { format = "sabund"; inputfile = sabundfile; m->setSabundFile(sabundfile); }
229 rabundfile = validParameter.validFile(parameters, "rabund", true);
230 if (rabundfile == "not open") { rabundfile = ""; abort = true; }
231 else if (rabundfile == "not found") { rabundfile = ""; }
232 else { format = "rabund"; inputfile = rabundfile; m->setRabundFile(rabundfile); }
234 sharedfile = validParameter.validFile(parameters, "shared", true);
235 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
236 else if (sharedfile == "not found") { sharedfile = ""; }
237 else { format = "sharedfile"; inputfile = sharedfile; m->setSharedFile(sharedfile); }
239 if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) {
240 //is there are current file available for any of these?
241 //give priority to shared, then list, then rabund, then sabund
242 //if there is a current shared file, use it
243 sharedfile = m->getSharedFile();
244 if (sharedfile != "") { inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
246 listfile = m->getListFile();
247 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
249 rabundfile = m->getRabundFile();
250 if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
252 sabundfile = m->getSabundFile();
253 if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
255 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();
263 //if the user changes the output directory command factory will send this info to us in the output parameter
264 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
266 //check for optional parameter and set defaults
267 // ...at some point should added some additional type checking...
268 label = validParameter.validFile(parameters, "label", false);
269 if (label == "not found") { label = ""; }
271 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
272 else { allLines = 1; }
275 calc = validParameter.validFile(parameters, "calc", false);
276 if (calc == "not found") { calc = "sobs"; }
278 if (calc == "default") { calc = "sobs"; }
280 m->splitAtDash(calc, Estimators);
281 if (m->inUsersGroups("citation", Estimators)) {
282 ValidCalculators validCalc; validCalc.printCitations(Estimators);
283 //remove citation from list of calcs
284 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") { Estimators.erase(Estimators.begin()+i); break; } }
288 temp = validParameter.validFile(parameters, "freq", false); if (temp == "not found") { temp = "100"; }
289 m->mothurConvert(temp, freq);
291 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
292 m->mothurConvert(temp, abund);
294 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
295 m->mothurConvert(temp, nIters);
297 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
298 m->setProcessors(temp);
299 m->mothurConvert(temp, processors);
301 temp = validParameter.validFile(parameters, "alpha", false); if (temp == "not found") { temp = "1"; }
302 m->mothurConvert(temp, alpha);
304 if ((alpha != 0) && (alpha != 1) && (alpha != 2)) { m->mothurOut("[ERROR]: Not a valid alpha value. Valid values are 0, 1 and 2."); m->mothurOutEndLine(); abort=true; }
306 temp = validParameter.validFile(parameters, "groupmode", false); if (temp == "not found") { temp = "T"; }
307 groupMode = m->isTrue(temp);
311 catch(exception& e) {
312 m->errorOut(e, "RareFactCommand", "RareFactCommand");
316 //**********************************************************************************************************************
318 int RareFactCommand::execute(){
321 if (abort == true) { if (calledHelp) { return 0; } return 2; }
323 map<string, set<int> > labelToEnds;
324 if ((format != "sharedfile")) { inputFileNames.push_back(inputfile); }
325 else { inputFileNames = parseSharedFile(sharedfile, labelToEnds); format = "rabund"; }
327 if (m->control_pressed) { return 0; }
329 map<int, string> file2Group; //index in outputNames[i] -> group
330 for (int p = 0; p < inputFileNames.size(); p++) {
332 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p]));
334 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } m->clearGroups(); return 0; }
336 if (inputFileNames.size() > 1) {
337 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
340 ValidCalculators validCalculator;
342 map<string, string> variables;
343 variables["[filename]"] = fileNameRoot;
345 for (i=0; i<Estimators.size(); i++) {
346 if (validCalculator.isValidCalculator("rarefaction", Estimators[i]) == true) {
347 if (Estimators[i] == "sobs") {
348 rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(getOutputFileName("rarefaction",variables))));
349 outputNames.push_back(getOutputFileName("rarefaction",variables)); outputTypes["rarefaction"].push_back(getOutputFileName("rarefaction",variables));
350 }else if (Estimators[i] == "chao") {
351 rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(getOutputFileName("r_chao",variables))));
352 outputNames.push_back(getOutputFileName("r_chao",variables)); outputTypes["r_chao"].push_back(getOutputFileName("r_chao",variables));
353 }else if (Estimators[i] == "ace") {
356 rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(getOutputFileName("r_ace",variables))));
357 outputNames.push_back(getOutputFileName("r_ace",variables)); outputTypes["r_ace"].push_back(getOutputFileName("r_ace",variables));
358 }else if (Estimators[i] == "jack") {
359 rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(getOutputFileName("r_jack",variables))));
360 outputNames.push_back(getOutputFileName("r_jack",variables)); outputTypes["r_jack"].push_back(getOutputFileName("r_jack",variables));
361 }else if (Estimators[i] == "shannon") {
362 rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(getOutputFileName("r_shannon",variables))));
363 outputNames.push_back(getOutputFileName("r_shannon",variables)); outputTypes["r_shannon"].push_back(getOutputFileName("r_shannon",variables));
364 }else if (Estimators[i] == "shannoneven") {
365 rDisplays.push_back(new RareDisplay(new ShannonEven(), new ThreeColumnFile(getOutputFileName("r_shannoneven",variables))));
366 outputNames.push_back(getOutputFileName("r_shannoneven",variables)); outputTypes["r_shannoneven"].push_back(getOutputFileName("r_shannoneven",variables));
367 }else if (Estimators[i] == "heip") {
368 rDisplays.push_back(new RareDisplay(new Heip(), new ThreeColumnFile(getOutputFileName("r_heip",variables))));
369 outputNames.push_back(getOutputFileName("r_heip",variables)); outputTypes["r_heip"].push_back(getOutputFileName("r_heip",variables));
370 }else if (Estimators[i] == "r_shannonrange") {
371 rDisplays.push_back(new RareDisplay(new RangeShannon(alpha), new ThreeColumnFile(getOutputFileName("r_shannonrange", variables))));
372 outputNames.push_back(getOutputFileName("r_shannonrange", variables)); outputTypes["r_shannoneven"].push_back(getOutputFileName("r_shannonrange", variables));
373 }else if (Estimators[i] == "smithwilson") {
374 rDisplays.push_back(new RareDisplay(new SmithWilson(), new ThreeColumnFile(getOutputFileName("r_smithwilson",variables))));
375 outputNames.push_back(getOutputFileName("r_smithwilson",variables)); outputTypes["r_smithwilson"].push_back(getOutputFileName("r_smithwilson",variables));
376 }else if (Estimators[i] == "npshannon") {
377 rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(getOutputFileName("r_npshannon",variables))));
378 outputNames.push_back(getOutputFileName("r_npshannon",variables)); outputTypes["r_npshannon"].push_back(getOutputFileName("r_npshannon",variables));
379 }else if (Estimators[i] == "simpson") {
380 rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(getOutputFileName("r_simpson",variables))));
381 outputNames.push_back(getOutputFileName("r_simpson",variables)); outputTypes["r_simpson"].push_back(getOutputFileName("r_simpson",variables));
382 }else if (Estimators[i] == "simpsoneven") {
383 rDisplays.push_back(new RareDisplay(new SimpsonEven(), new ThreeColumnFile(getOutputFileName("r_simpsoneven",variables))));
384 outputNames.push_back(getOutputFileName("r_simpsoneven",variables)); outputTypes["r_simpsoneven"].push_back(getOutputFileName("r_simpsoneven",variables));
385 }else if (Estimators[i] == "invsimpson") {
386 rDisplays.push_back(new RareDisplay(new InvSimpson(), new ThreeColumnFile(getOutputFileName("r_invsimpson",variables))));
387 outputNames.push_back(getOutputFileName("r_invsimpson",variables)); outputTypes["r_invsimpson"].push_back(getOutputFileName("r_invsimpson",variables));
388 }else if (Estimators[i] == "bootstrap") {
389 rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(getOutputFileName("r_bootstrap",variables))));
390 outputNames.push_back(getOutputFileName("r_bootstrap",variables)); outputTypes["r_bootstrap"].push_back(getOutputFileName("r_bootstrap",variables));
391 }else if (Estimators[i] == "coverage") {
392 rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(getOutputFileName("r_coverage",variables))));
393 outputNames.push_back(getOutputFileName("r_coverage",variables)); outputTypes["r_coverage"].push_back(getOutputFileName("r_coverage",variables));
394 }else if (Estimators[i] == "nseqs") {
395 rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(getOutputFileName("r_nseqs",variables))));
396 outputNames.push_back(getOutputFileName("r_nseqs",variables)); outputTypes["r_nseqs"].push_back(getOutputFileName("r_nseqs",variables));
398 if (inputFileNames.size() > 1) { file2Group[outputNames.size()-1] = groups[p]; }
403 //if the users entered no valid calculators don't execute command
404 if (rDisplays.size() == 0) { for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; } return 0; }
406 input = new InputData(inputFileNames[p], format);
407 order = input->getOrderVector();
408 string lastLabel = order->getLabel();
410 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
411 set<string> processedLabels;
412 set<string> userLabels = labels;
414 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++) { m->mothurRemove(outputNames[i]); } return 0; }
416 //as long as you are not at the end of the file or done wih the lines you want
417 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
419 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++) { m->mothurRemove(outputNames[i]); } return 0; }
422 if(allLines == 1 || labels.count(order->getLabel()) == 1){
424 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
425 map<string, set<int> >::iterator itEndings = labelToEnds.find(order->getLabel());
427 if (itEndings != labelToEnds.end()) { ends = itEndings->second; }
428 rCurve = new Rarefact(order, rDisplays, processors, ends);
429 rCurve->getCurve(freq, nIters);
432 processedLabels.insert(order->getLabel());
433 userLabels.erase(order->getLabel());
436 if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
437 string saveLabel = order->getLabel();
440 order = (input->getOrderVector(lastLabel));
442 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
443 map<string, set<int> >::iterator itEndings = labelToEnds.find(order->getLabel());
445 if (itEndings != labelToEnds.end()) { ends = itEndings->second; }
446 rCurve = new Rarefact(order, rDisplays, processors, ends);
448 rCurve->getCurve(freq, nIters);
451 processedLabels.insert(order->getLabel());
452 userLabels.erase(order->getLabel());
454 //restore real lastlabel to save below
455 order->setLabel(saveLabel);
458 lastLabel = order->getLabel();
461 order = (input->getOrderVector());
464 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++) { m->mothurRemove(outputNames[i]); } return 0; }
466 //output error messages about any remaining user labels
467 set<string>::iterator it;
468 bool needToRun = false;
469 for (it = userLabels.begin(); it != userLabels.end(); it++) {
470 m->mothurOut("Your file does not include the label " + *it);
471 if (processedLabels.count(lastLabel) != 1) {
472 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
475 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
479 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++) { m->mothurRemove(outputNames[i]); } return 0; }
481 //run last label if you need to
482 if (needToRun == true) {
483 if (order != NULL) { delete order; }
484 order = (input->getOrderVector(lastLabel));
486 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
487 map<string, set<int> >::iterator itEndings = labelToEnds.find(order->getLabel());
489 if (itEndings != labelToEnds.end()) { ends = itEndings->second; }
490 rCurve = new Rarefact(order, rDisplays, processors, ends);
492 rCurve->getCurve(freq, nIters);
499 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
505 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
507 //create summary file containing all the groups data for each label - this function just combines the info from the files already created.
508 if ((sharedfile != "") && (groupMode)) { outputNames = createGroupFile(outputNames, file2Group); }
510 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
512 m->mothurOutEndLine();
513 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
514 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
515 m->mothurOutEndLine();
519 catch(exception& e) {
520 m->errorOut(e, "RareFactCommand", "execute");
524 //**********************************************************************************************************************
525 vector<string> RareFactCommand::createGroupFile(vector<string>& outputNames, map<int, string> file2Group) {
528 vector<string> newFileNames;
530 //find different types of files
531 map<string, map<string, string> > typesFiles;
532 map<string, vector< vector<string> > > fileLabels; //combofile name to labels. each label is a vector because it may be unique lci hci.
533 vector<string> groupNames;
534 for (int i = 0; i < outputNames.size(); i++) {
536 string extension = m->getExtension(outputNames[i]);
537 string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "groups" + extension;
538 m->mothurRemove(combineFileName); //remove old file
541 m->openInputFile(outputNames[i], in);
543 string labels = m->getline(in);
545 istringstream iss (labels,istringstream::in);
546 string newLabel = ""; vector<string> theseLabels;
547 while(!iss.eof()) { iss >> newLabel; m->gobble(iss); theseLabels.push_back(newLabel); }
548 vector< vector<string> > allLabels;
549 vector<string> thisSet; thisSet.push_back(theseLabels[0]); allLabels.push_back(thisSet); thisSet.clear(); //makes "numSampled" its own grouping
550 for (int j = 1; j < theseLabels.size()-1; j++) {
551 if (theseLabels[j+1] == "lci") {
552 thisSet.push_back(theseLabels[j]);
553 thisSet.push_back(theseLabels[j+1]);
554 thisSet.push_back(theseLabels[j+2]);
556 }else{ //no lci or hci for this calc.
557 thisSet.push_back(theseLabels[j]);
559 allLabels.push_back(thisSet);
562 fileLabels[combineFileName] = allLabels;
564 map<string, map<string, string> >::iterator itfind = typesFiles.find(extension);
565 if (itfind != typesFiles.end()) {
566 (itfind->second)[outputNames[i]] = file2Group[i];
568 map<string, string> temp;
569 temp[outputNames[i]] = file2Group[i];
570 typesFiles[extension] = temp;
572 if (!(m->inUsersGroups(file2Group[i], groupNames))) { groupNames.push_back(file2Group[i]); }
576 //for each type create a combo file
578 for (map<string, map<string, string> >::iterator it = typesFiles.begin(); it != typesFiles.end(); it++) {
581 string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "groups" + it->first;
582 m->openOutputFileAppend(combineFileName, out);
583 newFileNames.push_back(combineFileName);
584 map<string, string> thisTypesFiles = it->second; //it->second maps filename to group
585 set<int> numSampledSet;
587 //open each type summary file
588 map<string, map<int, vector< vector<string> > > > files; //maps file name to lines in file
590 for (map<string, string>::iterator itFileNameGroup = thisTypesFiles.begin(); itFileNameGroup != thisTypesFiles.end(); itFileNameGroup++) {
592 string thisfilename = itFileNameGroup->first;
593 string group = itFileNameGroup->second;
596 m->openInputFile(thisfilename, temp);
598 //read through first line - labels
599 m->getline(temp); m->gobble(temp);
601 map<int, vector< vector<string> > > thisFilesLines;
604 temp >> numSampled; m->gobble(temp);
606 vector< vector<string> > theseReads;
607 vector<string> thisSet; thisSet.push_back(toString(numSampled)); theseReads.push_back(thisSet); thisSet.clear();
608 for (int k = 1; k < fileLabels[combineFileName].size(); k++) { //output thing like 0.03-A lci-A hci-A
609 vector<string> reads;
611 for (int l = 0; l < fileLabels[combineFileName][k].size(); l++) { //output modified labels
612 temp >> next; m->gobble(temp);
613 reads.push_back(next);
615 theseReads.push_back(reads);
617 thisFilesLines[numSampled] = theseReads;
620 numSampledSet.insert(numSampled);
623 files[group] = thisFilesLines;
625 //save longest file for below
626 if (maxLines < thisFilesLines.size()) { maxLines = thisFilesLines.size(); }
629 m->mothurRemove(thisfilename);
632 //output new labels line
633 out << fileLabels[combineFileName][0][0] << '\t';
634 for (int k = 1; k < fileLabels[combineFileName].size(); k++) { //output thing like 0.03-A lci-A hci-A
635 for (int n = 0; n < groupNames.size(); n++) { // for each group
636 for (int l = 0; l < fileLabels[combineFileName][k].size(); l++) { //output modified labels
637 out << fileLabels[combineFileName][k][l] << '-' << groupNames[n] << '\t';
644 for (set<int>::iterator itNumSampled = numSampledSet.begin(); itNumSampled != numSampledSet.end(); itNumSampled++) {
646 out << (*itNumSampled) << '\t';
648 if (m->control_pressed) { break; }
650 for (int k = 1; k < fileLabels[combineFileName].size(); k++) { //each chunk
651 //grab data for each group
652 for (int n = 0; n < groupNames.size(); n++) {
653 string group = groupNames[n];
655 map<int, vector< vector<string> > >::iterator itLine = files[group].find(*itNumSampled);
656 if (itLine != files[group].end()) {
657 for (int l = 0; l < (itLine->second)[k].size(); l++) {
658 out << (itLine->second)[k][l] << '\t';
662 for (int l = 0; l < fileLabels[combineFileName][k].size(); l++) {
673 //return combine file name
677 catch(exception& e) {
678 m->errorOut(e, "RareFactCommand", "createGroupFile");
682 //**********************************************************************************************************************
683 vector<string> RareFactCommand::parseSharedFile(string filename, map<string, set<int> >& label2Ends) {
685 vector<string> filenames;
687 map<string, ofstream*> filehandles;
688 map<string, ofstream*>::iterator it3;
690 input = new InputData(filename, "sharedfile");
691 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
693 string sharedFileRoot = m->getRootName(filename);
695 //clears file before we start to write to it below
696 for (int i=0; i<lookup.size(); i++) {
697 m->mothurRemove((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
698 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
702 for (int i=0; i<lookup.size(); i++) {
704 filehandles[lookup[i]->getGroup()] = temp;
705 groups.push_back(lookup[i]->getGroup());
708 while(lookup[0] != NULL) {
710 for (int i = 0; i < lookup.size(); i++) {
711 RAbundVector rav = lookup[i]->getRAbundVector();
712 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
713 rav.print(*(filehandles[lookup[i]->getGroup()]));
714 (*(filehandles[lookup[i]->getGroup()])).close();
715 label2Ends[lookup[i]->getLabel()].insert(rav.getNumSeqs());
718 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
719 lookup = input->getSharedRAbundVectors();
723 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
732 catch(exception& e) {
733 m->errorOut(e, "RareFactCommand", "parseSharedFile");
737 //**********************************************************************************************************************