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