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