X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=heatmap.cpp;h=6fd68614ce5e9ed4a28bcdc7c679e2f6d3d5f939;hb=510b1cfc25cd79391d6973ca20c5ec25fb1bb3b2;hp=86786e7e309dd718a4c4476d8745c9a0a0cd6f20;hpb=2657127a3e1f3b5463f2b3a3b49bc19843f5c9d2;p=mothur.git diff --git a/heatmap.cpp b/heatmap.cpp index 86786e7..6fd6861 100644 --- a/heatmap.cpp +++ b/heatmap.cpp @@ -10,203 +10,184 @@ #include "heatmap.h" //********************************************************************************************************************** -HeatMap::HeatMap(){ +HeatMap::HeatMap(string sort, string scale){ try { globaldata = GlobalData::getInstance(); - format = globaldata->getFormat(); - +// 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"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the HeatMap class function HeatMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + errorOut(e, "HeatMap", "HeatMap"); exit(1); } } + //********************************************************************************************************************** -void HeatMap::getPic(OrderVector* order) { + +void HeatMap::getPic(RAbundVector* rabund) { try { - colorScale.clear(); - rabund = order->getRAbundVector(); - for (int i = 0; i < rabund.size(); i++) { - colorScale[rabund.get(i)] = ""; + float maxRelAbund = 0.0; + + for(int i=0;isize();i++){ + float relAbund = rabund->get(i) / (float)rabund->getNumSeqs(); + if(relAbund > maxRelAbund){ maxRelAbund = relAbund; } } - 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; } + 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 = globaldata->inputFileName + ".heatmap." + order->getLabel() + ".svg"; + + string filenamesvg = getRootName(globaldata->inputFileName) + rabund->getLabel() + ".heatmap.bin.svg"; openOutputFile(filenamesvg, outsvg); - //scale max rank so the maxrank = bright red - //svg image - outsvg << "\n"; + outsvg << "getNumBins()*5 + 120)) + "\">\n"; outsvg << "\n"; - int x = 15; - int y = 15; + //white backround + outsvg << "getNumBins()*5 + 120)) + "\"/>"; + outsvg << "Heatmap at distance " + rabund->getLabel() + "\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 << "\n"; + outsvg << "\n"; y += 5; } - outsvg << "\n\n"; + 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"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + errorOut(e, "HeatMap", "getPic"); exit(1); } } + //********************************************************************************************************************** -void HeatMap::getPic(SharedOrderVector* sharedorder) { + +void HeatMap::getPic(vector lookup) { try { - colorScale.clear(); - - //fills vector of sharedsabunds - lookup - getSharedVectors(sharedorder); - //sort lookup so shared bins are on top - sortSharedVectors(); + if (isTrue(sorted) == true) { sortSharedVectors(lookup); } - //get maxBin - for (int i = 0; i < lookup.size(); i++) { - for (int j = 0; j < lookup[i]->size(); j++) { - colorScale[lookup[i]->getAbundance(j)] = ""; + vector > scaleRelAbund; + vector maxRelAbund(lookup.size(), 0.0); + float superMaxRelAbund = 0; + + 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]; } } - //get scaler - 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; } + 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") { + scaleRelAbund[i][j] = toHex(int(255 * log10(relAbund) / log10(maxRelAbund[i]))) + "0000"; + }else if (scaler == "log2") { + 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"; + }else { //if user enters invalid scaler option. + scaleRelAbund[i][j] = toHex(int(255 * log10(relAbund / log10(maxRelAbund[i])))) + "0000"; + } + }else { scaleRelAbund[i][j] = "FFFFFF"; } + + } } - - string filenamesvg = globaldata->inputFileName + ".heatmap." + sharedorder->getLabel() + "." + groupComb + ".svg"; + + string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + ".heatmap.bin.svg"; openOutputFile(filenamesvg, outsvg); //svg image - outsvg << "getNumBins()*5 + 15)) + "\">\n"; + outsvg << "getNumBins()*5 + 120)) + "\">\n"; outsvg << "\n"; - int x = 15; - int y = 15; - string color; - - for (int i = 0; i <= lookup[0]->getNumBins(); i++) { + //white backround + outsvg << "getNumBins()*5 + 120)) + "\"/>"; + outsvg << "Heatmap at distance " + lookup[0]->getLabel() + "\n"; - for (int j = 0; j < lookup.size(); j++) { - - color = colorScale[lookup[j]->getAbundance(i)]; - - outsvg << "\n"; + //column labels + for (int h = 0; h < lookup.size(); h++) { + outsvg << "getGroup().length() / 2)) + "\" y=\"50\">" + lookup[h]->getGroup() + "\n"; + } + + //output legend and color labels + string color; + int x = 0; + int y = 103 + (lookup[0]->getNumBins()*5); + printLegend(y, superMaxRelAbund); + + y = 70; + for (int i = 0; i < scaleRelAbund[0].size(); i++) { + for (int j = 0; j < scaleRelAbund.size(); j++) { + + outsvg << "\n"; x += 300; } - x = 15; + x = 0; y += 5; } - outsvg << "\n\n"; + 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"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the HeatMap class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + errorOut(e, "HeatMap", "getPic"); exit(1); } } -//********************************************************************************************************************** -void HeatMap::getSharedVectors(SharedOrderVector* order){ - try { - - //delete lookup - for (int j = 0; j < lookup.size(); j++) { - delete lookup[j]; - } - - lookup.clear(); - - groupComb = ""; - - //create and initialize vector of sharedvectors, one for each group - for (int i = 0; i < globaldata->Groups.size(); i++) { - SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins()); - temp->setLabel(order->getLabel()); - temp->setGroup(globaldata->Groups[i]); - groupComb += globaldata->Groups[i]; - lookup.push_back(temp); - } - - int numSeqs = order->size(); - //sample all the members - for(int i=0;iget(i); - int abundance; - - //set info for sharedvector in chosens group - for (int j = 0; j < lookup.size(); j++) { - if (chosen.group == lookup[j]->getGroup()) { - abundance = lookup[j]->getAbundance(chosen.bin); - lookup[j]->set(chosen.bin, (abundance + 1), chosen.group); - break; - } - } - } - - } - 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"; - 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"; - exit(1); - } - -} - //********************************************************************************************************************** -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 //if they are then insert in the front //if not push to back - bool shared; 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; + int count; //create and initialize looktemp as a copy of lookup for (int i = 0; i < lookup.size(); i++) { @@ -220,50 +201,100 @@ 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++) { SharedRAbundVector* temp = new SharedRAbundVector(); - lookup.push_back(temp); + temp->setLabel(looktemp[i]->getLabel()); + temp->setGroup(looktemp[i]->getGroup()); + lookup.push_back(temp); + + //initialize place map + place[i] = 0; } + //for each bin for (int i = 0; i < looktemp[0]->size(); i++) { - shared = true; + count = 0; + bool updatePlace = false; //for each group for (int j = 0; j < looktemp.size(); j++) { - if (looktemp[j]->getAbundance(i) == 0) { shared = false; } + 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 (shared == false) { lookup[j]->push_back(looktemp[j]->getAbundance(i), i, looktemp[j]->getGroup()); } - else { lookup[j]->push_front(looktemp[j]->getAbundance(i), i, looktemp[j]->getGroup()); } + 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++; } + } } } - //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]; } } catch(exception& e) { - 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 sortSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + errorOut(e, "HeatMap", "sortSharedVectors"); 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) { + errorOut(e, "HeatMap", "printLegend"); + exit(1); + } +}