]> git.donarmstrong.com Git - mothur.git/blob - venncommand.cpp
created mothurOut class to handle logfiles
[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
19
20 //**********************************************************************************************************************
21
22 VennCommand::VennCommand(string option)  {
23         try {
24                 globaldata = GlobalData::getInstance();
25                 abort = false;
26                 allLines = 1;
27                 labels.clear();
28                         
29                 //allow user to run help
30                 if(option == "help") { help(); abort = true; }
31                 
32                 else {
33                         //valid paramters for this command
34                         string AlignArray[] =  {"groups","label","calc", "abund","outputdir","inputdir"};
35                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
36                         
37                         OptionParser parser(option);
38                         map<string,string> parameters = parser.getParameters();
39                         
40                         ValidParameters validParameter;
41                         
42                         //check to make sure all parameters are valid for command
43                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
44                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
45                         }
46                         
47                         //make sure the user has already run the read.otu command
48                         if ((globaldata->getListFile() == "") && (globaldata->getSharedFile() == "")) {
49                                 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; 
50                         }
51                         
52                         //if the user changes the output directory command factory will send this info to us in the output parameter 
53                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
54                                 outputDir = ""; 
55                                 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it  
56                         }
57
58                         //check for optional parameter and set defaults
59                         // ...at some point should added some additional type checking...
60                         label = validParameter.validFile(parameters, "label", false);                   
61                         if (label == "not found") { label = ""; }
62                         else { 
63                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
64                                 else { allLines = 1;  }
65                         }
66                         
67                         //if the user has not specified any labels use the ones from read.otu
68                         if (label == "") {  
69                                 allLines = globaldata->allLines; 
70                                 labels = globaldata->labels; 
71                         }
72                         
73                         groups = validParameter.validFile(parameters, "groups", false);                 
74                         if (groups == "not found") { groups = ""; }
75                         else { 
76                                 splitAtDash(groups, Groups);
77                                 globaldata->Groups = Groups;
78                         }
79                         
80                         format = globaldata->getFormat();
81                         calc = validParameter.validFile(parameters, "calc", false);                     
82                         if (calc == "not found") { 
83                                 if(format == "list") { calc = "sobs"; }
84                                 else { calc = "sharedsobs"; }
85                         }
86                         else { 
87                                  if (calc == "default")  {  
88                                         if(format == "list") { calc = "sobs"; }
89                                         else { calc = "sharedsobs"; }
90                                 }
91                         }
92                         splitAtDash(calc, Estimators);
93                         
94                         string temp;
95                         temp = validParameter.validFile(parameters, "abund", false);            if (temp == "not found") { temp = "10"; }
96                         convert(temp, abund); 
97
98                         if (abort == false) {
99                                 validCalculator = new ValidCalculators();
100                 
101                                 int i;
102                                 
103                                 if (format == "list") {
104                                         for (i=0; i<Estimators.size(); i++) {
105                                                 if (validCalculator->isValidCalculator("vennsingle", Estimators[i]) == true) { 
106                                                         if (Estimators[i] == "sobs") { 
107                                                                 vennCalculators.push_back(new Sobs());
108                                                         }else if (Estimators[i] == "chao") { 
109                                                                 vennCalculators.push_back(new Chao1());
110                                                         }else if (Estimators[i] == "ace") {
111                                                                 if(abund < 5)
112                                                                         abund = 10;
113                                                                 vennCalculators.push_back(new Ace(abund));
114                                                         }
115                                                 }
116                                         }
117                                 }else {
118                                         for (i=0; i<Estimators.size(); i++) {
119                                                 if (validCalculator->isValidCalculator("vennshared", Estimators[i]) == true) { 
120                                                         if (Estimators[i] == "sharedsobs") { 
121                                                                 vennCalculators.push_back(new SharedSobsCS());
122                                                         }else if (Estimators[i] == "sharedchao") { 
123                                                                 vennCalculators.push_back(new SharedChao1());
124                                                         }else if (Estimators[i] == "sharedace") { 
125                                                                 vennCalculators.push_back(new SharedAce());
126                                                         }
127                                                 }
128                                         }
129                                 }
130                                 
131                                 venn = new Venn(outputDir);
132                         }
133                 }
134
135                 
136                                 
137         }
138         catch(exception& e) {
139                 m->errorOut(e, "VennCommand", "VennCommand");
140                 exit(1);
141         }
142 }
143
144 //**********************************************************************************************************************
145
146 void VennCommand::help(){
147         try {
148                 m->mothurOut("The venn command can only be executed after a successful read.otu command.\n");
149                 m->mothurOut("The venn command parameters are groups, calc, abund and label.  No parameters are required.\n");
150                 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");
151                 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");
152                 m->mothurOut("The venn command should be in the following format: venn(groups=yourGroups, calc=yourCalcs, label=yourLabels, abund=yourAbund).\n");
153                 m->mothurOut("Example venn(groups=A-B-C, calc=sharedsobs-sharedchao, abund=20).\n");
154                 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");
155                 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");
156                 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");
157                 m->mothurOut("The only estmiator available four 4 groups is sharedsobs.\n");
158                 m->mothurOut("The venn command outputs a .svg file for each calculator you specify at each distance you choose.\n");
159                 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
160         }
161         catch(exception& e) {
162                 m->errorOut(e, "VennCommand", "help");
163                 exit(1);
164         }
165 }
166
167
168 //**********************************************************************************************************************
169
170 VennCommand::~VennCommand(){
171         if (abort == false) {
172                 delete input; globaldata->ginput = NULL;
173                 delete read;
174                 delete venn;
175                 globaldata->sabund = NULL;
176         }
177         
178 }
179
180 //**********************************************************************************************************************
181
182 int VennCommand::execute(){
183         try {
184         
185                 if (abort == true) { return 0; }
186                 
187                 string lastLabel;
188                 vector<string> outputNames;
189                 
190                 //if the users entered no valid calculators don't execute command
191                 if (vennCalculators.size() == 0) { return 0; }
192                 
193                 if (format == "sharedfile") {
194                         //you have groups
195                         read = new ReadOTUFile(globaldata->inputFileName);      
196                         read->read(&*globaldata); 
197                         
198                         input = globaldata->ginput;
199                         lookup = input->getSharedRAbundVectors();
200                         lastLabel = lookup[0]->getLabel();
201                 }else if (format == "list") {
202                         //you are using just a list file and have only one group
203                         read = new ReadOTUFile(globaldata->inputFileName);      
204                         read->read(&*globaldata); 
205                 
206                         sabund = globaldata->sabund;
207                         lastLabel = sabund->getLabel();
208                         input = globaldata->ginput;
209                 }
210                 
211                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
212                 set<string> processedLabels;
213                 set<string> userLabels = labels;
214                 
215                 if (format != "list") { 
216                         
217                         //as long as you are not at the end of the file or done wih the lines you want
218                         while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
219
220                                 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
221                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
222                                         processedLabels.insert(lookup[0]->getLabel());
223                                         userLabels.erase(lookup[0]->getLabel());
224                                         
225                                         if (lookup.size() > 4) {
226                                                 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();
227                                                 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
228                                         }
229                                         
230                                         vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
231                                         for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); } 
232                                 }
233                                 
234                                 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
235                                         string saveLabel = lookup[0]->getLabel();
236                                         
237                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
238                                         lookup = input->getSharedRAbundVectors(lastLabel);
239
240                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
241                                         processedLabels.insert(lookup[0]->getLabel());
242                                         userLabels.erase(lookup[0]->getLabel());
243
244                                         if (lookup.size() > 4) {
245                                                 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();
246                                                 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
247                                         }                               
248                                         vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
249                                         for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); } 
250                                         
251                                         //restore real lastlabel to save below
252                                         lookup[0]->setLabel(saveLabel);
253                                 }
254                                 
255                                 
256                                 lastLabel = lookup[0]->getLabel();      
257                                                 
258                                 //get next line to process
259                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
260                                 lookup = input->getSharedRAbundVectors();
261                         }
262                         
263                         //output error messages about any remaining user labels
264                         set<string>::iterator it;
265                         bool needToRun = false;
266                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
267                                 m->mothurOut("Your file does not include the label " + *it); 
268                                 if (processedLabels.count(lastLabel) != 1) {
269                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
270                                         needToRun = true;
271                                 }else {
272                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
273                                 }
274                         }
275                 
276                         //run last label if you need to
277                         if (needToRun == true)  {
278                                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {     delete lookup[i]; }  } 
279                                         lookup = input->getSharedRAbundVectors(lastLabel);
280
281                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
282                                         processedLabels.insert(lookup[0]->getLabel());
283                                         userLabels.erase(lookup[0]->getLabel());
284
285                                         if (lookup.size() > 4) {
286                                                 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();
287                                                 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
288                                         }                               
289                                         vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
290                                         for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); } 
291
292                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
293                         }
294                 
295
296                         //reset groups parameter
297                         globaldata->Groups.clear();  
298                         
299                 }else{
300                 
301                         while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
302                 
303                                 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){                     
304         
305                                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
306                                         vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
307                                         for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); } 
308
309                                         
310                                         processedLabels.insert(sabund->getLabel());
311                                         userLabels.erase(sabund->getLabel());
312                                 }
313                                 
314                                 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
315                                         string saveLabel = sabund->getLabel();
316                                 
317                                         delete sabund;
318                                         sabund = input->getSAbundVector(lastLabel);
319                                         
320                                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
321                                         vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
322                                         for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); } 
323
324                                         
325                                         processedLabels.insert(sabund->getLabel());
326                                         userLabels.erase(sabund->getLabel());
327                                         
328                                         //restore real lastlabel to save below
329                                         sabund->setLabel(saveLabel);
330                                 }               
331                                 
332                                 lastLabel = sabund->getLabel();         
333                                 
334                                 delete sabund;
335                                 sabund = input->getSAbundVector();
336                         }
337                         
338                         //output error messages about any remaining user labels
339                         set<string>::iterator it;
340                         bool needToRun = false;
341                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
342                                 m->mothurOut("Your file does not include the label " + *it); 
343                                 if (processedLabels.count(lastLabel) != 1) {
344                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
345                                         needToRun = true;
346                                 }else {
347                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
348                                 }
349                         }
350                 
351                         //run last label if you need to
352                         if (needToRun == true)  {
353                                 if (sabund != NULL) {   delete sabund;  }
354                                 sabund = input->getSAbundVector(lastLabel);
355                                         
356                                 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
357                                 vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
358                                 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); } 
359
360                                 delete sabund;
361                                         
362                         }
363                         
364                 }
365                 
366                 for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
367                 
368                 m->mothurOutEndLine();
369                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
370                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
371                 m->mothurOutEndLine();
372
373                 
374                 return 0;
375         }
376         catch(exception& e) {
377                 m->errorOut(e, "VennCommand", "execute");
378                 exit(1);
379         }
380 }
381
382 //**********************************************************************************************************************