]> git.donarmstrong.com Git - mothur.git/blob - venncommand.cpp
1.23.0
[mothur.git] / venncommand.cpp
1 /*
2  *  venncommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 3/30/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "venncommand.h"
11 #include "ace.h"
12 #include "sobs.h"
13 #include "chao1.h"
14 //#include "jackknife.h"
15 #include "sharedsobscollectsummary.h"
16 #include "sharedchao1.h"
17 #include "sharedace.h"
18 #include "nseqs.h"
19
20 //**********************************************************************************************************************
21 vector<string> VennCommand::setParameters(){    
22         try {
23                 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(plist);
24                 CommandParameter pshared("shared", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(pshared);    
25                 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
26                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
27                 CommandParameter pcalc("calc", "String", "", "", "", "", "",false,false); parameters.push_back(pcalc);
28                 CommandParameter pabund("abund", "Number", "", "10", "", "", "",false,false); parameters.push_back(pabund);
29                 CommandParameter pnseqs("nseqs", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pnseqs);
30                 CommandParameter pfontsize("fontsize", "Number", "", "24", "", "", "",false,false); parameters.push_back(pfontsize);
31                 CommandParameter ppermute("permute", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(ppermute);
32                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
33                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
34                 
35                 vector<string> myArray;
36                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
37                 return myArray;
38         }
39         catch(exception& e) {
40                 m->errorOut(e, "VennCommand", "setParameters");
41                 exit(1);
42         }
43 }
44 //**********************************************************************************************************************
45 string VennCommand::getHelpString(){    
46         try {
47                 string helpString = "";
48                 helpString += "The venn command parameters are list, shared, groups, calc, abund, nseqs, permute, fontsize and label.   shared, relabund, list, rabund or sabund is required unless you have a valid current file.\n";
49                 helpString += "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";
50                 helpString += "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";
51                 helpString += "The fontsize parameter allows you to adjust the font size of the picture created, default=24.\n";
52                 helpString += "The venn command should be in the following format: venn(groups=yourGroups, calc=yourCalcs, label=yourLabels, abund=yourAbund).\n";
53                 helpString += "Example venn(groups=A-B-C, calc=sharedsobs-sharedchao, abund=20).\n";
54                 helpString += "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";
55                 helpString += "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";
56                 helpString += "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 shared file.\n";
57                 helpString += "The nseqs parameter will output the number of sequences represented by the otus in the picture, default=F.\n";
58                 helpString += "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";
59                 helpString += "The only estimators available four 4 groups are sharedsobs and sharedchao.\n";
60                 helpString += "The venn command outputs a .svg file for each calculator you specify at each distance you choose.\n";
61                 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
62                 return helpString;
63         }
64         catch(exception& e) {
65                 m->errorOut(e, "VennCommand", "getHelpString");
66                 exit(1);
67         }
68 }
69
70 //**********************************************************************************************************************
71 VennCommand::VennCommand(){     
72         try {
73                 abort = true; calledHelp = true; 
74                 setParameters();
75                 vector<string> tempOutNames;
76                 outputTypes["svg"] = tempOutNames;
77         }
78         catch(exception& e) {
79                 m->errorOut(e, "VennCommand", "VennCommand");
80                 exit(1);
81         }
82 }
83 //**********************************************************************************************************************
84
85 VennCommand::VennCommand(string option)  {
86         try {
87                 abort = false; calledHelp = false;   
88                 allLines = 1;
89                         
90                 //allow user to run help
91                 if(option == "help") { help(); abort = true; calledHelp = true; }
92                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
93                 
94                 else {
95                         vector<string> myArray = setParameters();
96                         
97                         OptionParser parser(option);
98                         map<string,string> parameters = parser.getParameters();
99                         map<string,string>::iterator it;
100                         
101                         ValidParameters validParameter;
102                         
103                         //check to make sure all parameters are valid for command
104                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
105                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
106                         }
107                         
108                         //if the user changes the input directory command factory will send this info to us in the output parameter 
109                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
110                         if (inputDir == "not found"){   inputDir = "";          }
111                         else {
112                                 string path;
113                                 it = parameters.find("shared");
114                                 //user has given a template file
115                                 if(it != parameters.end()){ 
116                                         path = m->hasPath(it->second);
117                                         //if the user has not given a path then, add inputdir. else leave path alone.
118                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
119                                 }
120                                 
121                                 it = parameters.find("list");
122                                 //user has given a template file
123                                 if(it != parameters.end()){ 
124                                         path = m->hasPath(it->second);
125                                         //if the user has not given a path then, add inputdir. else leave path alone.
126                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
127                                 }
128                         }
129                         
130                         //check for required parameters
131                         listfile = validParameter.validFile(parameters, "list", true);
132                         if (listfile == "not open") { listfile = ""; abort = true; }
133                         else if (listfile == "not found") { listfile = ""; }
134                         else {  format = "list"; inputfile = listfile; m->setListFile(listfile); }
135                         
136                         sharedfile = validParameter.validFile(parameters, "shared", true);
137                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }        
138                         else if (sharedfile == "not found") { sharedfile = ""; }
139                         else {  format = "sharedfile"; inputfile = sharedfile; m->setSharedFile(sharedfile); }
140                         
141                         if ((sharedfile == "") && (listfile == "")) { 
142                                 //is there are current file available for any of these?
143                                 //give priority to shared, then list, then rabund, then sabund
144                                 //if there is a current shared file, use it
145                                 sharedfile = m->getSharedFile(); 
146                                 if (sharedfile != "") { inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
147                                 else { 
148                                         listfile = m->getListFile(); 
149                                         if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
150                                         else { 
151                                                 m->mothurOut("No valid current files. You must provide a list or shared file."); m->mothurOutEndLine(); 
152                                                 abort = true;
153                                         }
154                                 }
155                         }
156                         
157                         //if the user changes the output directory command factory will send this info to us in the output parameter 
158                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(inputfile);              }
159
160                         //check for optional parameter and set defaults
161                         // ...at some point should added some additional type checking...
162                         label = validParameter.validFile(parameters, "label", false);                   
163                         if (label == "not found") { label = ""; }
164                         else { 
165                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
166                                 else { allLines = 1;  }
167                         }
168                         
169                         groups = validParameter.validFile(parameters, "groups", false);                 
170                         if (groups == "not found") { groups = ""; }
171                         else { 
172                                 m->splitAtDash(groups, Groups);
173                                 m->setGroups(Groups);
174                         }
175                         
176                         calc = validParameter.validFile(parameters, "calc", false);                     
177                         if (calc == "not found") { 
178                                 if(format == "list") { calc = "sobs"; }
179                                 else { calc = "sharedsobs"; }
180                         }
181                         else { 
182                                  if (calc == "default")  {  
183                                         if(format == "list") { calc = "sobs"; }
184                                         else { calc = "sharedsobs"; }
185                                 }
186                         }
187                         m->splitAtDash(calc, Estimators);
188                         if (m->inUsersGroups("citation", Estimators)) { 
189                                 ValidCalculators validCalc; validCalc.printCitations(Estimators); 
190                                 //remove citation from list of calcs
191                                 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") {  Estimators.erase(Estimators.begin()+i); break; } }
192                         }
193                         
194                         string temp;
195                         temp = validParameter.validFile(parameters, "abund", false);            if (temp == "not found") { temp = "10"; }
196                         m->mothurConvert(temp, abund); 
197                         
198                         temp = validParameter.validFile(parameters, "nseqs", false);            if (temp == "not found"){       temp = "f";                             }
199                         nseqs = m->isTrue(temp); 
200
201                         temp = validParameter.validFile(parameters, "permute", false);          if (temp == "not found"){       temp = "f";                             }
202                         perm = m->isTrue(temp); 
203                         
204                         temp = validParameter.validFile(parameters, "fontsize", false);         if (temp == "not found") { temp = "24"; }
205                         m->mothurConvert(temp, fontsize);
206
207                 }
208                                 
209         }
210         catch(exception& e) {
211                 m->errorOut(e, "VennCommand", "VennCommand");
212                 exit(1);
213         }
214 }
215 //**********************************************************************************************************************
216
217 int VennCommand::execute(){
218         try {
219         
220                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
221         
222                 ValidCalculators validCalculator;
223                                         
224                 if (format == "list") {
225                         for (int i=0; i<Estimators.size(); i++) {
226                                 if (validCalculator.isValidCalculator("vennsingle", Estimators[i]) == true) { 
227                                         if (Estimators[i] == "sobs") { 
228                                                 vennCalculators.push_back(new Sobs());
229                                         }else if (Estimators[i] == "chao") { 
230                                                 vennCalculators.push_back(new Chao1());
231                                         }else if (Estimators[i] == "ace") {
232                                                 if(abund < 5)
233                                                         abund = 10;
234                                                 vennCalculators.push_back(new Ace(abund));
235                                         }
236                                 }
237                         }
238                 }else {
239                         for (int i=0; i<Estimators.size(); i++) {
240                                 if (validCalculator.isValidCalculator("vennshared", Estimators[i]) == true) { 
241                                         if (Estimators[i] == "sharedsobs") { 
242                                                 vennCalculators.push_back(new SharedSobsCS());
243                                         }else if (Estimators[i] == "sharedchao") { 
244                                                 vennCalculators.push_back(new SharedChao1());
245                                         }else if (Estimators[i] == "sharedace") { 
246                                                 vennCalculators.push_back(new SharedAce());
247                                         }
248                                 }
249                         }
250                 }
251                         
252                 //if the users entered no valid calculators don't execute command
253                 if (vennCalculators.size() == 0) { m->mothurOut("No valid calculators given, please correct."); m->mothurOutEndLine(); return 0;  }
254                 
255                 venn = new Venn(outputDir, nseqs, inputfile, fontsize); 
256                 input = new InputData(inputfile, format);
257                 
258                 string lastLabel;
259                 
260                 if (format == "sharedfile") {
261                         lookup = input->getSharedRAbundVectors();
262                         lastLabel = lookup[0]->getLabel();
263                         
264                         if ((lookup.size() > 4) && (perm)) { combosOfFour = findCombinations(lookup.size()); }
265                 }else if (format == "list") {
266                         sabund = input->getSAbundVector();
267                         lastLabel = sabund->getLabel();
268                 }
269                 
270                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
271                 set<string> processedLabels;
272                 set<string> userLabels = labels;
273
274                 if (format != "list") { 
275                         
276                         //as long as you are not at the end of the file or done wih the lines you want
277                         while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
278                         
279                                 if (m->control_pressed) {
280                                         for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
281                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
282                                         m->clearGroups(); delete venn; delete input;
283                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  }
284                                         return 0;
285                                 }
286
287                                 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
288                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
289                                         processedLabels.insert(lookup[0]->getLabel());
290                                         userLabels.erase(lookup[0]->getLabel());
291                                         
292                                         if ((lookup.size() > 4) && (!perm)){
293                                                 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();
294                                                 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
295                                         
296                                                 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
297                                                 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  } }
298
299                                         }else if ((lookup.size() > 4) && (perm)) {
300                                                 set< set<int> >::iterator it3;
301                                                 set<int>::iterator it2;
302                                                 for (it3 = combosOfFour.begin(); it3 != combosOfFour.end(); it3++) {  
303                         
304                                                         set<int> poss = *it3;
305                                                         vector<SharedRAbundVector*> subset;
306                                                         for (it2 = poss.begin(); it2 != poss.end(); it2++) {   subset.push_back(lookup[*it2]);   }
307                                                         
308                                                         vector<string> outfilenames = venn->getPic(subset, vennCalculators);
309                                                         for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]); }  }
310                                                 }               
311                                         }else {
312                                                 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
313                                                 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
314                                         }                                       
315                                 }
316                                 
317                                 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
318                                         string saveLabel = lookup[0]->getLabel();
319                                         
320                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
321                                         lookup = input->getSharedRAbundVectors(lastLabel);
322
323                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
324                                         processedLabels.insert(lookup[0]->getLabel());
325                                         userLabels.erase(lookup[0]->getLabel());
326
327                                         if ((lookup.size() > 4) && (!perm)){
328                                                 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();
329                                                 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
330                                         
331                                                 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
332                                                 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
333
334                                         }else if ((lookup.size() > 4) && (perm)) {
335                                                 set< set<int> >::iterator it3;
336                                                 set<int>::iterator it2;
337                                                 for (it3 = combosOfFour.begin(); it3 != combosOfFour.end(); it3++) {  
338                         
339                                                         set<int> poss = *it3;
340                                                         vector<SharedRAbundVector*> subset;
341                                                         for (it2 = poss.begin(); it2 != poss.end(); it2++) {   subset.push_back(lookup[*it2]);   }
342                                                         
343                                                         vector<string> outfilenames = venn->getPic(subset, vennCalculators);
344                                                         for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
345                                                 }               
346                                         }else {
347                                                 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
348                                                 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
349                                         }
350                                                                                 
351                                         //restore real lastlabel to save below
352                                         lookup[0]->setLabel(saveLabel);
353                                 }
354                                 
355                                 
356                                 lastLabel = lookup[0]->getLabel();      
357                                                 
358                                 //get next line to process
359                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
360                                 lookup = input->getSharedRAbundVectors();
361                         }
362                         
363                         if (m->control_pressed) {
364                                         for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
365                                         m->clearGroups(); delete venn; delete input; 
366                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  }
367                                         return 0;
368                         }
369
370                         
371                         //output error messages about any remaining user labels
372                         set<string>::iterator it;
373                         bool needToRun = false;
374                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
375                                 m->mothurOut("Your file does not include the label " + *it); 
376                                 if (processedLabels.count(lastLabel) != 1) {
377                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
378                                         needToRun = true;
379                                 }else {
380                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
381                                 }
382                         }
383                 
384                         //run last label if you need to
385                         if (needToRun == true)  {
386                                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {     delete lookup[i]; }  } 
387                                         lookup = input->getSharedRAbundVectors(lastLabel);
388
389                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
390                                         processedLabels.insert(lookup[0]->getLabel());
391                                         userLabels.erase(lookup[0]->getLabel());
392
393                                         if ((lookup.size() > 4) && (!perm)){
394                                                 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();
395                                                 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
396                                         
397                                                 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
398                                                 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]); }  }
399
400                                         }else if ((lookup.size() > 4) && (perm)) {
401                                                 set< set<int> >::iterator it3;
402                                                 set<int>::iterator it2;
403                                                 for (it3 = combosOfFour.begin(); it3 != combosOfFour.end(); it3++) {  
404                         
405                                                         set<int> poss = *it3;
406                                                         vector<SharedRAbundVector*> subset;
407                                                         for (it2 = poss.begin(); it2 != poss.end(); it2++) {   subset.push_back(lookup[*it2]);   }
408                                                         
409                                                         vector<string> outfilenames = venn->getPic(subset, vennCalculators);
410                                                         for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]); }  }
411                                                 }               
412                                         }else {
413                                                 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
414                                                 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
415                                         }
416                                         
417                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
418                         }
419                 
420
421                         //reset groups parameter
422                         m->clearGroups();  
423                         
424                         if (m->control_pressed) {
425                                         m->clearGroups(); delete venn; delete input;
426                                         for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
427                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  }
428                                         return 0;
429                         }
430
431                         
432                 }else{
433                 
434                         while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
435                         
436                                 if (m->control_pressed) {
437                                         for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
438                                         delete sabund; delete venn; delete input;
439                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  }
440                                         return 0;
441                                 }
442                 
443                                 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){                     
444         
445                                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
446                                         vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
447                                         for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
448
449                                         
450                                         processedLabels.insert(sabund->getLabel());
451                                         userLabels.erase(sabund->getLabel());
452                                 }
453                                 
454                                 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
455                                         string saveLabel = sabund->getLabel();
456                                 
457                                         delete sabund;
458                                         sabund = input->getSAbundVector(lastLabel);
459                                         
460                                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
461                                         vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
462                                         for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
463
464                                         
465                                         processedLabels.insert(sabund->getLabel());
466                                         userLabels.erase(sabund->getLabel());
467                                         
468                                         //restore real lastlabel to save below
469                                         sabund->setLabel(saveLabel);
470                                 }               
471                                 
472                                 lastLabel = sabund->getLabel();         
473                                 
474                                 delete sabund;
475                                 sabund = input->getSAbundVector();
476                         }
477                         
478                         if (m->control_pressed) {
479                                         for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
480                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  }
481                                         delete venn; delete input;
482                                         return 0;
483                         }
484                         
485                         //output error messages about any remaining user labels
486                         set<string>::iterator it;
487                         bool needToRun = false;
488                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
489                                 m->mothurOut("Your file does not include the label " + *it); 
490                                 if (processedLabels.count(lastLabel) != 1) {
491                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
492                                         needToRun = true;
493                                 }else {
494                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
495                                 }
496                         }
497                 
498                         //run last label if you need to
499                         if (needToRun == true)  {
500                                 if (sabund != NULL) {   delete sabund;  }
501                                 sabund = input->getSAbundVector(lastLabel);
502                                         
503                                 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
504                                 vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
505                                 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
506
507                                 delete sabund;
508                                         
509                         }
510                         
511                         if (m->control_pressed) {
512                                         delete venn; delete input;
513                                         for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
514                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  }
515                                         return 0;
516                         }
517                 }
518                 
519                 for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
520                 delete venn; delete input;
521                 
522                 m->mothurOutEndLine();
523                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
524                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
525                 m->mothurOutEndLine();
526
527                 
528                 return 0;
529         }
530         catch(exception& e) {
531                 m->errorOut(e, "VennCommand", "execute");
532                 exit(1);
533         }
534 }
535 //**********************************************************************************************************************
536 //returns a vector of sets containing the 4 group combinations
537 set< set<int> > VennCommand::findCombinations(int lookupSize){
538         try {
539                 set< set<int> > combos;
540                 
541                 set<int> possibles;
542                 for (int i = 0; i < lookupSize; i++) {  possibles.insert(i);  }
543                 
544                 getCombos(possibles, combos);
545                 
546                 return combos;
547                 
548         }
549         catch(exception& e) {
550                 m->errorOut(e, "VennCommand", "findCombinations");
551                 exit(1);
552         }
553 }
554 //**********************************************************************************************************************
555 //recusively finds combos of 4
556 int VennCommand::getCombos(set<int> possibles, set< set<int> >& combos){
557         try {
558                 
559                 if (possibles.size() == 4) { //done
560                         if (combos.count(possibles) == 0) { //no dups
561                                 combos.insert(possibles);
562                         }
563                 }else { //we still have work to do
564                         set<int>::iterator it;
565                         set<int>::iterator it2;
566                         for (it = possibles.begin(); it != possibles.end(); it++) {  
567                                 
568                                 set<int> newPossibles;
569                                 for (it2 = possibles.begin(); it2 != possibles.end(); it2++) {  //all possible combos of one length smaller
570                                         if (*it != *it2) { 
571                                                 newPossibles.insert(*it2);
572                                         }
573                                 }
574                                 getCombos(newPossibles, combos);
575                         }
576                 }
577                 
578                 return 0;
579         }
580         catch(exception& e) {
581                 m->errorOut(e, "VennCommand", "getCombos");
582                 exit(1);
583         }
584 }
585
586 //**********************************************************************************************************************