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