5 * Created by Sarah Westcott on 3/25/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
12 //**********************************************************************************************************************
15 globaldata = GlobalData::getInstance();
16 format = globaldata->getFormat();
17 sorted = globaldata->getSorted();
18 util = new SharedUtil();
22 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function HeatMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
26 cout << "An unknown error has occurred in the HeatMap class function HeatMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
30 //**********************************************************************************************************************
31 void HeatMap::getPic(OrderVector* order) {
35 rabund = order->getRAbundVector();
37 //get users scaling method
38 scaler = globaldata->getScale();
41 for (int i = 0; i < rabund.size(); i++) {
42 if (rabund.get(i) != 0) { //don't want log value of 0.
43 if (scaler == "log10") {
44 colorScale[(log10((rabund.get(i) / (float)rabund.getNumSeqs()) * 1000))] = "";
45 if (maxbin < (log10((rabund.get(i) / (float)rabund.getNumSeqs()) * 1000))) { maxbin = (log10((rabund.get(i) / (float)rabund.getNumSeqs()) * 1000)); }
46 }else if (scaler == "log2") {
47 colorScale[(log2((rabund.get(i) / (float)rabund.getNumSeqs()) * 1000))] = "";
48 if (maxbin < (log2((rabund.get(i) / (float)rabund.getNumSeqs()) * 1000))) { maxbin = (log2((rabund.get(i) / (float)rabund.getNumSeqs()) * 1000)); }
49 }else if (scaler == "linear") {
50 colorScale[rabund.get(i)] = "";
51 if (maxbin < rabund.get(i)) { maxbin = rabund.get(i); }
52 }else { //if user enters invalid scaler option.
53 cout << scaler << " is not a valid scaler option. I will use log10." << endl;
54 colorScale[(log10((rabund.get(i) / (float)rabund.getNumSeqs()) * 1000))] = "";
55 if (maxbin < (log10((rabund.get(i) / (float)rabund.getNumSeqs()) * 1000))) { maxbin = (log10((rabund.get(i)) / (float)rabund.getNumSeqs()) * 1000); }
57 }else { colorScale[0] = "00"; }
60 float scalers = 255 / (float) maxbin;
62 //go through map and give each score a color value
63 for (it = colorScale.begin(); it != colorScale.end(); it++) {
64 it->second = toHex(int(float(it->first) * scalers));
65 if(it->second.length() == 1) { it->second = "0" + it->second; }
68 string filenamesvg = getRootName(globaldata->inputFileName) + order->getLabel() + ".heatmap.svg";
69 openOutputFile(filenamesvg, outsvg);
72 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 300 " + toString((rabund.getNumBins()*5 + 120)) + "\">\n";
76 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"300\" height=\"" + toString((rabund.getNumBins()*5 + 120)) + "\"/>";
77 outsvg << "<text fill=\"black\" class=\"seri\" x=\"100\" y=\"25\">Heatmap at distance " + order->getLabel() + "</text>\n";
79 //output legend and color labels
82 int y = 103 + (rabund.getNumBins()*5);
83 printLegend(y, maxbin);
86 for (int i = 0; i <= rabund.getNumBins(); i++) {
87 if (rabund.get(i) != 0) { //don't want log value of 0.
88 if (scaler == "log10") {
89 color = colorScale[(log10((rabund.get(i) / (float)rabund.getNumSeqs()) * 1000))];
90 }else if (scaler == "log2") {
91 color = colorScale[(log2((rabund.get(i) / (float)rabund.getNumSeqs()) * 1000))];
92 }else if (scaler == "linear") {
93 color = colorScale[rabund.get(i)];
94 }else { color = colorScale[(log10((rabund.get(i) / (float)rabund.getNumSeqs()) * 1000))]; }
95 }else { color = "OO"; }
97 outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
101 outsvg << "</g>\n</svg>\n";
105 catch(exception& e) {
106 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
110 cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
114 //**********************************************************************************************************************
115 void HeatMap::getPic(SharedOrderVector* sharedorder) {
119 //fills vector of sharedsabunds - lookup
120 util->getSharedVectors(globaldata->Groups, lookup, sharedorder); //fills group vectors from order vector.
122 //sort lookup so shared bins are on top
123 if (sorted == "T") { sortSharedVectors(); }
125 //get users scaling method
126 scaler = globaldata->getScale();
129 for (int i = 0; i < lookup.size(); i++) {
130 for (int j = 0; j < lookup[i]->size(); j++) {
131 if (lookup[i]->getAbundance(j) != 0) { //don't want log value of 0.
132 if (scaler == "log10") {
133 colorScale[(log10((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 1000))] = "";
134 if (maxbin < (log10((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 1000))) { maxbin = (log10((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 1000)); }
135 }else if (scaler == "log2") {
136 colorScale[(log2((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 1000))] = "";
137 if (maxbin < (log2((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 1000))) { maxbin = (log2((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 1000)); }
138 }else if (scaler == "linear") {
139 colorScale[lookup[i]->getAbundance(j)] = "";
140 if (maxbin < lookup[i]->getAbundance(j)) { maxbin = lookup[i]->getAbundance(j); }
141 }else { //if user enters invalid scaler option.
142 cout << scaler << " is not a valid scaler option. I will use log10." << endl;
143 colorScale[(log10((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 1000))] = "";
144 if (maxbin < (log10((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 1000))) { maxbin = (log10((lookup[i]->getAbundance(j)) / (float)lookup[i]->getNumSeqs()) * 1000); }
146 }else { colorScale[0] = "00"; }
151 float scalers = 255 / (float) maxbin;
154 //go through map and give each score a color value
155 for (it = colorScale.begin(); it != colorScale.end(); it++) {
156 it->second = toHex(int(float(it->first) * scalers));
157 if(it->second.length() == 1) { it->second = "0" + it->second; }
160 string filenamesvg = getRootName(globaldata->inputFileName) + sharedorder->getLabel() + ".heatmap.svg";
161 openOutputFile(filenamesvg, outsvg);
164 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(lookup.size() * 300) + " " + toString((lookup[0]->getNumBins()*5 + 120)) + "\">\n";
168 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString(lookup.size() * 300) + "\" height=\"" + toString((lookup[0]->getNumBins()*5 + 120)) + "\"/>";
169 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((lookup.size() * 150) - 40) + "\" y=\"25\">Heatmap at distance " + sharedorder->getLabel() + "</text>\n";
172 for (int h = 0; h < lookup.size(); h++) {
173 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";
176 //output legend and color labels
179 int y = 103 + (lookup[0]->getNumBins()*5);
180 printLegend(y, maxbin);
183 for (int i = 0; i < lookup[0]->size(); i++) {
184 for (int j = 0; j < lookup.size(); j++) {
186 if (lookup[j]->getAbundance(i) != 0) { //don't want log value of 0.
187 if (scaler == "log10") {
188 color = colorScale[(log10((lookup[j]->getAbundance(i) / (float)lookup[j]->getNumSeqs()) * 1000))];
189 }else if (scaler == "log2") {
190 color = colorScale[(log2((lookup[j]->getAbundance(i) / (float)lookup[j]->getNumSeqs()) * 1000))];
191 }else if (scaler == "linear") {
192 color = colorScale[lookup[j]->getAbundance(i)];
193 }else { color = colorScale[(log10((lookup[j]->getAbundance(i) / (float)lookup[j]->getNumSeqs()) * 1000))]; }
194 }else { color = "OO"; }
197 outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
205 outsvg << "</g>\n</svg>\n";
209 catch(exception& e) {
210 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
214 cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
219 //**********************************************************************************************************************
220 void HeatMap::sortSharedVectors(){
222 //copy lookup and then clear it to refill with sorted.
223 //loop though lookup and determine if they are shared
224 //if they are then insert in the front
225 //if not push to back
227 vector<SharedRAbundVector*> looktemp;
228 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.
229 map<int, int>::iterator it;
232 //create and initialize looktemp as a copy of lookup
233 for (int i = 0; i < lookup.size(); i++) {
234 SharedRAbundVector* temp = new SharedRAbundVector(lookup[i]->getNumBins());
235 temp->setLabel(lookup[i]->getLabel());
236 temp->setGroup(lookup[i]->getGroup());
237 //copy lookup i's info
238 for (int j = 0; j < lookup[i]->size(); j++) {
239 temp->set(j, lookup[i]->getAbundance(j), lookup[i]->getGroup());
241 looktemp.push_back(temp);
244 //clear out lookup to create sorted lookup
247 //create and initialize lookup to empty vectors
248 for (int i = 0; i < looktemp.size(); i++) {
249 SharedRAbundVector* temp = new SharedRAbundVector();
250 temp->setLabel(looktemp[i]->getLabel());
251 temp->setGroup(looktemp[i]->getGroup());
252 lookup.push_back(temp);
254 //initialize place map
260 for (int i = 0; i < looktemp[0]->size(); i++) {
262 bool updatePlace = false;
264 for (int j = 0; j < looktemp.size(); j++) {
265 if (looktemp[j]->getAbundance(i) != 0) { count++; }
269 for (int j = 0; j < looktemp.size(); j++) {
270 //if they are not shared then push to back, if they are not insert in front
271 if (count < 2) { lookup[j]->push_back(looktemp[j]->getAbundance(i), i, looktemp[j]->getGroup()); }
272 //they are shared by some
273 else { lookup[j]->insert(looktemp[j]->getAbundance(i), place[count], looktemp[j]->getGroup()); updatePlace = true; }
276 if (updatePlace == true) {
277 //move place holders below where you entered up to "make space" for you entry
278 for (it = place.begin(); it!= place.end(); it++) {
279 if (it->first < count) { it->second++; }
285 for (int j = 0; j < looktemp.size(); j++) {
290 catch(exception& e) {
291 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
295 cout << "An unknown error has occurred in the HeatMap class function sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
301 //**********************************************************************************************************************
302 void HeatMap::printLegend(int y, float maxbin) {
305 //output legend and color labels
306 //go through map and give each score a color value
310 //convert maxbin to relative abundance again
311 if (scaler == "log10") {
312 maxbin = pow(10, maxbin) / 1000;
313 }else if (scaler == "log2") {
314 maxbin = pow(2, maxbin) / 1000;
315 }else { maxbin = pow(10, maxbin) / 1000; }
316 }else { maxbin = 0.00; }
318 //5 is the number of boxes in the legend
319 float maxbinScaler = maxbin / 10;
320 float colorScaler = 255 / 10;
323 for (int i = 0; i < 10; i++) {
324 color = toHex(int((float)(i+1) * colorScaler));
325 outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"30\" height=\"10\"/>\n";
329 //prints legend labels
331 for (int i = 10; i > 0; i = i-2) {
332 string label = toString((i * maxbinScaler));
333 //set precision of relative abundance to 3
334 int pos = label.find_first_of('.');
335 label = label.substr(0,pos+4);
337 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(x) + "\" y=\"" + toString(y-3) + "\">" + label + "</text>\n";
341 catch(exception& e) {
342 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function printLegend. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
346 cout << "An unknown error has occurred in the HeatMap class function printLegend. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";