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