From 3329385a816a97bdde934d98a615865c29eaacad Mon Sep 17 00:00:00 2001 From: westcott Date: Tue, 14 Apr 2009 13:17:08 +0000 Subject: [PATCH] added log10 and log2 scalers for heatmap --- globaldata.cpp | 2 +- heatmap.cpp | 75 ++++++++++++++++++++++++++++++------------------- helpcommand.cpp | 2 +- 3 files changed, 48 insertions(+), 31 deletions(-) diff --git a/globaldata.cpp b/globaldata.cpp index 2c02ca9..6ab20f3 100644 --- a/globaldata.cpp +++ b/globaldata.cpp @@ -305,7 +305,7 @@ void GlobalData::clear() { step = "0.01"; form = "integral"; sorted = "1"; //0 means don't sort, 1 means sort. - scaler = "log2"; + scaler = "log10"; } //*******************************************************/ diff --git a/heatmap.cpp b/heatmap.cpp index 75a2b87..d75f6f6 100644 --- a/heatmap.cpp +++ b/heatmap.cpp @@ -95,35 +95,39 @@ void HeatMap::getPic(SharedOrderVector* sharedorder) { //get users scaling method scaler = globaldata->getScaler(); - int maxbin = 0; + 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[-log((log10(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 << -log((log10(lookup[i]->getAbundance(j)) / lookup[i]->getNumSeqs())) << endl; - //}else if (scaler == "log2") { - //colorScale[-log((log2(lookup[i]->getAbundance(j)) / (float)lookup[i]->getNumSeqs()))] = ""; //cout << (int)log2(lookup[i]->getAbundance(j)) << endl; + //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 << -log((log2(lookup[i]->getAbundance(j)) / lookup[i]->getNumSeqs())) << endl; - // }else if (scaler == "linear") { + //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) / (float)lookup[i]->getNumSeqs() << '\t'; - //cout << lookup[i]->getAbundance(j) /(float) lookup[i]->getNumSeqs() << endl; - //}else { //if user enters invalid scaler option. - // cout << scaler << " is not a valid scaler option. I will use log10." << endl; - // colorScale[-log(log10(lookup[i]->getAbundance(j) / lookup[i]->getNumSeqs()))] = ""; - //} - //}else { colorScale[0] = "00"; } + //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)); @@ -155,8 +159,23 @@ void HeatMap::getPic(SharedOrderVector* sharedorder) { 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 << "\n"; - outsvg << "" + toString(it->first) + "\n"; + outsvg << "" + itprec + "\n"; x += 25; } @@ -167,15 +186,15 @@ void HeatMap::getPic(SharedOrderVector* sharedorder) { 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[(int)log10(lookup[j]->getAbundance(i))]; - //}else if (scaler == "log2") { - // color = colorScale[(int)log2(lookup[j]->getAbundance(i))]; - // }else if (scaler == "linear") { + 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[(int)log10(lookup[j]->getAbundance(i))]; } - //}else { color = "OO"; } + }else { color = colorScale[(log10((lookup[j]->getAbundance(i) / (float)lookup[j]->getNumSeqs()) * 100))]; } + }else { color = "OO"; } outsvg << "\n"; @@ -185,11 +204,9 @@ void HeatMap::getPic(SharedOrderVector* sharedorder) { y += 5; } - outsvg << "\n\n"; + outsvg << "\n\n"; outsvg.close(); - - } catch(exception& e) { diff --git a/helpcommand.cpp b/helpcommand.cpp index 7ac616f..9e85590 100644 --- a/helpcommand.cpp +++ b/helpcommand.cpp @@ -203,7 +203,7 @@ int HelpCommand::execute(){ cout << "Example heatmap(groups=A-B-C, line=1-3-5, sorted=0, scaler=log10)." << "\n"; cout << "The default value for groups is all the groups in your groupfile, and all lines in your inputfile will be used." << "\n"; cout << "The default value for sorted is 1 meaning you want the shared otus on top, you may change it to 0 meaning the exact representation of your input file." << "\n"; - cout << "The default value for scaler is log2; your other options are log10 and linear." << "\n"; + cout << "The default value for scaler is log10; your other options are log2 and linear." << "\n"; cout << "The heatmap command outputs a .svg file for each line or label you specify." << "\n"; cout << "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups)." << "\n" << "\n"; }else if (globaldata->helpRequest == "venn") { -- 2.39.2