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(string dir) : outputDir(dir) {
24 globaldata = GlobalData::getInstance();
25 m = MothurOut::getInstance();
27 //**********************************************************************************************************************
28 vector<string> HeatMapSim::getPic(vector<SharedRAbundVector*> lookup, vector<Calculator*> calcs) {
32 vector<string> outputNames;
34 //make file for each calculator selected
35 for (int k = 0; k < calcs.size(); k++) {
37 if (m->control_pressed) { return outputNames; }
39 string filenamesvg = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + lookup[0]->getLabel() + calcs[k]->getName() + ".heatmap.sim.svg";
40 m->openOutputFile(filenamesvg, outsvg);
41 outputNames.push_back(filenamesvg);
44 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";
48 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString((lookup.size() * 150) + 160) + "\" height=\"" + toString((lookup.size() * 150) + 160) + "\"/>";
49 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((lookup.size() * 75) - 40) + "\" y=\"25\">Heatmap at distance " + lookup[0]->getLabel() + "</text>\n";
52 for (int h = 0; h < lookup.size(); h++) {
53 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(((150 * (h+1)) ) - ((int)lookup[h]->getGroup().length() / 2)) + "\" y=\"50\">" + lookup[h]->getGroup() + "</text>\n";
54 outsvg << "<text fill=\"black\" class=\"seri\" y=\"" + toString(((150 * (h+1)) ) - ((int)lookup[h]->getGroup().length() / 2)) + "\" x=\"50\">" + lookup[h]->getGroup() + "</text>\n";
58 // double biggest = -1;
62 //get sim for each comparison and save them so you can find the relative similairity
63 for(int i = 0; i < (lookup.size()-1); i++){
64 for(int j = (i+1); j < lookup.size(); j++){
66 if (m->control_pressed) { outsvg.close(); return outputNames; }
68 vector<SharedRAbundVector*> subset;
69 subset.push_back(lookup[i]); subset.push_back(lookup[j]);
71 //get similairity between groups
72 data = calcs[k]->getValues(subset);
73 sims.push_back(data[0]);
75 //save biggest similairity to set relative sim
76 // if (data[0] > biggest) { biggest = data[0]; }
80 //map biggest similairity found to red
81 scaler = 255.0 / biggest;
84 //output similairites to file
85 for(int i = 0; i < (lookup.size()-1); i++){
86 for(int j = (i+1); j < lookup.size(); j++){
89 int color = scaler * sims[count];
91 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";
96 int y = ((lookup.size() * 150) + 120);
97 printLegend(y, biggest);
99 outsvg << "</g>\n</svg>\n";
106 catch(exception& e) {
107 m->errorOut(e, "HeatMapSim", "getPic");
111 //**********************************************************************************************************************
112 string HeatMapSim::getPic(vector< vector<double> > dists, vector<string> groups) {
117 string filenamesvg = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + "heatmap.sim.svg";
118 m->openOutputFile(filenamesvg, outsvg);
121 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";
125 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString((dists.size() * 150) + 160) + "\" height=\"" + toString((dists.size() * 150) + 160) + "\"/>";
126 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((dists.size() * 75) - 40) + "\" y=\"25\">Heatmap for " + globaldata->inputFileName + "</text>\n";
129 for (int h = 0; h < groups.size(); h++) {
130 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(((150 * (h+1)) ) - ((int)groups[h].length() / 2)) + "\" y=\"50\">" + groups[h] + "</text>\n";
131 outsvg << "<text fill=\"black\" class=\"seri\" y=\"" + toString(((150 * (h+1)) ) - ((int)groups[h].length() / 2)) + "\" x=\"50\">" + groups[h] + "</text>\n";
137 //get sim for each comparison and save them so you can find the relative similairity
138 for(int i = 0; i < (dists.size()-1); i++){
139 for(int j = (i+1); j < dists.size(); j++){
141 if (m->control_pressed) { outsvg.close(); return filenamesvg; }
143 float sim = 1.0 - dists[i][j];
146 //save biggest similairity to set relative sim
147 if (sim > biggest) { biggest = sim; }
151 //map biggest similairity found to red
152 scaler = 255.0 / biggest;
155 //output similairites to file
156 for(int i = 0; i < (dists.size()-1); i++){
157 for(int j = (i+1); j < dists.size(); j++){
159 //find relative color
160 int color = scaler * sims[count];
162 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";
167 int y = ((dists.size() * 150) + 120);
168 printLegend(y, biggest);
170 outsvg << "</g>\n</svg>\n";
176 catch(exception& e) {
177 m->errorOut(e, "HeatMapSim", "getPic");
182 //**********************************************************************************************************************
184 void HeatMapSim::printLegend(int y, float maxSim) {
188 //output legend and color labels
189 //go through map and give each score a color value
194 for (int i = 1; i < 255; i++) {
195 color = toHex(int((float)(i)));
196 outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"3\" height=\"30\"/>\n";
200 float scaler = maxSim / 5.0;
202 //prints legend labels
204 for (int i = 0; i<=5; i++) {
205 float label = scaler*i;
206 label = int(label * 1000 + 0.5);
208 string text = toString(label, 1);
210 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(x) + "\" y=\"" + toString(y-3) + "\">" + text + "</text>\n";
215 catch(exception& e) {
216 m->errorOut(e, "HeatMapSim", "printLegend");
221 //**********************************************************************************************************************