X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=heatmap.cpp;h=254b70668f9539929dcd3d60360cd23bd7c6dc2a;hb=a65438a717aba8cad1ff86c53279b191710b8810;hp=7d6eeaa146b84db4233974a70c16dc482bc12ef9;hpb=7ae40733a270f73ec8a331540cc7f28fd60b24fc;p=mothur.git diff --git a/heatmap.cpp b/heatmap.cpp index 7d6eeaa..254b706 100644 --- a/heatmap.cpp +++ b/heatmap.cpp @@ -10,9 +10,8 @@ #include "heatmap.h" //********************************************************************************************************************** -HeatMap::HeatMap(string sort, string scale, int num, int fsize, string dir){ +HeatMap::HeatMap(string sort, string scale, int num, int fsize, string dir, string i){ try { - globaldata = GlobalData::getInstance(); m = MothurOut::getInstance(); // format = globaldata->getFormat(); sorted = sort; @@ -20,6 +19,7 @@ HeatMap::HeatMap(string sort, string scale, int num, int fsize, string dir){ outputDir = dir; numOTU = num; fontSize = fsize; + inputfile = i; } catch(exception& e) { m->errorOut(e, "HeatMap", "HeatMap"); @@ -70,7 +70,7 @@ string HeatMap::getPic(RAbundVector* rabund) { } - string filenamesvg = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + rabund->getLabel() + ".heatmap.bin.svg"; + string filenamesvg = outputDir + m->getRootName(m->getSimpleName(inputfile)) + rabund->getLabel() + ".heatmap.bin.svg"; m->openOutputFile(filenamesvg, outsvg); //svg image @@ -119,7 +119,8 @@ string HeatMap::getPic(vector lookup) { } //sort lookup so shared bins are on top - if (sorted != "none") { sortSharedVectors(lookup); } + vector sortedLabels = m->currentSharedBinLabels; + if (sorted != "none") { sortedLabels = sortSharedVectors(lookup); } vector > scaleRelAbund; vector maxRelAbund(lookup[0]->size(), 0.0); @@ -143,12 +144,17 @@ string HeatMap::getPic(vector lookup) { if (lookup[i]->getAbundance(j) != 0) { //don't want log value of 0. if (scaler == "log10") { - scaleRelAbund[i][j] = toHex(int(255 * log10(relAbund) / log10(maxRelAbund[i]))) + "0000"; + if (maxRelAbund[i] == 1) { maxRelAbund[i] -= 0.001; } + if (relAbund == 1) { relAbund -= 0.001; } + scaleRelAbund[i][j] = toHex(int(255 * log10(relAbund) / log10(maxRelAbund[i]))) + "0000"; }else if (scaler == "log2") { + if (maxRelAbund[i] == 1) { maxRelAbund[i] -= 0.001; } + if (relAbund == 1) { relAbund -= 0.001; } scaleRelAbund[i][j] = toHex(int(255 * log2(relAbund) / log2(maxRelAbund[i]))) + "0000"; }else if (scaler == "linear") { - scaleRelAbund[i][j] = toHex(int(255 * relAbund / maxRelAbund[i])) + "0000"; + scaleRelAbund[i][j] = toHex(int(255 * relAbund / maxRelAbund[i])) + "0000"; }else { //if user enters invalid scaler option. + if (maxRelAbund[i] == 1) { maxRelAbund[i] += 0.001; } scaleRelAbund[i][j] = toHex(int(255 * log10(relAbund / log10(maxRelAbund[i])))) + "0000"; } }else { scaleRelAbund[i][j] = "FFFFFF"; } @@ -156,38 +162,48 @@ string HeatMap::getPic(vector lookup) { } } - string filenamesvg = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + lookup[0]->getLabel() + ".heatmap.bin.svg"; + string filenamesvg = outputDir + m->getRootName(m->getSimpleName(inputfile)) + lookup[0]->getLabel() + ".heatmap.bin.svg"; m->openOutputFile(filenamesvg, outsvg); + int binHeight = 20; + int labelBump = 100; + int binWidth = 300; //svg image - outsvg << "\n"; + outsvg << "\n"; outsvg << "\n"; //white backround - outsvg << ""; + outsvg << ""; outsvg << "Heatmap at distance " + lookup[0]->getLabel() + "\n"; //column labels - for (int h = 0; h < lookup.size(); h++) { - outsvg << "getGroup().length() / 2)) + "\" y=\"50\">" + lookup[h]->getGroup() + "\n"; + for (int h = 0; h < lookup.size()+1; h++) { + if (h == 0) { + string tempLabel = "OTU"; + outsvg << "" + tempLabel + "\n"; + }else { + outsvg << "getGroup().length() / 2)+labelBump/2) + "\" y=\"50\">" + lookup[h-1]->getGroup() + "\n"; + } } //output legend and color labels string color; int x = 0; - int y = 103 + (numBinsToDisplay*5); + int y = 103 + (numBinsToDisplay*binHeight); printLegend(y, superMaxRelAbund); y = 70; for (int i = 0; i < numBinsToDisplay; i++) { + outsvg << "" + sortedLabels[i] + "\n"; + x += labelBump; for (int j = 0; j < scaleRelAbund.size(); j++) { if (m->control_pressed) { outsvg.close(); return "control"; } - outsvg << "\n"; - x += 300; + outsvg << "\n"; + x += binWidth; } x = 0; - y += 5; + y += binHeight; } outsvg << "\n\n"; @@ -203,12 +219,14 @@ string HeatMap::getPic(vector lookup) { } //********************************************************************************************************************** -int HeatMap::sortSharedVectors(vector& lookup){ +vector HeatMap::sortSharedVectors(vector& lookup){ try { vector looktemp; map place; //spot in lookup where you insert shared by, ie, 3 -> 2 if they are shared by 3 inset into location 2. map::iterator it; + + vector sortedLabels = m->currentSharedBinLabels; /****************** find order of otus **********************/ if (sorted == "shared") { @@ -217,7 +235,7 @@ int HeatMap::sortSharedVectors(vector& lookup){ place = orderTopOtu(lookup); }else if (sorted == "topgroup") { place = orderTopGroup(lookup); - }else { m->mothurOut("Error: invalid sort option."); m->mothurOutEndLine(); return 1; } + }else { m->mothurOut("Error: invalid sort option."); m->mothurOutEndLine(); return sortedLabels; } /******************* create copy of lookup *********************/ @@ -241,6 +259,7 @@ int HeatMap::sortSharedVectors(vector& lookup){ int newAbund = looktemp[j]->getAbundance(i); // 1 -> 3 lookup[j]->set(place[i], newAbund, looktemp[j]->getGroup()); //binNumber, abundance, group } + sortedLabels[place[i]] = m->currentSharedBinLabels[i]; } //delete looktemp -- Sarah look at - this is causing segmentation faults @@ -248,7 +267,7 @@ int HeatMap::sortSharedVectors(vector& lookup){ // delete looktemp[j]; } - return 0; + return sortedLabels; } catch(exception& e) { @@ -416,7 +435,8 @@ string HeatMap::getPic(vector lookup) { } //sort lookup so shared bins are on top - if (sorted != "none") { sortSharedVectors(lookup); } + vector sortedLabels = m->currentSharedBinLabels; + if (sorted != "none") { sortedLabels = sortSharedVectors(lookup); } vector > scaleRelAbund; vector maxRelAbund(lookup.size(), 0.0); @@ -440,11 +460,15 @@ string HeatMap::getPic(vector lookup) { if (lookup[i]->getAbundance(j) != 0) { //don't want log value of 0. if (scaler == "log10") { + if (maxRelAbund[i] == 1) { maxRelAbund[i] -= 0.001; } + if (relAbund == 1) { relAbund -= 0.001; } scaleRelAbund[i][j] = toHex(int(255 * log10(relAbund) / log10(maxRelAbund[i]))) + "0000"; }else if (scaler == "log2") { + if (maxRelAbund[i] == 1) { maxRelAbund[i] -= 0.001; } + if (relAbund == 1) { relAbund -= 0.001; } scaleRelAbund[i][j] = toHex(int(255 * log2(relAbund) / log2(maxRelAbund[i]))) + "0000"; }else if (scaler == "linear") { - scaleRelAbund[i][j] = toHex(int(255 * relAbund / maxRelAbund[i])) + "0000"; + scaleRelAbund[i][j] = toHex(int(255 * relAbund / maxRelAbund[i])) + "0000"; }else { //if user enters invalid scaler option. scaleRelAbund[i][j] = toHex(int(255 * log10(relAbund / log10(maxRelAbund[i])))) + "0000"; } @@ -453,38 +477,49 @@ string HeatMap::getPic(vector lookup) { } } - string filenamesvg = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + lookup[0]->getLabel() + ".heatmap.bin.svg"; + string filenamesvg = outputDir + m->getRootName(m->getSimpleName(inputfile)) + lookup[0]->getLabel() + ".heatmap.bin.svg"; m->openOutputFile(filenamesvg, outsvg); + + int binHeight = 20; + int labelBump = 100; + int binWidth = 300; //svg image - outsvg << "\n"; + outsvg << "\n"; outsvg << "\n"; //white backround - outsvg << ""; + outsvg << ""; outsvg << "Heatmap at distance " + lookup[0]->getLabel() + "\n"; //column labels - for (int h = 0; h < lookup.size(); h++) { - outsvg << "getGroup().length() / 2)) + "\" y=\"50\">" + lookup[h]->getGroup() + "\n"; - } - + for (int h = 0; h < lookup.size()+1; h++) { + if (h == 0) { + string tempLabel = "OTU"; + outsvg << "" + tempLabel + "\n"; + }else { + outsvg << "getGroup().length() / 2)+labelBump/2) + "\" y=\"50\">" + lookup[h-1]->getGroup() + "\n"; + } + } + //output legend and color labels string color; int x = 0; - int y = 103 + (numBinsToDisplay*5); + int y = 103 + (numBinsToDisplay*binHeight); printLegend(y, superMaxRelAbund); y = 70; for (int i = 0; i < numBinsToDisplay; i++) { + outsvg << "" + sortedLabels[i] + "\n"; + x += labelBump; for (int j = 0; j < scaleRelAbund.size(); j++) { if (m->control_pressed) { outsvg.close(); return "control"; } - outsvg << "\n"; - x += 300; + outsvg << "\n"; + x += binWidth; } x = 0; - y += 5; + y += binHeight; } outsvg << "\n\n"; @@ -499,12 +534,14 @@ string HeatMap::getPic(vector lookup) { } } //********************************************************************************************************************** -int HeatMap::sortSharedVectors(vector& lookup){ +vector HeatMap::sortSharedVectors(vector& lookup){ try { vector looktemp; map place; //spot in lookup where you insert shared by, ie, 3 -> 2 if they are shared by 3 inset into location 2. map::iterator it; + + vector sortedLabels = m->currentSharedBinLabels; /****************** find order of otus **********************/ if (sorted == "shared") { @@ -513,7 +550,7 @@ int HeatMap::sortSharedVectors(vector& lookup){ place = orderTopOtu(lookup); }else if (sorted == "topgroup") { place = orderTopGroup(lookup); - }else { m->mothurOut("Error: invalid sort option."); m->mothurOutEndLine(); return 1; } + }else { m->mothurOut("Error: invalid sort option."); m->mothurOutEndLine(); return sortedLabels; } /******************* create copy of lookup *********************/ @@ -536,6 +573,7 @@ int HeatMap::sortSharedVectors(vector& lookup){ for (int j = 0; j < looktemp.size(); j++) { // 3 -> 2 float newAbund = looktemp[j]->getAbundance(i); // 1 -> 3 lookup[j]->set(place[i], newAbund, looktemp[j]->getGroup()); //binNumber, abundance, group + sortedLabels[place[i]] = m->currentSharedBinLabels[i]; } } @@ -544,7 +582,7 @@ int HeatMap::sortSharedVectors(vector& lookup){ // delete looktemp[j]; } - return 0; + return sortedLabels; } catch(exception& e) {