]> git.donarmstrong.com Git - mothur.git/blobdiff - heatmap.cpp
added log10 and log2 scalers for heatmap
[mothur.git] / heatmap.cpp
index 3da6dcc569b012352d4fd5852d4c5e50e7814f40..d75f6f6410d8a6f9cc97a40a959c17caf1b6e0e5 100644 (file)
@@ -14,8 +14,8 @@ HeatMap::HeatMap(){
        try {
                globaldata = GlobalData::getInstance();
                format = globaldata->getFormat();
-               
-               if (format != "list") {  setGroups();  }
+               sorted = globaldata->getSorted();
+               util = new SharedUtil();
                
        }
        catch(exception& e) {
@@ -30,8 +30,47 @@ HeatMap::HeatMap(){
 //**********************************************************************************************************************
 void HeatMap::getPic(OrderVector* order) {
        try {
-               sabund = order->getSAbundVector();
-               string filename = getRootName(globaldata->inputFileName) + "heatmap" + order->getLabel();
+               colorScale.clear();
+               
+               rabund = order->getRAbundVector();
+               
+               for (int i = 0; i < rabund.size(); i++) {
+                       colorScale[rabund.get(i)] = "";
+               }
+               
+               float scaler = 255 / (float) colorScale.size();
+               
+               //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;  }
+               }
+
+               string filenamesvg = getRootName(globaldata->inputFileName) + order->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 << "<g>\n";
+               
+               int x = 15;
+               int y = 15;
+               string color;
+
+               for (int i = 0; i <= rabund.getNumBins(); i++) {
+               
+                       color = colorScale[rabund.get(i)];
+                       
+                       outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
+                       y += 5;
+               }
+               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";
@@ -45,11 +84,129 @@ void HeatMap::getPic(OrderVector* order) {
 //**********************************************************************************************************************
 void HeatMap::getPic(SharedOrderVector* sharedorder) {
        try {
+               colorScale.clear();
+               
                //fills vector of sharedsabunds - lookup
-               getSharedVectors(sharedorder);
+               util->getSharedVectors(globaldata->Groups, lookup, sharedorder);  //fills group vectors from order vector.
+               
+               //sort lookup so shared bins are on top
+               if (sorted == "1") {  sortSharedVectors();  }
+               
+               //get users scaling method
+               scaler = globaldata->getScaler();
+               
+               float maxbin = 0.0;
+               for (int i = 0; i < lookup.size(); i++) {
+                       for (int j = 0; j < lookup[i]->size(); j++) {
+                               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;
+                                       }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;
+                                       }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;
+                                       }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); }  
+                                       } 
+                               }else { colorScale[0] = "00";  }
+                               
+                       }
+               }
+//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";
+               openOutputFile(filenamesvg, outsvg);
+               
+               //svg image
+               outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(lookup.size() * 300) + " " + toString((lookup[0]->getNumBins()*5 + 120))  + "\">\n";
+               outsvg << "<g>\n";
+               
+               //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";
+               
+               //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;
+               }
                
-               string filename = getRootName(globaldata->inputFileName) + "heatmap" + sharedorder->getLabel();
+               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++) {
+                               
+                               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";
+                               x += 300;
+                       }
+                       x = 0;
+                       y += 5;
+               }
+               
+               
+               outsvg << "</g>\n</svg>\n";
+               outsvg.close();
                
        }
        catch(exception& e) {
@@ -61,101 +218,92 @@ void HeatMap::getPic(SharedOrderVector* sharedorder) {
                exit(1);
        }
 }
+
 //**********************************************************************************************************************
