]> git.donarmstrong.com Git - mothur.git/blob - heatmap.cpp
started shared utilities, updates to venn and heatmap added tree.groups command
[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                 util = new SharedUtil();
19                 
20         }
21         catch(exception& e) {
22                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function HeatMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
23                 exit(1);
24         }
25         catch(...) {
26                 cout << "An unknown error has occurred in the HeatMap class function HeatMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
27                 exit(1);
28         }
29 }
30 //**********************************************************************************************************************
31 void HeatMap::getPic(OrderVector* order) {
32         try {
33                 colorScale.clear();
34                 
35                 rabund = order->getRAbundVector();
36                 
37                 for (int i = 0; i < rabund.size(); i++) {
38                         colorScale[rabund.get(i)] = "";
39                 }
40                 
41                 float scaler = 255 / (float) colorScale.size();
42                 
43                 //go through map and give each score a color value
44                 for (it = colorScale.begin(); it != colorScale.end(); it++) {
45                         it->second = toHex(int(float(it->first) * scaler));
46                         if(it->second.length() == 1) {  it->second = "0" + it->second;  }
47                 }
48
49                 string filenamesvg = getRootName(globaldata->inputFileName) + order->getLabel() + ".heatmap.svg";
50                 
51                 openOutputFile(filenamesvg, outsvg);
52                 
53                 //scale max rank so the maxrank = bright red
54                         
55                 //svg image
56                 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 300 " + toString((rabund.getNumBins()*5 + 15))  + "\">\n";
57                 outsvg << "<g>\n";
58                 
59                 int x = 15;
60                 int y = 15;
61                 string color;
62
63                 for (int i = 0; i <= rabund.getNumBins(); i++) {
64                 
65                         color = colorScale[rabund.get(i)];
66                         
67                         outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
68                         y += 5;
69                 }
70                 outsvg << "</g>\n</svg>\n";
71                 
72                 outsvg.close();
73                 
74         }
75         catch(exception& e) {
76                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
77                 exit(1);
78         }
79         catch(...) {
80                 cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
81                 exit(1);
82         }
83 }
84 //**********************************************************************************************************************
85 void HeatMap::getPic(SharedOrderVector* sharedorder) {
86         try {
87                 colorScale.clear();
88                 
89                 //fills vector of sharedsabunds - lookup
90                 util->getSharedVectors(globaldata->Groups, lookup, sharedorder);  //fills group vectors from order vector.
91                 
92                 //sort lookup so shared bins are on top
93                 if (sorted == "1") {  sortSharedVectors();  }
94                 
95                 //get users scaling method
96                 scaler = globaldata->getScaler();
97                 
98                 int maxbin = 0;
99                 for (int i = 0; i < lookup.size(); i++) {
100                         for (int j = 0; j < lookup[i]->size(); j++) {
101                                 //if (lookup[i]->getAbundance(j) != 0) { //don't want log value of 0.
102                                         //if (scaler == "log10") {
103                                         //      colorScale[-log((log10(lookup[i]->getAbundance(j)) / (float)lookup[i]->getNumSeqs()))] = "";  
104         //cout << "abundance  = " << lookup[i]->getAbundance(j) << '\t' << " relative adundance = " << (lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) << '\t';
105         //cout << -log((log10(lookup[i]->getAbundance(j)) / lookup[i]->getNumSeqs())) << endl;
106                                         //}else if (scaler == "log2") {
107                                                 //colorScale[-log((log2(lookup[i]->getAbundance(j)) / (float)lookup[i]->getNumSeqs()))] = "";  //cout << (int)log2(lookup[i]->getAbundance(j)) << endl;
108         //cout << "abundance  = " << lookup[i]->getAbundance(j) << '\t' << " relative adundance = " << lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs() << '\t';
109         //cout << -log((log2(lookup[i]->getAbundance(j)) / lookup[i]->getNumSeqs())) << endl;
110                                 //      }else if (scaler == "linear") {
111                                                 colorScale[lookup[i]->getAbundance(j)] = "";
112                                                 if (maxbin < lookup[i]->getAbundance(j)) { maxbin = lookup[i]->getAbundance(j); }
113         //cout << "abundance  = " << lookup[i]->getAbundance(j) << '\t' << " relative adundance = " << lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs() << '\t';
114         //cout << lookup[i]->getAbundance(j) /(float) lookup[i]->getNumSeqs() << endl;
115                                         //}else {  //if user enters invalid scaler option.
116                                         //      cout << scaler << " is not a valid scaler option. I will use log10." << endl;
117                                         //      colorScale[-log(log10(lookup[i]->getAbundance(j) / lookup[i]->getNumSeqs()))] = "";   
118                                         //} 
119                                 //}else { colorScale[0] = "00";  }
120                                 
121                         }
122                 }
123                 
124                 //get scaler
125                 float scalers = 255 / (float) maxbin;
126                 
127                 //go through map and give each score a color value
128                 for (it = colorScale.begin(); it != colorScale.end(); it++) {
129                         it->second = toHex(int(float(it->first) * scalers));
130                         if(it->second.length() == 1) {  it->second = "0" + it->second;  }
131 //cout << it->first << " " << it->second << endl;
132                 }
133                 
134                 string filenamesvg = getRootName(globaldata->inputFileName) + sharedorder->getLabel() + ".heatmap.svg";
135                 openOutputFile(filenamesvg, outsvg);
136                 
137                 //svg image
138                 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(lookup.size() * 300) + " " + toString((lookup[0]->getNumBins()*5 + 120))  + "\">\n";
139                 outsvg << "<g>\n";
140                 
141                 //white backround
142                 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString(lookup.size() * 300) + "\" height=\"" + toString((lookup[0]->getNumBins()*5 + 120))  + "\"/>"; 
143                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((lookup.size() * 150) - 40) + "\" y=\"25\">Heatmap at distance " + sharedorder->getLabel() + "</text>\n";
144                 
145                 //column labels
146                 for (int h = 0; h < lookup.size(); h++) {
147                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(((300 * (h+1)) - 150) - ((int)lookup[h]->getGroup().length() / 2)) + "\" y=\"50\">" + lookup[h]->getGroup() + "</text>\n"; 
148                 }
149                 
150                 
151                 //output legend and color labels
152                 //go through map and give each score a color value
153                 string color;
154                 int x = 0;
155                 int y = 90 + (lookup[0]->getNumBins()*5);
156                 for (it = colorScale.begin(); it != colorScale.end(); it++) {
157                         color = it->second;     
158                         outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"25\" height=\"10\"/>\n";
159                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\">" + toString(it->first) + "</text>\n";
160                         x += 25;
161                 }
162                 
163                 x = 0;
164                 y = 70;
165                 
166                 //start at 1 since bin 0 is nothing
167                 for (int i = 1; i <= lookup[0]->getNumBins(); i++) {
168                         for (int j = 0; j < lookup.size(); j++) {
169                                 
170                                 //if (lookup[j]->getAbundance(i) != 0) { //don't want log value of 0.
171                                         //if (scaler == "log10") {
172                                         //      color = colorScale[(int)log10(lookup[j]->getAbundance(i))];  
173                                         //}else if (scaler == "log2") {
174                                         //      color = colorScale[(int)log2(lookup[j]->getAbundance(i))];  
175                                 //      }else if (scaler == "linear") {
176                                                 color = colorScale[lookup[j]->getAbundance(i)]; 
177                                         //}else {  color = colorScale[(int)log10(lookup[j]->getAbundance(i))];  } 
178                                 //}else { color = "OO";  }
179
180                                 
181                                 outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
182                                 x += 300;
183                         }
184                         x = 0;
185                         y += 5;
186                 }
187                 
188                 outsvg << "</g>\n</svg>\n";
189                 
190                 outsvg.close();
191
192                 
193                 
194         }
195         catch(exception& e) {
196                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
197                 exit(1);
198         }
199         catch(...) {
200                 cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
201                 exit(1);
202         }
203 }
204
205 //**********************************************************************************************************************
206 void HeatMap::sortSharedVectors(){
207         try {
208                 //copy lookup and then clear it to refill with sorted.
209                 //loop though lookup and determine if they are shared
210                 //if they are then insert in the front
211                 //if not push to back
212                 
213                 vector<SharedRAbundVector*> looktemp;
214                 map<int, int> place; //spot in lookup where you insert shared by, ie, 3 -> 2 if they are shared by 3 inset into location 2.
215                 map<int, int>::iterator it;
216                 int count;
217                 
218                 //create and initialize looktemp as a copy of lookup
219                 for (int i = 0; i < lookup.size(); i++) { 
220                         SharedRAbundVector* temp = new SharedRAbundVector(lookup[i]->getNumBins());
221                         temp->setLabel(lookup[i]->getLabel());
222                         temp->setGroup(lookup[i]->getGroup());
223                         //copy lookup i's info
224                         for (int j = 0; j < lookup[i]->size(); j++) {
225                                 temp->set(j, lookup[i]->getAbundance(j), lookup[i]->getGroup());
226                         }
227                         looktemp.push_back(temp);
228                 }
229                 
230                 //clear out lookup to create sorted lookup
231                 lookup.clear();
232                 
233                 //create and initialize lookup to empty vectors
234                 for (int i = 0; i < looktemp.size(); i++) { 
235                         SharedRAbundVector* temp = new SharedRAbundVector();
236                         temp->setLabel(looktemp[i]->getLabel());
237                         temp->setGroup(looktemp[i]->getGroup());
238                         lookup.push_back(temp); 
239                         
240                         //initialize place map
241                         place[i] = 0;
242                 }
243                 
244                 
245                 //for each bin
246                 for (int i = 0; i < looktemp[0]->size(); i++) {
247                         count = 0;
248                         bool updatePlace = false;
249                         //for each group
250                         for (int j = 0; j < looktemp.size(); j++) {
251                                 if (looktemp[j]->getAbundance(i) != 0) { count++; }
252                         }
253                         
254                         //fill lookup
255                         for (int j = 0; j < looktemp.size(); j++) {
256                                 //if they are not shared then push to back, if they are not insert in front
257                                 if (count < 2)  { lookup[j]->push_back(looktemp[j]->getAbundance(i), i, looktemp[j]->getGroup()); }
258                                 //they are shared by some
259                                 else {  lookup[j]->insert(looktemp[j]->getAbundance(i), place[count], looktemp[j]->getGroup());   updatePlace = true; }
260                         }
261                         
262                         if (updatePlace == true) {
263                                 //move place holders below where you entered up to "make space" for you entry
264                                 for (it = place.begin(); it!= place.end(); it++) {  
265                                         if (it->first < count) { it->second++; }
266                                 }
267                         }
268                 }
269                 
270                 //delete looktemp
271                 for (int j = 0; j < looktemp.size(); j++) {
272                         delete looktemp[j];
273                 }
274                 
275         }
276         catch(exception& e) {
277                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
278                 exit(1);
279         }
280         catch(...) {
281                 cout << "An unknown error has occurred in the HeatMap class function sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
282                 exit(1);
283         }
284
285 }
286
287 //**********************************************************************************************************************
288
289
290
291
292