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 //**********************************************************************************************************************
23 VennCommand::VennCommand(string option) {
25 globaldata = GlobalData::getInstance();
30 //allow user to run help
31 if(option == "help") { help(); abort = true; }
34 //valid paramters for this command
35 string AlignArray[] = {"groups","label","calc", "abund","nseqs","outputdir","inputdir"};
36 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
38 OptionParser parser(option);
39 map<string,string> parameters = parser.getParameters();
41 ValidParameters validParameter;
43 //check to make sure all parameters are valid for command
44 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
45 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
48 //make sure the user has already run the read.otu command
49 if ((globaldata->getListFile() == "") && (globaldata->getSharedFile() == "")) {
50 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;
53 //if the user changes the output directory command factory will send this info to us in the output parameter
54 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
56 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
59 //check for optional parameter and set defaults
60 // ...at some point should added some additional type checking...
61 label = validParameter.validFile(parameters, "label", false);
62 if (label == "not found") { label = ""; }
64 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
65 else { allLines = 1; }
68 //if the user has not specified any labels use the ones from read.otu
70 allLines = globaldata->allLines;
71 labels = globaldata->labels;
74 groups = validParameter.validFile(parameters, "groups", false);
75 if (groups == "not found") { groups = ""; }
77 m->splitAtDash(groups, Groups);
78 globaldata->Groups = Groups;
81 format = globaldata->getFormat();
82 calc = validParameter.validFile(parameters, "calc", false);
83 if (calc == "not found") {
84 if(format == "list") { calc = "sobs"; }
85 else { calc = "sharedsobs"; }
88 if (calc == "default") {
89 if(format == "list") { calc = "sobs"; }
90 else { calc = "sharedsobs"; }
93 m->splitAtDash(calc, Estimators);
96 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
99 temp = validParameter.validFile(parameters, "nseqs", false); if (temp == "not found"){ temp = "f"; }
100 nseqs = m->isTrue(temp);
103 if (abort == false) {
104 validCalculator = new ValidCalculators();
108 if (format == "list") {
109 for (i=0; i<Estimators.size(); i++) {
110 if (validCalculator->isValidCalculator("vennsingle", Estimators[i]) == true) {
111 if (Estimators[i] == "sobs") {
112 vennCalculators.push_back(new Sobs());
113 }else if (Estimators[i] == "chao") {
114 vennCalculators.push_back(new Chao1());
115 }else if (Estimators[i] == "ace") {
118 vennCalculators.push_back(new Ace(abund));
123 for (i=0; i<Estimators.size(); i++) {
124 if (validCalculator->isValidCalculator("vennshared", Estimators[i]) == true) {
125 if (Estimators[i] == "sharedsobs") {
126 vennCalculators.push_back(new SharedSobsCS());
127 }else if (Estimators[i] == "sharedchao") {
128 vennCalculators.push_back(new SharedChao1());
129 }else if (Estimators[i] == "sharedace") {
130 vennCalculators.push_back(new SharedAce());
136 //if the users entered no valid calculators don't execute command
137 if (vennCalculators.size() == 0) { m->mothurOut("No valid calculators given, please correct."); m->mothurOutEndLine(); abort = true; }
138 else { venn = new Venn(outputDir, nseqs); }
146 catch(exception& e) {
147 m->errorOut(e, "VennCommand", "VennCommand");
152 //**********************************************************************************************************************
154 void VennCommand::help(){
156 m->mothurOut("The venn command can only be executed after a successful read.otu command.\n");
157 m->mothurOut("The venn command parameters are groups, calc, abund, nseqs and label. No parameters are required.\n");
158 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");
159 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");
160 m->mothurOut("The venn command should be in the following format: venn(groups=yourGroups, calc=yourCalcs, label=yourLabels, abund=yourAbund).\n");
161 m->mothurOut("Example venn(groups=A-B-C, calc=sharedsobs-sharedchao, abund=20).\n");
162 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");
163 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");
164 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");
165 m->mothurOut("The nseqs parameter will output the number of sequences represented by the otus in the picture, default=F.\n");
166 m->mothurOut("The only estimators available four 4 groups are sharedsobs and sharedchao.\n");
167 m->mothurOut("The venn command outputs a .svg file for each calculator you specify at each distance you choose.\n");
168 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
170 catch(exception& e) {
171 m->errorOut(e, "VennCommand", "help");
177 //**********************************************************************************************************************
179 VennCommand::~VennCommand(){
180 if (abort == false) {
181 delete input; globaldata->ginput = NULL;
184 globaldata->sabund = NULL;
185 delete validCalculator;
190 //**********************************************************************************************************************
192 int VennCommand::execute(){
195 if (abort == true) { return 0; }
198 vector<string> outputNames;
200 if (format == "sharedfile") {
202 read = new ReadOTUFile(globaldata->inputFileName);
203 read->read(&*globaldata);
205 input = globaldata->ginput;
206 lookup = input->getSharedRAbundVectors();
207 lastLabel = lookup[0]->getLabel();
208 }else if (format == "list") {
209 //you are using just a list file and have only one group
210 read = new ReadOTUFile(globaldata->inputFileName);
211 read->read(&*globaldata);
213 sabund = globaldata->sabund;
214 lastLabel = sabund->getLabel();
215 input = globaldata->ginput;
218 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
219 set<string> processedLabels;
220 set<string> userLabels = labels;
222 if (format != "list") {
224 //as long as you are not at the end of the file or done wih the lines you want
225 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
227 if (m->control_pressed) {
228 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
229 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
230 globaldata->Groups.clear();
231 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
235 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
236 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
237 processedLabels.insert(lookup[0]->getLabel());
238 userLabels.erase(lookup[0]->getLabel());
240 if (lookup.size() > 4) {
241 m->mothurOut("Error: Too many groups chosen. You may use up to 4 groups with the venn command. I will use the first four groups in your groupfile."); m->mothurOutEndLine();
242 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
245 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
246 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } }
249 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
250 string saveLabel = lookup[0]->getLabel();
252 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
253 lookup = input->getSharedRAbundVectors(lastLabel);
255 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
256 processedLabels.insert(lookup[0]->getLabel());
257 userLabels.erase(lookup[0]->getLabel());
259 if (lookup.size() > 4) {
260 m->mothurOut("Error: Too many groups chosen. You may use up to 4 groups with the venn command. I will use the first four groups in your groupfile."); m->mothurOutEndLine();
261 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
263 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
264 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } }
266 //restore real lastlabel to save below
267 lookup[0]->setLabel(saveLabel);
271 lastLabel = lookup[0]->getLabel();
273 //get next line to process
274 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
275 lookup = input->getSharedRAbundVectors();
278 if (m->control_pressed) {
279 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
280 globaldata->Groups.clear();
281 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
286 //output error messages about any remaining user labels
287 set<string>::iterator it;
288 bool needToRun = false;
289 for (it = userLabels.begin(); it != userLabels.end(); it++) {
290 m->mothurOut("Your file does not include the label " + *it);
291 if (processedLabels.count(lastLabel) != 1) {
292 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
295 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
299 //run last label if you need to
300 if (needToRun == true) {
301 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
302 lookup = input->getSharedRAbundVectors(lastLabel);
304 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
305 processedLabels.insert(lookup[0]->getLabel());
306 userLabels.erase(lookup[0]->getLabel());
308 if (lookup.size() > 4) {
309 m->mothurOut("Error: Too many groups chosen. You may use up to 4 groups with the venn command. I will use the first four groups in your groupfile."); m->mothurOutEndLine();
310 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
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]); } }
315 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
319 //reset groups parameter
320 globaldata->Groups.clear();
322 if (m->control_pressed) {
323 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
324 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
331 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
333 if (m->control_pressed) {
334 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
336 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
340 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
342 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
343 vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
344 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } }
347 processedLabels.insert(sabund->getLabel());
348 userLabels.erase(sabund->getLabel());
351 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
352 string saveLabel = sabund->getLabel();
355 sabund = input->getSAbundVector(lastLabel);
357 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
358 vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
359 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } }
362 processedLabels.insert(sabund->getLabel());
363 userLabels.erase(sabund->getLabel());
365 //restore real lastlabel to save below
366 sabund->setLabel(saveLabel);
369 lastLabel = sabund->getLabel();
372 sabund = input->getSAbundVector();
375 if (m->control_pressed) {
376 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
377 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
381 //output error messages about any remaining user labels
382 set<string>::iterator it;
383 bool needToRun = false;
384 for (it = userLabels.begin(); it != userLabels.end(); it++) {
385 m->mothurOut("Your file does not include the label " + *it);
386 if (processedLabels.count(lastLabel) != 1) {
387 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
390 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
394 //run last label if you need to
395 if (needToRun == true) {
396 if (sabund != NULL) { delete sabund; }
397 sabund = input->getSAbundVector(lastLabel);
399 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
400 vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
401 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]); } }
407 if (m->control_pressed) {
408 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
409 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
414 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
416 m->mothurOutEndLine();
417 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
418 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
419 m->mothurOutEndLine();
424 catch(exception& e) {
425 m->errorOut(e, "VennCommand", "execute");
430 //**********************************************************************************************************************