]> git.donarmstrong.com Git - mothur.git/blob - heatmapcommand.cpp
changed how we do "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                 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                                  cout << "You must read a list, or a list and a group, or a shared before you can use the heatmap.bin command." << endl; 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 != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; 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                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapCommand class Function HeatMapCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
91                 exit(1);
92         }
93         catch(...) {
94                 cout << "An unknown error has occurred in the HeatMapCommand class function HeatMapCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
95                 exit(1);
96         }       
97 }
98
99 //**********************************************************************************************************************
100
101 void HeatMapCommand::help(){
102         try {
103                 cout << "The heatmap.bin command can only be executed after a successful read.otu command." << "\n";
104                 cout << "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";
105                 cout << "The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap." << "\n";
106                 cout << "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";
107                 cout << "The scale parameter allows you to choose the range of color your bin information will be displayed with." << "\n";
108                 cout << "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";
109                 cout << "The heatmap.bin command should be in the following format: heatmap.bin(groups=yourGroups, sorted=yourSorted, line=yourLines, label=yourLabels)." << "\n";
110                 cout << "Example heatmap.bin(groups=A-B-C, line=1-3-5, sorted=F, scale=log10)." << "\n";
111                 cout << "The default value for groups is all the groups in your groupfile, and all lines in your inputfile will be used." << "\n";
112                 cout << "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";
113                 cout << "The default value for scale is log10; your other options are log2 and linear." << "\n";
114                 cout << "The heatmap.bin command outputs a .svg file for each line or label you specify." << "\n";
115                 cout << "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups)." << "\n" << "\n";
116
117         }
118         catch(exception& e) {
119                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
120                 exit(1);
121         }
122         catch(...) {
123                 cout << "An unknown error has occurred in the HeatMapCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
124                 exit(1);
125         }       
126 }
127
128 //**********************************************************************************************************************
129
130 HeatMapCommand::~HeatMapCommand(){
131         if (abort == false) {
132                 delete read;
133                 delete heatmap;
134         }
135 }
136
137 //**********************************************************************************************************************
138
139 int HeatMapCommand::execute(){
140         try {
141         
142                 if (abort == true) { return 0; }
143         
144                 int count = 1;  
145                 string lastLabel;
146         
147                 if (format == "sharedfile") {
148                         //you have groups
149                         read = new ReadOTUFile(globaldata->inputFileName);      
150                         read->read(&*globaldata); 
151                         
152                         input = globaldata->ginput;
153                         lookup = input->getSharedRAbundVectors();
154                         lastLabel = lookup[0]->getLabel();
155                 }else if (format == "list") {
156                         //you are using just a list file and have only one group
157                         read = new ReadOTUFile(globaldata->inputFileName);      
158                         read->read(&*globaldata); 
159                         
160                         rabund = globaldata->rabund;
161                         lastLabel = rabund->getLabel();
162                         input = globaldata->ginput;             
163                 }
164                 
165                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
166                 set<string> processedLabels;
167                 set<string> userLabels = labels;
168                 set<int> userLines = lines;
169
170                 if (format != "list") { 
171                 
172                         //as long as you are not at the end of the file or done wih the lines you want
173                         while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
174                 
175                                 if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){                       
176         
177                                         cout << lookup[0]->getLabel() << '\t' << count << endl;
178                                         heatmap->getPic(lookup);
179                                         
180                                         processedLabels.insert(lookup[0]->getLabel());
181                                         userLabels.erase(lookup[0]->getLabel());
182                                         userLines.erase(count);
183                                 }
184                                 
185                                 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
186                                         cout << lastLabel << '\t' << count << endl;
187                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  
188                                         lookup = input->getSharedRAbundVectors(lastLabel);
189                                         
190                                         heatmap->getPic(lookup);
191                                         
192                                         processedLabels.insert(lookup[0]->getLabel());
193                                         userLabels.erase(lookup[0]->getLabel());
194                                 }
195                                 
196                                 lastLabel = lookup[0]->getLabel();
197                                 //prevent memory leak
198                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
199                                                         
200                                 //get next line to process
201                                 lookup = input->getSharedRAbundVectors();                               
202                                 count++;
203                         }
204                         
205                         //output error messages about any remaining user labels
206                         set<string>::iterator it;
207                         bool needToRun = false;
208                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
209                                 cout << "Your file does not include the label "<< *it; 
210                                 if (processedLabels.count(lastLabel) != 1) {
211                                         cout << ". I will use " << lastLabel << "." << endl;
212                                         needToRun = true;
213                                 }else {
214                                         cout << ". Please refer to " << lastLabel << "." << endl;
215                                 }
216                         }
217                 
218                         //run last line if you need to
219                         if (needToRun == true)  {
220                                 cout << lastLabel << '\t' << count << endl;
221                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  
222                                 lookup = input->getSharedRAbundVectors(lastLabel);
223
224                                 heatmap->getPic(lookup);
225                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
226                         }
227                 
228                         
229                         
230                         //reset groups parameter
231                         globaldata->Groups.clear();  
232                         
233                 }else{
234                 
235                         while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
236
237                                 if(allLines == 1 || lines.count(count) == 1 || labels.count(rabund->getLabel()) == 1){                  
238         
239                                         cout << rabund->getLabel() << '\t' << count << endl;
240                                         heatmap->getPic(rabund);
241                                         
242                                         processedLabels.insert(rabund->getLabel());
243                                         userLabels.erase(rabund->getLabel());
244                                         userLines.erase(count);
245                                 }
246                                 
247                                 if ((anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
248
249                                         cout << lastLabel << '\t' << count << endl;
250                                         delete rabund;
251                                         rabund = input->getRAbundVector(lastLabel);
252                                         
253                                         heatmap->getPic(rabund);
254                                         
255                                         processedLabels.insert(rabund->getLabel());
256                                         userLabels.erase(rabund->getLabel());
257                                 }               
258                                 
259                                                                 
260                                                                 
261                                 lastLabel = rabund->getLabel();                 
262                                 delete rabund;
263                                 rabund = input->getRAbundVector();
264                                 count++;
265                         }
266                         
267                         //output error messages about any remaining user labels
268                         set<string>::iterator it;
269                         bool needToRun = false;
270                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
271                                 cout << "Your file does not include the label "<< *it; 
272                                 if (processedLabels.count(lastLabel) != 1) {
273                                         cout << ". I will use " << lastLabel << "." << endl;
274                                         needToRun = true;
275                                 }else {
276                                         cout << ". Please refer to " << lastLabel << "." << endl;
277                                 }
278                         }
279                 
280                         //run last line if you need to
281                         if (needToRun == true)  {
282                                 cout << lastLabel << '\t' << count << endl;
283                                 delete rabund;
284                                 rabund = input->getRAbundVector(lastLabel);
285                                         
286                                 heatmap->getPic(rabund);
287                                 delete rabund; globaldata->rabund = NULL;
288                         }
289                 
290                 }
291                 
292                 delete input; globaldata->ginput = NULL;
293                 return 0;
294         }
295         catch(exception& e) {
296                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
297                 exit(1);
298         }
299         catch(...) {
300                 cout << "An unknown error has occurred in the HeatMapCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
301                 exit(1);
302         }               
303 }
304
305 //**********************************************************************************************************************
306
307