]> git.donarmstrong.com Git - mothur.git/blobdiff - summarysharedcommand.cpp
line and label no longer persist between commands. Added phylip formated output...
[mothur.git] / summarysharedcommand.cpp
index 486e6c4982b885c82a39e5387b0b987e54f843ec..6a09ada5d3e8e4141c56a0c0ef4943beae6ef815 100644 (file)
@@ -30,7 +30,7 @@
 #include "sharedmorisitahorn.h"
 #include "sharedbraycurtis.h"
 #include "sharedjackknife.h"
-#include "sharedwhittaker.h"
+#include "whittaker.h"
 
 
 //**********************************************************************************************************************
@@ -42,7 +42,6 @@ SummarySharedCommand::SummarySharedCommand(){
                openOutputFile(outputFileName, outputFileHandle);
                format = globaldata->getFormat();
                validCalculator = new ValidCalculators();
-               util = new SharedUtil();
                mult = false;
                
                int i;
@@ -111,7 +110,6 @@ SummarySharedCommand::SummarySharedCommand(){
 SummarySharedCommand::~SummarySharedCommand(){
        delete input;
        delete read;
-       delete util;
 }
 
 //**********************************************************************************************************************
@@ -119,7 +117,7 @@ SummarySharedCommand::~SummarySharedCommand(){
 int SummarySharedCommand::execute(){
        try {
                int count = 1;  
-               
+       
                //if the users entered no valid calculators don't execute command
                if (sumCalculators.size() == 0) { return 0; }
                //check if any calcs can do multiples
@@ -129,14 +127,13 @@ int SummarySharedCommand::execute(){
                        }
                }
                
+               //read first line
                read = new ReadOTUFile(globaldata->inputFileName);      
                read->read(&*globaldata); 
                        
                input = globaldata->ginput;
-               order = input->getSharedOrderVector();
-                               
-               //set users groups
-               util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "summary");
+               lookup = input->getSharedRAbundVectors();
+               vector<SharedRAbundVector*> lastLookup = lookup;
                
                //output estimator names as column headers
                outputFileHandle << "label" <<'\t' << "comparison" << '\t'; 
@@ -146,7 +143,7 @@ int SummarySharedCommand::execute(){
                outputFileHandle << endl;
                
                //create file and put column headers for multiple groups file
-               if (mult = true) {
+               if (mult == true) {
                        outAllFileName = ((getRootName(globaldata->inputFileName)) + "sharedmultiple.summary");
                        openOutputFile(outAllFileName, outAll);
                        
@@ -159,55 +156,142 @@ int SummarySharedCommand::execute(){
                        outAll << endl;
                }
                
-               while(order != NULL){
+               if (lookup.size() < 2) { 
+                       cout << "I cannot run the command without at least 2 valid groups."; 
+                       for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
+                       
+                       //close files and clean up
+                       outputFileHandle.close();  remove(outputFileName.c_str());
+                       if (mult == true) {  outAll.close();  remove(outAllFileName.c_str());  }
+                       return 0;
+               //if you only have 2 groups you don't need a .sharedmultiple file
+               }else if ((lookup.size() == 2) && (mult == true)) { 
+                       mult = false;
+                       outAll.close();  
+                       remove(outAllFileName.c_str());
+               }
+                                       
+               //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 = globaldata->labels;
+               set<int> userLines = globaldata->lines;
                
-                       if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){                       
-       
-                               cout << order->getLabel() << '\t' << count << endl;
-                               util->getSharedVectors(globaldata->Groups, lookup, order);  //fills group vectors from order vector.  //fills group vectors from order vector.
+               //as long as you are not at the end of the file or done wih the lines you want
+               while((lookup[0] != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
+               
+                       if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){                   
+                               cout << lookup[0]->getLabel() << '\t' << count << endl;
+                               process(lookup);
+                               
+                               processedLabels.insert(lookup[0]->getLabel());
+                               userLabels.erase(lookup[0]->getLabel());
+                               userLines.erase(count);
+                       }
+                       
+                       if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
+                                       cout << lastLookup[0]->getLabel() << '\t' << count << endl;
+                                       process(lastLookup);
+                                       
+                                       processedLabels.insert(lastLookup[0]->getLabel());
+                                       userLabels.erase(lastLookup[0]->getLabel());
+                       }
+
+               
+                       //prevent memory leak
+                       if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  } }
+                       lastLookup = lookup;                    
                                
+                       //get next line to process
+                       lookup = input->getSharedRAbundVectors();
+                       count++;
+               }
+               
+               //output error messages about any remaining user labels
+               set<string>::iterator it;
+               bool needToRun = false;
+               for (it = userLabels.begin(); it != userLabels.end(); it++) {  
+                       cout << "Your file does not include the label "<< *it; 
+                       if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
+                               cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
+                               needToRun = true;
+                       }else {
+                               cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
+                       }
+               }
+               
+               //run last line if you need to
+               if (needToRun == true)  {
+                       cout << lastLookup[0]->getLabel() << '\t' << count << endl;
+                       process(lastLookup);
+               }
+               
+               for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  }
+
+               //reset groups parameter
+               globaldata->Groups.clear();  globaldata->setGroups("");
+               
+               //close files
+               outputFileHandle.close();
+               if (mult == true) {  outAll.close();  }
+
+               return 0;
+       }
+       catch(exception& e) {
+               cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+       catch(...) {
+               cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }               
+}
+
+/***********************************************************/
+void SummarySharedCommand::process(vector<SharedRAbundVector*> thisLookup) {
+       try {
                                //loop through calculators and add to file all for all calcs that can do mutiple groups
-                               if (mult = true) {
+                               if (mult == true) {
                                        //output label
-                                       outAll << order->getLabel() << '\t';
+                                       outAll << thisLookup[0]->getLabel() << '\t';
                                        
                                        //output groups names
                                        string outNames = "";
-                                       for (int j = 0; j < lookup.size(); j++) {
-                                               outNames += lookup[j]->getGroup() +  "-";
+                                       for (int j = 0; j < thisLookup.size(); j++) {
+                                               outNames += thisLookup[j]->getGroup() +  "-";
                                        }
                                        outNames = outNames.substr(0, outNames.length()-1); //rip off extra '-';
                                        outAll << outNames << '\t';
                                        
                                        for(int i=0;i<sumCalculators.size();i++){
                                                if (sumCalculators[i]->getMultiple() == true) { 
-                                                       sumCalculators[i]->getValues(lookup);
+                                                       sumCalculators[i]->getValues(thisLookup);
                                                        outAll << '\t';
                                                        sumCalculators[i]->print(outAll);
                                                }
                                        }
                                        outAll << endl;
                                }
-
+       
                                int n = 1; 
                                vector<SharedRAbundVector*> subset;
-                               for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
-                                       for (int l = n; l < lookup.size(); l++) {
+                               for (int k = 0; k < (thisLookup.size() - 1); k++) { // pass cdd each set of groups to commpare
+                                       for (int l = n; l < thisLookup.size(); l++) {
                                                
-                                               outputFileHandle << order->getLabel() << '\t';
+                                               outputFileHandle << thisLookup[0]->getLabel() << '\t';
                                                
                                                subset.clear(); //clear out old pair of sharedrabunds
                                                //add new pair of sharedrabunds
-                                               subset.push_back(lookup[k]); subset.push_back(lookup[l]); 
+                                               subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]); 
                                                
                                                //sort groups to be alphanumeric
-                                               if (lookup[k]->getGroup() > lookup[l]->getGroup()) {
-                                                       outputFileHandle << (lookup[l]->getGroup() +'\t' + lookup[k]->getGroup()) << '\t'; //print out groups
+                                               if (thisLookup[k]->getGroup() > thisLookup[l]->getGroup()) {
+                                                       outputFileHandle << (thisLookup[l]->getGroup() +'\t' + thisLookup[k]->getGroup()) << '\t'; //print out groups
                                                }else{
-                                                       outputFileHandle << (lookup[k]->getGroup() +'\t' + lookup[l]->getGroup()) << '\t'; //print out groups
+                                                       outputFileHandle << (thisLookup[k]->getGroup() +'\t' + thisLookup[l]->getGroup()) << '\t'; //print out groups
                                                }
                                                
                                                for(int i=0;i<sumCalculators.size();i++) {
+
                                                        sumCalculators[i]->getValues(subset); //saves the calculator outputs
                                                        outputFileHandle << '\t';
                                                        sumCalculators[i]->print(outputFileHandle);
@@ -216,26 +300,16 @@ int SummarySharedCommand::execute(){
                                        }
                                        n++;
                                }
-                       }
-               
-                       //get next line to process
-                       order = input->getSharedOrderVector();
-                       count++;
-               }
-               
-               //reset groups parameter
-               globaldata->Groups.clear();  globaldata->setGroups("");
 
-               return 0;
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
                exit(1);
        }
        catch(...) {
-               cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               cout << "An unknown error has occurred in the SummarySharedCommand class function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
                exit(1);
        }               
 }
 
-/***********************************************************/
+/***********************************************************/
\ No newline at end of file