X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=heatmap.cpp;h=86786e7e309dd718a4c4476d8745c9a0a0cd6f20;hb=2657127a3e1f3b5463f2b3a3b49bc19843f5c9d2;hp=5a02b9a040d680b460be4966412490d5508af203;hpb=485c02be0cdcd06ce08dd58d551a525b6a1b47c5;p=mothur.git diff --git a/heatmap.cpp b/heatmap.cpp index 5a02b9a..86786e7 100644 --- a/heatmap.cpp +++ b/heatmap.cpp @@ -15,8 +15,6 @@ HeatMap::HeatMap(){ globaldata = GlobalData::getInstance(); format = globaldata->getFormat(); - if (format != "list") { setGroups(); } - } 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"; @@ -30,8 +28,47 @@ HeatMap::HeatMap(){ //********************************************************************************************************************** void HeatMap::getPic(OrderVector* order) { try { - sabund = order->getSAbundVector(); - string filename = getRootName(globaldata->inputFileName) + 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 = globaldata->inputFileName + ".heatmap." + order->getLabel() + ".svg"; + + openOutputFile(filenamesvg, outsvg); + + //scale max rank so the maxrank = bright red + + //svg image + outsvg << "\n"; + outsvg << "\n"; + + int x = 15; + int y = 15; + string color; + + for (int i = 0; i <= rabund.getNumBins(); i++) { + + color = colorScale[rabund.get(i)]; + + 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"; @@ -45,8 +82,58 @@ void HeatMap::getPic(OrderVector* order) { //********************************************************************************************************************** void HeatMap::getPic(SharedOrderVector* sharedorder) { try { + colorScale.clear(); + + //fills vector of sharedsabunds - lookup getSharedVectors(sharedorder); + //sort lookup so shared bins are on top + sortSharedVectors(); + + //get maxBin + for (int i = 0; i < lookup.size(); i++) { + for (int j = 0; j < lookup[i]->size(); j++) { + colorScale[lookup[i]->getAbundance(j)] = ""; + } + } + + //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; } + } + + string filenamesvg = globaldata->inputFileName + ".heatmap." + sharedorder->getLabel() + "." + groupComb + ".svg"; + openOutputFile(filenamesvg, outsvg); + + //svg image + outsvg << "getNumBins()*5 + 15)) + "\">\n"; + outsvg << "\n"; + + int x = 15; + int y = 15; + string color; + + for (int i = 0; i <= lookup[0]->getNumBins(); i++) { + + for (int j = 0; j < lookup.size(); j++) { + + color = colorScale[lookup[j]->getAbundance(i)]; + + outsvg << "\n"; + x += 300; + } + x = 15; + y += 5; + } + outsvg << "\n\n"; + + outsvg.close(); + + } catch(exception& e) { @@ -61,16 +148,23 @@ void HeatMap::getPic(SharedOrderVector* sharedorder) { //********************************************************************************************************************** void HeatMap::getSharedVectors(SharedOrderVector* order){ try { + + //delete lookup + for (int j = 0; j < lookup.size(); j++) { + delete lookup[j]; + } + lookup.clear(); - vector templookup; + 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]); - templookup.push_back(temp); + groupComb += globaldata->Groups[i]; + lookup.push_back(temp); } int numSeqs = order->size(); @@ -81,22 +175,15 @@ void HeatMap::getSharedVectors(SharedOrderVector* order){ 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); + 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; } } } - //convert templookups rabunds to lookups sabunds - for (int j = 0; j < templookup.size(); j++) { - lookup.push_back(templookup[j]->getSharedSAbundVector()); - delete templookup[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"; @@ -109,50 +196,75 @@ void HeatMap::getSharedVectors(SharedOrderVector* order){ } + //********************************************************************************************************************** -void HeatMap::setGroups() { +void HeatMap::sortSharedVectors(){ 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(""); + //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; + + //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()); } - }else { - for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) { - globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]); + looktemp.push_back(temp); + } + + //clear out lookup to create sorted lookup + lookup.clear(); + + //create and initialize lookup to empty vectors + for (int i = 0; i < looktemp.size(); i++) { + SharedRAbundVector* temp = new SharedRAbundVector(); + lookup.push_back(temp); + } + + //for each bin + for (int i = 0; i < looktemp[0]->size(); i++) { + shared = true; + //for each group + for (int j = 0; j < looktemp.size(); j++) { + if (looktemp[j]->getAbundance(i) == 0) { shared = false; } } + + //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()); } + } + } + + //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 setGroups. 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 setGroups. 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); - } + } } -/***********************************************************/ + +//********************************************************************************************************************** + + + + +