]> git.donarmstrong.com Git - mothur.git/blob - venncommand.cpp
added cluster.fragments command as well as the nseqs parameter to the venn command
[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 //**********************************************************************************************************************
22
23 VennCommand::VennCommand(string option)  {
24         try {
25                 globaldata = GlobalData::getInstance();
26                 abort = false;
27                 allLines = 1;
28                 labels.clear();
29                         
30                 //allow user to run help
31                 if(option == "help") { help(); abort = true; }
32                 
33                 else {
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)));
37                         
38                         OptionParser parser(option);
39                         map<string,string> parameters = parser.getParameters();
40                         
41                         ValidParameters validParameter;
42                         
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;  }
46                         }
47                         
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; 
51                         }
52                         
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"){  
55                                 outputDir = ""; 
56                                 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it       
57                         }
58
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 = ""; }
63                         else { 
64                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
65                                 else { allLines = 1;  }
66                         }
67                         
68                         //if the user has not specified any labels use the ones from read.otu
69                         if (label == "") {  
70                                 allLines = globaldata->allLines; 
71                                 labels = globaldata->labels; 
72                         }
73                         
74                         groups = validParameter.validFile(parameters, "groups", false);                 
75                         if (groups == "not found") { groups = ""; }
76                         else { 
77                                 m->splitAtDash(groups, Groups);
78                                 globaldata->Groups = Groups;
79                         }
80                         
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"; }
86                         }
87                         else { 
88                                  if (calc == "default")  {  
89                                         if(format == "list") { calc = "sobs"; }
90                                         else { calc = "sharedsobs"; }
91                                 }
92                         }
93                         m->splitAtDash(calc, Estimators);
94                         
95                         string temp;
96                         temp = validParameter.validFile(parameters, "abund", false);            if (temp == "not found") { temp = "10"; }
97                         convert(temp, abund); 
98                         
99                         temp = validParameter.validFile(parameters, "nseqs", false);                    if (temp == "not found"){       temp = "f";                             }
100                         nseqs = m->isTrue(temp); 
101
102
103                         if (abort == false) {
104                                 validCalculator = new ValidCalculators();
105                 
106                                 int i;
107                                 
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") {
116                                                                 if(abund < 5)
117                                                                         abund = 10;
118                                                                 vennCalculators.push_back(new Ace(abund));
119                                                         }
120                                                 }
121                                         }
122                                 }else {
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());
131                                                         }
132                                                 }
133                                         }
134                                 }
135                                 
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);  }
139                         }
140                         
141                 }
142
143                 
144                                 
145         }
146         catch(exception& e) {
147                 m->errorOut(e, "VennCommand", "VennCommand");
148                 exit(1);
149         }
150 }
151
152 //**********************************************************************************************************************
153
154 void VennCommand::help(){
155         try {
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");
169         }
170         catch(exception& e) {
171                 m->errorOut(e, "VennCommand", "help");
172                 exit(1);
173         }
174 }
175
176
177 //**********************************************************************************************************************
178
179 VennCommand::~VennCommand(){
180         if (abort == false) {
181                 delete input; globaldata->ginput = NULL;
182                 delete read;
183                 delete venn;
184                 globaldata->sabund = NULL;
185                 delete validCalculator;
186         }
187         
188 }
189
190 //**********************************************************************************************************************
191
192 int VennCommand::execute(){
193         try {
194         
195                 if (abort == true) { return 0; }
196                 
197                 string lastLabel;
198                 vector<string> outputNames;
199                 
200                 if (format == "sharedfile") {
201                         //you have groups
202                         read = new ReadOTUFile(globaldata->inputFileName);      
203                         read->read(&*globaldata); 
204                         
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); 
212                 
213                         sabund = globaldata->sabund;
214                         lastLabel = sabund->getLabel();
215                         input = globaldata->ginput;
216                 }
217                 
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;
221                 
222                 if (format != "list") { 
223                         
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))) {
226                         
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());  }
232                                         return 0;
233                                 }
234
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());
239                                         
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
243                                         }
244                                         
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]); }  }
247                                 }
248                                 
249                                 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
250                                         string saveLabel = lookup[0]->getLabel();
251                                         
252                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
253                                         lookup = input->getSharedRAbundVectors(lastLabel);
254
255                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
256                                         processedLabels.insert(lookup[0]->getLabel());
257                                         userLabels.erase(lookup[0]->getLabel());
258
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
262                                         }                               
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]); }  }
265                                         
266                                         //restore real lastlabel to save below
267                                         lookup[0]->setLabel(saveLabel);
268                                 }
269                                 
270                                 
271                                 lastLabel = lookup[0]->getLabel();      
272                                                 
273                                 //get next line to process
274                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
275                                 lookup = input->getSharedRAbundVectors();
276                         }
277                         
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());  }
282                                         return 0;
283                         }
284
285                         
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();
293                                         needToRun = true;
294                                 }else {
295                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
296                                 }
297                         }
298                 
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);
303
304                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
305                                         processedLabels.insert(lookup[0]->getLabel());
306                                         userLabels.erase(lookup[0]->getLabel());
307
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
311                                         }                               
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]); }  }
314
315                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
316                         }
317                 
318
319                         //reset groups parameter
320                         globaldata->Groups.clear();  
321                         
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());  }
325                                         return 0;
326                         }
327
328                         
329                 }else{
330                 
331                         while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
332                         
333                                 if (m->control_pressed) {
334                                         for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
335                                         delete sabund;
336                                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  }
337                                         return 0;
338                                 }
339                 
340                                 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){                     
341         
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]); }  }
345
346                                         
347                                         processedLabels.insert(sabund->getLabel());
348                                         userLabels.erase(sabund->getLabel());
349                                 }
350                                 
351                                 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
352                                         string saveLabel = sabund->getLabel();
353                                 
354                                         delete sabund;
355                                         sabund = input->getSAbundVector(lastLabel);
356                                         
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]); }  }
360
361                                         
362                                         processedLabels.insert(sabund->getLabel());
363                                         userLabels.erase(sabund->getLabel());
364                                         
365                                         //restore real lastlabel to save below
366                                         sabund->setLabel(saveLabel);
367                                 }               
368                                 
369                                 lastLabel = sabund->getLabel();         
370                                 
371                                 delete sabund;
372                                 sabund = input->getSAbundVector();
373                         }
374                         
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());  }
378                                         return 0;
379                         }
380                         
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();
388                                         needToRun = true;
389                                 }else {
390                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
391                                 }
392                         }
393                 
394                         //run last label if you need to
395                         if (needToRun == true)  {
396                                 if (sabund != NULL) {   delete sabund;  }
397                                 sabund = input->getSAbundVector(lastLabel);
398                                         
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]); }  }
402
403                                 delete sabund;
404                                         
405                         }
406                         
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());  }
410                                         return 0;
411                         }
412                 }
413                 
414                 for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
415                 
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();
420
421                 
422                 return 0;
423         }
424         catch(exception& e) {
425                 m->errorOut(e, "VennCommand", "execute");
426                 exit(1);
427         }
428 }
429
430 //**********************************************************************************************************************