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