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();
42 //allow user to run help
43 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
46 //valid paramters for this command
47 string Array[] = {"label","calc","abund","size"};
48 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
50 OptionParser parser(option);
51 map<string,string> parameters = parser.getParameters();
53 ValidParameters validParameter;
55 //check to make sure all parameters are valid for command
56 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
57 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
60 //make sure the user has already run the read.otu command
61 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; }
63 //check for optional parameter and set defaults
64 // ...at some point should added some additional type checking...
65 label = validParameter.validFile(parameters, "label", false);
66 if (label == "not found") { label = ""; }
68 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
69 else { allLines = 1; }
72 //if the user has not specified any labels use the ones from read.otu
74 allLines = globaldata->allLines;
75 labels = globaldata->labels;
78 calc = validParameter.validFile(parameters, "calc", false);
79 if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
81 if (calc == "default") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
83 splitAtDash(calc, Estimators);
86 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
89 temp = validParameter.validFile(parameters, "size", false); if (temp == "not found") { temp = "0"; }
94 validCalculator = new ValidCalculators();
97 for (i=0; i<Estimators.size(); i++) {
98 if (validCalculator->isValidCalculator("summary", Estimators[i]) == true) {
99 if(Estimators[i] == "sobs"){
100 sumCalculators.push_back(new Sobs());
101 }else if(Estimators[i] == "chao"){
102 sumCalculators.push_back(new Chao1());
103 }else if(Estimators[i] == "coverage"){
104 sumCalculators.push_back(new Coverage());
105 }else if(Estimators[i] == "geometric"){
106 sumCalculators.push_back(new Geom());
107 }else if(Estimators[i] == "logseries"){
108 sumCalculators.push_back(new LogSD());
109 }else if(Estimators[i] == "qstat"){
110 sumCalculators.push_back(new QStat());
111 }else if(Estimators[i] == "bergerparker"){
112 sumCalculators.push_back(new BergerParker());
113 }else if(Estimators[i] == "bstick"){
114 sumCalculators.push_back(new BStick());
115 }else if(Estimators[i] == "ace"){
118 sumCalculators.push_back(new Ace(abund));
119 }else if(Estimators[i] == "jack"){
120 sumCalculators.push_back(new Jackknife());
121 }else if(Estimators[i] == "shannon"){
122 sumCalculators.push_back(new Shannon());
123 }else if(Estimators[i] == "npshannon"){
124 sumCalculators.push_back(new NPShannon());
125 }else if(Estimators[i] == "simpson"){
126 sumCalculators.push_back(new Simpson());
127 }else if(Estimators[i] == "bootstrap"){
128 sumCalculators.push_back(new Bootstrap());
129 }else if (Estimators[i] == "nseqs") {
130 sumCalculators.push_back(new NSeqs());
131 }else if (Estimators[i] == "goodscoverage") {
132 sumCalculators.push_back(new GoodsCoverage());
133 }else if (Estimators[i] == "efron") {
134 sumCalculators.push_back(new Efron(size));
135 }else if (Estimators[i] == "boneh") {
136 sumCalculators.push_back(new Boneh(size));
137 }else if (Estimators[i] == "solow") {
138 sumCalculators.push_back(new Solow(size));
139 }else if (Estimators[i] == "shen") {
140 sumCalculators.push_back(new Shen(size, abund));
149 catch(exception& e) {
150 errorOut(e, "SummaryCommand", "SummaryCommand");
154 //**********************************************************************************************************************
156 void SummaryCommand::help(){
158 mothurOut("The summary.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
159 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");
160 mothurOut("The summary.single command parameters are label, calc, abund. No parameters are required.\n");
161 mothurOut("The summary.single command should be in the following format: \n");
162 mothurOut("summary.single(label=yourLabel, calc=yourEstimators).\n");
163 mothurOut("Example summary.single(label=unique-.01-.03, calc=sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson).\n");
164 validCalculator->printCalc("summary", cout);
165 mothurOut("The default value calc is sobs-chao-ace-jack-shannon-npshannon-simpson\n");
166 mothurOut("The label parameter is used to analyze specific labels in your input.\n");
167 mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabels).\n\n");
169 catch(exception& e) {
170 errorOut(e, "SummaryCommand", "help");
175 //**********************************************************************************************************************
177 SummaryCommand::~SummaryCommand(){
178 if (abort == false) {
179 delete input; globaldata->ginput = NULL;
181 delete validCalculator;
182 globaldata->sabund = NULL;
186 //**********************************************************************************************************************
188 int SummaryCommand::execute(){
191 if (abort == true) { return 0; }
193 //if the users entered no valid calculators don't execute command
194 if (sumCalculators.size() == 0) { return 0; }
196 outputFileName = ((getRootName(globaldata->inputFileName)) + "summary");
197 openOutputFile(outputFileName, outputFileHandle);
198 outputFileHandle << "label";
200 read = new ReadOTUFile(globaldata->inputFileName);
201 read->read(&*globaldata);
203 sabund = globaldata->sabund;
204 string lastLabel = sabund->getLabel();
205 input = globaldata->ginput;
207 for(int i=0;i<sumCalculators.size();i++){
208 if(sumCalculators[i]->getCols() == 1){
209 outputFileHandle << '\t' << sumCalculators[i]->getName();
212 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
215 outputFileHandle << endl;
217 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
218 set<string> processedLabels;
219 set<string> userLabels = labels;
221 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
223 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
225 mothurOut(sabund->getLabel()); mothurOutEndLine();
226 processedLabels.insert(sabund->getLabel());
227 userLabels.erase(sabund->getLabel());
229 outputFileHandle << sabund->getLabel();
230 for(int i=0;i<sumCalculators.size();i++){
231 vector<double> data = sumCalculators[i]->getValues(sabund);
232 outputFileHandle << '\t';
233 sumCalculators[i]->print(outputFileHandle);
235 outputFileHandle << endl;
238 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
239 string saveLabel = sabund->getLabel();
242 sabund = input->getSAbundVector(lastLabel);
244 mothurOut(sabund->getLabel()); mothurOutEndLine();
245 processedLabels.insert(sabund->getLabel());
246 userLabels.erase(sabund->getLabel());
248 outputFileHandle << sabund->getLabel();
249 for(int i=0;i<sumCalculators.size();i++){
250 vector<double> data = sumCalculators[i]->getValues(sabund);
251 outputFileHandle << '\t';
252 sumCalculators[i]->print(outputFileHandle);
254 outputFileHandle << endl;
256 //restore real lastlabel to save below
257 sabund->setLabel(saveLabel);
260 lastLabel = sabund->getLabel();
263 sabund = input->getSAbundVector();
266 //output error messages about any remaining user labels
267 set<string>::iterator it;
268 bool needToRun = false;
269 for (it = userLabels.begin(); it != userLabels.end(); it++) {
270 mothurOut("Your file does not include the label " + *it);
271 if (processedLabels.count(lastLabel) != 1) {
272 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
275 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
279 //run last label if you need to
280 if (needToRun == true) {
281 if (sabund != NULL) { delete sabund; }
282 sabund = input->getSAbundVector(lastLabel);
284 mothurOut(sabund->getLabel()); mothurOutEndLine();
285 outputFileHandle << sabund->getLabel();
286 for(int i=0;i<sumCalculators.size();i++){
287 vector<double> data = sumCalculators[i]->getValues(sabund);
288 outputFileHandle << '\t';
289 sumCalculators[i]->print(outputFileHandle);
291 outputFileHandle << endl;
295 outputFileHandle.close();
299 catch(exception& e) {
300 errorOut(e, "SummaryCommand", "execute");
305 //**********************************************************************************************************************