]> git.donarmstrong.com Git - mothur.git/blob - heatmap.cpp
added heatmap 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                 
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                 //get maxBin
91                 for (int i = 0; i < lookup.size(); i++) {
92                         for (int j = 0; j < lookup[i]->size(); j++) {
93                                 colorScale[lookup[i]->getAbundance(j)] = "";
94                         }
95                 }
96                 
97                 //get scaler
98                 float scaler = 255 / (float) colorScale.size();
99                 
100                 //go through map and give each score a color value
101                 for (it = colorScale.begin(); it != colorScale.end(); it++) {
102                         it->second = toHex(int(float(it->first) * scaler));
103                         if(it->second.length() == 1) {  it->second = "0" + it->second;  }
104                 }
105                 
106                 string filenamesvg = globaldata->inputFileName + ".heatmap." + sharedorder->getLabel() + "." + groupComb + ".svg";
107                 openOutputFile(filenamesvg, outsvg);
108                 
109                 //svg image
110                 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(lookup.size() * 300) + " " + toString((lookup[0]->getNumBins()*5 + 15))  + "\">\n";
111                 outsvg << "<g>\n";
112                 
113                 int x = 15;
114                 int y = 15;
115                 string color;
116
117                 for (int i = 0; i <= lookup[0]->getNumBins(); i++) {
118                 
119                         for (int j = 0; j < lookup.size(); j++) {
120                         
121                                 color = colorScale[lookup[j]->getAbundance(i)]; 
122                                                 
123                                 outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
124                                 x += 300;
125                         }
126                         x = 15;
127                         y += 5;
128                 }
129                 outsvg << "</g>\n</svg>\n";
130                 
131                 outsvg.close();
132
133                 
134                 
135         }
136         catch(exception& e) {
137                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
138                 exit(1);
139         }
140         catch(...) {
141                 cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
142                 exit(1);
143         }
144 }
145 //**********************************************************************************************************************
146 void HeatMap::getSharedVectors(SharedOrderVector* order){
147         try {
148                 lookup.clear();
149                 
150                 groupComb = "";
151                 
152                 //create and initialize vector of sharedvectors, one for each group
153                 for (int i = 0; i < globaldata->Groups.size(); i++) { 
154                         SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
155                         temp->setLabel(order->getLabel());
156                         temp->setGroup(globaldata->Groups[i]);
157                         groupComb += globaldata->Groups[i];
158                         lookup.push_back(temp);
159                 }
160                 
161                 int numSeqs = order->size();
162                 //sample all the members
163                 for(int i=0;i<numSeqs;i++){
164                         //get first sample
165                         individual chosen = order->get(i);
166                         int abundance; 
167                                         
168                         //set info for sharedvector in chosens group
169                         for (int j = 0; j < lookup.size(); j++) { 
170                                 if (chosen.group == lookup[j]->getGroup()) {
171                                          abundance = lookup[j]->getAbundance(chosen.bin);
172                                          lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
173                                          break;
174                                 }
175                         }
176                 }
177                 
178         }
179         catch(exception& e) {
180                 cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
181                 exit(1);
182         }
183         catch(...) {
184                 cout << "An unknown error has occurred in the HeatMap class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
185                 exit(1);
186         }
187
188 }
189
190 //**********************************************************************************************************************
191
192
193