]> git.donarmstrong.com Git - mothur.git/blob - heatmapcommand.cpp
added logfile feature
[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                 lines.clear();
21                 labels.clear();
22                 
23                 //allow user to run help
24                 if(option == "help") { help(); abort = true; }
25                 
26                 else {
27                         //valid paramters for this command
28                         string AlignArray[] =  {"groups","line","label","sorted","scale"};
29                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
30                         
31                         OptionParser parser(option);
32                         map<string,string> parameters = parser.getParameters();
33                         
34                         ValidParameters validParameter;
35                         
36                         //check to make sure all parameters are valid for command
37                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
38                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
39                         }
40                         
41                         //make sure the user has already run the read.otu command
42                         if ((globaldata->getListFile() == "") && (globaldata->getSharedFile() == "")) {
43                                  mothurOut("You must read a list, or a list and a group, or a shared before you can use the heatmap.bin command."); mothurOutEndLine(); abort = true; 
44                         }
45
46                         //check for optional parameter and set defaults
47                         // ...at some point should added some additional type checking...
48                         line = validParameter.validFile(parameters, "line", false);                             
49                         if (line == "not found") { line = "";  }
50                         else { 
51                                 if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
52                                 else { allLines = 1;  }
53                         }
54                         
55                         label = validParameter.validFile(parameters, "label", false);                   
56                         if (label == "not found") { label = ""; }
57                         else { 
58                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
59                                 else { allLines = 1;  }
60                         }
61                         
62                         //make sure user did not use both the line and label parameters
63                         if ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; }
64                         //if the user has not specified any line or labels use the ones from read.otu
65                         else if ((line == "") && (label == "")) {  
66                                 allLines = globaldata->allLines; 
67                                 labels = globaldata->labels; 
68                                 lines = globaldata->lines;
69                         }
70                         
71                         groups = validParameter.validFile(parameters, "groups", false);                 
72                         if (groups == "not found") { groups = ""; }
73                         else { 
74                                 splitAtDash(groups, Groups);
75                                 globaldata->Groups = Groups;
76                         }
77                         
78                         sorted = validParameter.validFile(parameters, "sorted", false);                 if (sorted == "not found") { sorted = "T"; }
79                  
80                         scale = validParameter.validFile(parameters, "scale", false);                           if (scale == "not found") { scale = "log10"; }
81                         
82                         if (abort == false) {
83                                 heatmap = new HeatMap(sorted, scale);
84                                 format = globaldata->getFormat();
85                         }
86                 }
87
88         }
89         catch(exception& e) {
90                 errorOut(e, "HeatMapCommand", "HeatMapCommand");
91                 exit(1);
92         }
93 }
94
95 //**********************************************************************************************************************
96
97 void HeatMapCommand::help(){
98         try {
99                 mothurOut("The heatmap.bin command can only be executed after a successful read.otu command.\n");
100                 mothurOut("The heatmap.bin command parameters are groups, sorted, scale, line and label.  No parameters are required, but you may not use line and label at the same time.\n");
101                 mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
102                 mothurOut("The sorted parameter allows you to choose to see the file with the shared otus at the top or the otus in the order they appear in your input file. \n");
103                 mothurOut("The scale parameter allows you to choose the range of color your bin information will be displayed with.\n");
104                 mothurOut("The group names are separated by dashes. The line and label allow you to select what distance levels you would like a heatmap created for, and are also separated by dashes.\n");
105                 mothurOut("The heatmap.bin command should be in the following format: heatmap.bin(groups=yourGroups, sorted=yourSorted, line=yourLines, label=yourLabels).\n");
106                 mothurOut("Example heatmap.bin(groups=A-B-C, line=1-3-5, sorted=F, scale=log10).\n");
107                 mothurOut("The default value for groups is all the groups in your groupfile, and all lines in your inputfile will be used.\n");
108                 mothurOut("The default value for sorted is T meaning you want the shared otus on top, you may change it to F meaning the exact representation of your input file.\n");
109                 mothurOut("The default value for scale is log10; your other options are log2 and linear.\n");
110                 mothurOut("The heatmap.bin command outputs a .svg file for each line or label you specify.\n");
111                 mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
112
113         }
114         catch(exception& e) {
115                 errorOut(e, "HeatMapCommand", "help");
116                 exit(1);
117         }
118 }
119
120 //**********************************************************************************************************************
121
122 HeatMapCommand::~HeatMapCommand(){
123         if (abort == false) {
124                 delete read;
125                 delete heatmap;
126         }
127 }
128
129 //**********************************************************************************************************************
130
131 int HeatMapCommand::execute(){
132         try {
133         
134                 if (abort == true) { return 0; }
135         
136                 int count = 1;  
137                 string lastLabel;
138         
139                 if (format == "sharedfile") {
140                         //you have groups
141                         read = new ReadOTUFile(globaldata->inputFileName);      
142                         read->read(&*globaldata); 
143                         
144                         input = globaldata->ginput;
145                         lookup = input->getSharedRAbundVectors();
146                         lastLabel = lookup[0]->getLabel();
147                 }else if (format == "list") {
148                         //you are using just a list file and have only one group
149                         read = new ReadOTUFile(globaldata->inputFileName);      
150                         read->read(&*globaldata); 
151                         
152                         rabund = globaldata->rabund;
153                         lastLabel = rabund->getLabel();
154                         input = globaldata->ginput;             
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                 set<int> userLines = lines;
161
162                 if (format != "list") { 
163                 
164                         //as long as you are not at the end of the file or done wih the lines you want
165                         while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
166                 
167                                 if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){                       
168         
169                                         mothurOut(lookup[0]->getLabel() + "\t" + toString(count)); mothurOutEndLine();
170                                         heatmap->getPic(lookup);
171                                         
172                                         processedLabels.insert(lookup[0]->getLabel());
173                                         userLabels.erase(lookup[0]->getLabel());
174                                         userLines.erase(count);
175                                 }
176                                 
177                                 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
178                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  
179                                         lookup = input->getSharedRAbundVectors(lastLabel);
180                                         mothurOut(lookup[0]->getLabel() + "\t" + toString(count)); mothurOutEndLine();
181                                         
182                                         heatmap->getPic(lookup);
183                                         
184                                         processedLabels.insert(lookup[0]->getLabel());
185                                         userLabels.erase(lookup[0]->getLabel());
186                                 }
187                                 
188                                 lastLabel = lookup[0]->getLabel();
189                                 //prevent memory leak
190                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
191                                                         
192                                 //get next line to process
193                                 lookup = input->getSharedRAbundVectors();                               
194                                 count++;
195                         }
196                         
197                         //output error messages about any remaining user labels
198                         set<string>::iterator it;
199                         bool needToRun = false;
200                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
201                                 mothurOut("Your file does not include the label " + *it); 
202                                 if (processedLabels.count(lastLabel) != 1) {
203                                         mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
204                                         needToRun = true;
205                                 }else {
206                                         mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
207                                 }
208                         }
209                 
210                         //run last line if you need to
211                         if (needToRun == true)  {
212                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  
213                                 lookup = input->getSharedRAbundVectors(lastLabel);
214                                 
215                                 mothurOut(lookup[0]->getLabel() + "\t" + toString(count)); mothurOutEndLine();
216                                 heatmap->getPic(lookup);
217                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
218                         }
219                 
220                         
221                         
222                         //reset groups parameter
223                         globaldata->Groups.clear();  
224                         
225                 }else{
226                 
227                         while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
228
229                                 if(allLines == 1 || lines.count(count) == 1 || labels.count(rabund->getLabel()) == 1){                  
230         
231                                         mothurOut(rabund->getLabel() + "\t" + toString(count)); mothurOutEndLine();
232                                         heatmap->getPic(rabund);
233                                         
234                                         processedLabels.insert(rabund->getLabel());
235                                         userLabels.erase(rabund->getLabel());
236                                         userLines.erase(count);
237                                 }
238                                 
239                                 if ((anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
240
241                                         delete rabund;
242                                         rabund = input->getRAbundVector(lastLabel);
243                                         mothurOut(rabund->getLabel() + "\t" + toString(count)); mothurOutEndLine();
244                                         
245                                         heatmap->getPic(rabund);
246                                         
247                                         processedLabels.insert(rabund->getLabel());
248                                         userLabels.erase(rabund->getLabel());
249                                 }               
250                                 
251                                                                 
252                                                                 
253                                 lastLabel = rabund->getLabel();                 
254                                 delete rabund;
255                                 rabund = input->getRAbundVector();
256                                 count++;
257                         }
258                         
259                         //output error messages about any remaining user labels
260                         set<string>::iterator it;
261                         bool needToRun = false;
262                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
263                                 mothurOut("Your file does not include the label " + *it); 
264                                 if (processedLabels.count(lastLabel) != 1) {
265                                         mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
266                                         needToRun = true;
267                                 }else {
268                                         mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
269                                 }
270                         }
271                 
272                         //run last line if you need to
273                         if (needToRun == true)  {
274                 
275                                 delete rabund;
276                                 rabund = input->getRAbundVector(lastLabel);
277                                 mothurOut(rabund->getLabel() + "\t" + toString(count)); mothurOutEndLine();
278                                         
279                                 heatmap->getPic(rabund);
280                                 delete rabund; globaldata->rabund = NULL;
281                         }
282                 
283                 }
284                 
285                 delete input; globaldata->ginput = NULL;
286                 return 0;
287         }
288         catch(exception& e) {
289                 errorOut(e, "HeatMapCommand", "execute");
290                 exit(1);
291         }
292 }
293
294 //**********************************************************************************************************************
295
296