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[(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); }
122 }else { colorScale[0] = "00"; }
126 //cout << "maxbin = " << maxbin << endl;
128 float scalers = 255 / (float) maxbin;
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;
138 string filenamesvg = getRootName(globaldata->inputFileName) + sharedorder->getLabel() + ".heatmap.svg";
139 openOutputFile(filenamesvg, outsvg);
142 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(lookup.size() * 300) + " " + toString((lookup[0]->getNumBins()*5 + 120)) + "\">\n";
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";
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";
155 //output legend and color labels
156 //go through map and give each score a color value
159 int y = 90 + (lookup[0]->getNumBins()*5);
160 for (it = colorScale.begin(); it != colorScale.end(); it++) {
162 float value = it->first;
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; }
171 string itprec = toString(value);
173 //set precision of relative abundance to 2
174 int pos = itprec.find_first_of('.');
175 itprec = itprec.substr(0,pos+3);
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";
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++) {
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"; }
200 outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
208 outsvg << "</g>\n</svg>\n";
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";
217 cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
222 //**********************************************************************************************************************
223 void HeatMap::sortSharedVectors(){
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
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;
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());
244 looktemp.push_back(temp);
247 //clear out lookup to create sorted lookup
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);
257 //initialize place map
263 for (int i = 0; i < looktemp[0]->size(); i++) {
265 bool updatePlace = false;
267 for (int j = 0; j < looktemp.size(); j++) {
268 if (looktemp[j]->getAbundance(i) != 0) { count++; }
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; }
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++; }
288 for (int j = 0; j < looktemp.size(); j++) {
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";
298 cout << "An unknown error has occurred in the HeatMap class function sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
304 //**********************************************************************************************************************