]> git.donarmstrong.com Git - mothur.git/blob - heatmapsim.cpp
added heatmap.sim command and changed heatmap to heatmap.bin
[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         try {
25                 globaldata = GlobalData::getInstance();
26         }
27         catch(exception& e) {
28                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function HeatMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
29                 exit(1);
30         }
31         catch(...) {
32                 cout << "An unknown error has occurred in the HeatMap class function HeatMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
33                 exit(1);
34         }
35 }
36 //**********************************************************************************************************************
37
38 void HeatMapSim::getPic(vector<SharedRAbundVector*> lookup, vector<Calculator*> calcs) {
39         try {
40                 EstOutput data;
41                 vector<double> sims;
42                                 
43                 //make file for each calculator selected
44                 for (int m = 0; m < calcs.size(); m++) {
45                         
46                         string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + calcs[m]->getName() + ".heatmap.sim.svg";
47                         openOutputFile(filenamesvg, outsvg);
48                         
49                         //svg image
50                         outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString((lookup.size() * 150) + 160) + " " + toString((lookup.size() * 150) + 160)  + "\">\n";
51                         outsvg << "<g>\n";
52                 
53                         //white backround
54                         outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString((lookup.size() * 150) + 160) + "\" height=\"" + toString((lookup.size() * 150) + 160)  + "\"/>"; 
55                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((lookup.size() * 75) - 40) + "\" y=\"25\">Heatmap at distance " + lookup[0]->getLabel() + "</text>\n";
56                 
57                         //column labels
58                         for (int h = 0; h < lookup.size(); h++) {
59                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(((150 * (h+1)) ) - ((int)lookup[h]->getGroup().length() / 2)) + "\" y=\"50\">" + lookup[h]->getGroup() + "</text>\n"; 
60                                 outsvg << "<text fill=\"black\" class=\"seri\" y=\"" + toString(((150 * (h+1)) ) - ((int)lookup[h]->getGroup().length() / 2)) + "\" x=\"50\">" + lookup[h]->getGroup() + "</text>\n";
61                         }
62                         
63                         sims.clear();
64                         double biggest = -1;
65                         float scaler;
66
67                         //get sim for each comparison and save them so you can find the relative similairity
68                         for(int i = 0; i < (lookup.size()-1); i++){
69                                 for(int j = (i+1); j < lookup.size(); j++){
70                                         
71                                                 vector<SharedRAbundVector*> subset;
72                                                 subset.push_back(lookup[i]);  subset.push_back(lookup[j]); 
73                                         
74                                                 //get similairity between groups
75                                                 data = calcs[m]->getValues(subset);
76                                                 sims.push_back(data[0]);
77                                         
78                                                 //save biggest similairity to set relative sim
79                                                 if (data[0] > biggest) { biggest = data[0]; }
80                                 }
81                         }
82                         
83                         //map biggest similairity found to red
84                         scaler = 255.0 / biggest;
85                         
86                         int count = 0;
87                         //output similairites to file
88                         for(int i = 0; i < (lookup.size()-1); i++){
89                                 for(int j = (i+1); j < lookup.size(); j++){
90                                 
91                                                 //find relative color
92                                                 int color = scaler * sims[count];                                       
93                                                 //draw box
94                                                 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";
95                                                 count++;
96                                 }
97                         }
98                         
99                         int y = ((lookup.size() * 150) + 120);
100                         printLegend(y, biggest);
101                 
102                         outsvg << "</g>\n</svg>\n";
103                         outsvg.close();
104
105                 }
106                 
107                 
108         }
109         catch(exception& e) {
110                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapSim class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
111                 exit(1);
112         }
113         catch(...) {
114                 cout << "An unknown error has occurred in the HeatMapSim class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
115                 exit(1);
116         }
117 }
118 //**********************************************************************************************************************
119
120 void HeatMapSim::printLegend(int y, float maxSim) {
121         try {
122                 
123                 //output legend and color labels
124                 //go through map and give each score a color value
125                 string color;
126                 int x = 10;
127                 
128                 //prints legend
129                 for (int i = 1; i < 255; i++) {
130                         color = toHex(int((float)(i)));
131                         outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"1\" height=\"10\"/>\n";
132                         x += 1;
133                 }
134                 
135                 float scaler = maxSim / 5.0;
136                 
137                 //prints legend labels
138                 x = 10;
139                 for (int i = 1; i<=5; i++) {
140                         float label = scaler*i;
141                         label = int(label * 1000 + 0.5);
142                         label /= 1000.0;
143                         string text = toString(label, 3);
144                         
145                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(x) + "\" y=\"" + toString(y-3) + "\">" + text + "</text>\n";
146                         x += 60;
147                 }
148         }
149         
150         catch(exception& e) {
151                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMapSim class Function printLegend. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
152                 exit(1);
153         }
154         catch(...) {
155                 cout << "An unknown error has occurred in the HeatMapSim class function printLegend. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
156                 exit(1);
157         }
158         
159 }
160
161 //**********************************************************************************************************************
162
163
164
165