5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "summarycommand.h"
15 #include "bootstrap.h"
17 #include "simpsoneven.h"
18 #include "invsimpson.h"
19 #include "npshannon.h"
22 #include "smithwilson.h"
23 #include "shannoneven.h"
24 #include "jackknife.h"
28 #include "bergerparker.h"
30 #include "goodscoverage.h"
36 #include "subsample.h"
37 #include "shannonrange.h"
39 //**********************************************************************************************************************
40 vector<string> SummaryCommand::setParameters(){
42 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none","summary",false,false,true); parameters.push_back(plist);
43 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none","summary",false,false); parameters.push_back(prabund);
44 CommandParameter psabund("sabund", "InputTypes", "", "", "LRSS", "LRSS", "none","summary",false,false); parameters.push_back(psabund);
45 CommandParameter pshared("shared", "InputTypes", "", "", "LRSS", "LRSS", "none","summary",false,false,true); parameters.push_back(pshared);
46 CommandParameter psubsample("subsample", "String", "", "", "", "", "","",false,false); parameters.push_back(psubsample);
47 CommandParameter piters("iters", "Number", "", "1000", "", "", "","",false,false); parameters.push_back(piters);
48 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
49 CommandParameter pcalc("calc", "Multiple", "sobs-chao-nseqs-coverage-ace-jack-shannon-shannoneven-npshannon-heip-smithwilson-simpson-simpsoneven-invsimpson-bootstrap-geometric-qstat-logseries-bergerparker-bstick-goodscoverage-efron-boneh-solow-shen", "sobs-chao-ace-jack-shannon-npshannon-simpson-shannonrange", "", "", "","",true,false,true); parameters.push_back(pcalc);
50 CommandParameter palpha("alpha", "Multiple", "0-1-2", "1", "", "", "","",false,false,true); parameters.push_back(palpha);
51 CommandParameter pabund("abund", "Number", "", "10", "", "", "","",false,false); parameters.push_back(pabund);
52 CommandParameter psize("size", "Number", "", "0", "", "", "","",false,false); parameters.push_back(psize);
53 CommandParameter pgroupmode("groupmode", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(pgroupmode);
54 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
55 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
57 vector<string> myArray;
58 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
62 m->errorOut(e, "SummaryCommand", "setParameters");
66 //**********************************************************************************************************************
67 string SummaryCommand::getHelpString(){
69 string helpString = "";
70 ValidCalculators validCalculator;
71 helpString += "The summary.single command parameters are list, sabund, rabund, shared, subsample, iters, label, calc, abund and groupmode. list, sabund, rabund or shared is required unless you have a valid current file.\n";
72 helpString += "The summary.single command should be in the following format: \n";
73 helpString += "summary.single(label=yourLabel, calc=yourEstimators).\n";
74 helpString += "Example summary.single(label=unique-.01-.03, calc=sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson).\n";
75 helpString += validCalculator.printCalc("summary");
76 helpString += "The subsample parameter allows you to enter the size of the sample or you can set subsample=T and mothur will use the size of your smallest group in the case of a shared file. With a list, sabund or rabund file you must provide a subsample size.\n";
77 helpString += "The iters parameter allows you to choose the number of times you would like to run the subsample.\n";
78 helpString += "The default value calc is sobs-chao-ace-jack-shannon-npshannon-simpson\n";
79 helpString += "If you are running summary.single with a shared file and would like your summary results collated in one file, set groupmode=t. (Default=true).\n";
80 helpString += "The alpha parameter is used to set the alpha value for the shannonrange calculator.\n";
81 helpString += "The label parameter is used to analyze specific labels in your input.\n";
82 helpString += "Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabels).\n";
86 m->errorOut(e, "SummaryCommand", "getHelpString");
90 //**********************************************************************************************************************
91 string SummaryCommand::getOutputPattern(string type) {
95 if (type == "summary") { pattern = "[filename],summary-[filename],[tag],summary"; }
96 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
100 catch(exception& e) {
101 m->errorOut(e, "SummaryCommand", "getOutputPattern");
105 //**********************************************************************************************************************
106 SummaryCommand::SummaryCommand(){
108 abort = true; calledHelp = true;
110 vector<string> tempOutNames;
111 outputTypes["summary"] = tempOutNames;
113 catch(exception& e) {
114 m->errorOut(e, "SummaryCommand", "SummaryCommand");
118 //**********************************************************************************************************************
120 SummaryCommand::SummaryCommand(string option) {
122 abort = false; calledHelp = false;
125 //allow user to run help
126 if(option == "help") { help(); abort = true; calledHelp = true; }
127 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
130 vector<string> myArray = setParameters();
132 OptionParser parser(option);
133 map<string,string> parameters = parser.getParameters();
134 map<string,string>::iterator it;
136 ValidParameters validParameter;
138 //check to make sure all parameters are valid for command
139 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
140 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
143 //initialize outputTypes
144 vector<string> tempOutNames;
145 outputTypes["summary"] = tempOutNames;
147 //if the user changes the input directory command factory will send this info to us in the output parameter
148 string inputDir = validParameter.validFile(parameters, "inputdir", false);
149 if (inputDir == "not found"){ inputDir = ""; }
152 it = parameters.find("shared");
153 //user has given a template file
154 if(it != parameters.end()){
155 path = m->hasPath(it->second);
156 //if the user has not given a path then, add inputdir. else leave path alone.
157 if (path == "") { parameters["shared"] = inputDir + it->second; }
160 it = parameters.find("rabund");
161 //user has given a template file
162 if(it != parameters.end()){
163 path = m->hasPath(it->second);
164 //if the user has not given a path then, add inputdir. else leave path alone.
165 if (path == "") { parameters["rabund"] = inputDir + it->second; }
168 it = parameters.find("sabund");
169 //user has given a template file
170 if(it != parameters.end()){
171 path = m->hasPath(it->second);
172 //if the user has not given a path then, add inputdir. else leave path alone.
173 if (path == "") { parameters["sabund"] = inputDir + it->second; }
176 it = parameters.find("list");
177 //user has given a template file
178 if(it != parameters.end()){
179 path = m->hasPath(it->second);
180 //if the user has not given a path then, add inputdir. else leave path alone.
181 if (path == "") { parameters["list"] = inputDir + it->second; }
185 //check for required parameters
186 listfile = validParameter.validFile(parameters, "list", true);
187 if (listfile == "not open") { listfile = ""; abort = true; }
188 else if (listfile == "not found") { listfile = ""; }
189 else { format = "list"; inputfile = listfile; m->setListFile(listfile); }
191 sabundfile = validParameter.validFile(parameters, "sabund", true);
192 if (sabundfile == "not open") { sabundfile = ""; abort = true; }
193 else if (sabundfile == "not found") { sabundfile = ""; }
194 else { format = "sabund"; inputfile = sabundfile; m->setSabundFile(sabundfile); }
196 rabundfile = validParameter.validFile(parameters, "rabund", true);
197 if (rabundfile == "not open") { rabundfile = ""; abort = true; }
198 else if (rabundfile == "not found") { rabundfile = ""; }
199 else { format = "rabund"; inputfile = rabundfile; m->setRabundFile(rabundfile); }
201 sharedfile = validParameter.validFile(parameters, "shared", true);
202 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
203 else if (sharedfile == "not found") { sharedfile = ""; }
204 else { format = "sharedfile"; inputfile = sharedfile; m->setSharedFile(sharedfile); }
206 if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) {
207 //is there are current file available for any of these?
208 //give priority to shared, then list, then rabund, then sabund
209 //if there is a current shared file, use it
210 sharedfile = m->getSharedFile();
211 if (sharedfile != "") { inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
213 listfile = m->getListFile();
214 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
216 rabundfile = m->getRabundFile();
217 if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
219 sabundfile = m->getSabundFile();
220 if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
222 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();
230 //if the user changes the output directory command factory will send this info to us in the output parameter
231 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
233 //check for optional parameter and set defaults
234 // ...at some point should added some additional type checking...
235 label = validParameter.validFile(parameters, "label", false);
236 if (label == "not found") { label = ""; }
238 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
239 else { allLines = 1; }
242 calc = validParameter.validFile(parameters, "calc", false);
243 if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
245 if (calc == "default") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
247 m->splitAtDash(calc, Estimators);
248 if (m->inUsersGroups("citation", Estimators)) {
249 ValidCalculators validCalc; validCalc.printCitations(Estimators);
250 //remove citation from list of calcs
251 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") { Estimators.erase(Estimators.begin()+i); break; } }
255 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
256 m->mothurConvert(temp, abund);
258 temp = validParameter.validFile(parameters, "size", false); if (temp == "not found") { temp = "0"; }
259 m->mothurConvert(temp, size);
261 temp = validParameter.validFile(parameters, "groupmode", false); if (temp == "not found") { temp = "T"; }
262 groupMode = m->isTrue(temp);
264 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
265 m->mothurConvert(temp, iters);
267 temp = validParameter.validFile(parameters, "subsample", false); if (temp == "not found") { temp = "F"; }
268 if (m->isNumeric1(temp)) { m->mothurConvert(temp, subsampleSize); subsample = true; }
270 if (m->isTrue(temp)) { subsample = true; subsampleSize = -1; } //we will set it to smallest group later
271 else { subsample = false; subsampleSize = -1; }
274 temp = validParameter.validFile(parameters, "alpha", false); if (temp == "not found") { temp = "1"; }
275 m->mothurConvert(temp, alpha);
277 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; }
279 if (subsample == false) { iters = 0; }
281 //if you did not set a samplesize and are not using a sharedfile
282 if ((subsampleSize == -1) && (format != "sharedfile")) { m->mothurOut("[ERROR]: If you want to subsample with a list, rabund or sabund file, you must provide the sample size. You can do this by setting subsample=yourSampleSize.\n"); abort=true; }
287 catch(exception& e) {
288 m->errorOut(e, "SummaryCommand", "SummaryCommand");
292 //**********************************************************************************************************************
294 int SummaryCommand::execute(){
297 if (abort == true) { if (calledHelp) { return 0; } return 2; }
299 if ((format != "sharedfile")) { inputFileNames.push_back(inputfile); }
300 else { inputFileNames = parseSharedFile(sharedfile); format = "rabund"; }
302 if (m->control_pressed) { return 0; }
306 map<string, string> groupIndex;
308 for (int p = 0; p < inputFileNames.size(); p++) {
313 map<string, string> variables;
314 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p]));
315 string fileNameRoot = getOutputFileName("summary",variables);
316 variables["[tag]"] = "ave-std";
317 string fileNameAve = getOutputFileName("summary",variables);
318 outputNames.push_back(fileNameRoot); outputTypes["summary"].push_back(fileNameRoot);
320 if (inputFileNames.size() > 1) {
321 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
322 groupIndex[fileNameRoot] = groups[p];
325 sumCalculators.clear();
327 ValidCalculators validCalculator;
329 for (int i=0; i<Estimators.size(); i++) {
330 if (validCalculator.isValidCalculator("summary", Estimators[i]) == true) {
331 if(Estimators[i] == "sobs"){
332 sumCalculators.push_back(new Sobs());
333 }else if(Estimators[i] == "chao"){
334 sumCalculators.push_back(new Chao1());
335 }else if(Estimators[i] == "coverage"){
336 sumCalculators.push_back(new Coverage());
337 }else if(Estimators[i] == "geometric"){
338 sumCalculators.push_back(new Geom());
339 }else if(Estimators[i] == "logseries"){
340 sumCalculators.push_back(new LogSD());
341 }else if(Estimators[i] == "qstat"){
342 sumCalculators.push_back(new QStat());
343 }else if(Estimators[i] == "bergerparker"){
344 sumCalculators.push_back(new BergerParker());
345 }else if(Estimators[i] == "bstick"){
346 sumCalculators.push_back(new BStick());
347 }else if(Estimators[i] == "ace"){
350 sumCalculators.push_back(new Ace(abund));
351 }else if(Estimators[i] == "jack"){
352 sumCalculators.push_back(new Jackknife());
353 }else if(Estimators[i] == "shannon"){
354 sumCalculators.push_back(new Shannon());
355 }else if(Estimators[i] == "shannoneven"){
356 sumCalculators.push_back(new ShannonEven());
357 }else if(Estimators[i] == "shannonrange"){
358 sumCalculators.push_back(new RangeShannon(alpha));
359 }else if(Estimators[i] == "npshannon"){
360 sumCalculators.push_back(new NPShannon());
361 }else if(Estimators[i] == "heip"){
362 sumCalculators.push_back(new Heip());
363 }else if(Estimators[i] == "smithwilson"){
364 sumCalculators.push_back(new SmithWilson());
365 }else if(Estimators[i] == "simpson"){
366 sumCalculators.push_back(new Simpson());
367 }else if(Estimators[i] == "simpsoneven"){
368 sumCalculators.push_back(new SimpsonEven());
369 }else if(Estimators[i] == "invsimpson"){
370 sumCalculators.push_back(new InvSimpson());
371 }else if(Estimators[i] == "bootstrap"){
372 sumCalculators.push_back(new Bootstrap());
373 }else if (Estimators[i] == "nseqs") {
374 sumCalculators.push_back(new NSeqs());
375 }else if (Estimators[i] == "goodscoverage") {
376 sumCalculators.push_back(new GoodsCoverage());
377 }else if (Estimators[i] == "efron") {
378 sumCalculators.push_back(new Efron(size));
379 }else if (Estimators[i] == "boneh") {
380 sumCalculators.push_back(new Boneh(size));
381 }else if (Estimators[i] == "solow") {
382 sumCalculators.push_back(new Solow(size));
383 }else if (Estimators[i] == "shen") {
384 sumCalculators.push_back(new Shen(size, abund));
389 //if the users entered no valid calculators don't execute command
390 if (sumCalculators.size() == 0) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
392 ofstream outputFileHandle;
393 m->openOutputFile(fileNameRoot, outputFileHandle);
394 outputFileHandle << "label";
398 m->openOutputFile(fileNameAve, outAve);
399 outputNames.push_back(fileNameAve); outputTypes["summary"].push_back(fileNameAve);
400 outAve << "label\tmethod";
401 outAve.setf(ios::fixed, ios::floatfield); outAve.setf(ios::showpoint);
402 if (inputFileNames.size() > 1) {
403 groupIndex[fileNameAve] = groups[p];
407 input = new InputData(inputFileNames[p], format);
408 sabund = input->getSAbundVector();
409 string lastLabel = sabund->getLabel();
411 for(int i=0;i<sumCalculators.size();i++){
412 if(sumCalculators[i]->getCols() == 1){
413 outputFileHandle << '\t' << sumCalculators[i]->getName();
414 if (subsample) { outAve << '\t' << sumCalculators[i]->getName(); }
418 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
419 if (subsample) { outAve << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci"; }
423 outputFileHandle << endl;
424 if (subsample) { outAve << endl; }
426 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
427 set<string> processedLabels;
428 set<string> userLabels = labels;
432 if (m->control_pressed) { outputFileHandle.close(); outAve.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; } delete sabund; delete input; return 0; }
434 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
436 if (m->control_pressed) { outputFileHandle.close(); outAve.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; } delete sabund; delete input; return 0; }
438 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
440 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
441 processedLabels.insert(sabund->getLabel());
442 userLabels.erase(sabund->getLabel());
444 process(sabund, outputFileHandle, outAve);
446 if (m->control_pressed) { outputFileHandle.close(); outAve.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; } delete sabund; delete input; return 0; }
450 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
451 string saveLabel = sabund->getLabel();
454 sabund = input->getSAbundVector(lastLabel);
456 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
457 processedLabels.insert(sabund->getLabel());
458 userLabels.erase(sabund->getLabel());
460 process(sabund, outputFileHandle, outAve);
462 if (m->control_pressed) { outputFileHandle.close(); outAve.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; } delete sabund; delete input; return 0; }
465 //restore real lastlabel to save below
466 sabund->setLabel(saveLabel);
469 lastLabel = sabund->getLabel();
472 sabund = input->getSAbundVector();
475 if (m->control_pressed) { outputFileHandle.close(); outAve.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; } delete input; return 0; }
477 //output error messages about any remaining user labels
478 set<string>::iterator it;
479 bool needToRun = false;
480 for (it = userLabels.begin(); it != userLabels.end(); it++) {
481 m->mothurOut("Your file does not include the label " + *it);
482 if (processedLabels.count(lastLabel) != 1) {
483 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
486 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
490 //run last label if you need to
491 if (needToRun == true) {
492 if (sabund != NULL) { delete sabund; }
493 sabund = input->getSAbundVector(lastLabel);
495 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
496 process(sabund, outputFileHandle, outAve);
498 if (m->control_pressed) { outputFileHandle.close(); outAve.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; } delete sabund; delete input; return 0; }
503 outputFileHandle.close();
504 if (subsample) { outAve.close(); }
506 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; } delete input; return 0; }
510 for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; }
513 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
515 //create summary file containing all the groups data for each label - this function just combines the info from the files already created.
516 if ((sharedfile != "") && (groupMode)) { vector<string> comboNames = createGroupSummaryFile(numLines, numCols, outputNames, groupIndex); for (int i = 0; i < comboNames.size(); i++) { outputNames.push_back(comboNames[i]); } }
518 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
520 m->mothurOutEndLine();
521 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
522 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
523 m->mothurOutEndLine();
527 catch(exception& e) {
528 m->errorOut(e, "SummaryCommand", "execute");
532 //**********************************************************************************************************************
533 int SummaryCommand::process(SAbundVector*& sabund, ofstream& outputFileHandle, ofstream& outAve) {
536 //calculator -> data -> values
537 vector< vector< vector<double> > > results; results.resize(sumCalculators.size());
539 outputFileHandle << sabund->getLabel();
542 for (int thisIter = 0; thisIter < iters+1; thisIter++) {
544 SAbundVector* thisIterSabund = sabund;
546 //we want the summary results for the whole dataset, then the subsampling
547 if ((thisIter > 0) && subsample) { //subsample sabund and run it
548 //copy sabund since getSample destroys it
549 RAbundVector rabund = sabund->getRAbundVector();
550 SAbundVector* newSabund = new SAbundVector();
551 *newSabund = rabund.getSAbundVector();
553 sample.getSample(newSabund, subsampleSize);
554 thisIterSabund = newSabund;
557 for(int i=0;i<sumCalculators.size();i++){
558 vector<double> data = sumCalculators[i]->getValues(thisIterSabund);
560 if (m->control_pressed) { return 0; }
563 outputFileHandle << '\t';
564 sumCalculators[i]->print(outputFileHandle);
566 //some of the calc have hci and lci need to make room for that
567 if (results[i].size() == 0) { results[i].resize(data.size()); }
568 //save results for ave and std.
569 for (int j = 0; j < data.size(); j++) {
570 if (m->control_pressed) { return 0; }
571 results[i][j].push_back(data[j]);
577 if ((thisIter > 0) && subsample) { delete thisIterSabund; }
579 outputFileHandle << endl;
582 outAve << sabund->getLabel() << '\t' << "ave\t";
583 //find ave and std for this label and output
584 //will need to modify the createGroupSummary to combine results and not mess with the .summary file.
587 vector< vector<double> > calcAverages; calcAverages.resize(sumCalculators.size());
588 for (int i = 0; i < calcAverages.size(); i++) { calcAverages[i].resize(results[i].size(), 0); }
590 for (int thisIter = 0; thisIter < iters; thisIter++) { //sum all groups dists for each calculator
591 for (int i = 0; i < calcAverages.size(); i++) { //initialize sums to zero.
592 for (int j = 0; j < calcAverages[i].size(); j++) {
593 calcAverages[i][j] += results[i][j][thisIter];
598 for (int i = 0; i < calcAverages.size(); i++) { //finds average.
599 for (int j = 0; j < calcAverages[i].size(); j++) {
600 calcAverages[i][j] /= (float) iters;
601 outAve << calcAverages[i][j] << '\t';
605 //find standard deviation
606 vector< vector<double> > stdDev; stdDev.resize(sumCalculators.size());
607 for (int i = 0; i < stdDev.size(); i++) { stdDev[i].resize(results[i].size(), 0); }
609 for (int thisIter = 0; thisIter < iters; thisIter++) { //compute the difference of each dist from the mean, and square the result of each
610 for (int i = 0; i < stdDev.size(); i++) {
611 for (int j = 0; j < stdDev[i].size(); j++) {
612 stdDev[i][j] += ((results[i][j][thisIter] - calcAverages[i][j]) * (results[i][j][thisIter] - calcAverages[i][j]));
617 outAve << endl << sabund->getLabel() << '\t' << "std\t";
618 for (int i = 0; i < stdDev.size(); i++) { //finds average.
619 for (int j = 0; j < stdDev[i].size(); j++) {
620 stdDev[i][j] /= (float) iters;
621 stdDev[i][j] = sqrt(stdDev[i][j]);
622 outAve << stdDev[i][j] << '\t';
630 catch(exception& e) {
631 m->errorOut(e, "SummaryCommand", "process");
635 //**********************************************************************************************************************
636 vector<string> SummaryCommand::parseSharedFile(string filename) {
638 vector<string> filenames;
640 map<string, ofstream*> filehandles;
641 map<string, ofstream*>::iterator it3;
643 input = new InputData(filename, "sharedfile");
644 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
646 string sharedFileRoot = m->getRootName(filename);
648 /******************************************************/
650 if (subsampleSize == -1) { //user has not set size, set size = smallest samples size
651 subsampleSize = lookup[0]->getNumSeqs();
652 for (int i = 1; i < lookup.size(); i++) {
653 int thisSize = lookup[i]->getNumSeqs();
655 if (thisSize < subsampleSize) { subsampleSize = thisSize; }
659 vector<string> Groups;
660 vector<SharedRAbundVector*> temp;
661 for (int i = 0; i < lookup.size(); i++) {
662 if (lookup[i]->getNumSeqs() < subsampleSize) {
663 m->mothurOut(lookup[i]->getGroup() + " contains " + toString(lookup[i]->getNumSeqs()) + ". Eliminating."); m->mothurOutEndLine();
666 Groups.push_back(lookup[i]->getGroup());
667 temp.push_back(lookup[i]);
671 m->setGroups(Groups);
674 if (lookup.size() < 1) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); m->control_pressed = true; delete input; return filenames; }
678 /******************************************************/
680 //clears file before we start to write to it below
681 for (int i=0; i<lookup.size(); i++) {
682 m->mothurRemove((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
683 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
687 for (int i=0; i<lookup.size(); i++) {
689 filehandles[lookup[i]->getGroup()] = temp;
690 groups.push_back(lookup[i]->getGroup());
693 while(lookup[0] != NULL) {
695 for (int i = 0; i < lookup.size(); i++) {
696 RAbundVector rav = lookup[i]->getRAbundVector();
697 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
698 rav.print(*(filehandles[lookup[i]->getGroup()]));
699 (*(filehandles[lookup[i]->getGroup()])).close();
702 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
703 lookup = input->getSharedRAbundVectors();
707 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
715 catch(exception& e) {
716 m->errorOut(e, "SummaryCommand", "parseSharedFile");
720 //**********************************************************************************************************************
721 vector<string> SummaryCommand::createGroupSummaryFile(int numLines, int numCols, vector<string>& outputNames, map<string, string> groupIndex) {
724 //open each groups summary file
725 vector<string> newComboNames;
727 map<string, map<string, vector<string> > > files;
728 map<string, string> filesTypesLabels;
729 map<string, int> filesTypesNumLines;
730 for (int i=0; i<outputNames.size(); i++) {
731 vector<string> thisFilesLines;
734 m->openInputFile(outputNames[i], temp);
736 //read through first line - labels
737 string labelsLine = m->getline(temp);
738 vector<string> theseLabels = m->splitWhiteSpace(labelsLine);
740 string newLabel = "";
741 for (int j = 0; j < theseLabels.size(); j++) {
742 if (j == 1) { newLabel += "group\t" + theseLabels[j] + '\t';
743 }else{ newLabel += theseLabels[j] + '\t'; }
749 if (theseLabels.size() != numCols+1) { stop = numLines*2; }
751 for (int k = 0; k < stop; k++) {
753 string thisLine = "";
756 for (int j = 0; j < theseLabels.size(); j++) {
760 if (j == 1) { thisLine += groupIndex[outputNames[i]] + "\t" + tempLabel + "\t"; }
761 else{ thisLine += tempLabel + "\t"; }
766 thisFilesLines.push_back(thisLine);
771 string extension = m->getExtension(outputNames[i]);
772 if (theseLabels.size() != numCols+1) { extension = ".ave-std" + extension; }
773 string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "groups" + extension;
774 m->mothurRemove(combineFileName); //remove old file
775 filesTypesLabels[extension] = newLabel;
776 filesTypesNumLines[extension] = stop;
778 map<string, map<string, vector<string> > >::iterator itFiles = files.find(extension);
779 if (itFiles != files.end()) { //add new files info to existing type
780 files[extension][outputNames[i]] = thisFilesLines;
782 map<string, vector<string> > thisFile;
783 thisFile[outputNames[i]] = thisFilesLines;
784 files[extension] = thisFile;
788 m->mothurRemove(outputNames[i]);
792 for (map<string, map<string, vector<string> > >::iterator itFiles = files.begin(); itFiles != files.end(); itFiles++) {
794 if (m->control_pressed) { break; }
796 string extension = itFiles->first;
797 map<string, vector<string> > thisType = itFiles->second;
798 string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "groups" + extension;
799 newComboNames.push_back(combineFileName);
802 m->openOutputFile(combineFileName, out);
804 //output label line to new file
805 out << filesTypesLabels[extension] << endl;
808 for (int k = 0; k < filesTypesNumLines[extension]; k++) {
810 //grab summary data for each group
811 for (map<string, vector<string> >::iterator itType = thisType.begin(); itType != thisType.end(); itType++) {
812 out << (itType->second)[k];
821 //return combine file name
822 return newComboNames;
825 catch(exception& e) {
826 m->errorOut(e, "SummaryCommand", "createGroupSummaryFile");
830 //**********************************************************************************************************************