]> git.donarmstrong.com Git - mothur.git/blobdiff - heatmap.cpp
fixed bug in heatmap
[mothur.git] / heatmap.cpp
index d75f6f6410d8a6f9cc97a40a959c17caf1b6e0e5..90b11159f4ea8016d397f72a045c1e2031e48476 100644 (file)
@@ -15,8 +15,6 @@ HeatMap::HeatMap(){
                globaldata = GlobalData::getInstance();
                format = globaldata->getFormat();
                sorted = globaldata->getSorted();
-               util = new SharedUtil();
-               
        }
        catch(exception& e) {
                cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function HeatMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
@@ -27,50 +25,71 @@ HeatMap::HeatMap(){
                exit(1);
        }
 }
+
 //**********************************************************************************************************************
-void HeatMap::getPic(OrderVector* order) {
+
+void HeatMap::getPic(RAbundVector* rabund) {
        try {
-               colorScale.clear();
+               //get users scaling method
+               scaler = globaldata->getScale();
                
-               rabund = order->getRAbundVector();
+               float maxRelAbund = 0.0;                
                
-               for (int i = 0; i < rabund.size(); i++) {
-                       colorScale[rabund.get(i)] = "";
+               for(int i=0;i<rabund->size();i++){                              
+                       float relAbund = rabund->get(i) / (float)rabund->getNumSeqs();
+                       if(relAbund > maxRelAbund){     maxRelAbund = relAbund; }
                }
                
-               float scaler = 255 / (float) colorScale.size();
+               scaler = globaldata->getScale();
                
-               //go through map and give each score a color value
-               for (it = colorScale.begin(); it != colorScale.end(); it++) {
-                       it->second = toHex(int(float(it->first) * scaler));
-                       if(it->second.length() == 1) {  it->second = "0" + it->second;  }
+               vector<string> scaleRelAbund(rabund->size(), "");
+               
+               for(int i=0;i<rabund->size();i++){
+                       float relAbund = rabund->get(i) / (float)rabund->getNumSeqs();
+                       
+                       if (rabund->get(i) != 0) { //don't want log value of 0.
+                               if (scaler == "log10") {
+                                       scaleRelAbund[i] = toHex(int(255 * log10(relAbund) / log10(maxRelAbund))) + "0000";  
+                               }else if (scaler == "log2") {
+                                       scaleRelAbund[i] = toHex(int(255 * log2(relAbund) / log2(maxRelAbund))) + "0000";  
+                               }else if (scaler == "linear") {
+                                       scaleRelAbund[i] = toHex(int(255 * relAbund / maxRelAbund)) + "0000";  
+                               }else {  //if user enters invalid scaler option.
+                                       scaleRelAbund[i] = toHex(int(255 * log10(relAbund / log10(maxRelAbund))))  + "0000"; 
+                               } 
+                       }
+                       else { scaleRelAbund[i] = "FFFFFF";  }
                }
-
-               string filenamesvg = getRootName(globaldata->inputFileName) + order->getLabel() + ".heatmap.svg";
                
+               
+               string filenamesvg = getRootName(globaldata->inputFileName) + rabund->getLabel() + ".heatmap.svg";
                openOutputFile(filenamesvg, outsvg);
                
-               //scale max rank so the maxrank = bright red
-                       
                //svg image
-               outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 300 " + toString((rabund.getNumBins()*5 + 15))  + "\">\n";
+               outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(300) + " " + toString((rabund->getNumBins()*5 + 120))  + "\">\n";
                outsvg << "<g>\n";
                
-               int x = 15;
-               int y = 15;
+               //white backround
+               outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString(300) + "\" height=\"" + toString((rabund->getNumBins()*5 + 120))  + "\"/>"; 
+               outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((150) - 40) + "\" y=\"25\">Heatmap at distance " + rabund->getLabel() + "</text>\n";
+                               
+               //output legend and color labels
                string color;
-
-               for (int i = 0; i <= rabund.getNumBins(); i++) {
+               int x = 0;
+               int y = 103 + (rabund->getNumBins()*5);
+               printLegend(y, maxRelAbund);
                
-                       color = colorScale[rabund.get(i)];
+               y = 70;
+               for (int i = 0; i < scaleRelAbund.size(); i++) {
                        
-                       outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
+                       outsvg << "<rect fill=\"#" + scaleRelAbund[i] + "\" stroke=\"#" + scaleRelAbund[i] + "\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
                        y += 5;
                }
-               outsvg << "</g>\n</svg>\n";
                
+               outsvg << "</g>\n</svg>\n";
                outsvg.close();
                
+               
        }
        catch(exception& e) {
                cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
@@ -81,61 +100,52 @@ void HeatMap::getPic(OrderVector* order) {
                exit(1);
        }
 }
+
 //**********************************************************************************************************************
-void HeatMap::getPic(SharedOrderVector* sharedorder) {
+
+void HeatMap::getPic(vector<SharedRAbundVector*> lookup) {
        try {
-               colorScale.clear();
-               
-               //fills vector of sharedsabunds - lookup
-               util->getSharedVectors(globaldata->Groups, lookup, sharedorder);  //fills group vectors from order vector.
                
                //sort lookup so shared bins are on top
-               if (sorted == "1") {  sortSharedVectors();  }
+               if (sorted == "T") {  sortSharedVectors(lookup);  }
                
-               //get users scaling method
-               scaler = globaldata->getScaler();
+               vector<vector<string> > scaleRelAbund;
+               vector<float> maxRelAbund(lookup.size(), 0.0);          
+               float superMaxRelAbund = 0;
                
-               float maxbin = 0.0;
-               for (int i = 0; i < lookup.size(); i++) {
-                       for (int j = 0; j < lookup[i]->size(); j++) {
+               for(int i = 0; i < lookup.size(); i++){
+                       for(int j=0; j<lookup[i]->size(); j++){
+                               
+                               float relAbund = lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs();
+                               if(relAbund > maxRelAbund[i]){  maxRelAbund[i] = relAbund;      }
+                       }
+                       if(maxRelAbund[i] > superMaxRelAbund){  superMaxRelAbund = maxRelAbund[i];      }
+               }
+               
+               scaler = globaldata->getScale();
+               
+               scaleRelAbund.resize(lookup.size());
+               for(int i=0;i<lookup.size();i++){
+                       scaleRelAbund[i].assign(lookup[i]->size(), "");
+                       for(int j=0;j<lookup[i]->size();j++){
+                               float relAbund = lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs();
+                               
                                if (lookup[i]->getAbundance(j) != 0) { //don't want log value of 0.
                                        if (scaler == "log10") {
-                                               colorScale[(log10((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 100))] = "";  
-                                               if (maxbin < (log10((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 100))) { maxbin = (log10((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 100)); }
-       //cout << "abundance  = " << lookup[i]->getAbundance(j) << '\t' << " relative adundance = " << (lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) << '\t';
-       //cout << (log10((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 100)) << endl;
+                                               scaleRelAbund[i][j] = toHex(int(255 * log10(relAbund) / log10(maxRelAbund[i]))) + "0000";  
                                        }else if (scaler == "log2") {
-                                               colorScale[(log2((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 100))] = "";  
-                                               if (maxbin < (log2((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 100))) { maxbin = (log2((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 100)); }
-       //cout << "abundance  = " << lookup[i]->getAbundance(j) << '\t' << " relative adundance = " << lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs() << '\t';
-       //cout << (log2((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 100)) << endl;
+                                               scaleRelAbund[i][j] = toHex(int(255 * log2(relAbund) / log2(maxRelAbund[i]))) + "0000";  
                                        }else if (scaler == "linear") {
-                                               colorScale[lookup[i]->getAbundance(j)] = "";
-                                               if (maxbin < lookup[i]->getAbundance(j)) { maxbin = lookup[i]->getAbundance(j); }
-       //cout << "abundance  = " << lookup[i]->getAbundance(j) << '\t' << " relative adundance = " << lookup[i]->getAbundance(j) << '\t';
-       //cout << lookup[i]->getAbundance(j) << endl;
+                                               scaleRelAbund[i][j] = toHex(int(255 * relAbund / maxRelAbund[i])) + "0000";  
                                        }else {  //if user enters invalid scaler option.
-                                               cout << scaler << " is not a valid scaler option. I will use log10." << endl;
-                                               colorScale[(log10((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 100))] = ""; 
-                                               if (maxbin < (log10((lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs()) * 100))) { maxbin = (log10((lookup[i]->getAbundance(j)) / (float)lookup[i]->getNumSeqs()) * 100); }  
+                                               scaleRelAbund[i][j] = toHex(int(255 * log10(relAbund / log10(maxRelAbund[i]))))  + "0000"; 
                                        } 
-                               }else { colorScale[0] = "00";  }
+                               }else { scaleRelAbund[i][j] = "FFFFFF";  }
                                
                        }
                }
-//cout << "maxbin = "  << maxbin << endl;      
-               //get scaler
-               float scalers = 255 / (float) maxbin;
-               
-               
-               //go through map and give each score a color value
-               for (it = colorScale.begin(); it != colorScale.end(); it++) {
-                       it->second = toHex(int(float(it->first) * scalers));
-                       if(it->second.length() == 1) {  it->second = "0" + it->second;  }
-//cout << it->first << " " << it->second << endl;
-               }
                
-               string filenamesvg = getRootName(globaldata->inputFileName) + sharedorder->getLabel() + ".heatmap.svg";
+               string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + ".heatmap.svg";
                openOutputFile(filenamesvg, outsvg);
                
                //svg image
@@ -144,67 +154,30 @@ void HeatMap::getPic(SharedOrderVector* sharedorder) {
                
                //white backround
                outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString(lookup.size() * 300) + "\" height=\"" + toString((lookup[0]->getNumBins()*5 + 120))  + "\"/>"; 
-               outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((lookup.size() * 150) - 40) + "\" y=\"25\">Heatmap at distance " + sharedorder->getLabel() + "</text>\n";
+               outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString((lookup.size() * 150) - 40) + "\" y=\"25\">Heatmap at distance " + lookup[0]->getLabel() + "</text>\n";
                
                //column labels
                for (int h = 0; h < lookup.size(); h++) {
                        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"; 
                }
                
-               
                //output legend and color labels
-               //go through map and give each score a color value
                string color;
                int x = 0;
-               int y = 90 + (lookup[0]->getNumBins()*5);
-               for (it = colorScale.begin(); it != colorScale.end(); it++) {
-                       color = it->second;     
-                       float value = it->first;
-                       
-                       //convert it->first to relative abundance again
-                       if (scaler == "log10") {
-                               value = pow(10, value) / 100;
-                       }else if (scaler == "log2") {
-                               value = pow(2, value) / 100;
-                       }else {  value = pow(10, value) / 100;  } 
-                       
-                       string itprec = toString(value);
-                       
-                       //set precision of relative abundance to 2
-                       int pos = itprec.find_first_of('.');
-                       itprec = itprec.substr(0,pos+3);
-
-                       outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"25\" height=\"10\"/>\n";
-                       outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\">" + itprec + "</text>\n";
-                       x += 25;
-               }
+               int y = 103 + (lookup[0]->getNumBins()*5);
+               printLegend(y, superMaxRelAbund);
                
-               x = 0;
                y = 70;
-               
-               //start at 1 since bin 0 is nothing
-               for (int i = 1; i <= lookup[0]->getNumBins(); i++) {
-                       for (int j = 0; j < lookup.size(); j++) {
+               for (int i = 0; i < scaleRelAbund[0].size(); i++) {
+                       for (int j = 0; j < scaleRelAbund.size(); j++) {
                                
-                               if (lookup[j]->getAbundance(i) != 0) { //don't want log value of 0.
-                                       if (scaler == "log10") {
-                                               color = colorScale[(log10((lookup[j]->getAbundance(i) / (float)lookup[j]->getNumSeqs()) * 100))];  
-                                       }else if (scaler == "log2") {
-                                               color = colorScale[(log2((lookup[j]->getAbundance(i) / (float)lookup[j]->getNumSeqs()) * 100))];  
-                                       }else if (scaler == "linear") {
-                                               color = colorScale[lookup[j]->getAbundance(i)]; 
-                                       }else {  color = colorScale[(log10((lookup[j]->getAbundance(i) / (float)lookup[j]->getNumSeqs()) * 100))];      } 
-                               }else { color = "OO";  }
-
-                               
-                               outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
+                               outsvg << "<rect fill=\"#" + scaleRelAbund[j][i] + "\" stroke=\"#" + scaleRelAbund[j][i] + "\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
                                x += 300;
                        }
                        x = 0;
                        y += 5;
                }
                
-               
                outsvg << "</g>\n</svg>\n";
                outsvg.close();
                
@@ -220,7 +193,7 @@ void HeatMap::getPic(SharedOrderVector* sharedorder) {
 }
 
 //**********************************************************************************************************************
-void HeatMap::sortSharedVectors(){
+void HeatMap::sortSharedVectors(vector<SharedRAbundVector*>& lookup){
        try {
                //copy lookup and then clear it to refill with sorted.
                //loop though lookup and determine if they are shared
@@ -245,6 +218,9 @@ void HeatMap::sortSharedVectors(){
                }
                
                //clear out lookup to create sorted lookup
+               for (int j = 0; j < lookup.size(); j++) {
+                       delete lookup[j];
+               }
                lookup.clear();
                
                //create and initialize lookup to empty vectors
@@ -298,11 +274,54 @@ void HeatMap::sortSharedVectors(){
                cout << "An unknown error has occurred in the HeatMap class function sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
                exit(1);
        }
-
+       
 }
 
 //**********************************************************************************************************************
 
+void HeatMap::printLegend(int y, float maxbin) {
+       try {
+               
+               //output legend and color labels
+               //go through map and give each score a color value
+               string color;
+               int x = 10;
+               
+               //prints legend
+               for (int i = 1; i < 255; i++) {
+                       color = toHex(int((float)(i)));
+                       outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"1\" height=\"10\"/>\n";
+                       x += 1;
+               }
+               
+               //prints legend labels
+               x = 10;
+               for (int i = 1; i<=5; i++) {
+                       float label;
+                       if(scaler== "log10")            {       label = maxbin * log10(51*i) / log10(255);      }
+                       else if(scaler== "log2")        {       label = maxbin * log2(51*i) / log2(255);        }
+                       else if(scaler== "linear")      {       label = maxbin * 51 * i / 255;                          }
+                       else                                            {       label = maxbin * log10(51*i) / log10(255);      }
+                       file://localhost/Users/westcott/Desktop/c.amazon.fn.0.19.rep.fasta
+                       label = int(label * 1000 + 0.5);
+                       label /= 1000.0;
+                       string text = toString(label, 3);
+                       
+                       outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(x) + "\" y=\"" + toString(y-3) + "\">" + text + "</text>\n";
+                       x += 60;
+               }
+       }
+       
+       catch(exception& e) {
+               cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function printLegend. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+       catch(...) {
+               cout << "An unknown error has occurred in the HeatMap class function printLegend. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+       
+}