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() == "")) { cout << "You must read a list, sabund or rabund before you can use the summary.single command." << endl; 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 != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; 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 cout << "Standard Error: " << e.what() << " has occurred in the SummaryCommand class Function SummaryCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
165 cout << "An unknown error has occurred in the SummaryCommand class function SummaryCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
169 //**********************************************************************************************************************
171 void SummaryCommand::help(){
173 cout << "The summary.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION." << "\n";
174 cout << "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";
175 cout << "The summary.single command parameters are label, line, calc, abund. No parameters are required, but you may not use " << "\n";
176 cout << "both the line and label parameters at the same time. The summary.single command should be in the following format: " << "\n";
177 cout << "summary.single(label=yourLabel, line=yourLines, calc=yourEstimators)." << "\n";
178 cout << "Example summary.single(label=unique-.01-.03, line=0,5,10, calc=sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson)." << "\n";
179 validCalculator->printCalc("summary", cout);
180 cout << "The default value calc is sobs-chao-ace-jack-shannon-npshannon-simpson" << "\n";
181 cout << "The label and line parameters are used to analyze specific lines in your input." << "\n";
182 cout << "Note: No spaces between parameter labels (i.e. line), '=' and parameters (i.e.yourLines)." << "\n" << "\n";
184 catch(exception& e) {
185 cout << "Standard Error: " << e.what() << " has occurred in the SummaryCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
189 cout << "An unknown error has occurred in the SummaryCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
194 //**********************************************************************************************************************
196 SummaryCommand::~SummaryCommand(){
197 if (abort == false) {
198 delete input; globaldata->ginput = NULL;
200 delete validCalculator;
201 globaldata->sabund = NULL;
205 //**********************************************************************************************************************
207 int SummaryCommand::execute(){
210 if (abort == true) { return 0; }
214 //if the users entered no valid calculators don't execute command
215 if (sumCalculators.size() == 0) { return 0; }
217 outputFileName = ((getRootName(globaldata->inputFileName)) + "summary");
218 openOutputFile(outputFileName, outputFileHandle);
219 outputFileHandle << "label";
221 read = new ReadOTUFile(globaldata->inputFileName);
222 read->read(&*globaldata);
224 sabund = globaldata->sabund;
225 SAbundVector* lastSAbund = sabund;
226 input = globaldata->ginput;
228 for(int i=0;i<sumCalculators.size();i++){
229 if(sumCalculators[i]->getCols() == 1){
230 outputFileHandle << '\t' << sumCalculators[i]->getName();
233 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
236 outputFileHandle << endl;
238 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
239 set<string> processedLabels;
240 set<string> userLabels = labels;
241 set<int> userLines = lines;
243 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
245 if(allLines == 1 || lines.count(count) == 1 || labels.count(sabund->getLabel()) == 1){
247 cout << sabund->getLabel() << '\t' << count << endl;
248 processedLabels.insert(sabund->getLabel());
249 userLabels.erase(sabund->getLabel());
250 userLines.erase(count);
253 outputFileHandle << sabund->getLabel();
254 for(int i=0;i<sumCalculators.size();i++){
255 vector<double> data = sumCalculators[i]->getValues(sabund);
256 outputFileHandle << '\t';
257 sumCalculators[i]->print(outputFileHandle);
259 outputFileHandle << endl;
262 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastSAbund->getLabel()) != 1)) {
264 cout << lastSAbund->getLabel() << '\t' << count << endl;
265 processedLabels.insert(lastSAbund->getLabel());
266 userLabels.erase(lastSAbund->getLabel());
268 outputFileHandle << lastSAbund->getLabel();
269 for(int i=0;i<sumCalculators.size();i++){
270 vector<double> data = sumCalculators[i]->getValues(lastSAbund);
271 outputFileHandle << '\t';
272 sumCalculators[i]->print(outputFileHandle);
274 outputFileHandle << endl;
277 if (count != 1) { delete lastSAbund; }
280 sabund = input->getSAbundVector();
284 //output error messages about any remaining user labels
285 set<string>::iterator it;
286 bool needToRun = false;
287 for (it = userLabels.begin(); it != userLabels.end(); it++) {
288 cout << "Your file does not include the label "<< *it;
289 if (processedLabels.count(lastSAbund->getLabel()) != 1) {
290 cout << ". I will use " << lastSAbund->getLabel() << "." << endl;
293 cout << ". Please refer to " << lastSAbund->getLabel() << "." << endl;
297 //run last line if you need to
298 if (needToRun == true) {
299 cout << lastSAbund->getLabel() << '\t' << count << endl;
300 outputFileHandle << lastSAbund->getLabel();
301 for(int i=0;i<sumCalculators.size();i++){
302 vector<double> data = sumCalculators[i]->getValues(lastSAbund);
303 outputFileHandle << '\t';
304 sumCalculators[i]->print(outputFileHandle);
306 outputFileHandle << endl;
309 outputFileHandle.close();
314 catch(exception& e) {
315 cout << "Standard Error: " << e.what() << " has occurred in the SummaryCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
319 cout << "An unknown error has occurred in the SummaryCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
324 //**********************************************************************************************************************