5 * Created by Sarah Westcott on 6/8/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "heatmapsim.h"
11 #include "sharedjabund.h"
12 #include "sharedsorabund.h"
13 #include "sharedjclass.h"
14 #include "sharedsorclass.h"
15 #include "sharedjest.h"
16 #include "sharedsorest.h"
17 #include "sharedthetayc.h"
18 #include "sharedthetan.h"
19 #include "sharedmorisitahorn.h"
20 #include "sharedbraycurtis.h"
22 //**********************************************************************************************************************
23 HeatMapSim::HeatMapSim(){
24 globaldata = GlobalData::getInstance();
26 //**********************************************************************************************************************
27 void HeatMapSim::getPic(vector<SharedRAbundVector*> lookup, vector<Calculator*> calcs) {
32 //make file for each calculator selected
33 for (int m = 0; m < calcs.size(); m++) {
35 string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + calcs[m]->getName() + ".heatmap.sim.svg";
36 openOutputFile(filenamesvg, outsvg);
39 outsvg << "<svg xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns=\"http://www.w3.org/2000/svg\" width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString((lookup.size() * 150) + 160) + " " + toString((lookup.size() * 150) + 160) + "\">\n";
43 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString((lookup.size() * 150) + 160) + "\" height=\"" + toString((lookup.size() * 150) + 160) + "\"/>";
44 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((lookup.size() * 75) - 40) + "\" y=\"25\">Heatmap at distance " + lookup[0]->getLabel() + "</text>\n";
47 for (int h = 0; h < lookup.size(); h++) {
48 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(((150 * (h+1)) ) - ((int)lookup[h]->getGroup().length() / 2)) + "\" y=\"50\">" + lookup[h]->getGroup() + "</text>\n";
49 outsvg << "<text fill=\"black\" class=\"seri\" y=\"" + toString(((150 * (h+1)) ) - ((int)lookup[h]->getGroup().length() / 2)) + "\" x=\"50\">" + lookup[h]->getGroup() + "</text>\n";
53 // double biggest = -1;
57 //get sim for each comparison and save them so you can find the relative similairity
58 for(int i = 0; i < (lookup.size()-1); i++){
59 for(int j = (i+1); j < lookup.size(); j++){
61 vector<SharedRAbundVector*> subset;
62 subset.push_back(lookup[i]); subset.push_back(lookup[j]);
64 //get similairity between groups
65 data = calcs[m]->getValues(subset);
66 sims.push_back(data[0]);
68 //save biggest similairity to set relative sim
69 // if (data[0] > biggest) { biggest = data[0]; }
73 //map biggest similairity found to red
74 scaler = 255.0 / biggest;
77 //output similairites to file
78 for(int i = 0; i < (lookup.size()-1); i++){
79 for(int j = (i+1); j < lookup.size(); j++){
82 int color = scaler * sims[count];
84 outsvg << "<rect fill=\"rgb(" + toString(color) + ",0,0)\" stroke=\"rgb(" + toString(color) + ",0,0)\" x=\"" + toString((i*150)+80) + "\" y=\"" + toString((j*150)+75) + "\" width=\"150\" height=\"150\"/>\n";
89 int y = ((lookup.size() * 150) + 120);
90 printLegend(y, biggest);
92 outsvg << "</g>\n</svg>\n";
100 errorOut(e, "HeatMapSim", "getPic");
104 //**********************************************************************************************************************
105 void HeatMapSim::getPic(vector< vector<double> > dists, vector<string> groups) {
110 string filenamesvg = getRootName(globaldata->inputFileName) + "heatmap.sim.svg";
111 openOutputFile(filenamesvg, outsvg);
114 outsvg << "<svg xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns=\"http://www.w3.org/2000/svg\" width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString((dists.size() * 150) + 160) + " " + toString((dists.size() * 150) + 160) + "\">\n";
118 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString((dists.size() * 150) + 160) + "\" height=\"" + toString((dists.size() * 150) + 160) + "\"/>";
119 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((dists.size() * 75) - 40) + "\" y=\"25\">Heatmap for " + globaldata->inputFileName + "</text>\n";
122 for (int h = 0; h < groups.size(); h++) {
123 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(((150 * (h+1)) ) - ((int)groups[h].length() / 2)) + "\" y=\"50\">" + groups[h] + "</text>\n";
124 outsvg << "<text fill=\"black\" class=\"seri\" y=\"" + toString(((150 * (h+1)) ) - ((int)groups[h].length() / 2)) + "\" x=\"50\">" + groups[h] + "</text>\n";
130 //get sim for each comparison and save them so you can find the relative similairity
131 for(int i = 0; i < (dists.size()-1); i++){
132 for(int j = (i+1); j < dists.size(); j++){
134 float sim = 1.0 - dists[i][j];
137 //save biggest similairity to set relative sim
138 if (sim > biggest) { biggest = sim; }
142 //map biggest similairity found to red
143 scaler = 255.0 / biggest;
146 //output similairites to file
147 for(int i = 0; i < (dists.size()-1); i++){
148 for(int j = (i+1); j < dists.size(); j++){
150 //find relative color
151 int color = scaler * sims[count];
153 outsvg << "<rect fill=\"rgb(" + toString(color) + ",0,0)\" stroke=\"rgb(" + toString(color) + ",0,0)\" x=\"" + toString((i*150)+80) + "\" y=\"" + toString((j*150)+75) + "\" width=\"150\" height=\"150\"/>\n";
158 int y = ((dists.size() * 150) + 120);
159 printLegend(y, biggest);
161 outsvg << "</g>\n</svg>\n";
165 catch(exception& e) {
166 errorOut(e, "HeatMapSim", "getPic");
171 //**********************************************************************************************************************
173 void HeatMapSim::printLegend(int y, float maxSim) {
177 //output legend and color labels
178 //go through map and give each score a color value
183 for (int i = 1; i < 255; i++) {
184 color = toHex(int((float)(i)));
185 outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"3\" height=\"30\"/>\n";
189 float scaler = maxSim / 5.0;
191 //prints legend labels
193 for (int i = 0; i<=5; i++) {
194 float label = scaler*i;
195 label = int(label * 1000 + 0.5);
197 string text = toString(label, 1);
199 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(x) + "\" y=\"" + toString(y-3) + "\">" + text + "</text>\n";
204 catch(exception& e) {
205 errorOut(e, "HeatMapSim", "printLegend");
210 //**********************************************************************************************************************