]> git.donarmstrong.com Git - mothur.git/blob - heatmapcommand.cpp
added [ERROR] flag if command aborts
[mothur.git] / heatmapcommand.cpp
1 /*
2  *  heatmapcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 3/25/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "heatmapcommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> HeatMapCommand::getValidParameters(){    
14         try {
15                 string Array[] =  {"groups","label","sorted","scale","fontsize","numotu","outputdir","inputdir"};
16                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
17                 return myArray;
18         }
19         catch(exception& e) {
20                 m->errorOut(e, "HeatMapCommand", "getValidParameters");
21                 exit(1);
22         }
23 }
24 //**********************************************************************************************************************
25 HeatMapCommand::HeatMapCommand(){       
26         try {
27                 abort = true; calledHelp = true; 
28                 vector<string> tempOutNames;
29                 outputTypes["svg"] = tempOutNames;
30         }
31         catch(exception& e) {
32                 m->errorOut(e, "HeatMapCommand", "HeatMapCommand");
33                 exit(1);
34         }
35 }
36 //**********************************************************************************************************************
37 vector<string> HeatMapCommand::getRequiredParameters(){ 
38         try {
39                 vector<string> myArray;
40                 return myArray;
41         }
42         catch(exception& e) {
43                 m->errorOut(e, "HeatMapCommand", "getRequiredParameters");
44                 exit(1);
45         }
46 }
47 //**********************************************************************************************************************
48 vector<string> HeatMapCommand::getRequiredFiles(){      
49         try {
50                 string Array[] =  {"list","sabund","rabund","shared","relabund","or"};
51                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
52                 return myArray;
53         }
54         catch(exception& e) {
55                 m->errorOut(e, "HeatMapCommand", "getRequiredFiles");
56                 exit(1);
57         }
58 }
59 //**********************************************************************************************************************
60
61 HeatMapCommand::HeatMapCommand(string option) {
62         try {
63                 globaldata = GlobalData::getInstance();
64                 abort = false; calledHelp = false;   
65                 allLines = 1;
66                 labels.clear();
67                 
68                 //allow user to run help
69                 if(option == "help") { help(); abort = true; calledHelp = true; }
70                 
71                 else {
72                         //valid paramters for this command
73                         string AlignArray[] =  {"groups","label","sorted","scale","fontsize","numotu","outputdir","inputdir"};
74                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
75                         
76                         OptionParser parser(option);
77                         map<string,string> parameters = parser.getParameters();
78                         
79                         ValidParameters validParameter;
80                         
81                         //check to make sure all parameters are valid for command
82                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
83                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
84                         }
85                         
86                         //initialize outputTypes
87                         vector<string> tempOutNames;
88                         outputTypes["svg"] = tempOutNames;
89                 
90                         //if the user changes the output directory command factory will send this info to us in the output parameter 
91                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
92                                 outputDir = ""; 
93                                 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it       
94                         }
95                         
96                         //make sure the user has already run the read.otu command
97                         if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "") && (globaldata->getSharedFile() == "") && (globaldata->getRelAbundFile() == "")) {
98                                  m->mothurOut("You must read a list, rabund, sabund, or a list and a group, shared, or relabund file before you can use the heatmap.bin command."); m->mothurOutEndLine(); abort = true; 
99                         }
100
101                         //check for optional parameter and set defaults
102                         // ...at some point should added some additional type checking...
103                         label = validParameter.validFile(parameters, "label", false);                   
104                         if (label == "not found") { label = ""; }
105                         else { 
106                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
107                                 else { allLines = 1;  }
108                         }
109                         
110                         //if the user has not specified any labels use the ones from read.otu
111                         if (label == "") {  
112                                 allLines = globaldata->allLines; 
113                                 labels = globaldata->labels; 
114                         }
115                         
116                         groups = validParameter.validFile(parameters, "groups", false);                 
117                         if (groups == "not found") { groups = ""; }
118                         else { 
119                                 m->splitAtDash(groups, Groups);
120                                 globaldata->Groups = Groups;
121                         }
122                         
123                         string temp = validParameter.validFile(parameters, "numotu", false);            if (temp == "not found") { temp = "0"; }
124                         convert(temp, numOTU);
125                         
126                         temp = validParameter.validFile(parameters, "fontsize", false);                         if (temp == "not found") { temp = "24"; }
127                         convert(temp, fontSize);
128                         
129                         sorted = validParameter.validFile(parameters, "sorted", false);                         
130                         if (sorted == "not found") { 
131                                 //if numOTU is used change default
132                                 if (numOTU != 0) { sorted = "topotu"; }
133                                 else { sorted = "shared"; }
134                         }
135                  
136                         scale = validParameter.validFile(parameters, "scale", false);                           if (scale == "not found") { scale = "log10"; }
137                         
138                         if ((sorted != "none") && (sorted != "shared") && (sorted != "topotu") && (sorted != "topgroup")) { m->mothurOut(sorted + " is not a valid sorting option. Sorted options are: none, shared, topotu, topgroup"); m->mothurOutEndLine(); abort=true;  }
139                 }
140
141         }
142         catch(exception& e) {
143                 m->errorOut(e, "HeatMapCommand", "HeatMapCommand");
144                 exit(1);
145         }
146 }
147
148 //**********************************************************************************************************************
149
150 void HeatMapCommand::help(){
151         try {
152                 m->mothurOut("The heatmap.bin command can only be executed after a successful read.otu command.\n");
153                 m->mothurOut("The heatmap.bin command parameters are groups, sorted, scale, numotu, fontsize and label.  No parameters are required.\n");
154                 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
155                 m->mothurOut("The sorted parameter allows you to order the otus displayed, default=shared, meaning display the shared otus first. Other options for sorted are none, meaning the exact representation of your otus, \n");
156                 m->mothurOut("topotu, meaning the otus with the greatest abundance when totaled across groups, topgroup, meaning the top otus for each group. \n");
157                 m->mothurOut("The scale parameter allows you to choose the range of color your bin information will be displayed with.\n");
158                 m->mothurOut("The numotu parameter allows you to display only the top N otus, by default all the otus are displayed. You could choose to look at the top 10, by setting numotu=10. The default for sorted is topotu when numotu is used.\n");
159                 m->mothurOut("The group names are separated by dashes. The label parameter allows you to select what distance levels you would like a heatmap created for, and are also separated by dashes.\n");
160                 m->mothurOut("The fontsize parameter allows you to adjust the font size of the picture created, default=24.\n");
161                 m->mothurOut("The heatmap.bin command should be in the following format: heatmap.bin(groups=yourGroups, sorted=yourSorted, label=yourLabels).\n");
162                 m->mothurOut("Example heatmap.bin(groups=A-B-C, sorted=none, scale=log10).\n");
163                 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
164                 m->mothurOut("The default value for scale is log10; your other options are log2 and linear.\n");
165                 m->mothurOut("The heatmap.bin command outputs a .svg file for each label you specify.\n");
166                 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
167
168         }
169         catch(exception& e) {
170                 m->errorOut(e, "HeatMapCommand", "help");
171                 exit(1);
172         }
173 }
174
175 //**********************************************************************************************************************
176
177 HeatMapCommand::~HeatMapCommand(){
178 }
179
180 //**********************************************************************************************************************
181
182 int HeatMapCommand::execute(){
183         try {
184         
185                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
186                 
187                 heatmap = new HeatMap(sorted, scale, numOTU, fontSize, outputDir);
188                 format = globaldata->getFormat();
189
190                 string lastLabel;
191                 
192                 read = new ReadOTUFile(globaldata->inputFileName);      
193                 read->read(&*globaldata); 
194                 input = globaldata->ginput;
195                 
196                 if (format == "sharedfile") {
197                         //you have groups
198                         lookup = input->getSharedRAbundVectors();
199                         lastLabel = lookup[0]->getLabel();
200         
201                 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
202                         //you are using just a list file and have only one group
203                         rabund = globaldata->rabund;
204                         lastLabel = rabund->getLabel();
205                 }else if (format == "relabund") {
206                         //you have groups
207                         lookupFloat = input->getSharedRAbundFloatVectors();
208                         lastLabel = lookupFloat[0]->getLabel();
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 == "sharedfile") {   
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                                 if (m->control_pressed) {
220                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
221                                         for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } } outputTypes.clear();
222                                         globaldata->Groups.clear(); 
223                                         delete read; delete heatmap; return 0;
224                                 }
225                 
226                                 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
227         
228                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
229                                         string outputFileName = heatmap->getPic(lookup);
230                                         outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
231                                         
232                                         processedLabels.insert(lookup[0]->getLabel());
233                                         userLabels.erase(lookup[0]->getLabel());
234                                 }
235                                 
236                                 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
237                                         string saveLabel = lookup[0]->getLabel();
238                         
239                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  
240                         
241                                         lookup = input->getSharedRAbundVectors(lastLabel);
242                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
243                                         
244                                         string outputFileName = heatmap->getPic(lookup);
245                                         outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
246                                         
247                                         processedLabels.insert(lookup[0]->getLabel());
248                                         userLabels.erase(lookup[0]->getLabel());
249                                         
250                                         //restore real lastlabel to save below
251                                         lookup[0]->setLabel(saveLabel);
252                                 }
253                                 
254                                 lastLabel = lookup[0]->getLabel();
255                                 //prevent memory leak
256                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
257                                                         
258                                 //get next line to process
259                                 lookup = input->getSharedRAbundVectors();                               
260                         }
261                         
262                         
263                         if (m->control_pressed) {
264                                 for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } } outputTypes.clear();
265                                 globaldata->Groups.clear(); 
266                                 delete read; delete heatmap; return 0;
267                         }
268
269                         //output error messages about any remaining user labels
270                         set<string>::iterator it;
271                         bool needToRun = false;
272                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
273                                 m->mothurOut("Your file does not include the label " + *it); 
274                                 if (processedLabels.count(lastLabel) != 1) {
275                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
276                                         needToRun = true;
277                                 }else {
278                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
279                                 }
280                         }
281                 
282                         //run last label if you need to
283                         if (needToRun == true)  {
284                                 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }  
285                                 lookup = input->getSharedRAbundVectors(lastLabel);
286                                 
287                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
288                                 string outputFileName = heatmap->getPic(lookup);
289                                 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
290                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
291                         }
292                 
293                         //reset groups parameter
294                         globaldata->Groups.clear();  
295                         
296                 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
297         
298                         while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
299                                 if (m->control_pressed) {   
300                                         for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } } outputTypes.clear();
301                                         delete rabund;  delete read; delete heatmap; return 0;  
302                                 }
303
304                                 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){                     
305         
306                                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
307                                         string outputFileName = heatmap->getPic(rabund);
308                                         outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
309                                         
310                                         processedLabels.insert(rabund->getLabel());
311                                         userLabels.erase(rabund->getLabel());
312                                 }
313                                 
314                                 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
315                                         string saveLabel = rabund->getLabel();
316                                         
317                                         delete rabund;
318                                         rabund = input->getRAbundVector(lastLabel);
319                                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
320                                         
321                                         string outputFileName = heatmap->getPic(rabund);
322                                         outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
323                                         
324                                         processedLabels.insert(rabund->getLabel());
325                                         userLabels.erase(rabund->getLabel());
326                                         
327                                         //restore real lastlabel to save below
328                                         rabund->setLabel(saveLabel);
329                                 }               
330                                 
331                                                                 
332                                                                 
333                                 lastLabel = rabund->getLabel();                 
334                                 delete rabund;
335                                 rabund = input->getRAbundVector();
336                         }
337                         
338                         if (m->control_pressed) {
339                                 for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } } outputTypes.clear();
340                                 delete read; delete heatmap; return 0;
341                         }
342
343                         //output error messages about any remaining user labels
344                         set<string>::iterator it;
345                         bool needToRun = false;
346                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
347                                 m->mothurOut("Your file does not include the label " + *it); 
348                                 if (processedLabels.count(lastLabel) != 1) {
349                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
350                                         needToRun = true;
351                                 }else {
352                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
353                                 }
354                         }
355                 
356                         //run last label if you need to
357                         if (needToRun == true)  {
358                 
359                                 if (rabund != NULL) {   delete rabund;  }
360                                 rabund = input->getRAbundVector(lastLabel);
361                                 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
362                                         
363                                 string outputFileName = heatmap->getPic(rabund);
364                                 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
365                                 delete rabund; globaldata->rabund = NULL;
366                         }
367                 
368                 }else {
369                 
370                         //as long as you are not at the end of the file or done wih the lines you want
371                         while((lookupFloat[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
372                                 if (m->control_pressed) {
373                                         for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  }
374                                         for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } } outputTypes.clear();
375                                         globaldata->Groups.clear(); 
376                                         delete read; delete heatmap; return 0;
377                                 }
378                 
379                                 if(allLines == 1 || labels.count(lookupFloat[0]->getLabel()) == 1){                     
380         
381                                         m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
382                                         string outputFileName = heatmap->getPic(lookupFloat);
383                                         outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
384                                         
385                                         processedLabels.insert(lookupFloat[0]->getLabel());
386                                         userLabels.erase(lookupFloat[0]->getLabel());
387                                 }
388                                 
389                                 if ((m->anyLabelsToProcess(lookupFloat[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
390                                         string saveLabel = lookupFloat[0]->getLabel();
391                                 
392                                         for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  }  
393                                         lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
394                                         m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
395                                         
396                                         string outputFileName = heatmap->getPic(lookupFloat);
397                                         outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
398                                         
399                                         processedLabels.insert(lookupFloat[0]->getLabel());
400                                         userLabels.erase(lookupFloat[0]->getLabel());
401                                         
402                                         //restore real lastlabel to save below
403                                         lookupFloat[0]->setLabel(saveLabel);
404                                 }
405                                 
406                                 lastLabel = lookupFloat[0]->getLabel();
407                                 //prevent memory leak
408                                 for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i]; lookupFloat[i] = NULL; }
409                                                         
410                                 //get next line to process
411                                 lookupFloat = input->getSharedRAbundFloatVectors();                             
412                         }
413                         
414                         
415                         if (m->control_pressed) {
416                                 for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } } outputTypes.clear();
417                                 globaldata->Groups.clear(); 
418                                 delete read; delete heatmap; return 0;
419                         }
420
421                         //output error messages about any remaining user labels
422                         set<string>::iterator it;
423                         bool needToRun = false;
424                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
425                                 m->mothurOut("Your file does not include the label " + *it); 
426                                 if (processedLabels.count(lastLabel) != 1) {
427                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
428                                         needToRun = true;
429                                 }else {
430                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
431                                 }
432                         }
433                 
434                         //run last label if you need to
435                         if (needToRun == true)  {
436                                 for (int i = 0; i < lookupFloat.size(); i++) { if (lookupFloat[i] != NULL) { delete lookupFloat[i]; } }  
437                                 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
438                                 
439                                 m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
440                                 string outputFileName = heatmap->getPic(lookupFloat);
441                                 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
442                                 for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  }
443                         }
444                 
445                         //reset groups parameter
446                         globaldata->Groups.clear();  
447
448                 }
449                 
450                 globaldata->rabund = NULL;
451                 delete input; globaldata->ginput = NULL;
452                 
453                 if (m->control_pressed) {
454                         for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } } outputTypes.clear();
455                         delete read; delete heatmap; return 0;
456                 }
457                 
458                 m->mothurOutEndLine();
459                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
460                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
461                 m->mothurOutEndLine();
462         
463                 delete read;
464                 delete heatmap;
465
466                 return 0;
467         }
468         catch(exception& e) {
469                 m->errorOut(e, "HeatMapCommand", "execute");
470                 exit(1);
471         }
472 }
473
474 //**********************************************************************************************************************
475
476