2 * summarysharedcommand.cpp
5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "summarysharedcommand.h"
11 #include "sharedsobscollectsummary.h"
12 #include "sharedchao1.h"
13 #include "sharedace.h"
14 #include "sharednseqs.h"
15 #include "sharedjabund.h"
16 #include "sharedsorabund.h"
17 #include "sharedjclass.h"
18 #include "sharedsorclass.h"
19 #include "sharedjest.h"
20 #include "sharedsorest.h"
21 #include "sharedthetayc.h"
22 #include "sharedthetan.h"
23 #include "sharedkstest.h"
24 #include "whittaker.h"
25 #include "sharedochiai.h"
26 #include "sharedanderbergs.h"
27 #include "sharedkulczynski.h"
28 #include "sharedkulczynskicody.h"
29 #include "sharedlennon.h"
30 #include "sharedmorisitahorn.h"
31 #include "sharedbraycurtis.h"
32 #include "sharedjackknife.h"
33 #include "whittaker.h"
36 //**********************************************************************************************************************
38 SummarySharedCommand::SummarySharedCommand(string option){
40 globaldata = GlobalData::getInstance();
47 //allow user to run help
48 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
51 //valid paramters for this command
52 string Array[] = {"line","label","calc","groups"};
53 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
55 OptionParser parser(option);
56 map<string, string> parameters = parser.getParameters();
58 ValidParameters validParameter;
60 //check to make sure all parameters are valid for command
61 for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
62 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
65 //make sure the user has already run the read.otu command
66 if (globaldata->getSharedFile() == "") {
67 if (globaldata->getListFile() == "") { cout << "You must read a list and a group, or a shared before you can use the summary.shared command." << endl; abort = true; }
68 else if (globaldata->getGroupFile() == "") { cout << "You must read a list and a group, or a shared before you can use the summary.shared command." << endl; abort = true; }
72 //check for optional parameter and set defaults
73 // ...at some point should added some additional type checking...
74 line = validParameter.validFile(parameters, "line", false);
75 if (line == "not found") { line = ""; }
77 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
78 else { allLines = 1; }
81 label = validParameter.validFile(parameters, "label", false);
82 if (label == "not found") { label = ""; }
84 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
85 else { allLines = 1; }
88 //make sure user did not use both the line and label parameters
89 if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
90 //if the user has not specified any line or labels use the ones from read.otu
91 else if((line == "") && (label == "")) {
92 allLines = globaldata->allLines;
93 labels = globaldata->labels;
94 lines = globaldata->lines;
97 calc = validParameter.validFile(parameters, "calc", false);
98 if (calc == "not found") { calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan"; }
100 if (calc == "default") { calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan"; }
102 splitAtDash(calc, Estimators);
104 groups = validParameter.validFile(parameters, "groups", false);
105 if (groups == "not found") { groups = ""; }
107 splitAtDash(groups, Groups);
108 globaldata->Groups = Groups;
111 if (abort == false) {
113 validCalculator = new ValidCalculators();
116 for (i=0; i<Estimators.size(); i++) {
117 if (validCalculator->isValidCalculator("sharedsummary", Estimators[i]) == true) {
118 if (Estimators[i] == "sharedsobs") {
119 sumCalculators.push_back(new SharedSobsCS());
120 }else if (Estimators[i] == "sharedchao") {
121 sumCalculators.push_back(new SharedChao1());
122 }else if (Estimators[i] == "sharedace") {
123 sumCalculators.push_back(new SharedAce());
124 }else if (Estimators[i] == "jabund") {
125 sumCalculators.push_back(new JAbund());
126 }else if (Estimators[i] == "sorabund") {
127 sumCalculators.push_back(new SorAbund());
128 }else if (Estimators[i] == "jclass") {
129 sumCalculators.push_back(new Jclass());
130 }else if (Estimators[i] == "sorclass") {
131 sumCalculators.push_back(new SorClass());
132 }else if (Estimators[i] == "jest") {
133 sumCalculators.push_back(new Jest());
134 }else if (Estimators[i] == "sorest") {
135 sumCalculators.push_back(new SorEst());
136 }else if (Estimators[i] == "thetayc") {
137 sumCalculators.push_back(new ThetaYC());
138 }else if (Estimators[i] == "thetan") {
139 sumCalculators.push_back(new ThetaN());
140 }else if (Estimators[i] == "kstest") {
141 sumCalculators.push_back(new KSTest());
142 }else if (Estimators[i] == "sharednseqs") {
143 sumCalculators.push_back(new SharedNSeqs());
144 }else if (Estimators[i] == "ochiai") {
145 sumCalculators.push_back(new Ochiai());
146 }else if (Estimators[i] == "anderberg") {
147 sumCalculators.push_back(new Anderberg());
148 }else if (Estimators[i] == "kulczynski") {
149 sumCalculators.push_back(new Kulczynski());
150 }else if (Estimators[i] == "kulczynskicody") {
151 sumCalculators.push_back(new KulczynskiCody());
152 }else if (Estimators[i] == "lennon") {
153 sumCalculators.push_back(new Lennon());
154 }else if (Estimators[i] == "morisitahorn") {
155 sumCalculators.push_back(new MorHorn());
156 }else if (Estimators[i] == "braycurtis") {
157 sumCalculators.push_back(new BrayCurtis());
158 }else if (Estimators[i] == "whittaker") {
159 sumCalculators.push_back(new Whittaker());
164 outputFileName = ((getRootName(globaldata->inputFileName)) + "shared.summary");
165 openOutputFile(outputFileName, outputFileHandle);
166 // format = globaldata->getFormat();
172 catch(exception& e) {
173 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
177 cout << "An unknown error has occurred in the SummarySharedCommand class function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
182 //**********************************************************************************************************************
184 void SummarySharedCommand::help(){
186 cout << "The summary.shared command can only be executed after a successful read.otu command." << "\n";
187 cout << "The summary.shared command parameters are label, line and calc. No parameters are required, but you may not use " << "\n";
188 cout << "both the line and label parameters at the same time. The summary.shared command should be in the following format: " << "\n";
189 cout << "summary.shared(label=yourLabel, line=yourLines, calc=yourEstimators, groups=yourGroups)." << "\n";
190 cout << "Example summary.shared(label=unique-.01-.03, line=0,5,10, groups=B-C, calc=sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan)." << "\n";
191 validCalculator->printCalc("sharedsummary", cout);
192 cout << "The default value for calc is sharedsobs-sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan" << "\n";
193 cout << "The default value for groups is all the groups in your groupfile." << "\n";
194 cout << "The label and line parameters are used to analyze specific lines in your input." << "\n";
195 cout << "The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed. You must enter at least 2 valid groups." << "\n";
196 cout << "Note: No spaces between parameter labels (i.e. line), '=' and parameters (i.e.yourLines)." << "\n" << "\n";
198 catch(exception& e) {
199 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203 cout << "An unknown error has occurred in the SummarySharedCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
208 //**********************************************************************************************************************
210 SummarySharedCommand::~SummarySharedCommand(){
213 delete validCalculator;
216 //**********************************************************************************************************************
218 int SummarySharedCommand::execute(){
221 if (abort == true) { return 0; }
225 //if the users entered no valid calculators don't execute command
226 if (sumCalculators.size() == 0) { return 0; }
227 //check if any calcs can do multiples
229 for (int i = 0; i < sumCalculators.size(); i++) {
230 if (sumCalculators[i]->getMultiple() == true) { mult = true; }
235 read = new ReadOTUFile(globaldata->inputFileName);
236 read->read(&*globaldata);
238 input = globaldata->ginput;
239 lookup = input->getSharedRAbundVectors();
240 vector<SharedRAbundVector*> lastLookup = lookup;
242 //output estimator names as column headers
243 outputFileHandle << "label" <<'\t' << "comparison" << '\t';
244 for(int i=0;i<sumCalculators.size();i++){
245 outputFileHandle << '\t' << sumCalculators[i]->getName();
247 outputFileHandle << endl;
249 //create file and put column headers for multiple groups file
251 outAllFileName = ((getRootName(globaldata->inputFileName)) + "sharedmultiple.summary");
252 openOutputFile(outAllFileName, outAll);
254 outAll << "label" <<'\t' << "comparison" << '\t';
255 for(int i=0;i<sumCalculators.size();i++){
256 if (sumCalculators[i]->getMultiple() == true) {
257 outAll << '\t' << sumCalculators[i]->getName();
263 if (lookup.size() < 2) {
264 cout << "I cannot run the command without at least 2 valid groups.";
265 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
267 //close files and clean up
268 outputFileHandle.close(); remove(outputFileName.c_str());
269 if (mult == true) { outAll.close(); remove(outAllFileName.c_str()); }
271 //if you only have 2 groups you don't need a .sharedmultiple file
272 }else if ((lookup.size() == 2) && (mult == true)) {
275 remove(outAllFileName.c_str());
278 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
279 set<string> processedLabels;
280 set<string> userLabels = labels;
281 set<int> userLines = lines;
283 //as long as you are not at the end of the file or done wih the lines you want
284 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
286 if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){
287 cout << lookup[0]->getLabel() << '\t' << count << endl;
290 processedLabels.insert(lookup[0]->getLabel());
291 userLabels.erase(lookup[0]->getLabel());
292 userLines.erase(count);
295 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
296 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
299 processedLabels.insert(lastLookup[0]->getLabel());
300 userLabels.erase(lastLookup[0]->getLabel());
304 //prevent memory leak
305 if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; } }
308 //get next line to process
309 lookup = input->getSharedRAbundVectors();
313 //output error messages about any remaining user labels
314 set<string>::iterator it;
315 bool needToRun = false;
316 for (it = userLabels.begin(); it != userLabels.end(); it++) {
317 cout << "Your file does not include the label "<< *it;
318 if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
319 cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
322 cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
326 //run last line if you need to
327 if (needToRun == true) {
328 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
332 for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; }
334 //reset groups parameter
335 globaldata->Groups.clear();
338 outputFileHandle.close();
339 if (mult == true) { outAll.close(); }
343 catch(exception& e) {
344 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
348 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
353 /***********************************************************/
354 void SummarySharedCommand::process(vector<SharedRAbundVector*> thisLookup) {
356 //loop through calculators and add to file all for all calcs that can do mutiple groups
359 outAll << thisLookup[0]->getLabel() << '\t';
361 //output groups names
362 string outNames = "";
363 for (int j = 0; j < thisLookup.size(); j++) {
364 outNames += thisLookup[j]->getGroup() + "-";
366 outNames = outNames.substr(0, outNames.length()-1); //rip off extra '-';
367 outAll << outNames << '\t';
369 for(int i=0;i<sumCalculators.size();i++){
370 if (sumCalculators[i]->getMultiple() == true) {
371 sumCalculators[i]->getValues(thisLookup);
373 sumCalculators[i]->print(outAll);
380 vector<SharedRAbundVector*> subset;
381 for (int k = 0; k < (thisLookup.size() - 1); k++) { // pass cdd each set of groups to commpare
382 for (int l = n; l < thisLookup.size(); l++) {
384 outputFileHandle << thisLookup[0]->getLabel() << '\t';
386 subset.clear(); //clear out old pair of sharedrabunds
387 //add new pair of sharedrabunds
388 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]);
390 //sort groups to be alphanumeric
391 if (thisLookup[k]->getGroup() > thisLookup[l]->getGroup()) {
392 outputFileHandle << (thisLookup[l]->getGroup() +'\t' + thisLookup[k]->getGroup()) << '\t'; //print out groups
394 outputFileHandle << (thisLookup[k]->getGroup() +'\t' + thisLookup[l]->getGroup()) << '\t'; //print out groups
397 for(int i=0;i<sumCalculators.size();i++) {
399 sumCalculators[i]->getValues(subset); //saves the calculator outputs
400 outputFileHandle << '\t';
401 sumCalculators[i]->print(outputFileHandle);
403 outputFileHandle << endl;
409 catch(exception& e) {
410 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
414 cout << "An unknown error has occurred in the SummarySharedCommand class function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
419 /***********************************************************/