]> git.donarmstrong.com Git - mothur.git/blob - heatmap.cpp
broke up globaldata and moved error checking and help into commands
[mothur.git] / heatmap.cpp
1 /*
2  *  heatmap.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 "heatmap.h"
11
12 //**********************************************************************************************************************
13 HeatMap::HeatMap(string sort, string scale){
14         try {
15                 globaldata = GlobalData::getInstance();
16                 format = globaldata->getFormat();
17                 sorted = sort;
18                 scaler = scale;
19         }
20         catch(exception& e) {
21                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function HeatMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
22                 exit(1);
23         }
24         catch(...) {
25                 cout << "An unknown error has occurred in the HeatMap class function HeatMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
26                 exit(1);
27         }
28 }
29
30 //**********************************************************************************************************************
31
32 void HeatMap::getPic(RAbundVector* rabund) {
33         try {
34                 
35                 
36                 float maxRelAbund = 0.0;                
37                 
38                 for(int i=0;i<rabund->size();i++){                              
39                         float relAbund = rabund->get(i) / (float)rabund->getNumSeqs();
40                         if(relAbund > maxRelAbund){     maxRelAbund = relAbund; }
41                 }
42                 
43                 
44                 vector<string> scaleRelAbund(rabund->size(), "");
45                 
46                 for(int i=0;i<rabund->size();i++){
47                         float relAbund = rabund->get(i) / (float)rabund->getNumSeqs();
48                         
49                         if (rabund->get(i) != 0) { //don't want log value of 0.
50                                 if (scaler == "log10") {
51                                         scaleRelAbund[i] = toHex(int(255 * log10(relAbund) / log10(maxRelAbund))) + "0000";  
52                                 }else if (scaler == "log2") {
53                                         scaleRelAbund[i] = toHex(int(255 * log2(relAbund) / log2(maxRelAbund))) + "0000";  
54                                 }else if (scaler == "linear") {
55                                         scaleRelAbund[i] = toHex(int(255 * relAbund / maxRelAbund)) + "0000";  
56                                 }else {  //if user enters invalid scaler option.
57                                         scaleRelAbund[i] = toHex(int(255 * log10(relAbund / log10(maxRelAbund))))  + "0000"; 
58                                 } 
59                         }
60                         else { scaleRelAbund[i] = "FFFFFF";  }
61                 }
62                 
63                 
64                 string filenamesvg = getRootName(globaldata->inputFileName) + rabund->getLabel() + ".heatmap.bin.svg";
65                 openOutputFile(filenamesvg, outsvg);
66                 
67                 //svg image
68                 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(300) + " " + toString((rabund->getNumBins()*5 + 120))  + "\">\n";
69                 outsvg << "<g>\n";
70                 
71                 //white backround
72                 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString(300) + "\" height=\"" + toString((rabund->getNumBins()*5 + 120))  + "\"/>"; 
73                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((150) - 40) + "\" y=\"25\">Heatmap at distance " + rabund->getLabel() + "</text>\n";
74                                 
75                 //output legend and color labels
76                 string color;
77                 int x = 0;
78                 int y = 103 + (rabund->getNumBins()*5);
79                 printLegend(y, maxRelAbund);
80                 
81                 y = 70;
82                 for (int i = 0; i < scaleRelAbund.size(); i++) {
83                         
84                         outsvg << "<rect fill=\"#" + scaleRelAbund[i] + "\" stroke=\"#" + scaleRelAbund[i] + "\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
85                         y += 5;
86                 }
87                 
88                 outsvg << "</g>\n</svg>\n";
89                 outsvg.close();
90                 
91                 
92         }
93         catch(exception& e) {
94                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
95                 exit(1);
96         }
97         catch(...) {
98                 cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
99                 exit(1);
100         }
101 }
102
103 //**********************************************************************************************************************
104
105 void HeatMap::getPic(vector<SharedRAbundVector*> lookup) {
106         try {
107                 //sort lookup so shared bins are on top
108                 if (isTrue(sorted) == true) {  sortSharedVectors(lookup);  }
109                 
110                 vector<vector<string> > scaleRelAbund;
111                 vector<float> maxRelAbund(lookup.size(), 0.0);          
112                 float superMaxRelAbund = 0;
113                 
114                 for(int i = 0; i < lookup.size(); i++){
115                         for(int j=0; j<lookup[i]->size(); j++){
116                                 
117                                 float relAbund = lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs();
118                                 if(relAbund > maxRelAbund[i]){  maxRelAbund[i] = relAbund;      }
119                         }
120                         if(maxRelAbund[i] > superMaxRelAbund){  superMaxRelAbund = maxRelAbund[i];      }
121                 }
122                 
123                 scaleRelAbund.resize(lookup.size());
124                 for(int i=0;i<lookup.size();i++){
125                         scaleRelAbund[i].assign(lookup[i]->size(), "");
126                         for(int j=0;j<lookup[i]->size();j++){
127                                 float relAbund = lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs();
128                                 
129                                 if (lookup[i]->getAbundance(j) != 0) { //don't want log value of 0.
130                                         if (scaler == "log10") {
131                                                 scaleRelAbund[i][j] = toHex(int(255 * log10(relAbund) / log10(maxRelAbund[i]))) + "0000";  
132                                         }else if (scaler == "log2") {
133                                                 scaleRelAbund[i][j] = toHex(int(255 * log2(relAbund) / log2(maxRelAbund[i]))) + "0000";  
134                                         }else if (scaler == "linear") {
135                                                 scaleRelAbund[i][j] = toHex(int(255 * relAbund / maxRelAbund[i])) + "0000";  
136                                         }else {  //if user enters invalid scaler option.
137                                                 scaleRelAbund[i][j] = toHex(int(255 * log10(relAbund / log10(maxRelAbund[i]))))  + "0000"; 
138                                         } 
139                                 }else { scaleRelAbund[i][j] = "FFFFFF";  }
140
141                         }
142                 }
143
144                 string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + ".heatmap.bin.svg";
145                 openOutputFile(filenamesvg, outsvg);
146                 
147                 //svg image
148                 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(lookup.size() * 300) + " " + toString((lookup[0]->getNumBins()*5 + 120))  + "\">\n";
149                 outsvg << "<g>\n";
150                 
151                 //white backround
152                 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString(lookup.size() * 300) + "\" height=\"" + toString((lookup[0]->getNumBins()*5 + 120))  + "\"/>"; 
153                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((lookup.size() * 150) - 40) + "\" y=\"25\">Heatmap at distance " + lookup[0]->getLabel() + "</text>\n";
154                 
155                 //column labels
156                 for (int h = 0; h < lookup.size(); h++) {
157                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(((300 * (h+1)) - 150) - ((int)lookup[h]->getGroup().length() / 2)) + "\" y=\"50\">" + lookup[h]->getGroup() + "</text>\n"; 
158                 }
159                 
160                 //output legend and color labels
161                 string color;
162                 int x = 0;
163                 int y = 103 + (lookup[0]->getNumBins()*5);
164                 printLegend(y, superMaxRelAbund);
165                 
166                 y = 70;
167                 for (int i = 0; i < scaleRelAbund[0].size(); i++) {
168                         for (int j = 0; j < scaleRelAbund.size(); j++) {
169                                 
170                                 outsvg << "<rect fill=\"#" + scaleRelAbund[j][i] + "\" stroke=\"#" + scaleRelAbund[j][i] + "\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
171                                 x += 300;
172                         }
173                         x = 0;
174                         y += 5;
175                 }
176                 
177                 outsvg << "</g>\n</svg>\n";
178                 outsvg.close();
179
180         }
181         catch(exception& e) {
182                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183                 exit(1);
184         }
185         catch(...) {
186                 cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
187                 exit(1);
188         }
189 }
190
191 //**********************************************************************************************************************
192 void HeatMap::sortSharedVectors(vector<SharedRAbundVector*>& lookup){
193         try {
194                 //copy lookup and then clear it to refill with sorted.
195                 //loop though lookup and determine if they are shared
196                 //if they are then insert in the front
197                 //if not push to back
198                 
199                 vector<SharedRAbundVector*> looktemp;
200                 map<int, int> place; //spot in lookup where you insert shared by, ie, 3 -> 2 if they are shared by 3 inset into location 2.
201                 map<int, int>::iterator it;
202                 int count;
203                 
204                 //create and initialize looktemp as a copy of lookup
205                 for (int i = 0; i < lookup.size(); i++) { 
206                         SharedRAbundVector* temp = new SharedRAbundVector(lookup[i]->getNumBins());
207                         temp->setLabel(lookup[i]->getLabel());
208                         temp->setGroup(lookup[i]->getGroup());
209                         //copy lookup i's info
210                         for (int j = 0; j < lookup[i]->size(); j++) {
211                                 temp->set(j, lookup[i]->getAbundance(j), lookup[i]->getGroup());
212                         }
213                         looktemp.push_back(temp);
214                 }
215                 
216                 //clear out lookup to create sorted lookup -- Sarah look at - this is causing segmentation faults
217                 for (int j = 0; j < lookup.size(); j++) {
218 //                      delete lookup[j];
219                 }
220                 lookup.clear();  //doesn't this do the job?
221                 
222                 //create and initialize lookup to empty vectors
223                 for (int i = 0; i < looktemp.size(); i++) { 
224                         SharedRAbundVector* temp = new SharedRAbundVector();
225                         temp->setLabel(looktemp[i]->getLabel());
226                         temp->setGroup(looktemp[i]->getGroup());
227                         lookup.push_back(temp); 
228                         
229                         //initialize place map
230                         place[i] = 0;
231                 }
232                 
233                 
234                 //for each bin
235                 for (int i = 0; i < looktemp[0]->size(); i++) {
236                         count = 0;
237                         bool updatePlace = false;
238                         //for each group
239                         for (int j = 0; j < looktemp.size(); j++) {
240                                 if (looktemp[j]->getAbundance(i) != 0) { count++; }
241                         }
242                         
243                         //fill lookup
244                         for (int j = 0; j < looktemp.size(); j++) {
245                                 //if they are not shared then push to back, if they are not insert in front
246                                 if (count < 2)  { lookup[j]->push_back(looktemp[j]->getAbundance(i), i, looktemp[j]->getGroup()); }
247                                 //they are shared by some
248                                 else {  lookup[j]->insert(looktemp[j]->getAbundance(i), place[count], looktemp[j]->getGroup());   updatePlace = true; }
249                         }
250                         
251                         if (updatePlace == true) {
252                                 //move place holders below where you entered up to "make space" for you entry
253                                 for (it = place.begin(); it!= place.end(); it++) {  
254                                         if (it->first < count) { it->second++; }
255                                 }
256                         }
257                 }
258                 
259                 //delete looktemp -- Sarah look at - this is causing segmentation faults
260                 for (int j = 0; j < looktemp.size(); j++) {
261 //                      delete looktemp[j];
262                 }
263                 
264         }
265         catch(exception& e) {
266                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
267                 exit(1);
268         }
269         catch(...) {
270                 cout << "An unknown error has occurred in the HeatMap class function sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
271                 exit(1);
272         }
273         
274 }
275
276 //**********************************************************************************************************************
277
278 void HeatMap::printLegend(int y, float maxbin) {
279         try {
280                 
281                 //output legend and color labels
282                 //go through map and give each score a color value
283                 string color;
284                 int x = 10;
285                 
286                 //prints legend
287                 for (int i = 1; i < 255; i++) {
288                         color = toHex(int((float)(i)));
289                         outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"1\" height=\"10\"/>\n";
290                         x += 1;
291                 }
292                 
293                 //prints legend labels
294                 x = 10;
295                 for (int i = 1; i<=5; i++) {
296                         float label;
297                         if(scaler== "log10")            {       label = maxbin * log10(51*i) / log10(255);      }
298                         else if(scaler== "log2")        {       label = maxbin * log2(51*i) / log2(255);        }
299                         else if(scaler== "linear")      {       label = maxbin * 51 * i / 255;                          }
300                         else                                            {       label = maxbin * log10(51*i) / log10(255);      }
301                         label = int(label * 1000 + 0.5);
302                         label /= 1000.0;
303                         string text = toString(label, 3);
304                         
305                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(x) + "\" y=\"" + toString(y-3) + "\">" + text + "</text>\n";
306                         x += 60;
307                 }
308         }
309         
310         catch(exception& e) {
311                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function printLegend. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
312                 exit(1);
313         }
314         catch(...) {
315                 cout << "An unknown error has occurred in the HeatMap class function printLegend. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
316                 exit(1);
317         }
318         
319 }
320
321
322
323