]> git.donarmstrong.com Git - mothur.git/commitdiff
added sharedRabundFloatVector class to represent the relabund file. modified read...
authorwestcott <westcott>
Tue, 24 Aug 2010 17:58:20 +0000 (17:58 +0000)
committerwestcott <westcott>
Tue, 24 Aug 2010 17:58:20 +0000 (17:58 +0000)
heatmap.cpp
heatmap.h
heatmapcommand.cpp
heatmapcommand.h
inputdata.cpp
inputdata.h
sharedrabundfloatvector.cpp [new file with mode: 0644]
sharedrabundfloatvector.h [new file with mode: 0644]

index 2aabfd702eb94e4b7b7279062e7e59c5821379fd..7d6eeaa146b84db4233974a70c16dc482bc12ef9 100644 (file)
@@ -32,6 +32,14 @@ HeatMap::HeatMap(string sort, string scale, int num, int fsize, string dir){
 string HeatMap::getPic(RAbundVector* rabund) {
        try {
                
+               int numBinsToDisplay = rabund->getNumBins();
+               
+               if (numOTU != 0) { //user want to display a portion of the otus
+                       if (numOTU < numBinsToDisplay) {  numBinsToDisplay = numOTU; }
+               }
+               
+               //sort lookup so shared bins are on top
+               if (sorted != "none") {  sortRabund(rabund);  }
                
                float maxRelAbund = 0.0;                
                
@@ -40,10 +48,9 @@ string HeatMap::getPic(RAbundVector* rabund) {
                        if(relAbund > maxRelAbund){     maxRelAbund = relAbund; }
                }
                
+               vector<string> scaleRelAbund(numBinsToDisplay, "");
                
-               vector<string> scaleRelAbund(rabund->size(), "");
-               
-               for(int i=0;i<rabund->size();i++){
+               for(int i=0;i<numBinsToDisplay;i++){
                        float relAbund = rabund->get(i) / (float)rabund->getNumSeqs();
                        
                        if (m->control_pressed) { return "control"; }
@@ -67,20 +74,21 @@ string HeatMap::getPic(RAbundVector* rabund) {
                m->openOutputFile(filenamesvg, outsvg);
                
                //svg image
-               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 << "<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((numBinsToDisplay*5 + 120))  + "\">\n";
                outsvg << "<g>\n";
                
                //white backround
-               outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString(300) + "\" height=\"" + toString((rabund->getNumBins()*5 + 120))  + "\"/>"; 
-               outsvg << "<text fill=\"black\" class=\"seri\" font-size=\"" + toString(fontSize) + "\" x=\"" + toString((150) - 40) + "\" y=\"25\">Heatmap at distance " + rabund->getLabel() + "</text>\n";
+               outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString(300) + "\" height=\"" + toString((numBinsToDisplay*5 + 120))  + "\"/>"; 
+               outsvg << "<text fill=\"black\" class=\"seri\" text-anchor=\"middle\" font-size=\"" + toString(fontSize) + "\" x=\"" + toString((150) - 40) + "\" y=\"25\">Heatmap at distance " + rabund->getLabel() + "</text>\n";
                                
                //output legend and color labels
                string color;
                int x = 0;
-               int y = 103 + (rabund->getNumBins()*5);
+               int y = 103 + (numBinsToDisplay*5);
                printLegend(y, maxRelAbund);
                
                y = 70;
+
                for (int i = 0; i < scaleRelAbund.size(); i++) {
                        if (m->control_pressed) { outsvg.close(); return "control"; }
                        
@@ -114,7 +122,7 @@ string HeatMap::getPic(vector<SharedRAbundVector*> lookup) {
                if (sorted != "none") {  sortSharedVectors(lookup);  }
                
                vector<vector<string> > scaleRelAbund;
-               vector<float> maxRelAbund(lookup.size(), 0.0);          
+               vector<float> maxRelAbund(lookup[0]->size(), 0.0);              
                float superMaxRelAbund = 0;
                
                for(int i = 0; i < lookup.size(); i++){
@@ -128,8 +136,8 @@ string HeatMap::getPic(vector<SharedRAbundVector*> lookup) {
                
                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++){
+                       scaleRelAbund[i].assign(numBinsToDisplay, "");
+                       for(int j=0;j<numBinsToDisplay;j++){
                                if (m->control_pressed) {  return "control"; }
                                float relAbund = lookup[i]->getAbundance(j) / (float)lookup[i]->getNumSeqs();
                                
@@ -152,11 +160,11 @@ string HeatMap::getPic(vector<SharedRAbundVector*> lookup) {
                m->openOutputFile(filenamesvg, outsvg);
                
                //svg image
-               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 << "<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((numBinsToDisplay*5 + 120))  + "\">\n";
                outsvg << "<g>\n";
                
                //white backround
-               outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString(lookup.size() * 300) + "\" height=\"" + toString((lookup[0]->getNumBins()*5 + 120))  + "\"/>"; 
+               outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString(lookup.size() * 300) + "\" height=\"" + toString((numBinsToDisplay*5 + 120))  + "\"/>"; 
                outsvg << "<text fill=\"black\" class=\"seri\" font-size=\"" + toString(fontSize) + "\" text-anchor=\"middle\" x=\"" + toString((lookup.size() * 150) - 40) + "\" y=\"25\">Heatmap at distance " + lookup[0]->getLabel() + "</text>\n";
                
                //column labels
@@ -167,7 +175,7 @@ string HeatMap::getPic(vector<SharedRAbundVector*> lookup) {
                //output legend and color labels
                string color;
                int x = 0;
-               int y = 103 + (lookup[0]->getNumBins()*5);
+               int y = 103 + (numBinsToDisplay*5);
                printLegend(y, superMaxRelAbund);
                
                y = 70;
@@ -398,6 +406,308 @@ void HeatMap::printLegend(int y, float maxbin) {
 }
 //**********************************************************************************************************************
 
+string HeatMap::getPic(vector<SharedRAbundFloatVector*> lookup) {
+       try {
+       
+               int numBinsToDisplay = lookup[0]->size();
+               
+               if (numOTU != 0) { //user want to display a portion of the otus
+                       if (numOTU < numBinsToDisplay) {  numBinsToDisplay = numOTU; }
+               }
+               
+               //sort lookup so shared bins are on top
+               if (sorted != "none") {  sortSharedVectors(lookup);  }
+               
+               vector<vector<string> > scaleRelAbund;
+               vector<float> maxRelAbund(lookup.size(), 0.0);          
+               float superMaxRelAbund = 0;
+               
+               for(int i = 0; i < lookup.size(); i++){
+                       for(int j=0; j<lookup[i]->size(); j++){
+                               
+                               float relAbund = lookup[i]->getAbundance(j);
+                               if(relAbund > maxRelAbund[i]){  maxRelAbund[i] = relAbund;      }
+                       }
+                       if(maxRelAbund[i] > superMaxRelAbund){  superMaxRelAbund = maxRelAbund[i];      }
+               }
+               
+               scaleRelAbund.resize(lookup.size());
+               for(int i=0;i<lookup.size();i++){
+                       scaleRelAbund[i].assign(numBinsToDisplay, "");
+                       for(int j=0;j<numBinsToDisplay;j++){
+                               if (m->control_pressed) {  return "control"; }
+                               float relAbund = lookup[i]->getAbundance(j);
+                               
+                               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 = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + lookup[0]->getLabel() + ".heatmap.bin.svg";
+               m->openOutputFile(filenamesvg, outsvg);
+               
+               //svg image
+               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((numBinsToDisplay*5 + 120))  + "\">\n";
+               outsvg << "<g>\n";
+               
+               //white backround
+               outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"" + toString(lookup.size() * 300) + "\" height=\"" + toString((numBinsToDisplay*5 + 120))  + "\"/>"; 
+               outsvg << "<text fill=\"black\" class=\"seri\" font-size=\"" + toString(fontSize) + "\" text-anchor=\"middle\" x=\"" + toString((lookup.size() * 150) - 40) + "\" y=\"25\">Heatmap at distance " + lookup[0]->getLabel() + "</text>\n";
+               
+               //column labels
+               for (int h = 0; h < lookup.size(); h++) {
+                       outsvg << "<text fill=\"black\" class=\"seri\" font-size=\"" + toString(fontSize) + "\" x=\"" + toString(((300 * (h+1)) - 150) - ((int)lookup[h]->getGroup().length() / 2)) + "\" y=\"50\">" + lookup[h]->getGroup() + "</text>\n"; 
+               }
+
+               //output legend and color labels
+               string color;
+               int x = 0;
+               int y = 103 + (numBinsToDisplay*5);
+               printLegend(y, superMaxRelAbund);
+               
+               y = 70;
+               for (int i = 0; i < numBinsToDisplay; 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;
+                       }
+                       x = 0;
+                       y += 5;
+               }
+               
+               outsvg << "</g>\n</svg>\n";
+               outsvg.close();
+               
+               return filenamesvg;
+
+       }
+       catch(exception& e) {
+               m->errorOut(e, "HeatMap", "getPic");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+int HeatMap::sortSharedVectors(vector<SharedRAbundFloatVector*>& lookup){
+       try {
+                               
+               vector<SharedRAbundFloatVector*> looktemp;
+               map<int, int> place; //spot in lookup where you insert shared by, ie, 3 -> 2 if they are shared by 3 inset into location 2.
+               map<int, int>::iterator it;
+               
+               /****************** find order of otus **********************/
+               if (sorted == "shared") {
+                       place = orderShared(lookup);    
+               }else if (sorted == "topotu") {
+                       place = orderTopOtu(lookup);    
+               }else if (sorted == "topgroup") {
+                       place = orderTopGroup(lookup);  
+               }else { m->mothurOut("Error: invalid sort option."); m->mothurOutEndLine();  return 1; }
+                               
+               
+               /******************* create copy of lookup *********************/
+               //create and initialize looktemp as a copy of lookup
+               for (int i = 0; i < lookup.size(); i++) { 
+                       SharedRAbundFloatVector* temp = new SharedRAbundFloatVector(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());
+                       }
+                       looktemp.push_back(temp);
+               }
+       
+               /************************ fill lookup in order given by place *********************/
+               //for each bin
+               for (int i = 0; i < looktemp[0]->size(); i++) {                                                                                                         //place
+                       //fill lookup                                                                                                                                                                   // 2 -> 1
+                       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
+                       }
+               }
+               
+               //delete looktemp -- Sarah look at - this is causing segmentation faults
+               for (int j = 0; j < looktemp.size(); j++) {
+//                     delete looktemp[j];
+               }
+               
+               return 0;
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "HeatMap", "sortSharedVectors");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+int HeatMap::sortRabund(RAbundVector*& r){
+       try {
+               map<int, int> place; //spot in lookup where you insert shared by, ie, 3 -> 2 if they are shared by 3 inset into location 2.
+               map<int, int>::iterator it;
+               
+               /****************** find order of otus **********************/
+               vector<binCount> totals;
+               
+               //for each bin
+               for (int i = 0; i < r->getNumBins(); i++) {     
+                       binCount temp(i, r->get(i));
+                       
+                       totals.push_back(temp);
+               }
+               
+               sort(totals.begin(), totals.end(), comparebinCounts);
+               
+               //fill place
+               for (int i = 0; i < totals.size(); i++) {   place[totals[i].bin] = i;  }
+               
+               /******************* create copy of lookup *********************/
+               //create and initialize rtemp as a copy of r
+               
+               RAbundVector* rtemp = new RAbundVector(r->getNumBins());
+               for (int i = 0; i < r->size(); i++) {  rtemp->set(i, r->get(i)); }
+               rtemp->setLabel(r->getLabel());
+                       
+               /************************ fill lookup in order given by place *********************/
+               //for each bin
+               for (int i = 0; i < rtemp->size(); i++) {                                                                               //place
+                       //fill lookup                                                                                                                           // 2 -> 1
+                                                                                                                                                                               // 3 -> 2
+                       int newAbund = rtemp->get(i);                                                                                           // 1 -> 3
+                       r->set(place[i], newAbund); //binNumber, abundance
+               }
+               
+               return 0;
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "HeatMap", "sortRabund");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+map<int, int> HeatMap::orderShared(vector<SharedRAbundFloatVector*>& lookup){
+       try {
+                               
+               map<int, int> place; //spot in lookup where you insert shared by, ie, 3 -> 2 if they are shared by 3 inset into location 2.
+               map<int, int>::iterator it;
+               
+               vector<int> sharedBins;
+               vector<int> uniqueBins;
+               
+               //for each bin
+               for (int i = 0; i < lookup[0]->size(); i++) {   
+                       int count = 0;                                                                                          
+                       
+                       //is this bin shared
+                       for (int j = 0; j < lookup.size(); j++) {               if (lookup[j]->getAbundance(i) != 0) { count++; }       }
+                       
+                       if (count < 2)  {  uniqueBins.push_back(i); }
+                       else                    {  sharedBins.push_back(i); }
+               }
+               
+               //fill place
+               for (int i = 0; i < sharedBins.size(); i++) { place[sharedBins[i]] = i; }
+               for (int i = 0; i < uniqueBins.size(); i++) { place[uniqueBins[i]] = (sharedBins.size() + i); }
+               
+               return place;
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "HeatMap", "orderShared");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+map<int, int> HeatMap::orderTopOtu(vector<SharedRAbundFloatVector*>& lookup){
+       try {
+                               
+               map<int, int> place; //spot in lookup where you insert shared by, ie, 3 -> 2 if they are shared by 3 inset into location 2.
+               map<int, int>::iterator it;
+               
+               vector<binCountFloat> totals;
+               
+               //for each bin
+               for (int i = 0; i < lookup[0]->size(); i++) {   
+                       int total = 0;                                                                                          
+                       
+                       for (int j = 0; j < lookup.size(); j++) {       total += lookup[j]->getAbundance(i);    }
+                       
+                       binCountFloat temp(i, total);
+                       
+                       totals.push_back(temp);
+               }
+               
+               sort(totals.begin(), totals.end(), comparebinFloatCounts);
+               
+               //fill place
+               for (int i = 0; i < totals.size(); i++) {   place[totals[i].bin] = i;  }
+                               
+               return place;
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "HeatMap", "orderTopOtu");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+map<int, int> HeatMap::orderTopGroup(vector<SharedRAbundFloatVector*>& lookup){
+       try {
+                               
+               map<int, int> place; //spot in lookup where you insert shared by, ie, 3 -> 2 if they are shared by 3 inset into location 2.
+               map<int, int>::iterator it;
+               
+               vector < vector<binCountFloat> > totals; //totals[0] = bin totals for group 0, totals[1] = bin totals for group 1, ...
+               totals.resize(lookup.size());
+               
+               //for each bin
+               for (int i = 0; i < lookup[0]->size(); i++) {   
+                       for (int j = 0; j < lookup.size(); j++) {
+                               binCountFloat temp(i, (lookup[j]->getAbundance(i)));
+                               totals[j].push_back(temp);
+                       }
+               }
+               
+               for (int i = 0; i < totals.size(); i++) { sort(totals[i].begin(), totals[i].end(), comparebinFloatCounts);  }
+               
+               //fill place
+               //grab the top otu for each group adding it if its not already added
+               int count = 0;
+               for (int i = 0; i < totals[0].size(); i++) { 
+               
+                       for (int j = 0; j < totals.size(); j++) {  
+                               it = place.find(totals[j][i].bin);
+                               
+                               if (it == place.end()) { //not added yet
+                                       place[totals[j][i].bin] = count;
+                                       count++;
+                               }
+                       }
+               }
+                               
+               return place;
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "HeatMap", "orderTopGroup");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+
 
 
 
index 11c67eaf439ebe3adec4f40615e1380ca870e0b5..c291a4ded677b6394aa5da9819a0140cda75a8c6 100644 (file)
--- a/heatmap.h
+++ b/heatmap.h
@@ -11,6 +11,7 @@
 
 #include "rabundvector.hpp"
 #include "sharedrabundvector.h"
+#include "sharedrabundfloatvector.h"
 #include "datavector.hpp"
 #include "globaldata.hpp"
 
@@ -20,12 +21,24 @@ struct binCount {
                int abund;
                binCount(int i, int j) : bin(i), abund(j) {}
 };
+/***********************************************************************/
+struct binCountFloat {
+               int bin;
+               float abund;
+               binCountFloat(int i, float j) : bin(i), abund(j) {}
+};
+
 /***********************************************************************/
 //sorts highest abund to lowest
 inline bool comparebinCounts(binCount left, binCount right){
        return (left.abund > right.abund);      
 }
 /***********************************************************************/
+//sorts highest abund to lowest
+inline bool comparebinFloatCounts(binCountFloat left, binCountFloat right){
+       return (left.abund > right.abund);      
+}
+/***********************************************************************/
 
 class HeatMap {
        
@@ -35,9 +48,12 @@ class HeatMap {
        
                string getPic(RAbundVector*);
                string getPic(vector<SharedRAbundVector*>);
+               string getPic(vector<SharedRAbundFloatVector*>);
 
        private:
                int sortSharedVectors(vector<SharedRAbundVector*>& );
+               int sortSharedVectors(vector<SharedRAbundFloatVector*>& );
+               int sortRabund(RAbundVector*&);
                void printLegend(int, float);
 
                GlobalData* globaldata;
@@ -49,6 +65,10 @@ class HeatMap {
                map<int, int> orderTopGroup(vector<SharedRAbundVector*>&);
                map<int, int> orderTopOtu(vector<SharedRAbundVector*>&);
                map<int, int> orderShared(vector<SharedRAbundVector*>&);
+               map<int, int> orderTopGroup(vector<SharedRAbundFloatVector*>&);
+               map<int, int> orderTopOtu(vector<SharedRAbundFloatVector*>&);
+               map<int, int> orderShared(vector<SharedRAbundFloatVector*>&);
+
                        
 };
 
index 1ca894fb391eae4e604736d0cb7102986ed3569d..c6345328e6d5ac5b0e577d03a797d35448446227 100644 (file)
@@ -44,8 +44,8 @@ HeatMapCommand::HeatMapCommand(string option) {
                        }
                        
                        //make sure the user has already run the read.otu command
-                       if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "") && (globaldata->getSharedFile() == "")) {
-                                m->mothurOut("You must read a list, rabund, sabund, or a list and a group, or a shared before you can use the heatmap.bin command."); m->mothurOutEndLine(); abort = true; 
+                       if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "") && (globaldata->getSharedFile() == "") && (globaldata->getRelAbundFile() == "")) {
+                                m->mothurOut("You must read a list, rabund, sabund, or a list and a group, shared, or relabund file before you can use the heatmap.bin command."); m->mothurOutEndLine(); abort = true; 
                        }
 
                        //check for optional parameter and set defaults
@@ -152,13 +152,17 @@ int HeatMapCommand::execute(){
                        //you are using just a list file and have only one group
                        rabund = globaldata->rabund;
                        lastLabel = rabund->getLabel();
+               }else if (format == "relabund") {
+                       //you have groups
+                       lookupFloat = input->getSharedRAbundFloatVectors();
+                       lastLabel = lookupFloat[0]->getLabel();
                }
                
                //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
                set<string> processedLabels;
                set<string> userLabels = labels;
 
-               if ((format != "list") && (format != "rabund") && (format != "sabund")) {       
+               if (format == "sharedfile") {   
                
                        //as long as you are not at the end of the file or done wih the lines you want
                        while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
@@ -235,7 +239,7 @@ int HeatMapCommand::execute(){
                        //reset groups parameter
                        globaldata->Groups.clear();  
                        
-               }else{
+               }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
        
                        while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
                                if (m->control_pressed) {   
@@ -304,6 +308,83 @@ int HeatMapCommand::execute(){
                                delete rabund; globaldata->rabund = NULL;
                        }
                
+               }else {
+               
+                       //as long as you are not at the end of the file or done wih the lines you want
+                       while((lookupFloat[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
+                               if (m->control_pressed) {
+                                       for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  }
+                                       for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
+                                       globaldata->Groups.clear(); 
+                                       delete read; delete heatmap; return 0;
+                               }
+               
+                               if(allLines == 1 || labels.count(lookupFloat[0]->getLabel()) == 1){                     
+       
+                                       m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
+                                       outputNames.push_back(heatmap->getPic(lookupFloat));
+                                       
+                                       processedLabels.insert(lookupFloat[0]->getLabel());
+                                       userLabels.erase(lookupFloat[0]->getLabel());
+                               }
+                               
+                               if ((m->anyLabelsToProcess(lookupFloat[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
+                                       string saveLabel = lookupFloat[0]->getLabel();
+                               
+                                       for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  }  
+                                       lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
+                                       m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
+                                       
+                                       outputNames.push_back(heatmap->getPic(lookupFloat));
+                                       
+                                       processedLabels.insert(lookupFloat[0]->getLabel());
+                                       userLabels.erase(lookupFloat[0]->getLabel());
+                                       
+                                       //restore real lastlabel to save below
+                                       lookupFloat[0]->setLabel(saveLabel);
+                               }
+                               
+                               lastLabel = lookupFloat[0]->getLabel();
+                               //prevent memory leak
+                               for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i]; lookupFloat[i] = NULL; }
+                                                       
+                               //get next line to process
+                               lookupFloat = input->getSharedRAbundFloatVectors();                             
+                       }
+                       
+                       
+                       if (m->control_pressed) {
+                               for (int i = 0; i < outputNames.size(); i++) {  if (outputNames[i] != "control") {  remove(outputNames[i].c_str());  } }
+                               globaldata->Groups.clear(); 
+                               delete read; delete heatmap; return 0;
+                       }
+
+                       //output error messages about any remaining user labels
+                       set<string>::iterator it;
+                       bool needToRun = false;
+                       for (it = userLabels.begin(); it != userLabels.end(); it++) {  
+                               m->mothurOut("Your file does not include the label " + *it); 
+                               if (processedLabels.count(lastLabel) != 1) {
+                                       m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
+                                       needToRun = true;
+                               }else {
+                                       m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
+                               }
+                       }
+               
+                       //run last label if you need to
+                       if (needToRun == true)  {
+                               for (int i = 0; i < lookupFloat.size(); i++) { if (lookupFloat[i] != NULL) { delete lookupFloat[i]; } }  
+                               lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
+                               
+                               m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
+                               outputNames.push_back(heatmap->getPic(lookupFloat));
+                               for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  }
+                       }
+               
+                       //reset groups parameter
+                       globaldata->Groups.clear();  
+
                }
                
                globaldata->rabund = NULL;
@@ -318,7 +399,7 @@ int HeatMapCommand::execute(){
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
                for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
                m->mothurOutEndLine();
-               
+       
                delete read;
                delete heatmap;
 
index f711cc341d6f2247cad50639a393bebaea767e1a..f3ae012fb7e5c161fc222e487d9d272acd8d0ff8 100644 (file)
@@ -35,6 +35,7 @@ private:
        SharedListVector* SharedList;
        RAbundVector* rabund;
        vector<SharedRAbundVector*> lookup;
+       vector<SharedRAbundFloatVector*> lookupFloat;
        HeatMap* heatmap;
 
        bool abort, allLines;
index 7c9fbe9c0eeda1472e09ce03c80cd5853103833e..a4d66921629cc700f2042d676f9e52e50674ba56 100644 (file)
@@ -503,7 +503,70 @@ vector<SharedRAbundVector*> InputData::getSharedRAbundVectors(string label){
        }
 }
 
-
+/***********************************************************************/
+//this is used when you don't need the order vector
+vector<SharedRAbundFloatVector*> InputData::getSharedRAbundFloatVectors(){
+       try {
+               if(fileHandle){
+                       if (format == "relabund")  {
+                               SharedRAbundFloatVector* SharedRelAbund = new SharedRAbundFloatVector(fileHandle);
+                               if (SharedRelAbund != NULL) {
+                                       return SharedRelAbund->getSharedRAbundFloatVectors();
+                               }
+                       }
+                       m->gobble(fileHandle);
+               }
+                               
+               //this is created to signal to calling function that the input file is at eof
+               vector<SharedRAbundFloatVector*> null;  null.push_back(NULL);
+               return null;
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "InputData", "getSharedRAbundFloatVectors");
+               exit(1);
+       }
+}
+/***********************************************************************/
+vector<SharedRAbundFloatVector*> InputData::getSharedRAbundFloatVectors(string label){
+       try {
+               ifstream in;
+               string  thisLabel;
+               
+               m->openInputFile(filename, in);
+               
+               if(in){
+                       if (format == "sharedfile")  {
+                               while (in.eof() != true) {
+                                       
+                                       SharedRAbundFloatVector* SharedRelAbund = new SharedRAbundFloatVector(in);
+                                       if (SharedRelAbund != NULL) {
+                                               thisLabel = SharedRelAbund->getLabel();
+                                               //if you are at the last label
+                                               if (thisLabel == label) {  in.close(); return SharedRelAbund->getSharedRAbundFloatVectors();  }
+                                               else {
+                                                       //so you don't loose this memory
+                                                       vector<SharedRAbundFloatVector*> lookup = SharedRelAbund->getSharedRAbundFloatVectors(); 
+                                                       for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
+                                                       delete SharedRelAbund;
+                                               }
+                                       }else{  break;  }
+                                       m->gobble(in);
+                               }
+                       }
+               }
+                               
+               //this is created to signal to calling function that the input file is at eof
+               vector<SharedRAbundFloatVector*> null;  null.push_back(NULL);
+               in.close();
+               return null;
+       
+       }
+       catch(exception& e) {
+               m->errorOut(e, "InputData", "getSharedRAbundFloatVectors");
+               exit(1);
+       }
+}
 /***********************************************************************/
 
 SAbundVector* InputData::getSAbundVector(){
index c91720b567838d38ff62379bfbc0fc381356d6de..658b21d8cdfe649d8434e883655ecd18922f2554 100644 (file)
@@ -6,6 +6,7 @@
 #include "sharedlistvector.h"
 #include "sharedordervector.h"
 #include "listvector.hpp"
+#include "sharedrabundfloatvector.h"
 
 
 class InputData {
@@ -29,6 +30,8 @@ public:
        RAbundVector* getRAbundVector(string);  //pass the label you want
        vector<SharedRAbundVector*> getSharedRAbundVectors();
        vector<SharedRAbundVector*> getSharedRAbundVectors(string);  //pass the label you want
+       vector<SharedRAbundFloatVector*> getSharedRAbundFloatVectors();
+       vector<SharedRAbundFloatVector*> getSharedRAbundFloatVectors(string);  //pass the label you want
        
 private:
        string format;
diff --git a/sharedrabundfloatvector.cpp b/sharedrabundfloatvector.cpp
new file mode 100644 (file)
index 0000000..f7405ae
--- /dev/null
@@ -0,0 +1,402 @@
+/*
+ *  sharedrabundfloatvector.cpp
+ *  Mothur
+ *
+ *  Created by westcott on 8/18/10.
+ *  Copyright 2010 Schloss Lab. All rights reserved.
+ *
+ */
+
+#include "sharedrabundfloatvector.h"
+#include "sharedutilities.h"
+
+/***********************************************************************/
+
+SharedRAbundFloatVector::SharedRAbundFloatVector() : DataVector(), maxRank(0.0), numBins(0), numSeqs(0.0) {globaldata = GlobalData::getInstance();}
+/***********************************************************************/
+
+SharedRAbundFloatVector::~SharedRAbundFloatVector() {}
+
+/***********************************************************************/
+SharedRAbundFloatVector::SharedRAbundFloatVector(int n) : DataVector(), maxRank(0.0), numBins(n), numSeqs(0.0) {
+               globaldata = GlobalData::getInstance();
+               individualFloat newGuy;
+               //initialize data
+               for (int i=0; i< n; i++) {
+                       newGuy.bin = i;
+                       newGuy.abundance = 0.0;
+                       data.push_back(newGuy);
+               }
+}
+/***********************************************************************/
+//reads a shared file
+SharedRAbundFloatVector::SharedRAbundFloatVector(ifstream& f) : DataVector(), maxRank(0.0), numBins(0), numSeqs(0.0) {
+       try {
+               globaldata = GlobalData::getInstance();
+               
+               if (globaldata->gGroupmap == NULL) {  groupmap = new GroupMap(); }
+               
+               int num, count;
+               float inputData;
+               count = 0;  
+               string holdLabel, nextLabel, groupN;
+               individualFloat newguy;
+               
+               for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
+               lookup.clear();
+               
+               //read in first row since you know there is at least 1 group.
+               f >> label >> groupN >> num;
+               holdLabel = label;
+               
+               //add new vector to lookup
+               SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
+               lookup.push_back(temp);
+               lookup[0]->setLabel(label);
+               lookup[0]->setGroup(groupN);
+               
+               if (globaldata->gGroupmap == NULL) { 
+                       //save group in groupmap
+                       groupmap->namesOfGroups.push_back(groupN);
+                       groupmap->groupIndex[groupN] = 0;
+               }
+               
+               //fill vector.  data = first sharedrabund in file
+               for(int i=0;i<num;i++){
+                       f >> inputData;
+                       
+                       lookup[0]->push_back(inputData, groupN); //abundance, bin, group
+                       push_back(inputData, groupN);
+                       
+                       if (inputData > maxRank) { maxRank = inputData; }
+               }
+               
+               m->gobble(f);
+               
+               if (f.eof() != true) { f >> nextLabel; }
+               
+               //read the rest of the groups info in
+               while ((nextLabel == holdLabel) && (f.eof() != true)) {
+                       f >> groupN >> num;
+                       count++;
+                       
+                       if (globaldata->gGroupmap == NULL) { 
+                               //save group in groupmap
+                               groupmap->namesOfGroups.push_back(groupN);
+                               groupmap->groupIndex[groupN] = count;
+                       }
+                       
+                       //add new vector to lookup
+                       temp = new SharedRAbundFloatVector();
+                       lookup.push_back(temp);
+                       lookup[count]->setLabel(label);
+                       lookup[count]->setGroup(groupN);
+
+                       //fill vector.  
+                       for(int i=0;i<num;i++){
+                               f >> inputData;
+                               lookup[count]->push_back(inputData, groupN); //abundance, bin, group
+                       }
+                       
+                       m->gobble(f);
+                               
+                       if (f.eof() != true) { f >> nextLabel; }
+               }
+               
+               //put file pointer back since you are now at a new distance label
+               for (int i = 0; i < nextLabel.length(); i++) { f.unget();  }
+       
+               if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap;  }
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedRAbundFloatVector", "SharedRAbundFloatVector");
+               exit(1);
+       }
+}
+
+/***********************************************************************/
+
+void SharedRAbundFloatVector::set(int binNumber, float newBinSize, string groupname){
+       try {
+               float oldBinSize = data[binNumber].abundance;
+               data[binNumber].abundance = newBinSize;
+               data[binNumber].group = groupname;
+               
+               if(newBinSize > maxRank)        {       newBinSize = newBinSize;        }
+       
+               numSeqs += (newBinSize - oldBinSize);
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedRAbundVector", "set");
+               exit(1);
+       }
+}
+
+/***********************************************************************/
+float SharedRAbundFloatVector::getAbundance(int index){
+       return data[index].abundance;   
+}
+/***********************************************************************/
+individualFloat SharedRAbundFloatVector::get(int index){
+       return data[index];     
+}
+/***********************************************************************/
+void SharedRAbundFloatVector::push_back(float binSize, string groupName){
+       try {
+               individualFloat newGuy;
+               newGuy.abundance = binSize;
+               newGuy.group = groupName;
+               newGuy.bin = data.size();
+               
+               data.push_back(newGuy);
+               numBins++;
+       
+               if(binSize > maxRank){  maxRank = binSize;      }
+       
+               numSeqs += binSize;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedRAbundFloatVector", "push_back");
+               exit(1);
+       }
+}
+/***********************************************************************/
+void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
+       try {
+               individualFloat newGuy;
+               newGuy.abundance = binSize;
+               newGuy.group = groupName;
+               newGuy.bin = otu;
+               
+               data.insert(data.begin()+otu, newGuy);
+               numBins++;
+       
+               if(binSize > maxRank){  maxRank = binSize;      }
+
+               numSeqs += binSize;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedRAbundFloatVector", "insert");
+               exit(1);
+       }
+}
+
+/***********************************************************************/
+void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
+       try {
+               individualFloat newGuy;
+               newGuy.abundance = binSize;
+               newGuy.group = groupName;
+               newGuy.bin = otu;
+               
+               data.insert(data.begin(), newGuy);
+               numBins++;
+       
+               if(binSize > maxRank){  maxRank = binSize;      }
+       
+               numSeqs += binSize;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedRAbundFloatVector", "push_front");
+               exit(1);
+       }
+}
+/**********************************************************************/
+void SharedRAbundFloatVector::pop_back(){
+       numSeqs -= data[data.size()-1].abundance;
+       numBins--;
+       data.pop_back();
+}
+/***********************************************************************/
+void SharedRAbundFloatVector::resize(int size){
+       data.resize(size);
+}
+/**********************************************************************/
+int SharedRAbundFloatVector::size(){
+       return data.size();
+}
+/***********************************************************************/
+void SharedRAbundFloatVector::print(ostream& output){
+       try {
+               output << numBins << '\t';
+       
+               for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
+               output << endl;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedRAbundFloatVector", "print");
+               exit(1);
+       }
+}
+/***********************************************************************/
+string SharedRAbundFloatVector::getGroup(){
+       return group;
+}
+/***********************************************************************/
+void SharedRAbundFloatVector::setGroup(string groupName){
+       group = groupName;
+}
+/***********************************************************************/
+int SharedRAbundFloatVector::getGroupIndex()  { return index; }
+/***********************************************************************/
+void SharedRAbundFloatVector::setGroupIndex(int vIndex)        { index = vIndex; }
+/***********************************************************************/
+int SharedRAbundFloatVector::getNumBins(){     return numBins; }
+/***********************************************************************/
+float SharedRAbundFloatVector::getNumSeqs(){   return numSeqs; }
+/***********************************************************************/
+float SharedRAbundFloatVector::getMaxRank(){   return maxRank; }
+/***********************************************************************/
+SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
+       return *this;                   
+}
+/***********************************************************************
+SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
+       try {
+               SharedRAbundVector rav(numBins);
+               rav.setLabel(label);
+               rav.setGroup(group);
+               
+               for (int i = 0; i < data.size(); i++) {
+                       
+                       rav.push_back(data[i].abundance);
+               }
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
+               exit(1);
+       }               
+}
+/***********************************************************************/
+vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
+       try {
+               SharedUtil* util;
+               util = new SharedUtil();
+               
+               util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
+
+               for (int i = 0; i < lookup.size(); i++) {
+                       //if this sharedrabund is not from a group the user wants then delete it.
+                       if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) { 
+                               delete lookup[i]; lookup[i] = NULL;
+                               lookup.erase(lookup.begin()+i); 
+                               i--; 
+                       }
+               }
+               
+               delete util;
+       
+               return lookup;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
+               exit(1);
+       }
+}
+/***********************************************************************/
+
+RAbundVector SharedRAbundFloatVector::getRAbundVector() {
+       try {
+               RAbundVector rav(numBins);
+               
+               //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
+               
+               rav.setLabel(label);
+               return rav;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
+               exit(1);
+       }
+}
+/***********************************************************************
+
+SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
+       try {
+               SharedSAbundVector sav(maxRank+1);
+               
+               for(int i=0;i<data.size();i++){
+                       int abund = data[i].abundance;
+                       sav.set(abund, sav.getAbundance(abund) + 1, group);
+               }
+               
+               sav.set(0, 0, group);
+               sav.setLabel(label);
+               sav.setGroup(group);
+               
+               return sav;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
+               exit(1);
+       }
+}
+/***********************************************************************/
+
+SAbundVector SharedRAbundFloatVector::getSAbundVector() {
+       try {
+               SAbundVector sav(ceil(maxRank)+1);
+               
+               //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
+               
+               sav.set(0, 0);
+               sav.setLabel(label);
+               return sav;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");           
+               exit(1);
+       }
+}
+
+/***********************************************************************
+
+SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
+       try {
+               SharedOrderVector ov;
+       
+               for(int i=0;i<data.size();i++){
+                       int round = ceil(data[i].abundance);
+                       for(int j=0;j<round;j++){
+                               ov.push_back(data[i].bin, round, data[i].group);
+                       }
+               }
+               random_shuffle(ov.begin(), ov.end());
+
+               ov.setLabel(label);     
+               ov.updateStats();
+               
+               return ov;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
+               exit(1);
+       }
+}
+/***********************************************************************/
+//this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
+OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
+       try {
+               OrderVector ov;
+       
+               for(int i=0;i<data.size();i++){
+                       int round = ceil(data[i].abundance);
+                       for(int j=0;j<round;j++){
+                               ov.push_back(i);
+                       }
+               }
+               random_shuffle(ov.begin(), ov.end());
+
+               ov.setLabel(label);     
+               return ov;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
+               exit(1);
+       }
+}
+
+/***********************************************************************/
+
diff --git a/sharedrabundfloatvector.h b/sharedrabundfloatvector.h
new file mode 100644 (file)
index 0000000..f826d9c
--- /dev/null
@@ -0,0 +1,82 @@
+#ifndef SHAREDRABUNDFLOATVECTOR_H
+#define SHAREDRABUNDFLOATVECTOR_H
+
+/*
+ *  sharedrabundfloatvector.h
+ *  Mothur
+ *
+ *  Created by westcott on 8/18/10.
+ *  Copyright 2010 Schloss Lab. All rights reserved.
+ *
+ */
+
+
+#include "datavector.hpp"
+#include "sharedordervector.h"
+#include "sharedsabundvector.h"
+#include "rabundvector.hpp"
+#include "groupmap.h"
+
+/* This class is a child to datavector.  It represents OTU information at a certain distance. 
+       It is similiar to an rabundvector except each member of data knows which group it belongs to.
+       Each member of the internal container "data" is a struct of type individualFloat. 
+       An individual which knows the OTU from which it came, 
+       the group it is in and its abundance.  */
+
+class GlobalData;
+
+class SharedRAbundFloatVector : public DataVector {
+       
+public:
+       SharedRAbundFloatVector();
+       SharedRAbundFloatVector(int);
+       SharedRAbundFloatVector(const SharedRAbundFloatVector& bv) : DataVector(bv), data(bv.data), maxRank(bv.maxRank), numBins(bv.numBins), numSeqs(bv.numSeqs), group(bv.group), index(bv.index){};
+    SharedRAbundFloatVector(ifstream&);
+       ~SharedRAbundFloatVector();
+
+       int getNumBins();               
+       float getNumSeqs();                                                     
+       float getMaxRank();
+       string getGroup();
+       void setGroup(string);
+       int getGroupIndex();
+       void setGroupIndex(int);                                                                
+
+       void set(int, float, string);                   //OTU, abundance, groupname
+       individualFloat get(int);
+       vector <individual> getData();
+       float getAbundance(int);
+       void push_front(float, int, string); //abundance, otu, groupname
+       void insert(float, int, string); //abundance, otu, groupname
+       void push_back(float, string);  //abundance, groupname
+       void pop_back();
+       void resize(int);
+       int size();
+       
+       void print(ostream&);
+               
+       RAbundVector getRAbundVector();
+       SAbundVector getSAbundVector();
+       OrderVector getOrderVector(map<string,int>*);
+       //SharedOrderVector getSharedOrderVector();
+       //SharedSAbundVector getSharedSAbundVector();
+       //SharedRAbundVector getSharedRAbundVector();
+       SharedRAbundFloatVector getSharedRAbundFloatVector();
+       vector<SharedRAbundFloatVector*> getSharedRAbundFloatVectors();
+       
+private:
+       vector<individualFloat>  data; 
+       vector<SharedRAbundFloatVector*> lookup;
+       GlobalData* globaldata;
+       GroupMap* groupmap;
+       float maxRank;
+       int numBins;
+       float numSeqs;
+       string group;
+       int index;      
+};
+
+
+#endif
+
+