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