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();
20 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function HeatMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
24 cout << "An unknown error has occurred in the HeatMap class function HeatMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
29 //**********************************************************************************************************************
31 void HeatMap::getPic(RAbundVector* rabund) {
33 //get users scaling method
34 scaler = globaldata->getScale();
36 float maxRelAbund = 0.0;
38 for(int i=0;i<rabund->size();i++){
39 float relAbund = rabund->get(i) / (float)rabund->getNumSeqs();
40 if(relAbund > maxRelAbund){ maxRelAbund = relAbund; }
43 scaler = globaldata->getScale();
45 vector<string> scaleRelAbund(rabund->size(), "");
47 for(int i=0;i<rabund->size();i++){
48 float relAbund = rabund->get(i) / (float)rabund->getNumSeqs();
50 if (rabund->get(i) != 0) { //don't want log value of 0.
51 if (scaler == "log10") {
52 scaleRelAbund[i] = toHex(int(255 * log10(relAbund) / log10(maxRelAbund))) + "0000";
53 }else if (scaler == "log2") {
54 scaleRelAbund[i] = toHex(int(255 * log2(relAbund) / log2(maxRelAbund))) + "0000";
55 }else if (scaler == "linear") {
56 scaleRelAbund[i] = toHex(int(255 * relAbund / maxRelAbund)) + "0000";
57 }else { //if user enters invalid scaler option.
58 scaleRelAbund[i] = toHex(int(255 * log10(relAbund / log10(maxRelAbund)))) + "0000";
61 else { scaleRelAbund[i] = "FFFFFF"; }
65 string filenamesvg = getRootName(globaldata->inputFileName) + rabund->getLabel() + ".heatmap.svg";
66 openOutputFile(filenamesvg, outsvg);
69 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(300) + " " + toString((rabund->getNumBins()*5 + 120)) + "\">\n";
73 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString(300) + "\" height=\"" + toString((rabund->getNumBins()*5 + 120)) + "\"/>";
74 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((150) - 40) + "\" y=\"25\">Heatmap at distance " + rabund->getLabel() + "</text>\n";
76 //output legend and color labels
79 int y = 103 + (rabund->getNumBins()*5);
80 printLegend(y, maxRelAbund);
83 for (int i = 0; i < scaleRelAbund.size(); i++) {
85 outsvg << "<rect fill=\"#" + scaleRelAbund[i] + "\" stroke=\"#" + scaleRelAbund[i] + "\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
89 outsvg << "</g>\n</svg>\n";
95 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
99 cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
104 //**********************************************************************************************************************
106 void HeatMap::getPic(vector<SharedRAbundVector*> lookup) {
109 //sort lookup so shared bins are on top
110 if (sorted == "T") { sortSharedVectors(lookup); }
112 vector<vector<string> > scaleRelAbund;
113 vector<float> maxRelAbund(lookup.size(), 0.0);
114 float superMaxRelAbund = 0;
116 for(int i = 0; i < lookup.size(); i++){
117 for(int j=0; j<lookup[i]->size(); j++){
119 float relAbund = lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs();
120 if(relAbund > maxRelAbund[i]){ maxRelAbund[i] = relAbund; }
122 if(maxRelAbund[i] > superMaxRelAbund){ superMaxRelAbund = maxRelAbund[i]; }
125 scaler = globaldata->getScale();
127 scaleRelAbund.resize(lookup.size());
128 for(int i=0;i<lookup.size();i++){
129 scaleRelAbund[i].assign(lookup[i]->size(), "");
130 for(int j=0;j<lookup[i]->size();j++){
131 float relAbund = lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs();
133 if (lookup[i]->getAbundance(j) != 0) { //don't want log value of 0.
134 if (scaler == "log10") {
135 scaleRelAbund[i][j] = toHex(int(255 * log10(relAbund) / log10(maxRelAbund[i]))) + "0000";
136 }else if (scaler == "log2") {
137 scaleRelAbund[i][j] = toHex(int(255 * log2(relAbund) / log2(maxRelAbund[i]))) + "0000";
138 }else if (scaler == "linear") {
139 scaleRelAbund[i][j] = toHex(int(255 * relAbund / maxRelAbund[i])) + "0000";
140 }else { //if user enters invalid scaler option.
141 scaleRelAbund[i][j] = toHex(int(255 * log10(relAbund / log10(maxRelAbund[i])))) + "0000";
143 }else { scaleRelAbund[i][j] = "FFFFFF"; }
148 string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + ".heatmap.svg";
149 openOutputFile(filenamesvg, outsvg);
152 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(lookup.size() * 300) + " " + toString((lookup[0]->getNumBins()*5 + 120)) + "\">\n";
156 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString(lookup.size() * 300) + "\" height=\"" + toString((lookup[0]->getNumBins()*5 + 120)) + "\"/>";
157 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((lookup.size() * 150) - 40) + "\" y=\"25\">Heatmap at distance " + lookup[0]->getLabel() + "</text>\n";
160 for (int h = 0; h < lookup.size(); h++) {
161 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";
164 //output legend and color labels
167 int y = 103 + (lookup[0]->getNumBins()*5);
168 printLegend(y, superMaxRelAbund);
171 for (int i = 0; i < scaleRelAbund[0].size(); i++) {
172 for (int j = 0; j < scaleRelAbund.size(); j++) {
174 outsvg << "<rect fill=\"#" + scaleRelAbund[j][i] + "\" stroke=\"#" + scaleRelAbund[j][i] + "\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
181 outsvg << "</g>\n</svg>\n";
185 catch(exception& e) {
186 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
190 cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
195 //**********************************************************************************************************************
196 void HeatMap::sortSharedVectors(vector<SharedRAbundVector*>& lookup){
198 //copy lookup and then clear it to refill with sorted.
199 //loop though lookup and determine if they are shared
200 //if they are then insert in the front
201 //if not push to back
203 vector<SharedRAbundVector*> looktemp;
204 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.
205 map<int, int>::iterator it;
208 //create and initialize looktemp as a copy of lookup
209 for (int i = 0; i < lookup.size(); i++) {
210 SharedRAbundVector* temp = new SharedRAbundVector(lookup[i]->getNumBins());
211 temp->setLabel(lookup[i]->getLabel());
212 temp->setGroup(lookup[i]->getGroup());
213 //copy lookup i's info
214 for (int j = 0; j < lookup[i]->size(); j++) {
215 temp->set(j, lookup[i]->getAbundance(j), lookup[i]->getGroup());
217 looktemp.push_back(temp);
220 //clear out lookup to create sorted lookup
223 //create and initialize lookup to empty vectors
224 for (int i = 0; i < looktemp.size(); i++) {
225 SharedRAbundVector* temp = new SharedRAbundVector();
226 temp->setLabel(looktemp[i]->getLabel());
227 temp->setGroup(looktemp[i]->getGroup());
228 lookup.push_back(temp);
230 //initialize place map
236 for (int i = 0; i < looktemp[0]->size(); i++) {
238 bool updatePlace = false;
240 for (int j = 0; j < looktemp.size(); j++) {
241 if (looktemp[j]->getAbundance(i) != 0) { count++; }
245 for (int j = 0; j < looktemp.size(); j++) {
246 //if they are not shared then push to back, if they are not insert in front
247 if (count < 2) { lookup[j]->push_back(looktemp[j]->getAbundance(i), i, looktemp[j]->getGroup()); }
248 //they are shared by some
249 else { lookup[j]->insert(looktemp[j]->getAbundance(i), place[count], looktemp[j]->getGroup()); updatePlace = true; }
252 if (updatePlace == true) {
253 //move place holders below where you entered up to "make space" for you entry
254 for (it = place.begin(); it!= place.end(); it++) {
255 if (it->first < count) { it->second++; }
261 for (int j = 0; j < looktemp.size(); j++) {
266 catch(exception& e) {
267 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
271 cout << "An unknown error has occurred in the HeatMap class function sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
277 //**********************************************************************************************************************
279 void HeatMap::printLegend(int y, float maxbin) {
282 //output legend and color labels
283 //go through map and give each score a color value
288 for (int i = 1; i < 255; i++) {
289 color = toHex(int((float)(i)));
290 outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"1\" height=\"10\"/>\n";
294 //prints legend labels
296 for (int i = 1; i<=5; i++) {
298 if(scaler== "log10") { label = maxbin * log10(51*i) / log10(255); }
299 else if(scaler== "log2") { label = maxbin * log2(51*i) / log2(255); }
300 else if(scaler== "linear") { label = maxbin * 51 * i / 255; }
301 else { label = maxbin * log10(51*i) / log10(255); }
302 file://localhost/Users/westcott/Desktop/c.amazon.fn.0.19.rep.fasta
303 label = int(label * 1000 + 0.5);
305 string text = toString(label, 3);
307 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(x) + "\" y=\"" + toString(y-3) + "\">" + text + "</text>\n";
312 catch(exception& e) {
313 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function printLegend. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
317 cout << "An unknown error has occurred in the HeatMap class function printLegend. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";