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