]> git.donarmstrong.com Git - mothur.git/blob - heatmapsim.cpp
you can now use a distance matrix as input for the heatmap.sim command.
[mothur.git] / heatmapsim.cpp
1 /*
2  *  heatmapsim.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 6/8/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
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"
21
22 //**********************************************************************************************************************
23 HeatMapSim::HeatMapSim(){
24                 globaldata = GlobalData::getInstance();
25 }
26 //**********************************************************************************************************************
27 void HeatMapSim::getPic(vector<SharedRAbundVector*> lookup, vector<Calculator*> calcs) {
28         try {
29                 EstOutput data;
30                 vector<double> sims;
31                                 
32                 //make file for each calculator selected
33                 for (int m = 0; m < calcs.size(); m++) {
34                         
35                         string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + calcs[m]->getName() + ".heatmap.sim.svg";
36                         openOutputFile(filenamesvg, outsvg);
37                         
38                         //svg image
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";
40                         outsvg << "<g>\n";
41                 
42                         //white backround
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";
45                 
46                         //column labels
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";
50                         }
51                         
52                         sims.clear();
53                         double biggest = -1;
54                         float scaler;
55
56                         //get sim for each comparison and save them so you can find the relative similairity
57                         for(int i = 0; i < (lookup.size()-1); i++){
58                                 for(int j = (i+1); j < lookup.size(); j++){
59                                         
60                                                 vector<SharedRAbundVector*> subset;
61                                                 subset.push_back(lookup[i]);  subset.push_back(lookup[j]); 
62                                         
63                                                 //get similairity between groups
64                                                 data = calcs[m]->getValues(subset);
65                                                 sims.push_back(data[0]);
66                                         
67                                                 //save biggest similairity to set relative sim
68                                                 if (data[0] > biggest) { biggest = data[0]; }
69                                 }
70                         }
71                         
72                         //map biggest similairity found to red
73                         scaler = 255.0 / biggest;
74                         
75                         int count = 0;
76                         //output similairites to file
77                         for(int i = 0; i < (lookup.size()-1); i++){
78                                 for(int j = (i+1); j < lookup.size(); j++){
79                                 
80                                                 //find relative color
81                                                 int color = scaler * sims[count];                                       
82                                                 //draw box
83                                                 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";
84                                                 count++;
85                                 }
86                         }
87                         
88                         int y = ((lookup.size() * 150) + 120);
89                         printLegend(y, biggest);
90                 
91                         outsvg << "</g>\n</svg>\n";
92                         outsvg.close();
93
94                 }
95                 
96                 
97         }
98         catch(exception& e) {
99                 errorOut(e, "HeatMapSim", "getPic");
100                 exit(1);
101         }
102 }
103 //**********************************************************************************************************************
104 void HeatMapSim::getPic(vector< vector<double> > dists, vector<string> groups) {
105         try {
106                 
107                 vector<double> sims;
108                 
109                 string filenamesvg = getRootName(globaldata->inputFileName) + "heatmap.sim.svg";
110                 openOutputFile(filenamesvg, outsvg);
111                         
112                 //svg image
113                 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";
114                 outsvg << "<g>\n";
115                 
116                 //white backround
117                 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString((dists.size() * 150) + 160) + "\" height=\"" + toString((dists.size() * 150) + 160)  + "\"/>"; 
118                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((dists.size() * 75) - 40) + "\" y=\"25\">Heatmap for " + globaldata->inputFileName + "</text>\n";
119                 
120                 //column labels
121                 for (int h = 0; h < groups.size(); h++) {
122                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(((150 * (h+1)) ) - ((int)groups[h].length() / 2)) + "\" y=\"50\">" + groups[h] + "</text>\n"; 
123                         outsvg << "<text fill=\"black\" class=\"seri\" y=\"" + toString(((150 * (h+1)) ) - ((int)groups[h].length() / 2)) + "\" x=\"50\">" + groups[h] + "</text>\n";
124                 }
125                         
126                 double biggest = -1;
127                 float scaler;
128
129                 //get sim for each comparison and save them so you can find the relative similairity
130                 for(int i = 0; i < (dists.size()-1); i++){
131                         for(int j = (i+1); j < dists.size(); j++){
132                                 
133                                 float sim = 1.0 - dists[i][j];
134                                 sims.push_back(sim);
135                                         
136                                 //save biggest similairity to set relative sim
137                                 if (sim > biggest) { biggest = sim; }
138                         }
139                 }
140                         
141                 //map biggest similairity found to red
142                 scaler = 255.0 / biggest;
143                         
144                 int count = 0;
145                 //output similairites to file
146                 for(int i = 0; i < (dists.size()-1); i++){
147                         for(int j = (i+1); j < dists.size(); j++){
148                                 
149                                         //find relative color
150                                         int color = scaler * sims[count];                                       
151                                         //draw box
152                                         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";
153                                         count++;
154                         }
155                 }
156                         
157                 int y = ((dists.size() * 150) + 120);
158                 printLegend(y, biggest);
159                 
160                 outsvg << "</g>\n</svg>\n";
161                 outsvg.close();
162                 
163         }
164         catch(exception& e) {
165                 errorOut(e, "HeatMapSim", "getPic");
166                 exit(1);
167         }
168 }
169
170 //**********************************************************************************************************************
171
172 void HeatMapSim::printLegend(int y, float maxSim) {
173         try {
174                 
175                 //output legend and color labels
176                 //go through map and give each score a color value
177                 string color;
178                 int x = 10;
179                 
180                 //prints legend
181                 for (int i = 1; i < 255; i++) {
182                         color = toHex(int((float)(i)));
183                         outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"1\" height=\"10\"/>\n";
184                         x += 1;
185                 }
186                 
187                 float scaler = maxSim / 5.0;
188                 
189                 //prints legend labels
190                 x = 10;
191                 for (int i = 1; i<=5; i++) {
192                         float label = scaler*i;
193                         label = int(label * 1000 + 0.5);
194                         label /= 1000.0;
195                         string text = toString(label, 3);
196                         
197                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(x) + "\" y=\"" + toString(y-3) + "\">" + text + "</text>\n";
198                         x += 60;
199                 }
200         }
201         
202         catch(exception& e) {
203                 errorOut(e, "HeatMapSim", "printLegend");
204                 exit(1);
205         }
206 }
207
208 //**********************************************************************************************************************
209
210
211
212