]> git.donarmstrong.com Git - mothur.git/blob - heatmapcommand.cpp
initial add of metastats
[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=none, 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                         
190                                         lookup = input->getSharedRAbundVectors(lastLabel);
191                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
192                                         
193                                         outputNames.push_back(heatmap->getPic(lookup));
194                                         
195                                         processedLabels.insert(lookup[0]->getLabel());
196                                         userLabels.erase(lookup[0]->getLabel());
197                                         
198                                         //restore real lastlabel to save below
199                                         lookup[0]->setLabel(saveLabel);
200                                 }
201                                 
202                                 lastLabel = lookup[0]->getLabel();
203                                 //prevent memory leak
204                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
205                                                         
206                                 //get next line to process
207                                 lookup = input->getSharedRAbundVectors();                               
208                         }
209                         
210                         
211                         if (m->control_pressed) {
212                                 for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
213                                 globaldata->Groups.clear(); 
214                                 delete read; delete heatmap; return 0;
215                         }
216
217                         //output error messages about any remaining user labels
218                         set<string>::iterator it;
219                         bool needToRun = false;
220                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
221                                 m->mothurOut("Your file does not include the label " + *it); 
222                                 if (processedLabels.count(lastLabel) != 1) {
223                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
224                                         needToRun = true;
225                                 }else {
226                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
227                                 }
228                         }
229                 
230                         //run last label if you need to
231                         if (needToRun == true)  {
232                                 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }  
233                                 lookup = input->getSharedRAbundVectors(lastLabel);
234                                 
235                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
236                                 outputNames.push_back(heatmap->getPic(lookup));
237                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
238                         }
239                 
240                         //reset groups parameter
241                         globaldata->Groups.clear();  
242                         
243                 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
244         
245                         while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
246                                 if (m->control_pressed) {   
247                                         for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
248                                         delete rabund;  delete read; delete heatmap; return 0;  
249                                 }
250
251                                 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){                     
252         
253                                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
254                                         outputNames.push_back(heatmap->getPic(rabund));
255                                         
256                                         processedLabels.insert(rabund->getLabel());
257                                         userLabels.erase(rabund->getLabel());
258                                 }
259                                 
260                                 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
261                                         string saveLabel = rabund->getLabel();
262                                         
263                                         delete rabund;
264                                         rabund = input->getRAbundVector(lastLabel);
265                                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
266                                         
267                                         outputNames.push_back(heatmap->getPic(rabund));
268                                         
269                                         processedLabels.insert(rabund->getLabel());
270                                         userLabels.erase(rabund->getLabel());
271                                         
272                                         //restore real lastlabel to save below
273                                         rabund->setLabel(saveLabel);
274                                 }               
275                                 
276                                                                 
277                                                                 
278                                 lastLabel = rabund->getLabel();                 
279                                 delete rabund;
280                                 rabund = input->getRAbundVector();
281                         }
282                         
283                         if (m->control_pressed) {
284                                 for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
285                                 delete read; delete heatmap; return 0;
286                         }
287
288                         //output error messages about any remaining user labels
289                         set<string>::iterator it;
290                         bool needToRun = false;
291                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
292                                 m->mothurOut("Your file does not include the label " + *it); 
293                                 if (processedLabels.count(lastLabel) != 1) {
294                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
295                                         needToRun = true;
296                                 }else {
297                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
298                                 }
299                         }
300                 
301                         //run last label if you need to
302                         if (needToRun == true)  {
303                 
304                                 if (rabund != NULL) {   delete rabund;  }
305                                 rabund = input->getRAbundVector(lastLabel);
306                                 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
307                                         
308                                 outputNames.push_back(heatmap->getPic(rabund));
309                                 delete rabund; globaldata->rabund = NULL;
310                         }
311                 
312                 }else {
313                 
314                         //as long as you are not at the end of the file or done wih the lines you want
315                         while((lookupFloat[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
316                                 if (m->control_pressed) {
317                                         for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  }
318                                         for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
319                                         globaldata->Groups.clear(); 
320                                         delete read; delete heatmap; return 0;
321                                 }
322                 
323                                 if(allLines == 1 || labels.count(lookupFloat[0]->getLabel()) == 1){                     
324         
325                                         m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
326                                         outputNames.push_back(heatmap->getPic(lookupFloat));
327                                         
328                                         processedLabels.insert(lookupFloat[0]->getLabel());
329                                         userLabels.erase(lookupFloat[0]->getLabel());
330                                 }
331                                 
332                                 if ((m->anyLabelsToProcess(lookupFloat[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
333                                         string saveLabel = lookupFloat[0]->getLabel();
334                                 
335                                         for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  }  
336                                         lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
337                                         m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
338                                         
339                                         outputNames.push_back(heatmap->getPic(lookupFloat));
340                                         
341                                         processedLabels.insert(lookupFloat[0]->getLabel());
342                                         userLabels.erase(lookupFloat[0]->getLabel());
343                                         
344                                         //restore real lastlabel to save below
345                                         lookupFloat[0]->setLabel(saveLabel);
346                                 }
347                                 
348                                 lastLabel = lookupFloat[0]->getLabel();
349                                 //prevent memory leak
350                                 for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i]; lookupFloat[i] = NULL; }
351                                                         
352                                 //get next line to process
353                                 lookupFloat = input->getSharedRAbundFloatVectors();                             
354                         }
355                         
356                         
357                         if (m->control_pressed) {
358                                 for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
359                                 globaldata->Groups.clear(); 
360                                 delete read; delete heatmap; return 0;
361                         }
362
363                         //output error messages about any remaining user labels
364                         set<string>::iterator it;
365                         bool needToRun = false;
366                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
367                                 m->mothurOut("Your file does not include the label " + *it); 
368                                 if (processedLabels.count(lastLabel) != 1) {
369                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
370                                         needToRun = true;
371                                 }else {
372                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
373                                 }
374                         }
375                 
376                         //run last label if you need to
377                         if (needToRun == true)  {
378                                 for (int i = 0; i < lookupFloat.size(); i++) { if (lookupFloat[i] != NULL) { delete lookupFloat[i]; } }  
379                                 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
380                                 
381                                 m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
382                                 outputNames.push_back(heatmap->getPic(lookupFloat));
383                                 for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  }
384                         }
385                 
386                         //reset groups parameter
387                         globaldata->Groups.clear();  
388
389                 }
390                 
391                 globaldata->rabund = NULL;
392                 delete input; globaldata->ginput = NULL;
393                 
394                 if (m->control_pressed) {
395                         for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
396                         delete read; delete heatmap; return 0;
397                 }
398                 
399                 m->mothurOutEndLine();
400                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
401                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
402                 m->mothurOutEndLine();
403         
404                 delete read;
405                 delete heatmap;
406
407                 return 0;
408         }
409         catch(exception& e) {
410                 m->errorOut(e, "HeatMapCommand", "execute");
411                 exit(1);
412         }
413 }
414
415 //**********************************************************************************************************************
416
417