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 "npshannon.h"
19 #include "jackknife.h"
23 #include "bergerparker.h"
25 #include "goodscoverage.h"
32 //**********************************************************************************************************************
34 SummaryCommand::SummaryCommand(string option){
36 globaldata = GlobalData::getInstance();
43 //allow user to run help
44 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
47 //valid paramters for this command
48 string Array[] = {"line","label","calc","abund","size"};
49 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
51 OptionParser parser(option);
52 map<string,string> parameters = parser.getParameters();
54 ValidParameters validParameter;
56 //check to make sure all parameters are valid for command
57 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
58 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
61 //make sure the user has already run the read.otu command
62 if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { mothurOut("You must read a list, sabund or rabund before you can use the summary.single command."); mothurOutEndLine(); abort = true; }
64 //check for optional parameter and set defaults
65 // ...at some point should added some additional type checking...
66 line = validParameter.validFile(parameters, "line", false);
67 if (line == "not found") { line = ""; }
69 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
70 else { allLines = 1; }
73 label = validParameter.validFile(parameters, "label", false);
74 if (label == "not found") { label = ""; }
76 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
77 else { allLines = 1; }
80 //make sure user did not use both the line and label parameters
81 if ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; }
82 //if the user has not specified any line or labels use the ones from read.otu
83 else if((line == "") && (label == "")) {
84 allLines = globaldata->allLines;
85 labels = globaldata->labels;
86 lines = globaldata->lines;
89 calc = validParameter.validFile(parameters, "calc", false);
90 if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
92 if (calc == "default") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
94 splitAtDash(calc, Estimators);
97 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
100 temp = validParameter.validFile(parameters, "size", false); if (temp == "not found") { temp = "0"; }
103 if (abort == false) {
105 validCalculator = new ValidCalculators();
108 for (i=0; i<Estimators.size(); i++) {
109 if (validCalculator->isValidCalculator("summary", Estimators[i]) == true) {
110 if(Estimators[i] == "sobs"){
111 sumCalculators.push_back(new Sobs());
112 }else if(Estimators[i] == "chao"){
113 sumCalculators.push_back(new Chao1());
114 }else if(Estimators[i] == "coverage"){
115 sumCalculators.push_back(new Coverage());
116 }else if(Estimators[i] == "geometric"){
117 sumCalculators.push_back(new Geom());
118 }else if(Estimators[i] == "logseries"){
119 sumCalculators.push_back(new LogSD());
120 }else if(Estimators[i] == "qstat"){
121 sumCalculators.push_back(new QStat());
122 }else if(Estimators[i] == "bergerparker"){
123 sumCalculators.push_back(new BergerParker());
124 }else if(Estimators[i] == "bstick"){
125 sumCalculators.push_back(new BStick());
126 }else if(Estimators[i] == "ace"){
129 sumCalculators.push_back(new Ace(abund));
130 }else if(Estimators[i] == "jack"){
131 sumCalculators.push_back(new Jackknife());
132 }else if(Estimators[i] == "shannon"){
133 sumCalculators.push_back(new Shannon());
134 }else if(Estimators[i] == "npshannon"){
135 sumCalculators.push_back(new NPShannon());
136 }else if(Estimators[i] == "simpson"){
137 sumCalculators.push_back(new Simpson());
138 }else if(Estimators[i] == "bootstrap"){
139 sumCalculators.push_back(new Bootstrap());
140 }else if (Estimators[i] == "nseqs") {
141 sumCalculators.push_back(new NSeqs());
142 }else if (Estimators[i] == "goodscoverage") {
143 sumCalculators.push_back(new GoodsCoverage());
144 }else if (Estimators[i] == "efron") {
145 sumCalculators.push_back(new Efron(size));
146 }else if (Estimators[i] == "boneh") {
147 sumCalculators.push_back(new Boneh(size));
148 }else if (Estimators[i] == "solow") {
149 sumCalculators.push_back(new Solow(size));
150 }else if (Estimators[i] == "shen") {
151 sumCalculators.push_back(new Shen(size, abund));
160 catch(exception& e) {
161 errorOut(e, "SummaryCommand", "SummaryCommand");
165 //**********************************************************************************************************************
167 void SummaryCommand::help(){
169 mothurOut("The summary.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
170 mothurOut("The summary.single command can be executed after a successful cluster command. It will use the .list file from the output of the cluster.\n");
171 mothurOut("The summary.single command parameters are label, line, calc, abund. No parameters are required, but you may not use \n");
172 mothurOut("both the line and label parameters at the same time. The summary.single command should be in the following format: \n");
173 mothurOut("summary.single(label=yourLabel, line=yourLines, calc=yourEstimators).\n");
174 mothurOut("Example summary.single(label=unique-.01-.03, line=0,5,10, calc=sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson).\n");
175 validCalculator->printCalc("summary", cout);
176 mothurOut("The default value calc is sobs-chao-ace-jack-shannon-npshannon-simpson\n");
177 mothurOut("The label and line parameters are used to analyze specific lines in your input.\n");
178 mothurOut("Note: No spaces between parameter labels (i.e. line), '=' and parameters (i.e.yourLines).\n\n");
180 catch(exception& e) {
181 errorOut(e, "SummaryCommand", "help");
186 //**********************************************************************************************************************
188 SummaryCommand::~SummaryCommand(){
189 if (abort == false) {
190 delete input; globaldata->ginput = NULL;
192 delete validCalculator;
193 globaldata->sabund = NULL;
197 //**********************************************************************************************************************
199 int SummaryCommand::execute(){
202 if (abort == true) { return 0; }
206 //if the users entered no valid calculators don't execute command
207 if (sumCalculators.size() == 0) { return 0; }
209 outputFileName = ((getRootName(globaldata->inputFileName)) + "summary");
210 openOutputFile(outputFileName, outputFileHandle);
211 outputFileHandle << "label";
213 read = new ReadOTUFile(globaldata->inputFileName);
214 read->read(&*globaldata);
216 sabund = globaldata->sabund;
217 string lastLabel = sabund->getLabel();
218 input = globaldata->ginput;
220 for(int i=0;i<sumCalculators.size();i++){
221 if(sumCalculators[i]->getCols() == 1){
222 outputFileHandle << '\t' << sumCalculators[i]->getName();
225 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
228 outputFileHandle << endl;
230 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
231 set<string> processedLabels;
232 set<string> userLabels = labels;
233 set<int> userLines = lines;
235 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
237 if(allLines == 1 || lines.count(count) == 1 || labels.count(sabund->getLabel()) == 1){
239 mothurOut(sabund->getLabel()); mothurOutEndLine();
240 processedLabels.insert(sabund->getLabel());
241 userLabels.erase(sabund->getLabel());
242 userLines.erase(count);
245 outputFileHandle << sabund->getLabel();
246 for(int i=0;i<sumCalculators.size();i++){
247 vector<double> data = sumCalculators[i]->getValues(sabund);
248 outputFileHandle << '\t';
249 sumCalculators[i]->print(outputFileHandle);
251 outputFileHandle << endl;
254 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
256 sabund = input->getSAbundVector(lastLabel);
258 mothurOut(sabund->getLabel()); mothurOutEndLine();
259 processedLabels.insert(sabund->getLabel());
260 userLabels.erase(sabund->getLabel());
262 outputFileHandle << sabund->getLabel();
263 for(int i=0;i<sumCalculators.size();i++){
264 vector<double> data = sumCalculators[i]->getValues(sabund);
265 outputFileHandle << '\t';
266 sumCalculators[i]->print(outputFileHandle);
268 outputFileHandle << endl;
271 lastLabel = sabund->getLabel();
274 sabund = input->getSAbundVector();
278 //output error messages about any remaining user labels
279 set<string>::iterator it;
280 bool needToRun = false;
281 for (it = userLabels.begin(); it != userLabels.end(); it++) {
282 mothurOut("Your file does not include the label " + *it);
283 if (processedLabels.count(lastLabel) != 1) {
284 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
287 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
291 //run last line if you need to
292 if (needToRun == true) {
294 sabund = input->getSAbundVector(lastLabel);
296 mothurOut(sabund->getLabel()); mothurOutEndLine();
297 outputFileHandle << sabund->getLabel();
298 for(int i=0;i<sumCalculators.size();i++){
299 vector<double> data = sumCalculators[i]->getValues(sabund);
300 outputFileHandle << '\t';
301 sumCalculators[i]->print(outputFileHandle);
303 outputFileHandle << endl;
307 outputFileHandle.close();
311 catch(exception& e) {
312 errorOut(e, "SummaryCommand", "execute");
317 //**********************************************************************************************************************