]> git.donarmstrong.com Git - mothur.git/blobdiff - heatmap.cpp
added sort to heatmap to put shared otus at the top and updated some error checking
[mothur.git] / heatmap.cpp
index 5a02b9a040d680b460be4966412490d5508af203..86786e7e309dd718a4c4476d8745c9a0a0cd6f20 100644 (file)
@@ -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 << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 300 " + toString((rabund.getNumBins()*5 + 15))  + "\">\n";
+               outsvg << "<g>\n";
+               
+               int x = 15;
+               int y = 15;
+               string color;
+
+               for (int i = 0; i <= rabund.getNumBins(); i++) {
+               
+                       color = colorScale[rabund.get(i)];
+                       
+                       outsvg << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
+                       y += 5;
+               }
+               outsvg << "</g>\n</svg>\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 << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(lookup.size() * 300) + " " + toString((lookup[0]->getNumBins()*5 + 15))  + "\">\n";
+               outsvg << "<g>\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 << "<rect fill=\"#" + color + "0000\" stroke=\"#" + color + "0000\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
+                               x += 300;
+                       }
+                       x = 15;
+                       y += 5;
+               }
+               outsvg << "</g>\n</svg>\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<SharedRAbundVector*> 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<SharedRAbundVector*> 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);
-       }               
+       }
 
 }
-/***********************************************************/
+
+//**********************************************************************************************************************
+
+
+
+
+