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"
21 //**********************************************************************************************************************
22 vector<string> VennCommand::getValidParameters(){
24 string Array[] = {"groups","label","calc","permute", "abund","nseqs","outputdir","inputdir"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
29 m->errorOut(e, "VennCommand", "getValidParameters");
33 //**********************************************************************************************************************
34 VennCommand::VennCommand(){
36 //initialize outputTypes
37 vector<string> tempOutNames;
38 outputTypes["svg"] = tempOutNames;
41 m->errorOut(e, "VennCommand", "VennCommand");
45 //**********************************************************************************************************************
46 vector<string> VennCommand::getRequiredParameters(){
48 vector<string> myArray;
52 m->errorOut(e, "VennCommand", "getRequiredParameters");
56 //**********************************************************************************************************************
57 vector<string> VennCommand::getRequiredFiles(){
59 string Array[] = {"list","shared","or"};
60 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
64 m->errorOut(e, "VennCommand", "getRequiredFiles");
68 //**********************************************************************************************************************
70 VennCommand::VennCommand(string option) {
72 globaldata = GlobalData::getInstance();
77 //allow user to run help
78 if(option == "help") { help(); abort = true; }
81 //valid paramters for this command
82 string AlignArray[] = {"groups","label","calc","permute", "abund","nseqs","outputdir","inputdir"};
83 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
85 OptionParser parser(option);
86 map<string,string> parameters = parser.getParameters();
88 ValidParameters validParameter;
90 //check to make sure all parameters are valid for command
91 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
92 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
95 //make sure the user has already run the read.otu command
96 if ((globaldata->getListFile() == "") && (globaldata->getSharedFile() == "")) {
97 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;
100 //if the user changes the output directory command factory will send this info to us in the output parameter
101 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
103 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
106 //check for optional parameter and set defaults
107 // ...at some point should added some additional type checking...
108 label = validParameter.validFile(parameters, "label", false);
109 if (label == "not found") { label = ""; }
111 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
112 else { allLines = 1; }
115 //if the user has not specified any labels use the ones from read.otu
117 allLines = globaldata->allLines;
118 labels = globaldata->labels;
121 groups = validParameter.validFile(parameters, "groups", false);
122 if (groups == "not found") { groups = ""; }
124 m->splitAtDash(groups, Groups);
125 globaldata->Groups = Groups;
128 format = globaldata->getFormat();
129 calc = validParameter.validFile(parameters, "calc", false);
130 if (calc == "not found") {
131 if(format == "list") { calc = "sobs"; }
132 else { calc = "sharedsobs"; }
135 if (calc == "default") {
136 if(format == "list") { calc = "sobs"; }
137 else { calc = "sharedsobs"; }
140 m->splitAtDash(calc, Estimators);
143 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
144 convert(temp, abund);
146 temp = validParameter.validFile(parameters, "nseqs", false); if (temp == "not found"){ temp = "f"; }
147 nseqs = m->isTrue(temp);
149 temp = validParameter.validFile(parameters, "permute", false); if (temp == "not found"){ temp = "f"; }
150 perm = m->isTrue(temp);
152 if (abort == false) {
153 validCalculator = new ValidCalculators();
157 if (format == "list") {
158 for (i=0; i<Estimators.size(); i++) {
159 if (validCalculator->isValidCalculator("vennsingle", Estimators[i]) == true) {
160 if (Estimators[i] == "sobs") {
161 vennCalculators.push_back(new Sobs());
162 }else if (Estimators[i] == "chao") {
163 vennCalculators.push_back(new Chao1());
164 }else if (Estimators[i] == "ace") {
167 vennCalculators.push_back(new Ace(abund));
172 for (i=0; i<Estimators.size(); i++) {
173 if (validCalculator->isValidCalculator("vennshared", Estimators[i]) == true) {
174 if (Estimators[i] == "sharedsobs") {
175 vennCalculators.push_back(new SharedSobsCS());
176 }else if (Estimators[i] == "sharedchao") {
177 vennCalculators.push_back(new SharedChao1());
178 }else if (Estimators[i] == "sharedace") {
179 vennCalculators.push_back(new SharedAce());
185 //if the users entered no valid calculators don't execute command
186 if (vennCalculators.size() == 0) { m->mothurOut("No valid calculators given, please correct."); m->mothurOutEndLine(); abort = true; }
187 else { venn = new Venn(outputDir, nseqs); }
195 catch(exception& e) {
196 m->errorOut(e, "VennCommand", "VennCommand");
201 //**********************************************************************************************************************
203 void VennCommand::help(){
205 m->mothurOut("The venn command can only be executed after a successful read.otu command.\n");
206 m->mothurOut("The venn command parameters are groups, calc, abund, nseqs, permute and label. No parameters are required.\n");
207 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");
208 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");
209 m->mothurOut("The venn command should be in the following format: venn(groups=yourGroups, calc=yourCalcs, label=yourLabels, abund=yourAbund).\n");
210 m->mothurOut("Example venn(groups=A-B-C, calc=sharedsobs-sharedchao, abund=20).\n");
211 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");
212 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");
213 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");
214 m->mothurOut("The nseqs parameter will output the number of sequences represented by the otus in the picture, default=F.\n");
215 m->mothurOut("If you have more than 4 groups, the permute parameter will find all possible combos of 4 of your groups and create pictures for them, default=F.\n");
216 m->mothurOut("The only estimators available four 4 groups are sharedsobs and sharedchao.\n");
217 m->mothurOut("The venn command outputs a .svg file for each calculator you specify at each distance you choose.\n");
218 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
220 catch(exception& e) {
221 m->errorOut(e, "VennCommand", "help");
227 //**********************************************************************************************************************
229 VennCommand::~VennCommand(){
230 if (abort == false) {
231 delete input; globaldata->ginput = NULL;
234 globaldata->sabund = NULL;
235 delete validCalculator;
240 //**********************************************************************************************************************
242 int VennCommand::execute(){
245 if (abort == true) { return 0; }
249 if (format == "sharedfile") {
251 read = new ReadOTUFile(globaldata->inputFileName);
252 read->read(&*globaldata);
254 input = globaldata->ginput;
255 lookup = input->getSharedRAbundVectors();
256 lastLabel = lookup[0]->getLabel();
258 if ((lookup.size() > 4) && (perm)) { combosOfFour = findCombinations(lookup.size()); }
259 }else if (format == "list") {
260 //you are using just a list file and have only one group
261 read = new ReadOTUFile(globaldata->inputFileName);
262 read->read(&*globaldata);
264 sabund = globaldata->sabund;
265 lastLabel = sabund->getLabel();
266 input = globaldata->ginput;
269 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
270 set<string> processedLabels;
271 set<string> userLabels = labels;
273 if (format != "list") {
275 //as long as you are not at the end of the file or done wih the lines you want
276 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
278 if (m->control_pressed) {
279 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
280 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
281 globaldata->Groups.clear();
282 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
286 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
287 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
288 processedLabels.insert(lookup[0]->getLabel());
289 userLabels.erase(lookup[0]->getLabel());
291 if ((lookup.size() > 4) && (!perm)){
292 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. If you set perm=t, I will find all possible combos of 4 groups."); m->mothurOutEndLine();
293 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
295 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
296 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } }
298 }else if ((lookup.size() > 4) && (perm)) {
299 set< set<int> >::iterator it3;
300 set<int>::iterator it2;
301 for (it3 = combosOfFour.begin(); it3 != combosOfFour.end(); it3++) {
303 set<int> poss = *it3;
304 vector<SharedRAbundVector*> subset;
305 for (it2 = poss.begin(); it2 != poss.end(); it2++) { subset.push_back(lookup[*it2]); }
307 vector<string> outfilenames = venn->getPic(subset, vennCalculators);
308 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } }
311 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
312 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } }
316 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
317 string saveLabel = lookup[0]->getLabel();
319 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
320 lookup = input->getSharedRAbundVectors(lastLabel);
322 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
323 processedLabels.insert(lookup[0]->getLabel());
324 userLabels.erase(lookup[0]->getLabel());
326 if ((lookup.size() > 4) && (!perm)){
327 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. If you set perm=t, I will find all possible combos of 4 groups."); m->mothurOutEndLine();
328 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
330 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
331 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } }
333 }else if ((lookup.size() > 4) && (perm)) {
334 set< set<int> >::iterator it3;
335 set<int>::iterator it2;
336 for (it3 = combosOfFour.begin(); it3 != combosOfFour.end(); it3++) {
338 set<int> poss = *it3;
339 vector<SharedRAbundVector*> subset;
340 for (it2 = poss.begin(); it2 != poss.end(); it2++) { subset.push_back(lookup[*it2]); }
342 vector<string> outfilenames = venn->getPic(subset, vennCalculators);
343 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } }
346 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
347 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } }
350 //restore real lastlabel to save below
351 lookup[0]->setLabel(saveLabel);
355 lastLabel = lookup[0]->getLabel();
357 //get next line to process
358 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
359 lookup = input->getSharedRAbundVectors();
362 if (m->control_pressed) {
363 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
364 globaldata->Groups.clear();
365 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
370 //output error messages about any remaining user labels
371 set<string>::iterator it;
372 bool needToRun = false;
373 for (it = userLabels.begin(); it != userLabels.end(); it++) {
374 m->mothurOut("Your file does not include the label " + *it);
375 if (processedLabels.count(lastLabel) != 1) {
376 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
379 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
383 //run last label if you need to
384 if (needToRun == true) {
385 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
386 lookup = input->getSharedRAbundVectors(lastLabel);
388 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
389 processedLabels.insert(lookup[0]->getLabel());
390 userLabels.erase(lookup[0]->getLabel());
392 if ((lookup.size() > 4) && (!perm)){
393 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. If you set perm=t, I will find all possible combos of 4 groups."); m->mothurOutEndLine();
394 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
396 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
397 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } }
399 }else if ((lookup.size() > 4) && (perm)) {
400 set< set<int> >::iterator it3;
401 set<int>::iterator it2;
402 for (it3 = combosOfFour.begin(); it3 != combosOfFour.end(); it3++) {
404 set<int> poss = *it3;
405 vector<SharedRAbundVector*> subset;
406 for (it2 = poss.begin(); it2 != poss.end(); it2++) { subset.push_back(lookup[*it2]); }
408 vector<string> outfilenames = venn->getPic(subset, vennCalculators);
409 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } }
412 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
413 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } }
416 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
420 //reset groups parameter
421 globaldata->Groups.clear();
423 if (m->control_pressed) {
424 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
425 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
432 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
434 if (m->control_pressed) {
435 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
437 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
441 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
443 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
444 vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
445 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } }
448 processedLabels.insert(sabund->getLabel());
449 userLabels.erase(sabund->getLabel());
452 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
453 string saveLabel = sabund->getLabel();
456 sabund = input->getSAbundVector(lastLabel);
458 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
459 vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
460 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } }
463 processedLabels.insert(sabund->getLabel());
464 userLabels.erase(sabund->getLabel());
466 //restore real lastlabel to save below
467 sabund->setLabel(saveLabel);
470 lastLabel = sabund->getLabel();
473 sabund = input->getSAbundVector();
476 if (m->control_pressed) {
477 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
478 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
482 //output error messages about any remaining user labels
483 set<string>::iterator it;
484 bool needToRun = false;
485 for (it = userLabels.begin(); it != userLabels.end(); it++) {
486 m->mothurOut("Your file does not include the label " + *it);
487 if (processedLabels.count(lastLabel) != 1) {
488 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
491 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
495 //run last label if you need to
496 if (needToRun == true) {
497 if (sabund != NULL) { delete sabund; }
498 sabund = input->getSAbundVector(lastLabel);
500 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
501 vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
502 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); } }
508 if (m->control_pressed) {
509 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
510 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
515 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
517 m->mothurOutEndLine();
518 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
519 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
520 m->mothurOutEndLine();
525 catch(exception& e) {
526 m->errorOut(e, "VennCommand", "execute");
530 //**********************************************************************************************************************
531 //returns a vector of sets containing the 4 group combinations
532 set< set<int> > VennCommand::findCombinations(int lookupSize){
534 set< set<int> > combos;
537 for (int i = 0; i < lookupSize; i++) { possibles.insert(i); }
539 getCombos(possibles, combos);
544 catch(exception& e) {
545 m->errorOut(e, "VennCommand", "findCombinations");
549 //**********************************************************************************************************************
550 //recusively finds combos of 4
551 int VennCommand::getCombos(set<int> possibles, set< set<int> >& combos){
554 if (possibles.size() == 4) { //done
555 if (combos.count(possibles) == 0) { //no dups
556 combos.insert(possibles);
558 }else { //we still have work to do
559 set<int>::iterator it;
560 set<int>::iterator it2;
561 for (it = possibles.begin(); it != possibles.end(); it++) {
563 set<int> newPossibles;
564 for (it2 = possibles.begin(); it2 != possibles.end(); it2++) { //all possible combos of one length smaller
566 newPossibles.insert(*it2);
569 getCombos(newPossibles, combos);
575 catch(exception& e) {
576 m->errorOut(e, "VennCommand", "getCombos");
581 //**********************************************************************************************************************