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