]> git.donarmstrong.com Git - mothur.git/blobdiff - heatmap.cpp
added checks for ^C to quit command instead of program
[mothur.git] / heatmap.cpp
index 0b1a80e6e9dca67a794653ce171d1b4011287b4a..3dca535b8600e38f29cf87160ab5a487282280d5 100644 (file)
 #include "heatmap.h"
 
 //**********************************************************************************************************************
-HeatMap::HeatMap(){
+HeatMap::HeatMap(string sort, string scale, string dir){
        try {
                globaldata = GlobalData::getInstance();
-               format = globaldata->getFormat();
-               sorted = globaldata->getSorted();
+               m = MothurOut::getInstance();
+//             format = globaldata->getFormat();
+               sorted = sort;
+               scaler = scale;
+               outputDir = dir;
        }
        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";
+               m->errorOut(e, "HeatMap", "HeatMap");
                exit(1);
        }
 }
 
 //**********************************************************************************************************************
 
-void HeatMap::getPic(RAbundVector* rabund) {
+string HeatMap::getPic(RAbundVector* rabund) {
        try {
-               //get users scaling method
-               scaler = globaldata->getScale();
+               
                
                float maxRelAbund = 0.0;                
                
@@ -40,13 +38,14 @@ void HeatMap::getPic(RAbundVector* rabund) {
                        if(relAbund > maxRelAbund){     maxRelAbund = relAbund; }
                }
                
-               scaler = globaldata->getScale();
                
                vector<string> scaleRelAbund(rabund->size(), "");
                
                for(int i=0;i<rabund->size();i++){
                        float relAbund = rabund->get(i) / (float)rabund->getNumSeqs();
                        
+                       if (m->control_pressed) { return "control"; }
+                       
                        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";  
@@ -62,11 +61,11 @@ void HeatMap::getPic(RAbundVector* rabund) {
                }
                
                
-               string filenamesvg = getRootName(globaldata->inputFileName) + rabund->getLabel() + ".heatmap.svg";
+               string filenamesvg = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + rabund->getLabel() + ".heatmap.bin.svg";
                openOutputFile(filenamesvg, outsvg);
                
                //svg image
-               outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(300) + " " + toString((rabund->getNumBins()*5 + 120))  + "\">\n";
+               outsvg << "<svg xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns=\"http://www.w3.org/2000/svg\" width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(300) + " " + toString((rabund->getNumBins()*5 + 120))  + "\">\n";
                outsvg << "<g>\n";
                
                //white backround
@@ -81,6 +80,7 @@ void HeatMap::getPic(RAbundVector* rabund) {
                
                y = 70;
                for (int i = 0; i < scaleRelAbund.size(); i++) {
+                       if (m->control_pressed) { outsvg.close(); return "control"; }
                        
                        outsvg << "<rect fill=\"#" + scaleRelAbund[i] + "\" stroke=\"#" + scaleRelAbund[i] + "\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
                        y += 5;
@@ -89,25 +89,20 @@ void HeatMap::getPic(RAbundVector* rabund) {
                outsvg << "</g>\n</svg>\n";
                outsvg.close();
                
-               
+               return filenamesvg;
        }
        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";
+               m->errorOut(e, "HeatMap", "getPic");
                exit(1);
        }
 }
 
 //**********************************************************************************************************************
 
-void HeatMap::getPic(vector<SharedRAbundVector*> lookup) {
+string HeatMap::getPic(vector<SharedRAbundVector*> lookup) {
        try {
-               
                //sort lookup so shared bins are on top
-               if (sorted == "T") {  sortSharedVectors(lookup);  }
+               if (isTrue(sorted) == true) {  sortSharedVectors(lookup);  }
                
                vector<vector<string> > scaleRelAbund;
                vector<float> maxRelAbund(lookup.size(), 0.0);          
@@ -122,12 +117,11 @@ void HeatMap::getPic(vector<SharedRAbundVector*> lookup) {
                        if(maxRelAbund[i] > superMaxRelAbund){  superMaxRelAbund = maxRelAbund[i];      }
                }
                
-               scaler = globaldata->getScale();
-               
                scaleRelAbund.resize(lookup.size());
                for(int i=0;i<lookup.size();i++){
                        scaleRelAbund[i].assign(lookup[i]->size(), "");
                        for(int j=0;j<lookup[i]->size();j++){
+                               if (m->control_pressed) {  return "control"; }
                                float relAbund = lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs();
                                
                                if (lookup[i]->getAbundance(j) != 0) { //don't want log value of 0.
@@ -141,15 +135,15 @@ void HeatMap::getPic(vector<SharedRAbundVector*> lookup) {
                                                scaleRelAbund[i][j] = toHex(int(255 * log10(relAbund / log10(maxRelAbund[i]))))  + "0000"; 
                                        } 
                                }else { scaleRelAbund[i][j] = "FFFFFF";  }
-                               
+
                        }
                }
-               
-               string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + ".heatmap.svg";
+
+               string filenamesvg = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + lookup[0]->getLabel() + ".heatmap.bin.svg";
                openOutputFile(filenamesvg, outsvg);
                
                //svg image
-               outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(lookup.size() * 300) + " " + toString((lookup[0]->getNumBins()*5 + 120))  + "\">\n";
+               outsvg << "<svg xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns=\"http://www.w3.org/2000/svg\" width=\"100%\" height=\"100%\" viewBox=\"0 0 " + toString(lookup.size() * 300) + " " + toString((lookup[0]->getNumBins()*5 + 120))  + "\">\n";
                outsvg << "<g>\n";
                
                //white backround
@@ -170,6 +164,7 @@ void HeatMap::getPic(vector<SharedRAbundVector*> lookup) {
                y = 70;
                for (int i = 0; i < scaleRelAbund[0].size(); i++) {
                        for (int j = 0; j < scaleRelAbund.size(); j++) {
+                               if (m->control_pressed) { outsvg.close(); return "control"; }
                                
                                outsvg << "<rect fill=\"#" + scaleRelAbund[j][i] + "\" stroke=\"#" + scaleRelAbund[j][i] + "\" x=\"" + toString(x) + "\" y=\"" + toString(y) + "\" width=\"300\" height=\"5\"/>\n";
                                x += 300;
@@ -181,13 +176,11 @@ void HeatMap::getPic(vector<SharedRAbundVector*> lookup) {
                outsvg << "</g>\n</svg>\n";
                outsvg.close();
                
+               return filenamesvg;
+
        }
        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";
+               m->errorOut(e, "HeatMap", "getPic");
                exit(1);
        }
 }
@@ -217,8 +210,11 @@ void HeatMap::sortSharedVectors(vector<SharedRAbundVector*>& lookup){
                        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++) { 
@@ -244,7 +240,7 @@ void HeatMap::sortSharedVectors(vector<SharedRAbundVector*>& lookup){
                        //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 (count < 2)  { lookup[j]->push_back(looktemp[j]->getAbundance(i), i, looktemp[j]->getGroup()); }
+                               if (count < 2)  { lookup[j]->push_back(looktemp[j]->getAbundance(i), looktemp[j]->getGroup()); }
                                //they are shared by some
                                else {  lookup[j]->insert(looktemp[j]->getAbundance(i), place[count], looktemp[j]->getGroup());   updatePlace = true; }
                        }
@@ -257,21 +253,16 @@ void HeatMap::sortSharedVectors(vector<SharedRAbundVector*>& lookup){
                        }
                }
                
-               //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";
+               m->errorOut(e, "HeatMap", "sortSharedVectors");
                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";
-               exit(1);
-       }
-       
 }
 
 //**********************************************************************************************************************
@@ -299,7 +290,6 @@ void HeatMap::printLegend(int y, float maxbin) {
                        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);      }
-                       file://localhost/Users/westcott/Desktop/c.amazon.fn.0.19.rep.fasta
                        label = int(label * 1000 + 0.5);
                        label /= 1000.0;
                        string text = toString(label, 3);
@@ -310,14 +300,9 @@ void HeatMap::printLegend(int y, float maxbin) {
        }
        
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the HeatMap class Function printLegend. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "HeatMap", "printLegend");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the HeatMap class function printLegend. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       
 }