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(string option) {
24 globaldata = GlobalData::getInstance();
29 //allow user to run help
30 if(option == "help") { help(); abort = true; }
33 //valid paramters for this command
34 string AlignArray[] = {"groups","label","calc", "abund","outputdir","inputdir"};
35 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
37 OptionParser parser(option);
38 map<string,string> parameters = parser.getParameters();
40 ValidParameters validParameter;
42 //check to make sure all parameters are valid for command
43 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
44 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
47 //make sure the user has already run the read.otu command
48 if ((globaldata->getListFile() == "") && (globaldata->getSharedFile() == "")) {
49 m->mothurOut("You must read a list, or a list and a group, or a shared before you can use the venn command."); m->mothurOutEndLine(); abort = true;
52 //if the user changes the output directory command factory will send this info to us in the output parameter
53 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
55 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
58 //check for optional parameter and set defaults
59 // ...at some point should added some additional type checking...
60 label = validParameter.validFile(parameters, "label", false);
61 if (label == "not found") { label = ""; }
63 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
64 else { allLines = 1; }
67 //if the user has not specified any labels use the ones from read.otu
69 allLines = globaldata->allLines;
70 labels = globaldata->labels;
73 groups = validParameter.validFile(parameters, "groups", false);
74 if (groups == "not found") { groups = ""; }
76 splitAtDash(groups, Groups);
77 globaldata->Groups = Groups;
80 format = globaldata->getFormat();
81 calc = validParameter.validFile(parameters, "calc", false);
82 if (calc == "not found") {
83 if(format == "list") { calc = "sobs"; }
84 else { calc = "sharedsobs"; }
87 if (calc == "default") {
88 if(format == "list") { calc = "sobs"; }
89 else { calc = "sharedsobs"; }
92 splitAtDash(calc, Estimators);
95 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
99 validCalculator = new ValidCalculators();
103 if (format == "list") {
104 for (i=0; i<Estimators.size(); i++) {
105 if (validCalculator->isValidCalculator("vennsingle", Estimators[i]) == true) {
106 if (Estimators[i] == "sobs") {
107 vennCalculators.push_back(new Sobs());
108 }else if (Estimators[i] == "chao") {
109 vennCalculators.push_back(new Chao1());
110 }else if (Estimators[i] == "ace") {
113 vennCalculators.push_back(new Ace(abund));
118 for (i=0; i<Estimators.size(); i++) {
119 if (validCalculator->isValidCalculator("vennshared", Estimators[i]) == true) {
120 if (Estimators[i] == "sharedsobs") {
121 vennCalculators.push_back(new SharedSobsCS());
122 }else if (Estimators[i] == "sharedchao") {
123 vennCalculators.push_back(new SharedChao1());
124 }else if (Estimators[i] == "sharedace") {
125 vennCalculators.push_back(new SharedAce());
131 venn = new Venn(outputDir);
138 catch(exception& e) {
139 m->errorOut(e, "VennCommand", "VennCommand");
144 //**********************************************************************************************************************
146 void VennCommand::help(){
148 m->mothurOut("The venn command can only be executed after a successful read.otu command.\n");
149 m->mothurOut("The venn command parameters are groups, calc, abund and label. No parameters are required.\n");
150 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your venn diagram, you may only use a maximum of 4 groups.\n");
151 m->mothurOut("The group names are separated by dashes. The label allows you to select what distance levels you would like a venn diagram created for, and are also separated by dashes.\n");
152 m->mothurOut("The venn command should be in the following format: venn(groups=yourGroups, calc=yourCalcs, label=yourLabels, abund=yourAbund).\n");
153 m->mothurOut("Example venn(groups=A-B-C, calc=sharedsobs-sharedchao, abund=20).\n");
154 m->mothurOut("The default value for groups is all the groups in your groupfile up to 4, and all labels in your inputfile will be used.\n");
155 m->mothurOut("The default value for calc is sobs if you have only read a list file or if you have selected only one group, and sharedsobs if you have multiple groups.\n");
156 m->mothurOut("The default available estimators for calc are sobs, chao and ace if you have only read a list file, and sharedsobs, sharedchao and sharedace if you have read a list and group file or a shared file.\n");
157 m->mothurOut("The only estmiator available four 4 groups is sharedsobs.\n");
158 m->mothurOut("The venn command outputs a .svg file for each calculator you specify at each distance you choose.\n");
159 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
161 catch(exception& e) {
162 m->errorOut(e, "VennCommand", "help");
168 //**********************************************************************************************************************
170 VennCommand::~VennCommand(){
171 if (abort == false) {
172 delete input; globaldata->ginput = NULL;
175 globaldata->sabund = NULL;
176 delete validCalculator;
181 //**********************************************************************************************************************
183 int VennCommand::execute(){
186 if (abort == true) { return 0; }
189 vector<string> outputNames;
191 //if the users entered no valid calculators don't execute command
192 if (vennCalculators.size() == 0) { return 0; }
194 if (format == "sharedfile") {
196 read = new ReadOTUFile(globaldata->inputFileName);
197 read->read(&*globaldata);
199 input = globaldata->ginput;
200 lookup = input->getSharedRAbundVectors();
201 lastLabel = lookup[0]->getLabel();
202 }else if (format == "list") {
203 //you are using just a list file and have only one group
204 read = new ReadOTUFile(globaldata->inputFileName);
205 read->read(&*globaldata);
207 sabund = globaldata->sabund;
208 lastLabel = sabund->getLabel();
209 input = globaldata->ginput;
212 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
213 set<string> processedLabels;
214 set<string> userLabels = labels;
216 if (format != "list") {
218 //as long as you are not at the end of the file or done wih the lines you want
219 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
221 if (m->control_pressed) {
222 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
223 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
224 globaldata->Groups.clear();
225 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
229 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
230 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
231 processedLabels.insert(lookup[0]->getLabel());
232 userLabels.erase(lookup[0]->getLabel());
234 if (lookup.size() > 4) {
235 m->mothurOut("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."); m->mothurOutEndLine();
236 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
239 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
240 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } }
243 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
244 string saveLabel = lookup[0]->getLabel();
246 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
247 lookup = input->getSharedRAbundVectors(lastLabel);
249 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
250 processedLabels.insert(lookup[0]->getLabel());
251 userLabels.erase(lookup[0]->getLabel());
253 if (lookup.size() > 4) {
254 m->mothurOut("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."); m->mothurOutEndLine();
255 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
257 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
258 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } }
260 //restore real lastlabel to save below
261 lookup[0]->setLabel(saveLabel);
265 lastLabel = lookup[0]->getLabel();
267 //get next line to process
268 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
269 lookup = input->getSharedRAbundVectors();
272 if (m->control_pressed) {
273 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
274 globaldata->Groups.clear();
275 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
280 //output error messages about any remaining user labels
281 set<string>::iterator it;
282 bool needToRun = false;
283 for (it = userLabels.begin(); it != userLabels.end(); it++) {
284 m->mothurOut("Your file does not include the label " + *it);
285 if (processedLabels.count(lastLabel) != 1) {
286 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
289 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
293 //run last label if you need to
294 if (needToRun == true) {
295 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
296 lookup = input->getSharedRAbundVectors(lastLabel);
298 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
299 processedLabels.insert(lookup[0]->getLabel());
300 userLabels.erase(lookup[0]->getLabel());
302 if (lookup.size() > 4) {
303 m->mothurOut("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."); m->mothurOutEndLine();
304 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
306 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
307 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } }
309 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
313 //reset groups parameter
314 globaldata->Groups.clear();
316 if (m->control_pressed) {
317 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
318 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
325 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
327 if (m->control_pressed) {
328 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
330 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
334 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
336 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
337 vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
338 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } }
341 processedLabels.insert(sabund->getLabel());
342 userLabels.erase(sabund->getLabel());
345 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
346 string saveLabel = sabund->getLabel();
349 sabund = input->getSAbundVector(lastLabel);
351 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
352 vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
353 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } }
356 processedLabels.insert(sabund->getLabel());
357 userLabels.erase(sabund->getLabel());
359 //restore real lastlabel to save below
360 sabund->setLabel(saveLabel);
363 lastLabel = sabund->getLabel();
366 sabund = input->getSAbundVector();
369 if (m->control_pressed) {
370 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
371 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
375 //output error messages about any remaining user labels
376 set<string>::iterator it;
377 bool needToRun = false;
378 for (it = userLabels.begin(); it != userLabels.end(); it++) {
379 m->mothurOut("Your file does not include the label " + *it);
380 if (processedLabels.count(lastLabel) != 1) {
381 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
384 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
388 //run last label if you need to
389 if (needToRun == true) {
390 if (sabund != NULL) { delete sabund; }
391 sabund = input->getSAbundVector(lastLabel);
393 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
394 vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
395 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } }
401 if (m->control_pressed) {
402 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
403 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
408 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
410 m->mothurOutEndLine();
411 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
412 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
413 m->mothurOutEndLine();
418 catch(exception& e) {
419 m->errorOut(e, "VennCommand", "execute");
424 //**********************************************************************************************************************