]> git.donarmstrong.com Git - mothur.git/blob - heatmapcommand.cpp
modified calculators to use doubles, added numotu and fontsize parameters to heatmap...
[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 += 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() == "")) {
48                                  m->mothurOut("You must read a list, rabund, sabund, or a list and a group, or a shared 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") {  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                                 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                 }
156                 
157                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
158                 set<string> processedLabels;
159                 set<string> userLabels = labels;
160
161                 if ((format != "list") && (format != "rabund") && (format != "sabund")) {       
162                 
163                         //as long as you are not at the end of the file or done wih the lines you want
164                         while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
165                                 if (m->control_pressed) {
166                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
167                                         for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
168                                         globaldata->Groups.clear(); 
169                                         delete read; delete heatmap; return 0;
170                                 }
171                 
172                                 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
173         
174                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
175                                         outputNames.push_back(heatmap->getPic(lookup));
176                                         
177                                         processedLabels.insert(lookup[0]->getLabel());
178                                         userLabels.erase(lookup[0]->getLabel());
179                                 }
180                                 
181                                 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
182                                         string saveLabel = lookup[0]->getLabel();
183                                 
184                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  
185                                         lookup = input->getSharedRAbundVectors(lastLabel);
186                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
187                                         
188                                         outputNames.push_back(heatmap->getPic(lookup));
189                                         
190                                         processedLabels.insert(lookup[0]->getLabel());
191                                         userLabels.erase(lookup[0]->getLabel());
192                                         
193                                         //restore real lastlabel to save below
194                                         lookup[0]->setLabel(saveLabel);
195                                 }
196                                 
197                                 lastLabel = lookup[0]->getLabel();
198                                 //prevent memory leak
199                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
200                                                         
201                                 //get next line to process
202                                 lookup = input->getSharedRAbundVectors();                               
203                         }
204                         
205                         
206                         if (m->control_pressed) {
207                                 for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
208                                 globaldata->Groups.clear(); 
209                                 delete read; delete heatmap; return 0;
210                         }
211
212                         //output error messages about any remaining user labels
213                         set<string>::iterator it;
214                         bool needToRun = false;
215                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
216                                 m->mothurOut("Your file does not include the label " + *it); 
217                                 if (processedLabels.count(lastLabel) != 1) {
218                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
219                                         needToRun = true;
220                                 }else {
221                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
222                                 }
223                         }
224                 
225                         //run last label if you need to
226                         if (needToRun == true)  {
227                                 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }  
228                                 lookup = input->getSharedRAbundVectors(lastLabel);
229                                 
230                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
231                                 outputNames.push_back(heatmap->getPic(lookup));
232                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
233                         }
234                 
235                         //reset groups parameter
236                         globaldata->Groups.clear();  
237                         
238                 }else{
239         
240                         while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
241                                 if (m->control_pressed) {   
242                                         for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
243                                         delete rabund;  delete read; delete heatmap; return 0;  
244                                 }
245
246                                 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){                     
247         
248                                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
249                                         outputNames.push_back(heatmap->getPic(rabund));
250                                         
251                                         processedLabels.insert(rabund->getLabel());
252                                         userLabels.erase(rabund->getLabel());
253                                 }
254                                 
255                                 if ((anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
256                                         string saveLabel = rabund->getLabel();
257                                         
258                                         delete rabund;
259                                         rabund = input->getRAbundVector(lastLabel);
260                                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
261                                         
262                                         outputNames.push_back(heatmap->getPic(rabund));
263                                         
264                                         processedLabels.insert(rabund->getLabel());
265                                         userLabels.erase(rabund->getLabel());
266                                         
267                                         //restore real lastlabel to save below
268                                         rabund->setLabel(saveLabel);
269                                 }               
270                                 
271                                                                 
272                                                                 
273                                 lastLabel = rabund->getLabel();                 
274                                 delete rabund;
275                                 rabund = input->getRAbundVector();
276                         }
277                         
278                         if (m->control_pressed) {
279                                 for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
280                                 delete read; delete heatmap; return 0;
281                         }
282
283                         //output error messages about any remaining user labels
284                         set<string>::iterator it;
285                         bool needToRun = false;
286                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
287                                 m->mothurOut("Your file does not include the label " + *it); 
288                                 if (processedLabels.count(lastLabel) != 1) {
289                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
290                                         needToRun = true;
291                                 }else {
292                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
293                                 }
294                         }
295                 
296                         //run last label if you need to
297                         if (needToRun == true)  {
298                 
299                                 if (rabund != NULL) {   delete rabund;  }
300                                 rabund = input->getRAbundVector(lastLabel);
301                                 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
302                                         
303                                 outputNames.push_back(heatmap->getPic(rabund));
304                                 delete rabund; globaldata->rabund = NULL;
305                         }
306                 
307                 }
308                 
309                 globaldata->rabund = NULL;
310                 delete input; globaldata->ginput = NULL;
311                 
312                 if (m->control_pressed) {
313                         for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
314                         delete read; delete heatmap; return 0;
315                 }
316                 
317                 m->mothurOutEndLine();
318                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
319                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
320                 m->mothurOutEndLine();
321                 
322                 delete read;
323                 delete heatmap;
324
325                 return 0;
326         }
327         catch(exception& e) {
328                 m->errorOut(e, "HeatMapCommand", "execute");
329                 exit(1);
330         }
331 }
332
333 //**********************************************************************************************************************
334
335