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