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