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 for (int i = 0; i < rabund.size(); i++) {
38 colorScale[rabund.get(i)] = "";
41 float scaler = 255 / (float) colorScale.size();
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; }
49 string filenamesvg = getRootName(globaldata->inputFileName) + order->getLabel() + ".heatmap.svg";
51 openOutputFile(filenamesvg, outsvg);
53 //scale max rank so the maxrank = bright red
56 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 300 " + toString((rabund.getNumBins()*5 + 15)) + "\">\n";
63 for (int i = 0; i <= rabund.getNumBins(); i++) {
65 color = colorScale[rabund.get(i)];
67 outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
70 outsvg << "</g>\n</svg>\n";
76 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
80 cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
84 //**********************************************************************************************************************
85 void HeatMap::getPic(SharedOrderVector* sharedorder) {
89 //fills vector of sharedsabunds - lookup
90 util->getSharedVectors(globaldata->Groups, lookup, sharedorder); //fills group vectors from order vector.
92 //sort lookup so shared bins are on top
93 if (sorted == "1") { sortSharedVectors(); }
95 //get users scaling method
96 scaler = globaldata->getScaler();
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[-log((log10(lookup[i]->getAbundance(j)) / (float)lookup[i]->getNumSeqs()))] = "";
104 //cout << "abundance = " << lookup[i]->getAbundance(j) << '\t' << " relative adundance = " << (lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) << '\t';
105 //cout << -log((log10(lookup[i]->getAbundance(j)) / lookup[i]->getNumSeqs())) << endl;
106 //}else if (scaler == "log2") {
107 //colorScale[-log((log2(lookup[i]->getAbundance(j)) / (float)lookup[i]->getNumSeqs()))] = ""; //cout << (int)log2(lookup[i]->getAbundance(j)) << endl;
108 //cout << "abundance = " << lookup[i]->getAbundance(j) << '\t' << " relative adundance = " << lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs() << '\t';
109 //cout << -log((log2(lookup[i]->getAbundance(j)) / lookup[i]->getNumSeqs())) << endl;
110 // }else if (scaler == "linear") {
111 colorScale[lookup[i]->getAbundance(j)] = "";
112 if (maxbin < lookup[i]->getAbundance(j)) { maxbin = lookup[i]->getAbundance(j); }
113 //cout << "abundance = " << lookup[i]->getAbundance(j) << '\t' << " relative adundance = " << lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs() << '\t';
114 //cout << lookup[i]->getAbundance(j) /(float) lookup[i]->getNumSeqs() << endl;
115 //}else { //if user enters invalid scaler option.
116 // cout << scaler << " is not a valid scaler option. I will use log10." << endl;
117 // colorScale[-log(log10(lookup[i]->getAbundance(j) / lookup[i]->getNumSeqs()))] = "";
119 //}else { colorScale[0] = "00"; }
125 float scalers = 255 / (float) maxbin;
127 //go through map and give each score a color value
128 for (it = colorScale.begin(); it != colorScale.end(); it++) {
129 it->second = toHex(int(float(it->first) * scalers));
130 if(it->second.length() == 1) { it->second = "0" + it->second; }
131 //cout << it->first << " " << it->second << endl;
134 string filenamesvg = getRootName(globaldata->inputFileName) + sharedorder->getLabel() + ".heatmap.svg";
135 openOutputFile(filenamesvg, outsvg);
138 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(lookup.size() * 300) + " " + toString((lookup[0]->getNumBins()*5 + 120)) + "\">\n";
142 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString(lookup.size() * 300) + "\" height=\"" + toString((lookup[0]->getNumBins()*5 + 120)) + "\"/>";
143 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((lookup.size() * 150) - 40) + "\" y=\"25\">Heatmap at distance " + sharedorder->getLabel() + "</text>\n";
146 for (int h = 0; h < lookup.size(); h++) {
147 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";
151 //output legend and color labels
152 //go through map and give each score a color value
155 int y = 90 + (lookup[0]->getNumBins()*5);
156 for (it = colorScale.begin(); it != colorScale.end(); it++) {
158 outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"25\" height=\"10\"/>\n";
159 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\">" + toString(it->first) + "</text>\n";
166 //start at 1 since bin 0 is nothing
167 for (int i = 1; i <= lookup[0]->getNumBins(); i++) {
168 for (int j = 0; j < lookup.size(); j++) {
170 //if (lookup[j]->getAbundance(i) != 0) { //don't want log value of 0.
171 //if (scaler == "log10") {
172 // color = colorScale[(int)log10(lookup[j]->getAbundance(i))];
173 //}else if (scaler == "log2") {
174 // color = colorScale[(int)log2(lookup[j]->getAbundance(i))];
175 // }else if (scaler == "linear") {
176 color = colorScale[lookup[j]->getAbundance(i)];
177 //}else { color = colorScale[(int)log10(lookup[j]->getAbundance(i))]; }
178 //}else { color = "OO"; }
181 outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
188 outsvg << "</g>\n</svg>\n";
195 catch(exception& e) {
196 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
200 cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
205 //**********************************************************************************************************************
206 void HeatMap::sortSharedVectors(){
208 //copy lookup and then clear it to refill with sorted.
209 //loop though lookup and determine if they are shared
210 //if they are then insert in the front
211 //if not push to back
213 vector<SharedRAbundVector*> looktemp;
214 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.
215 map<int, int>::iterator it;
218 //create and initialize looktemp as a copy of lookup
219 for (int i = 0; i < lookup.size(); i++) {
220 SharedRAbundVector* temp = new SharedRAbundVector(lookup[i]->getNumBins());
221 temp->setLabel(lookup[i]->getLabel());
222 temp->setGroup(lookup[i]->getGroup());
223 //copy lookup i's info
224 for (int j = 0; j < lookup[i]->size(); j++) {
225 temp->set(j, lookup[i]->getAbundance(j), lookup[i]->getGroup());
227 looktemp.push_back(temp);
230 //clear out lookup to create sorted lookup
233 //create and initialize lookup to empty vectors
234 for (int i = 0; i < looktemp.size(); i++) {
235 SharedRAbundVector* temp = new SharedRAbundVector();
236 temp->setLabel(looktemp[i]->getLabel());
237 temp->setGroup(looktemp[i]->getGroup());
238 lookup.push_back(temp);
240 //initialize place map
246 for (int i = 0; i < looktemp[0]->size(); i++) {
248 bool updatePlace = false;
250 for (int j = 0; j < looktemp.size(); j++) {
251 if (looktemp[j]->getAbundance(i) != 0) { count++; }
255 for (int j = 0; j < looktemp.size(); j++) {
256 //if they are not shared then push to back, if they are not insert in front
257 if (count < 2) { lookup[j]->push_back(looktemp[j]->getAbundance(i), i, looktemp[j]->getGroup()); }
258 //they are shared by some
259 else { lookup[j]->insert(looktemp[j]->getAbundance(i), place[count], looktemp[j]->getGroup()); updatePlace = true; }
262 if (updatePlace == true) {
263 //move place holders below where you entered up to "make space" for you entry
264 for (it = place.begin(); it!= place.end(); it++) {
265 if (it->first < count) { it->second++; }
271 for (int j = 0; j < looktemp.size(); j++) {
276 catch(exception& e) {
277 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
281 cout << "An unknown error has occurred in the HeatMap class function sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287 //**********************************************************************************************************************