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"
28 //**********************************************************************************************************************
29 vector<string> RareFactCommand::setParameters(){
31 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(plist);
32 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(prabund);
33 CommandParameter psabund("sabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(psabund);
34 CommandParameter pshared("shared", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(pshared);
35 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
36 CommandParameter pfreq("freq", "Number", "", "100", "", "", "",false,false); parameters.push_back(pfreq);
37 CommandParameter piters("iters", "Number", "", "1000", "", "", "",false,false); parameters.push_back(piters);
38 CommandParameter pcalc("calc", "Multiple", "sobs-chao-nseqs-coverage-ace-jack-shannon-shannoneven-npshannon-heip-smithwilson-simpson-simpsoneven-invsimpson-bootstrap", "sobs", "", "", "",true,false); parameters.push_back(pcalc);
39 CommandParameter pabund("abund", "Number", "", "10", "", "", "",false,false); parameters.push_back(pabund);
40 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
41 CommandParameter pgroupmode("groupmode", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pgroupmode);
42 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
43 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
45 vector<string> myArray;
46 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
50 m->errorOut(e, "RareFactCommand", "setParameters");
54 //**********************************************************************************************************************
55 string RareFactCommand::getHelpString(){
57 ValidCalculators validCalculator;
58 string helpString = "";
59 helpString += "The rarefaction.single command parameters are list, sabund, rabund, shared, label, iters, freq, calc, processors and abund. list, sabund, rabund or shared is required unless you have a valid current file. \n";
60 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";
61 helpString += "The processors parameter allows you to specify the number of processors to use. The default is 1.\n";
62 helpString += "The rarefaction.single command should be in the following format: \n";
63 helpString += "rarefaction.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n";
64 helpString += "Example rarefaction.single(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-rchao-race-rjack-rbootstrap-rshannon-rnpshannon-rsimpson).\n";
65 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";
66 validCalculator.printCalc("rarefaction");
67 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";
68 helpString += "The label parameter is used to analyze specific labels in your input.\n";
69 helpString += "Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n";
73 m->errorOut(e, "RareFactCommand", "getHelpString");
77 //**********************************************************************************************************************
78 string RareFactCommand::getOutputFileNameTag(string type, string inputName=""){
80 string outputFileName = "";
81 map<string, vector<string> >::iterator it;
83 //is this a type this command creates
84 it = outputTypes.find(type);
85 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
87 if (type == "rarefaction") { outputFileName = "rarefaction"; }
88 else if (type == "r_chao") { outputFileName = "r_chao"; }
89 else if (type == "r_ace") { outputFileName = "r_ace"; }
90 else if (type == "r_jack") { outputFileName = "r_jack"; }
91 else if (type == "r_shannon") { outputFileName = "r_shannon"; }
92 else if (type == "r_shannoneven") { outputFileName = "r_shannoneven"; }
93 else if (type == "r_smithwilson") { outputFileName = "r_smithwilson"; }
94 else if (type == "r_npshannon") { outputFileName = "r_npshannon"; }
95 else if (type == "r_simpson") { outputFileName = "r_simpson"; }
96 else if (type == "r_simpsoneven") { outputFileName = "r_simpsoneven"; }
97 else if (type == "r_invsimpson") { outputFileName = "r_invsimpson"; }
98 else if (type == "r_bootstrap") { outputFileName = "r_bootstrap"; }
99 else if (type == "r_coverage") { outputFileName = "r_coverage"; }
100 else if (type == "r_nseqs") { outputFileName = "r_nseqs"; }
101 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
103 return outputFileName;
105 catch(exception& e) {
106 m->errorOut(e, "RareFactCommand", "getOutputFileNameTag");
111 //**********************************************************************************************************************
112 RareFactCommand::RareFactCommand(){
114 abort = true; calledHelp = true;
116 vector<string> tempOutNames;
117 outputTypes["rarefaction"] = tempOutNames;
118 outputTypes["r_chao"] = tempOutNames;
119 outputTypes["r_ace"] = tempOutNames;
120 outputTypes["r_jack"] = tempOutNames;
121 outputTypes["r_shannon"] = tempOutNames;
122 outputTypes["r_shannoneven"] = tempOutNames;
123 outputTypes["r_heip"] = tempOutNames;
124 outputTypes["r_smithwilson"] = tempOutNames;
125 outputTypes["r_npshannon"] = tempOutNames;
126 outputTypes["r_simpson"] = tempOutNames;
127 outputTypes["r_simpsoneven"] = tempOutNames;
128 outputTypes["r_invsimpson"] = tempOutNames;
129 outputTypes["r_bootstrap"] = tempOutNames;
130 outputTypes["r_coverage"] = tempOutNames;
131 outputTypes["r_nseqs"] = tempOutNames;
133 catch(exception& e) {
134 m->errorOut(e, "RareFactCommand", "RareFactCommand");
138 //**********************************************************************************************************************
139 RareFactCommand::RareFactCommand(string option) {
141 abort = false; calledHelp = false;
144 //allow user to run help
145 if(option == "help") { help(); abort = true; calledHelp = true; }
146 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
149 vector<string> myArray = setParameters();
151 OptionParser parser(option);
152 map<string,string> parameters = parser.getParameters();
153 map<string,string>::iterator it;
155 ValidParameters validParameter;
157 //check to make sure all parameters are valid for command
158 for (it = parameters.begin(); it != parameters.end(); it++) {
159 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
162 //initialize outputTypes
163 vector<string> tempOutNames;
164 outputTypes["rarefaction"] = tempOutNames;
165 outputTypes["r_chao"] = tempOutNames;
166 outputTypes["r_ace"] = tempOutNames;
167 outputTypes["r_jack"] = tempOutNames;
168 outputTypes["r_shannon"] = tempOutNames;
169 outputTypes["r_shannoneven"] = 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, "groupmode", false); if (temp == "not found") { temp = "T"; }
302 groupMode = m->isTrue(temp);
306 catch(exception& e) {
307 m->errorOut(e, "RareFactCommand", "RareFactCommand");
311 //**********************************************************************************************************************
313 int RareFactCommand::execute(){
316 if (abort == true) { if (calledHelp) { return 0; } return 2; }
318 map<string, set<int> > labelToEnds;
319 if ((format != "sharedfile")) { inputFileNames.push_back(inputfile); }
320 else { inputFileNames = parseSharedFile(sharedfile, labelToEnds); format = "rabund"; }
322 if (m->control_pressed) { return 0; }
324 map<int, string> file2Group; //index in outputNames[i] -> group
325 for (int p = 0; p < inputFileNames.size(); p++) {
327 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p]));
329 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } m->clearGroups(); return 0; }
331 if (inputFileNames.size() > 1) {
332 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
335 ValidCalculators validCalculator;
338 for (i=0; i<Estimators.size(); i++) {
339 if (validCalculator.isValidCalculator("rarefaction", Estimators[i]) == true) {
340 if (Estimators[i] == "sobs") {
341 rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("rarefaction"))));
342 outputNames.push_back(fileNameRoot+getOutputFileNameTag("rarefaction")); outputTypes["rarefaction"].push_back(fileNameRoot+getOutputFileNameTag("rarefaction"));
343 }else if (Estimators[i] == "chao") {
344 rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_chao"))));
345 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_chao")); outputTypes["r_chao"].push_back(fileNameRoot+getOutputFileNameTag("r_chao"));
346 }else if (Estimators[i] == "ace") {
349 rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_ace"))));
350 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_ace")); outputTypes["r_ace"].push_back(fileNameRoot+getOutputFileNameTag("r_ace"));
351 }else if (Estimators[i] == "jack") {
352 rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_jack"))));
353 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_jack")); outputTypes["r_jack"].push_back(fileNameRoot+getOutputFileNameTag("r_jack"));
354 }else if (Estimators[i] == "shannon") {
355 rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_shannon"))));
356 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_shannon")); outputTypes["r_shannon"].push_back(fileNameRoot+getOutputFileNameTag("r_shannon"));
357 }else if (Estimators[i] == "shannoneven") {
358 rDisplays.push_back(new RareDisplay(new ShannonEven(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_shannoneven"))));
359 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_shannoneven")); outputTypes["r_shannoneven"].push_back(fileNameRoot+getOutputFileNameTag("r_shannoneven"));
360 }else if (Estimators[i] == "heip") {
361 rDisplays.push_back(new RareDisplay(new Heip(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_heip"))));
362 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_heip")); outputTypes["r_heip"].push_back(fileNameRoot+getOutputFileNameTag("r_heip"));
363 }else if (Estimators[i] == "smithwilson") {
364 rDisplays.push_back(new RareDisplay(new SmithWilson(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_smithwilson"))));
365 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_smithwilson")); outputTypes["r_smithwilson"].push_back(fileNameRoot+getOutputFileNameTag("r_smithwilson"));
366 }else if (Estimators[i] == "npshannon") {
367 rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_npshannon"))));
368 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_npshannon")); outputTypes["r_npshannon"].push_back(fileNameRoot+getOutputFileNameTag("r_npshannon"));
369 }else if (Estimators[i] == "simpson") {
370 rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_simpson"))));
371 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_simpson")); outputTypes["r_simpson"].push_back(fileNameRoot+getOutputFileNameTag("r_simpson"));
372 }else if (Estimators[i] == "simpsoneven") {
373 rDisplays.push_back(new RareDisplay(new SimpsonEven(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_simpsoneven"))));
374 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_simpsoneven")); outputTypes["r_simpsoneven"].push_back(fileNameRoot+getOutputFileNameTag("r_simpsoneven"));
375 }else if (Estimators[i] == "invsimpson") {
376 rDisplays.push_back(new RareDisplay(new InvSimpson(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_invsimpson"))));
377 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_invsimpson")); outputTypes["r_invsimpson"].push_back(fileNameRoot+getOutputFileNameTag("r_invsimpson"));
378 }else if (Estimators[i] == "bootstrap") {
379 rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_bootstrap"))));
380 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_bootstrap")); outputTypes["r_bootstrap"].push_back(fileNameRoot+getOutputFileNameTag("r_bootstrap"));
381 }else if (Estimators[i] == "coverage") {
382 rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_coverage"))));
383 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_coverage")); outputTypes["r_coverage"].push_back(fileNameRoot+getOutputFileNameTag("r_coverage"));
384 }else if (Estimators[i] == "nseqs") {
385 rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_nseqs"))));
386 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_nseqs")); outputTypes["r_nseqs"].push_back(fileNameRoot+getOutputFileNameTag("r_nseqs"));
388 if (inputFileNames.size() > 1) { file2Group[outputNames.size()-1] = groups[p]; }
393 //if the users entered no valid calculators don't execute command
394 if (rDisplays.size() == 0) { for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; } return 0; }
396 input = new InputData(inputFileNames[p], format);
397 order = input->getOrderVector();
398 string lastLabel = order->getLabel();
400 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
401 set<string> processedLabels;
402 set<string> userLabels = labels;
404 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; }
406 //as long as you are not at the end of the file or done wih the lines you want
407 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
409 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; }
412 if(allLines == 1 || labels.count(order->getLabel()) == 1){
414 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
415 map<string, set<int> >::iterator itEndings = labelToEnds.find(order->getLabel());
417 if (itEndings != labelToEnds.end()) { ends = itEndings->second; }
418 rCurve = new Rarefact(order, rDisplays, processors, ends);
419 rCurve->getCurve(freq, nIters);
422 processedLabels.insert(order->getLabel());
423 userLabels.erase(order->getLabel());
426 if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
427 string saveLabel = order->getLabel();
430 order = (input->getOrderVector(lastLabel));
432 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
433 map<string, set<int> >::iterator itEndings = labelToEnds.find(order->getLabel());
435 if (itEndings != labelToEnds.end()) { ends = itEndings->second; }
436 rCurve = new Rarefact(order, rDisplays, processors, ends);
438 rCurve->getCurve(freq, nIters);
441 processedLabels.insert(order->getLabel());
442 userLabels.erase(order->getLabel());
444 //restore real lastlabel to save below
445 order->setLabel(saveLabel);
448 lastLabel = order->getLabel();
451 order = (input->getOrderVector());
454 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; }
456 //output error messages about any remaining user labels
457 set<string>::iterator it;
458 bool needToRun = false;
459 for (it = userLabels.begin(); it != userLabels.end(); it++) {
460 m->mothurOut("Your file does not include the label " + *it);
461 if (processedLabels.count(lastLabel) != 1) {
462 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
465 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
469 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; }
471 //run last label if you need to
472 if (needToRun == true) {
473 if (order != NULL) { delete order; }
474 order = (input->getOrderVector(lastLabel));
476 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
477 map<string, set<int> >::iterator itEndings = labelToEnds.find(order->getLabel());
479 if (itEndings != labelToEnds.end()) { ends = itEndings->second; }
480 rCurve = new Rarefact(order, rDisplays, processors, ends);
482 rCurve->getCurve(freq, nIters);
489 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
495 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
497 //create summary file containing all the groups data for each label - this function just combines the info from the files already created.
498 if ((sharedfile != "") && (groupMode)) { outputNames = createGroupFile(outputNames, file2Group); }
500 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
502 m->mothurOutEndLine();
503 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
504 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
505 m->mothurOutEndLine();
509 catch(exception& e) {
510 m->errorOut(e, "RareFactCommand", "execute");
514 //**********************************************************************************************************************
515 vector<string> RareFactCommand::createGroupFile(vector<string>& outputNames, map<int, string> file2Group) {
518 vector<string> newFileNames;
520 //find different types of files
521 map<string, map<string, string> > typesFiles;
522 map<string, vector< vector<string> > > fileLabels; //combofile name to labels. each label is a vector because it may be unique lci hci.
523 vector<string> groupNames;
524 for (int i = 0; i < outputNames.size(); i++) {
526 string extension = m->getExtension(outputNames[i]);
527 string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "groups" + extension;
528 m->mothurRemove(combineFileName); //remove old file
531 m->openInputFile(outputNames[i], in);
533 string labels = m->getline(in);
535 istringstream iss (labels,istringstream::in);
536 string newLabel = ""; vector<string> theseLabels;
537 while(!iss.eof()) { iss >> newLabel; m->gobble(iss); theseLabels.push_back(newLabel); }
538 vector< vector<string> > allLabels;
539 vector<string> thisSet; thisSet.push_back(theseLabels[0]); allLabels.push_back(thisSet); thisSet.clear(); //makes "numSampled" its own grouping
540 for (int j = 1; j < theseLabels.size()-1; j++) {
541 if (theseLabels[j+1] == "lci") {
542 thisSet.push_back(theseLabels[j]);
543 thisSet.push_back(theseLabels[j+1]);
544 thisSet.push_back(theseLabels[j+2]);
546 }else{ //no lci or hci for this calc.
547 thisSet.push_back(theseLabels[j]);
549 allLabels.push_back(thisSet);
552 fileLabels[combineFileName] = allLabels;
554 map<string, map<string, string> >::iterator itfind = typesFiles.find(extension);
555 if (itfind != typesFiles.end()) {
556 (itfind->second)[outputNames[i]] = file2Group[i];
558 map<string, string> temp;
559 temp[outputNames[i]] = file2Group[i];
560 typesFiles[extension] = temp;
562 if (!(m->inUsersGroups(file2Group[i], groupNames))) { groupNames.push_back(file2Group[i]); }
565 //for each type create a combo file
567 for (map<string, map<string, string> >::iterator it = typesFiles.begin(); it != typesFiles.end(); it++) {
570 string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "groups" + it->first;
571 m->openOutputFileAppend(combineFileName, out);
572 newFileNames.push_back(combineFileName);
573 map<string, string> thisTypesFiles = it->second; //it->second maps filename to group
574 set<int> numSampledSet;
576 //open each type summary file
577 map<string, map<int, vector< vector<string> > > > files; //maps file name to lines in file
579 for (map<string, string>::iterator itFileNameGroup = thisTypesFiles.begin(); itFileNameGroup != thisTypesFiles.end(); itFileNameGroup++) {
581 string thisfilename = itFileNameGroup->first;
582 string group = itFileNameGroup->second;
585 m->openInputFile(thisfilename, temp);
587 //read through first line - labels
588 m->getline(temp); m->gobble(temp);
590 map<int, vector< vector<string> > > thisFilesLines;
593 temp >> numSampled; m->gobble(temp);
595 vector< vector<string> > theseReads;
596 vector<string> thisSet; thisSet.push_back(toString(numSampled)); theseReads.push_back(thisSet); thisSet.clear();
597 for (int k = 1; k < fileLabels[combineFileName].size(); k++) { //output thing like 0.03-A lci-A hci-A
598 vector<string> reads;
600 for (int l = 0; l < fileLabels[combineFileName][k].size(); l++) { //output modified labels
601 temp >> next; m->gobble(temp);
602 reads.push_back(next);
604 theseReads.push_back(reads);
606 thisFilesLines[numSampled] = theseReads;
609 numSampledSet.insert(numSampled);
612 files[group] = thisFilesLines;
614 //save longest file for below
615 if (maxLines < thisFilesLines.size()) { maxLines = thisFilesLines.size(); }
618 m->mothurRemove(thisfilename);
621 //output new labels line
622 out << fileLabels[combineFileName][0][0] << '\t';
623 for (int k = 1; k < fileLabels[combineFileName].size(); k++) { //output thing like 0.03-A lci-A hci-A
624 for (int n = 0; n < groupNames.size(); n++) { // for each group
625 for (int l = 0; l < fileLabels[combineFileName][k].size(); l++) { //output modified labels
626 out << fileLabels[combineFileName][k][l] << '-' << groupNames[n] << '\t';
633 for (set<int>::iterator itNumSampled = numSampledSet.begin(); itNumSampled != numSampledSet.end(); itNumSampled++) {
635 out << (*itNumSampled) << '\t';
637 if (m->control_pressed) { break; }
639 for (int k = 1; k < fileLabels[combineFileName].size(); k++) { //each chunk
640 //grab data for each group
641 for (map<string, map<int, vector< vector<string> > > >::iterator itFileNameGroup = files.begin(); itFileNameGroup != files.end(); itFileNameGroup++) {
643 string group = itFileNameGroup->first;
645 map<int, vector< vector<string> > >::iterator itLine = files[group].find(*itNumSampled);
646 if (itLine != files[group].end()) {
647 for (int l = 0; l < (itLine->second)[k].size(); l++) {
648 out << (itLine->second)[k][l] << '\t';
652 for (int l = 0; l < fileLabels[combineFileName][k].size(); l++) {
663 //return combine file name
667 catch(exception& e) {
668 m->errorOut(e, "RareFactCommand", "createGroupFile");
672 //**********************************************************************************************************************
673 vector<string> RareFactCommand::parseSharedFile(string filename, map<string, set<int> >& label2Ends) {
675 vector<string> filenames;
677 map<string, ofstream*> filehandles;
678 map<string, ofstream*>::iterator it3;
680 input = new InputData(filename, "sharedfile");
681 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
683 string sharedFileRoot = m->getRootName(filename);
685 //clears file before we start to write to it below
686 for (int i=0; i<lookup.size(); i++) {
687 m->mothurRemove((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
688 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
692 for (int i=0; i<lookup.size(); i++) {
694 filehandles[lookup[i]->getGroup()] = temp;
695 groups.push_back(lookup[i]->getGroup());
698 while(lookup[0] != NULL) {
700 for (int i = 0; i < lookup.size(); i++) {
701 RAbundVector rav = lookup[i]->getRAbundVector();
702 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
703 rav.print(*(filehandles[lookup[i]->getGroup()]));
704 (*(filehandles[lookup[i]->getGroup()])).close();
705 label2Ends[lookup[i]->getLabel()].insert(rav.getNumSeqs());
708 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
709 lookup = input->getSharedRAbundVectors();
713 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
722 catch(exception& e) {
723 m->errorOut(e, "RareFactCommand", "parseSharedFile");
727 //**********************************************************************************************************************