]> git.donarmstrong.com Git - mothur.git/blob - heatmap.cpp
made sorting optional but sort by default, in case the user wants to see the heatmap...
[mothur.git] / heatmap.cpp
1 /*
2  *  heatmap.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 3/25/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "heatmap.h"
11
12 //**********************************************************************************************************************
13 HeatMap::HeatMap(){
14         try {
15                 globaldata = GlobalData::getInstance();
16                 format = globaldata->getFormat();
17                 sorted = globaldata->getSorted();
18                 
19         }
20         catch(exception& e) {
21                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function HeatMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
22                 exit(1);
23         }
24         catch(...) {
25                 cout << "An unknown error has occurred in the HeatMap class function HeatMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
26                 exit(1);
27         }
28 }
29 //**********************************************************************************************************************
30 void HeatMap::getPic(OrderVector* order) {
31         try {
32                 colorScale.clear();
33                 
34                 rabund = order->getRAbundVector();
35                 
36                 for (int i = 0; i < rabund.size(); i++) {
37                         colorScale[rabund.get(i)] = "";
38                 }
39                 
40                 float scaler = 255 / (float) colorScale.size();
41                 
42                 //go through map and give each score a color value
43                 for (it = colorScale.begin(); it != colorScale.end(); it++) {
44                         it->second = toHex(int(float(it->first) * scaler));
45                         if(it->second.length() == 1) {  it->second = "0" + it->second;  }
46                 }
47
48                 string filenamesvg = globaldata->inputFileName + ".heatmap." + order->getLabel() + ".svg";
49                 
50                 openOutputFile(filenamesvg, outsvg);
51                 
52                 //scale max rank so the maxrank = bright red
53                         
54                 //svg image
55                 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 300 " + toString((rabund.getNumBins()*5 + 15))  + "\">\n";
56                 outsvg << "<g>\n";
57                 
58                 int x = 15;
59                 int y = 15;
60                 string color;
61
62                 for (int i = 0; i <= rabund.getNumBins(); i++) {
63                 
64                         color = colorScale[rabund.get(i)];
65                         
66                         outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
67                         y += 5;
68                 }
69                 outsvg << "</g>\n</svg>\n";
70                 
71                 outsvg.close();
72                 
73         }
74         catch(exception& e) {
75                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
76                 exit(1);
77         }
78         catch(...) {
79                 cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
80                 exit(1);
81         }
82 }
83 //**********************************************************************************************************************
84 void HeatMap::getPic(SharedOrderVector* sharedorder) {
85         try {
86                 colorScale.clear();
87                 
88                 //fills vector of sharedsabunds - lookup
89                 getSharedVectors(sharedorder);
90                 
91                 //sort lookup so shared bins are on top
92                 if (sorted == "1") {  sortSharedVectors();  }
93                 
94                 //get maxBin
95                 for (int i = 0; i < lookup.size(); i++) {
96                         for (int j = 0; j < lookup[i]->size(); j++) {
97                                 colorScale[lookup[i]->getAbundance(j)] = "";
98                         }
99                 }
100                 
101                 //get scaler
102                 float scaler = 255 / (float) colorScale.size();
103                 
104                 //go through map and give each score a color value
105                 for (it = colorScale.begin(); it != colorScale.end(); it++) {
106                         it->second = toHex(int(float(it->first) * scaler));
107                         if(it->second.length() == 1) {  it->second = "0" + it->second;  }
108                 }
109                 
110                 string filenamesvg = globaldata->inputFileName + ".heatmap." + sharedorder->getLabel() + "." + groupComb + ".svg";
111                 openOutputFile(filenamesvg, outsvg);
112                 
113                 //svg image
114                 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(lookup.size() * 300) + " " + toString((lookup[0]->getNumBins()*5 + 15))  + "\">\n";
115                 outsvg << "<g>\n";
116                 
117                 int x = 15;
118                 int y = 15;
119                 string color;
120
121                 for (int i = 0; i <= lookup[0]->getNumBins(); i++) {
122                 
123                         for (int j = 0; j < lookup.size(); j++) {
124                         
125                                 color = colorScale[lookup[j]->getAbundance(i)]; 
126                                                 
127                                 outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
128                                 x += 300;
129                         }
130                         x = 15;
131                         y += 5;
132                 }
133                 outsvg << "</g>\n</svg>\n";
134                 
135                 outsvg.close();
136
137                 
138                 
139         }
140         catch(exception& e) {
141                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
142                 exit(1);
143         }
144         catch(...) {
145                 cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
146                 exit(1);
147         }
148 }
149 //**********************************************************************************************************************
150 void HeatMap::getSharedVectors(SharedOrderVector* order){
151         try {
152         
153                 //delete lookup
154                 for (int j = 0; j < lookup.size(); j++) {
155                         delete lookup[j];
156                 }
157
158                 lookup.clear();
159                 
160                 groupComb = "";
161                 
162                 //create and initialize vector of sharedvectors, one for each group
163                 for (int i = 0; i < globaldata->Groups.size(); i++) { 
164                         SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
165                         temp->setLabel(order->getLabel());
166                         temp->setGroup(globaldata->Groups[i]);
167                         groupComb += globaldata->Groups[i];
168                         lookup.push_back(temp);
169                 }
170                 
171                 int numSeqs = order->size();
172                 //sample all the members
173                 for(int i=0;i<numSeqs;i++){
174                         //get first sample
175                         individual chosen = order->get(i);
176                         int abundance; 
177                                         
178                         //set info for sharedvector in chosens group
179                         for (int j = 0; j < lookup.size(); j++) { 
180                                 if (chosen.group == lookup[j]->getGroup()) {
181                                          abundance = lookup[j]->getAbundance(chosen.bin);
182                                          lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
183                                          break;
184                                 }
185                         }
186                 }
187                 
188         }
189         catch(exception& e) {
190                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
191                 exit(1);
192         }
193         catch(...) {
194                 cout << "An unknown error has occurred in the HeatMap class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
195                 exit(1);
196         }
197
198 }
199
200
201 //**********************************************************************************************************************
202 void HeatMap::sortSharedVectors(){
203         try {
204                 //copy lookup and then clear it to refill with sorted.
205                 //loop though lookup and determine if they are shared
206                 //if they are then insert in the front
207                 //if not push to back
208                 
209                 bool shared;
210                 vector<SharedRAbundVector*> looktemp;
211                 
212                 //create and initialize looktemp as a copy of lookup
213                 for (int i = 0; i < lookup.size(); i++) { 
214                         SharedRAbundVector* temp = new SharedRAbundVector(lookup[i]->getNumBins());
215                         temp->setLabel(lookup[i]->getLabel());
216                         temp->setGroup(lookup[i]->getGroup());
217                         //copy lookup i's info
218                         for (int j = 0; j < lookup[i]->size(); j++) {
219                                 temp->set(j, lookup[i]->getAbundance(j), lookup[i]->getGroup());
220                         }
221                         looktemp.push_back(temp);
222                 }
223                 
224                 //clear out lookup to create sorted lookup
225                 lookup.clear();
226                 
227                 //create and initialize lookup to empty vectors
228                 for (int i = 0; i < looktemp.size(); i++) { 
229                         SharedRAbundVector* temp = new SharedRAbundVector();
230                         lookup.push_back(temp);
231                 }
232                 
233                 //for each bin
234                 for (int i = 0; i < looktemp[0]->size(); i++) {
235                         shared = true;
236                         //for each group
237                         for (int j = 0; j < looktemp.size(); j++) {
238                                 if (looktemp[j]->getAbundance(i) == 0) { shared = false; }
239                         }
240                         
241                         //fill lookup
242                         for (int j = 0; j < looktemp.size(); j++) {
243                                 //if they are not shared then push to back, if they are not insert in front
244                                 if (shared == false)  { lookup[j]->push_back(looktemp[j]->getAbundance(i), i, looktemp[j]->getGroup()); }
245                                 else { lookup[j]->push_front(looktemp[j]->getAbundance(i), i, looktemp[j]->getGroup()); }
246                         }
247                 }
248                 
249                 //delete looktemp
250                 for (int j = 0; j < looktemp.size(); j++) {
251                         delete looktemp[j];
252                 }
253                 
254         }
255         catch(exception& e) {
256                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
257                 exit(1);
258         }
259         catch(...) {
260                 cout << "An unknown error has occurred in the HeatMap class function sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
261                 exit(1);
262         }
263
264 }
265
266 //**********************************************************************************************************************
267
268
269
270
271