5 * Created by Sarah Westcott on 3/30/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "venncommand.h"
14 //#include "jackknife.h"
15 #include "sharedsobscollectsummary.h"
16 #include "sharedchao1.h"
17 #include "sharedace.h"
20 //**********************************************************************************************************************
22 VennCommand::VennCommand(){
24 globaldata = GlobalData::getInstance();
25 format = globaldata->getFormat();
26 validCalculator = new ValidCalculators();
30 if (format == "list") {
31 for (i=0; i<globaldata->Estimators.size(); i++) {
32 if (validCalculator->isValidCalculator("vennsingle", globaldata->Estimators[i]) == true) {
33 if (globaldata->Estimators[i] == "sobs") {
34 vennCalculators.push_back(new Sobs());
35 }else if (globaldata->Estimators[i] == "chao") {
36 vennCalculators.push_back(new Chao1());
37 }else if (globaldata->Estimators[i] == "ace") {
38 convert(globaldata->getAbund(), abund);
41 vennCalculators.push_back(new Ace(abund));
42 //}else if (globaldata->Estimators[i] == "jack") {
43 //vennCalculators.push_back(new Jackknife());
48 for (i=0; i<globaldata->Estimators.size(); i++) {
49 if (validCalculator->isValidCalculator("vennshared", globaldata->Estimators[i]) == true) {
50 if (globaldata->Estimators[i] == "sharedsobs") {
51 vennCalculators.push_back(new SharedSobsCS());
52 }else if (globaldata->Estimators[i] == "sharedchao") {
53 vennCalculators.push_back(new SharedChao1());
54 }else if (globaldata->Estimators[i] == "sharedace") {
55 vennCalculators.push_back(new SharedAce());
63 //reset calc for next command
64 globaldata->setCalc("");
69 cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function VennCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
73 cout << "An unknown error has occurred in the VennCommand class function VennCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
77 //**********************************************************************************************************************
79 VennCommand::~VennCommand(){
83 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
86 //**********************************************************************************************************************
88 int VennCommand::execute(){
91 SAbundVector* lastSAbund;
92 vector<SharedRAbundVector*> lastLookup;
94 //if the users entered no valid calculators don't execute command
95 if (vennCalculators.size() == 0) { return 0; }
97 if (format == "sharedfile") {
99 read = new ReadOTUFile(globaldata->inputFileName);
100 read->read(&*globaldata);
102 input = globaldata->ginput;
103 lookup = input->getSharedRAbundVectors();
105 }else if (format == "list") {
106 //you are using just a list file and have only one group
107 read = new ReadOTUFile(globaldata->inputFileName);
108 read->read(&*globaldata);
110 sabund = globaldata->sabund;
111 lastSAbund = globaldata->sabund;
112 input = globaldata->ginput;
115 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
116 set<string> processedLabels;
117 set<string> userLabels = globaldata->labels;
119 if (format != "list") {
121 //as long as you are not at the end of the file or done wih the lines you want
122 while((lookup[0] != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
124 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){
125 cout << lookup[0]->getLabel() << '\t' << count << endl;
126 processedLabels.insert(lookup[0]->getLabel());
127 userLabels.erase(lookup[0]->getLabel());
129 if (lookup.size() > 4) {
130 cout << "Error: Too many groups chosen. You may use up to 4 groups with the venn command. I will use the first four groups in your groupfile." << endl;
131 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
133 venn->getPic(lookup, vennCalculators);
136 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
137 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
138 processedLabels.insert(lastLookup[0]->getLabel());
139 userLabels.erase(lastLookup[0]->getLabel());
141 if (lastLookup.size() > 4) {
142 cout << "Error: Too many groups chosen. You may use up to 4 groups with the venn command. I will use the first four groups in your groupfile." << endl;
143 for (int i = lastLookup.size(); i > 4; i--) { lastLookup.pop_back(); } //no memmory leak because pop_back calls destructor
145 venn->getPic(lastLookup, vennCalculators);
148 //prevent memory leak
149 if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; } }
152 //get next line to process
153 lookup = input->getSharedRAbundVectors();
157 //output error messages about any remaining user labels
158 set<string>::iterator it;
159 bool needToRun = false;
160 for (it = userLabels.begin(); it != userLabels.end(); it++) {
161 cout << "Your file does not include the label "<< *it;
162 if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
163 cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
166 cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
170 //run last line if you need to
171 if (needToRun == true) {
172 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
173 if (lastLookup.size() > 4) {
174 cout << "Error: Too many groups chosen. You may use up to 4 groups with the venn command. I will use the first four groups in your groupfile." << endl;
175 for (int i = lastLookup.size(); i > 3; i--) { delete lastLookup[i]; lastLookup.pop_back(); }
177 venn->getPic(lastLookup, vennCalculators);
180 for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; }
182 //reset groups parameter
183 globaldata->Groups.clear();
187 while((sabund != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
189 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(sabund->getLabel()) == 1){
191 cout << sabund->getLabel() << '\t' << count << endl;
192 venn->getPic(sabund, vennCalculators);
194 processedLabels.insert(sabund->getLabel());
195 userLabels.erase(sabund->getLabel());
198 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastSAbund->getLabel()) != 1)) {
200 cout << lastSAbund->getLabel() << '\t' << count << endl;
201 venn->getPic(lastSAbund, vennCalculators);
203 processedLabels.insert(lastSAbund->getLabel());
204 userLabels.erase(lastSAbund->getLabel());
207 if (count != 1) { delete lastSAbund; }
210 sabund = input->getSAbundVector();
214 //output error messages about any remaining user labels
215 set<string>::iterator it;
216 bool needToRun = false;
217 for (it = userLabels.begin(); it != userLabels.end(); it++) {
218 cout << "Your file does not include the label "<< *it;
219 if (processedLabels.count(lastSAbund->getLabel()) != 1) {
220 cout << ". I will use " << lastSAbund->getLabel() << "." << endl;
223 cout << ". Please refer to " << lastSAbund->getLabel() << "." << endl;
227 //run last line if you need to
228 if (needToRun == true) {
229 cout << lastSAbund->getLabel() << '\t' << count << endl;
230 venn->getPic(lastSAbund, vennCalculators);
236 globaldata->setGroups("");
239 catch(exception& e) {
240 cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
244 cout << "An unknown error has occurred in the VennCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
249 //**********************************************************************************************************************