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 parser = new OptionParser();
56 parser->parse(option, parameters); delete parser;
58 ValidParameters* validParameter = new ValidParameters();
60 //check to make sure all parameters are valid for command
61 for (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 delete validParameter;
113 if (abort == false) {
115 validCalculator = new ValidCalculators();
118 for (i=0; i<Estimators.size(); i++) {
119 if (validCalculator->isValidCalculator("sharedsummary", Estimators[i]) == true) {
120 if (Estimators[i] == "sharedsobs") {
121 sumCalculators.push_back(new SharedSobsCS());
122 }else if (Estimators[i] == "sharedchao") {
123 sumCalculators.push_back(new SharedChao1());
124 }else if (Estimators[i] == "sharedace") {
125 sumCalculators.push_back(new SharedAce());
126 }else if (Estimators[i] == "jabund") {
127 sumCalculators.push_back(new JAbund());
128 }else if (Estimators[i] == "sorabund") {
129 sumCalculators.push_back(new SorAbund());
130 }else if (Estimators[i] == "jclass") {
131 sumCalculators.push_back(new Jclass());
132 }else if (Estimators[i] == "sorclass") {
133 sumCalculators.push_back(new SorClass());
134 }else if (Estimators[i] == "jest") {
135 sumCalculators.push_back(new Jest());
136 }else if (Estimators[i] == "sorest") {
137 sumCalculators.push_back(new SorEst());
138 }else if (Estimators[i] == "thetayc") {
139 sumCalculators.push_back(new ThetaYC());
140 }else if (Estimators[i] == "thetan") {
141 sumCalculators.push_back(new ThetaN());
142 }else if (Estimators[i] == "kstest") {
143 sumCalculators.push_back(new KSTest());
144 }else if (Estimators[i] == "sharednseqs") {
145 sumCalculators.push_back(new SharedNSeqs());
146 }else if (Estimators[i] == "ochiai") {
147 sumCalculators.push_back(new Ochiai());
148 }else if (Estimators[i] == "anderberg") {
149 sumCalculators.push_back(new Anderberg());
150 }else if (Estimators[i] == "kulczynski") {
151 sumCalculators.push_back(new Kulczynski());
152 }else if (Estimators[i] == "kulczynskicody") {
153 sumCalculators.push_back(new KulczynskiCody());
154 }else if (Estimators[i] == "lennon") {
155 sumCalculators.push_back(new Lennon());
156 }else if (Estimators[i] == "morisitahorn") {
157 sumCalculators.push_back(new MorHorn());
158 }else if (Estimators[i] == "braycurtis") {
159 sumCalculators.push_back(new BrayCurtis());
160 }else if (Estimators[i] == "whittaker") {
161 sumCalculators.push_back(new Whittaker());
166 outputFileName = ((getRootName(globaldata->inputFileName)) + "shared.summary");
167 openOutputFile(outputFileName, outputFileHandle);
168 format = globaldata->getFormat();
174 catch(exception& e) {
175 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
179 cout << "An unknown error has occurred in the SummarySharedCommand class function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
184 //**********************************************************************************************************************
186 void SummarySharedCommand::help(){
188 cout << "The summary.shared command can only be executed after a successful read.otu command." << "\n";
189 cout << "The summary.shared command parameters are label, line and calc. No parameters are required, but you may not use " << "\n";
190 cout << "both the line and label parameters at the same time. The summary.shared command should be in the following format: " << "\n";
191 cout << "summary.shared(label=yourLabel, line=yourLines, calc=yourEstimators, groups=yourGroups)." << "\n";
192 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";
193 validCalculator->printCalc("sharedsummary", cout);
194 cout << "The default value for calc is sharedsobs-sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan" << "\n";
195 cout << "The default value for groups is all the groups in your groupfile." << "\n";
196 cout << "The label and line parameters are used to analyze specific lines in your input." << "\n";
197 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";
198 cout << "Note: No spaces between parameter labels (i.e. line), '=' and parameters (i.e.yourLines)." << "\n" << "\n";
200 catch(exception& e) {
201 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
205 cout << "An unknown error has occurred in the SummarySharedCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
210 //**********************************************************************************************************************
212 SummarySharedCommand::~SummarySharedCommand(){
215 delete validCalculator;
218 //**********************************************************************************************************************
220 int SummarySharedCommand::execute(){
223 if (abort == true) { return 0; }
227 //if the users entered no valid calculators don't execute command
228 if (sumCalculators.size() == 0) { return 0; }
229 //check if any calcs can do multiples
231 for (int i = 0; i < sumCalculators.size(); i++) {
232 if (sumCalculators[i]->getMultiple() == true) { mult = true; }
237 read = new ReadOTUFile(globaldata->inputFileName);
238 read->read(&*globaldata);
240 input = globaldata->ginput;
241 lookup = input->getSharedRAbundVectors();
242 vector<SharedRAbundVector*> lastLookup = lookup;
244 //output estimator names as column headers
245 outputFileHandle << "label" <<'\t' << "comparison" << '\t';
246 for(int i=0;i<sumCalculators.size();i++){
247 outputFileHandle << '\t' << sumCalculators[i]->getName();
249 outputFileHandle << endl;
251 //create file and put column headers for multiple groups file
253 outAllFileName = ((getRootName(globaldata->inputFileName)) + "sharedmultiple.summary");
254 openOutputFile(outAllFileName, outAll);
256 outAll << "label" <<'\t' << "comparison" << '\t';
257 for(int i=0;i<sumCalculators.size();i++){
258 if (sumCalculators[i]->getMultiple() == true) {
259 outAll << '\t' << sumCalculators[i]->getName();
265 if (lookup.size() < 2) {
266 cout << "I cannot run the command without at least 2 valid groups.";
267 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
269 //close files and clean up
270 outputFileHandle.close(); remove(outputFileName.c_str());
271 if (mult == true) { outAll.close(); remove(outAllFileName.c_str()); }
273 //if you only have 2 groups you don't need a .sharedmultiple file
274 }else if ((lookup.size() == 2) && (mult == true)) {
277 remove(outAllFileName.c_str());
280 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
281 set<string> processedLabels;
282 set<string> userLabels = labels;
283 set<int> userLines = lines;
285 //as long as you are not at the end of the file or done wih the lines you want
286 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
288 if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){
289 cout << lookup[0]->getLabel() << '\t' << count << endl;
292 processedLabels.insert(lookup[0]->getLabel());
293 userLabels.erase(lookup[0]->getLabel());
294 userLines.erase(count);
297 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
298 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
301 processedLabels.insert(lastLookup[0]->getLabel());
302 userLabels.erase(lastLookup[0]->getLabel());
306 //prevent memory leak
307 if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; } }
310 //get next line to process
311 lookup = input->getSharedRAbundVectors();
315 //output error messages about any remaining user labels
316 set<string>::iterator it;
317 bool needToRun = false;
318 for (it = userLabels.begin(); it != userLabels.end(); it++) {
319 cout << "Your file does not include the label "<< *it;
320 if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
321 cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
324 cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
328 //run last line if you need to
329 if (needToRun == true) {
330 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
334 for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; }
336 //reset groups parameter
337 globaldata->Groups.clear();
340 outputFileHandle.close();
341 if (mult == true) { outAll.close(); }
345 catch(exception& e) {
346 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
350 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
355 /***********************************************************/
356 void SummarySharedCommand::process(vector<SharedRAbundVector*> thisLookup) {
358 //loop through calculators and add to file all for all calcs that can do mutiple groups
361 outAll << thisLookup[0]->getLabel() << '\t';
363 //output groups names
364 string outNames = "";
365 for (int j = 0; j < thisLookup.size(); j++) {
366 outNames += thisLookup[j]->getGroup() + "-";
368 outNames = outNames.substr(0, outNames.length()-1); //rip off extra '-';
369 outAll << outNames << '\t';
371 for(int i=0;i<sumCalculators.size();i++){
372 if (sumCalculators[i]->getMultiple() == true) {
373 sumCalculators[i]->getValues(thisLookup);
375 sumCalculators[i]->print(outAll);
382 vector<SharedRAbundVector*> subset;
383 for (int k = 0; k < (thisLookup.size() - 1); k++) { // pass cdd each set of groups to commpare
384 for (int l = n; l < thisLookup.size(); l++) {
386 outputFileHandle << thisLookup[0]->getLabel() << '\t';
388 subset.clear(); //clear out old pair of sharedrabunds
389 //add new pair of sharedrabunds
390 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]);
392 //sort groups to be alphanumeric
393 if (thisLookup[k]->getGroup() > thisLookup[l]->getGroup()) {
394 outputFileHandle << (thisLookup[l]->getGroup() +'\t' + thisLookup[k]->getGroup()) << '\t'; //print out groups
396 outputFileHandle << (thisLookup[k]->getGroup() +'\t' + thisLookup[l]->getGroup()) << '\t'; //print out groups
399 for(int i=0;i<sumCalculators.size();i++) {
401 sumCalculators[i]->getValues(subset); //saves the calculator outputs
402 outputFileHandle << '\t';
403 sumCalculators[i]->print(outputFileHandle);
405 outputFileHandle << endl;
411 catch(exception& e) {
412 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
416 cout << "An unknown error has occurred in the SummarySharedCommand class function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
421 /***********************************************************/