-void HeatMap::getSharedVectors(SharedOrderVector* order){
+void HeatMap::sortSharedVectors(){
        try {
-               lookup.clear();
+               //copy lookup and then clear it to refill with sorted.
+               //loop though lookup and determine if they are shared
+               //if they are then insert in the front
+               //if not push to back
+               
+               vector<SharedRAbundVector*> looktemp;
+               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.
+               map<int, int>::iterator it;
+               int count;
+               
+               //create and initialize looktemp as a copy of lookup
+               for (int i = 0; i < lookup.size(); i++) { 
+                       SharedRAbundVector* temp = new SharedRAbundVector(lookup[i]->getNumBins());
+                       temp->setLabel(lookup[i]->getLabel());
+                       temp->setGroup(lookup[i]->getGroup());
+                       //copy lookup i's info
+                       for (int j = 0; j < lookup[i]->size(); j++) {
+                               temp->set(j, lookup[i]->getAbundance(j), lookup[i]->getGroup());
+                       }
+                       looktemp.push_back(temp);
+               }
                
-               vector<SharedRAbundVector*> templookup;
+               //clear out lookup to create sorted lookup
+               lookup.clear();
                
-               //create and initialize vector of sharedvectors, one for each group
-               for (int i = 0; i < globaldata->Groups.size(); i++) { 
-                       SharedRAbundVector* temp = new SharedRAbundVector(order->getMaxRank());
-                       temp->setLabel(order->getLabel());
-                       temp->setGroup(globaldata->Groups[i]);
-                       templookup.push_back(temp);
+               //create and initialize lookup to empty vectors
+               for (int i = 0; i < looktemp.size(); i++) { 
+                       SharedRAbundVector* temp = new SharedRAbundVector();
+                       temp->setLabel(looktemp[i]->getLabel());
+                       temp->setGroup(looktemp[i]->getGroup());
+                       lookup.push_back(temp); 
+                       
+                       //initialize place map
+                       place[i] = 0;
                }
                
-               int numSeqs = order->size();
-               //sample all the members
-               for(int i=0;i<numSeqs;i++){
-                       //get first sample
-                       individual chosen = order->get(i);
-                       int abundance; 
-                                       
-                       //set info for sharedvector in chosens group
-                       for (int j = 0; j < templookup.size(); j++) { 
-                               if (chosen.group == templookup[j]->getGroup()) {
-                                        abundance = templookup[j]->getAbundance(chosen.bin);
-                                        templookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
-                                        break;
+               
+               //for each bin
+               for (int i = 0; i < looktemp[0]->size(); i++) {
+                       count = 0;
+                       bool updatePlace = false;
+                       //for each group
+                       for (int j = 0; j < looktemp.size(); j++) {
+                               if (looktemp[j]->getAbundance(i) != 0) { count++; }
+                       }
+                       
+                       //fill lookup
+                       for (int j = 0; j < looktemp.size(); j++) {
+                               //if they are not shared then push to back, if they are not insert in front
+                               if (count < 2)  { lookup[j]->push_back(looktemp[j]->getAbundance(i), i, looktemp[j]->getGroup()); }
+                               //they are shared by some
+                               else {  lookup[j]->insert(looktemp[j]->getAbundance(i), place[count], looktemp[j]->getGroup());   updatePlace = true; }
+                       }
+                       
+                       if (updatePlace == true) {
+                               //move place holders below where you entered up to "make space" for you entry
+                               for (it = place.begin(); it!= place.end(); it++) {  
+                                       if (it->first < count) { it->second++; }
                                }
                        }
                }
                
-               //convert templookups rabunds to lookups sabunds
-               for (int j = 0; j < templookup.size(); j++) { 
-                       lookup.push_back(templookup[j]->getSharedSAbundVector());
-                       delete templookup[j];
+               //delete looktemp
+               for (int j = 0; j < looktemp.size(); j++) {
+                       delete looktemp[j];
                }
                
-               
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
                exit(1);
        }
        catch(...) {
-               cout << "An unknown error has occurred in the HeatMap class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               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::setGroups() {
-       try {
-               //if the user has not entered specific groups to analyze then do them all
-               if (globaldata->Groups.size() != 0) {
-                       if (globaldata->Groups[0] != "all") {
-                               //check that groups are valid
-                               for (int i = 0; i < globaldata->Groups.size(); i++) {
-                                       if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
-                                               cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
-                                               // erase the invalid group from globaldata->Groups
-                                               globaldata->Groups.erase(globaldata->Groups.begin()+i);
-                                       }
-                               }
-                       
-                               //if the user only entered invalid groups
-                               if (globaldata->Groups.size() == 0) { 
-                                       cout << "When using the groups parameter you must have at least 1 valid groups. I will run the command using all the groups in your groupfile." << endl; 
-                                       for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
-                                               globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
-                                       }
-                               }
-                       }else{//user has enter "all" and wants the default groups
-                               globaldata->Groups.clear();
-                               for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
-                                       globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
-                               }
-                               globaldata->setGroups("");
-                       }
-               }else {
-                       for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
-                               globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
-                       }
-               }
-               
-       }
-       catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       catch(...) {
-               cout << "An unknown error has occurred in the HeatMap class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }               
 
-}
-/***********************************************************/
+
+
+
+