X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=heatmap.cpp;h=3ed606f17be79024c72cf9834f499128a06d6a2e;hb=cd37904452dc95b183ff313ff05720c562902487;hp=523662ece3dae73f4ff48461621ba6b92ae9d8cc;hpb=c537597ec5ebd47b4898da87ae03b0c2aeced354;p=mothur.git diff --git a/heatmap.cpp b/heatmap.cpp index 523662e..3ed606f 100644 --- a/heatmap.cpp +++ b/heatmap.cpp @@ -10,13 +10,12 @@ #include "heatmap.h" //********************************************************************************************************************** -HeatMap::HeatMap(){ +HeatMap::HeatMap(string sort, string scale){ try { globaldata = GlobalData::getInstance(); - format = globaldata->getFormat(); - sorted = globaldata->getSorted(); - util = new SharedUtil(); - +// format = globaldata->getFormat(); + sorted = sort; + scaler = scale; } 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,105 +26,69 @@ HeatMap::HeatMap(){ exit(1); } } + //********************************************************************************************************************** -void HeatMap::getPic(OrderVector* order) { + +void HeatMap::getPic(RAbundVector* rabund) { try { - colorScale.clear(); - rabund = order->getRAbundVector(); - //get users scaling method - scaler = globaldata->getScaler(); + float maxRelAbund = 0.0; - float maxbin = 0.0; - for (int i = 0; i < rabund.size(); i++) { - if (rabund.get(i) != 0) { //don't want log value of 0. - if (scaler == "log10") { - colorScale[(log10((rabund.get(i) / (float)rabund.getNumSeqs()) * 100))] = ""; - if (maxbin < (log10((rabund.get(i) / (float)rabund.getNumSeqs()) * 100))) { maxbin = (log10((rabund.get(i) / (float)rabund.getNumSeqs()) * 100)); } - }else if (scaler == "log2") { - colorScale[(log2((rabund.get(i) / (float)rabund.getNumSeqs()) * 100))] = ""; - if (maxbin < (log2((rabund.get(i) / (float)rabund.getNumSeqs()) * 100))) { maxbin = (log2((rabund.get(i) / (float)rabund.getNumSeqs()) * 100)); } - }else if (scaler == "linear") { - colorScale[rabund.get(i)] = ""; - if (maxbin < rabund.get(i)) { maxbin = rabund.get(i); } - }else { //if user enters invalid scaler option. - cout << scaler << " is not a valid scaler option. I will use log10." << endl; - colorScale[(log10((rabund.get(i) / (float)rabund.getNumSeqs()) * 100))] = ""; - if (maxbin < (log10((rabund.get(i) / (float)rabund.getNumSeqs()) * 100))) { maxbin = (log10((rabund.get(i)) / (float)rabund.getNumSeqs()) * 100); } - } - }else { colorScale[0] = "00"; } + for(int i=0;isize();i++){ + float relAbund = rabund->get(i) / (float)rabund->getNumSeqs(); + if(relAbund > maxRelAbund){ maxRelAbund = relAbund; } } - 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; } + vector scaleRelAbund(rabund->size(), ""); + + for(int i=0;isize();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.bin.svg"; openOutputFile(filenamesvg, outsvg); //svg image - outsvg << "\n"; + outsvg << "getNumBins()*5 + 120)) + "\">\n"; outsvg << "\n"; //white backround - outsvg << ""; - outsvg << "Heatmap at distance " + order->getLabel() + "\n"; - + outsvg << "getNumBins()*5 + 120)) + "\"/>"; + outsvg << "Heatmap at distance " + rabund->getLabel() + "\n"; + //output legend and color labels - //go through map and give each score a color value string color; int x = 0; - int y = 103 + (rabund.getNumBins()*5); - if (maxbin != 0) { - //convert maxbin to relative abundance again - if (scaler == "log10") { - maxbin = pow(10, maxbin) / 100; - }else if (scaler == "log2") { - maxbin = pow(2, maxbin) / 100; - }else { maxbin = pow(10, maxbin) / 100; } - }else { maxbin = 0.00; } - - //5 is the number of boxes in the legend - float maxbinScaler = maxbin / 10; - float colorScaler = 255 / 10; - - for (int i = 0; i < 10; i++) { - string label = toString(((i+1) * maxbinScaler)); - //set precision of relative abundance to 3 - int pos = label.find_first_of('.'); - label = label.substr(0,pos+4); - color = toHex(int((float)(i+1) * colorScaler)); - - outsvg << "\n"; - outsvg << "" + label + "\n"; - x += 30; - } - - x = 0; + int y = 103 + (rabund->getNumBins()*5); + printLegend(y, maxRelAbund); + y = 70; - for (int i = 0; i <= rabund.getNumBins(); i++) { - if (rabund.get(i) != 0) { //don't want log value of 0. - if (scaler == "log10") { - color = colorScale[(log10((rabund.get(i) / (float)rabund.getNumSeqs()) * 100))]; - }else if (scaler == "log2") { - color = colorScale[(log2((rabund.get(i) / (float)rabund.getNumSeqs()) * 100))]; - }else if (scaler == "linear") { - color = colorScale[rabund.get(i)]; - }else { color = colorScale[(log10((rabund.get(i) / (float)rabund.getNumSeqs()) * 100))]; } - }else { color = "OO"; } + for (int i = 0; i < scaleRelAbund.size(); i++) { - outsvg << "\n"; + outsvg << "\n"; y += 5; } outsvg << "\n\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"; @@ -136,53 +99,49 @@ void HeatMap::getPic(OrderVector* order) { exit(1); } } + //********************************************************************************************************************** -void HeatMap::getPic(SharedOrderVector* sharedorder) { + +void HeatMap::getPic(vector 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 (isTrue(sorted) == true) { sortSharedVectors(lookup); } - //get users scaling method - scaler = globaldata->getScaler(); + vector > scaleRelAbund; + vector 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; jsize(); 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]; } + } + + scaleRelAbund.resize(lookup.size()); + for(int i=0;isize(), ""); + for(int j=0;jsize();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)); } + 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)); } + 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); } + 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"; } + } } - - //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; } - } - - string filenamesvg = getRootName(globaldata->inputFileName) + sharedorder->getLabel() + ".heatmap.svg"; + + string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + ".heatmap.bin.svg"; openOutputFile(filenamesvg, outsvg); //svg image @@ -191,7 +150,7 @@ void HeatMap::getPic(SharedOrderVector* sharedorder) { //white backround outsvg << "getNumBins()*5 + 120)) + "\"/>"; - outsvg << "Heatmap at distance " + sharedorder->getLabel() + "\n"; + outsvg << "Heatmap at distance " + lookup[0]->getLabel() + "\n"; //column labels for (int h = 0; h < lookup.size(); h++) { @@ -199,63 +158,25 @@ void HeatMap::getPic(SharedOrderVector* sharedorder) { } //output legend and color labels - //go through map and give each score a color value string color; int x = 0; int y = 103 + (lookup[0]->getNumBins()*5); - if (maxbin != 0) { - //convert maxbin to relative abundance again - if (scaler == "log10") { - maxbin = pow(10, maxbin) / 100; - }else if (scaler == "log2") { - maxbin = pow(2, maxbin) / 100; - }else { maxbin = pow(10, maxbin) / 100; } - }else { maxbin = 0.00; } - - //((lookup.size() * 300) / 60) is the number of boxes in the legend - float maxbinScaler = maxbin / ((lookup.size() * 300) / 60); - float colorScaler = 255 / ((lookup.size() * 300) / 60); - - for (int i = 0; i < ((lookup.size() * 300) / 60); i++) { - string label = toString(((i+1) * maxbinScaler)); - //set precision of relative abundance to 3 - int pos = label.find_first_of('.'); - label = label.substr(0,pos+4); - - color = toHex(int(((i+1) * colorScaler) + 15)); - outsvg << "\n"; - outsvg << "" + label + "\n"; - x += 30; - } + printLegend(y, superMaxRelAbund); - x = 0; y = 70; - - for (int i = 0; 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 << "\n"; + outsvg << "\n"; x += 300; } x = 0; y += 5; } - outsvg << "\n\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"; @@ -268,7 +189,7 @@ void HeatMap::getPic(SharedOrderVector* sharedorder) { } //********************************************************************************************************************** -void HeatMap::sortSharedVectors(){ +void HeatMap::sortSharedVectors(vector& lookup){ try { //copy lookup and then clear it to refill with sorted. //loop though lookup and determine if they are shared @@ -292,8 +213,11 @@ void HeatMap::sortSharedVectors(){ looktemp.push_back(temp); } - //clear out lookup to create sorted lookup - lookup.clear(); + //clear out lookup to create sorted lookup -- Sarah look at - this is causing segmentation faults + for (int j = 0; j < lookup.size(); j++) { +// delete lookup[j]; + } + lookup.clear(); //doesn't this do the job? //create and initialize lookup to empty vectors for (int i = 0; i < looktemp.size(); i++) { @@ -332,9 +256,9 @@ void HeatMap::sortSharedVectors(){ } } - //delete looktemp + //delete looktemp -- Sarah look at - this is causing segmentation faults for (int j = 0; j < looktemp.size(); j++) { - delete looktemp[j]; +// delete looktemp[j]; } } @@ -346,11 +270,53 @@ 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 << "\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); } + label = int(label * 1000 + 0.5); + label /= 1000.0; + string text = toString(label, 3); + + outsvg << "" + 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); + } + +}