]> git.donarmstrong.com Git - mothur.git/blobdiff - matrixoutputcommand.cpp
added smart distance feature and optimized all commands using line by line processing
[mothur.git] / matrixoutputcommand.cpp
index c792cc91e966203505733979cf9133a025393912..3ecf6df36e5ea33266d409fecee2620aec7a2d9f 100644 (file)
@@ -79,9 +79,7 @@ MatrixOutputCommand::~MatrixOutputCommand(){
 int MatrixOutputCommand::execute(){
        try {
                int count = 1;  
-               EstOutput data;
-               vector<SharedRAbundVector*> subset;
-               
+                               
                //if the users entered no valid calculators don't execute command
                if (matrixCalculators.size() == 0) { cout << "No valid calculators." << endl; return 0; }
 
@@ -91,62 +89,66 @@ int MatrixOutputCommand::execute(){
                        
                input = globaldata->ginput;
                lookup = input->getSharedRAbundVectors();
+               vector<SharedRAbundVector*> lastLookup = lookup;
+               
+               //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;
                                
                if (lookup.size() < 2) { cout << "You have not provided enough valid groups.  I cannot run the command." << endl; return 0;}
                
                numGroups = globaldata->Groups.size();
                                
-               while(lookup[0] != NULL){
+               //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))) {
                
                        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);
                                
-                               //for each calculator                                                                                           
-                               for(int i = 0 ; i < matrixCalculators.size(); i++) {
-                                       
-                                       //initialize simMatrix
-                                       simMatrix.clear();
-                                       simMatrix.resize(numGroups);
-                                       for (int m = 0; m < simMatrix.size(); m++)      {
-                                               for (int j = 0; j < simMatrix.size(); j++)      {
-                                                       simMatrix[m].push_back(0.0);
-                                               }
-                                       }
-                               
-                                       for (int k = 0; k < lookup.size(); k++) { 
-                                               for (int l = k; l < lookup.size(); l++) {
-                                                       if (k != l) { //we dont need to similiarity of a groups to itself
-                                                               //get estimated similarity between 2 groups
-                                                               
-                                                               subset.clear(); //clear out old pair of sharedrabunds
-                                                               //add new pair of sharedrabunds
-                                                               subset.push_back(lookup[k]); subset.push_back(lookup[l]); 
-                                                               
-                                                               data = matrixCalculators[i]->getValues(subset); //saves the calculator outputs
-                                                               //save values in similarity matrix
-                                                               simMatrix[k][l] = 1.0 - data[0];  //convert similiarity to distance
-                                                               simMatrix[l][k] = 1.0 - data[0];  //convert similiarity to distance
-                                                       }
-                                               }
-                                       }
-                                       
-                                       exportFileName = getRootName(globaldata->inputFileName) + matrixCalculators[i]->getName() + "." + lookup[0]->getLabel() + ".dist";
-                                       openOutputFile(exportFileName, out);
-                                       printSims(out);
-                                       out.close();
-                                       
-                               }
+                               processedLabels.insert(lookup[0]->getLabel());
+                               userLabels.erase(lookup[0]->getLabel());
                        }
                        
+                       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
-                       for (int i = 0; i < lookup.size(); i++) {       delete lookup[i];       }
+                       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("");
 
@@ -187,5 +189,61 @@ void MatrixOutputCommand::printSims(ostream& out) {
        }               
 }
 /***********************************************************/
+void MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
+       try {
+       
+                               EstOutput data;
+                               vector<SharedRAbundVector*> subset;
+
+                               //for each calculator                                                                                           
+                               for(int i = 0 ; i < matrixCalculators.size(); i++) {
+                                       
+                                       //initialize simMatrix
+                                       simMatrix.clear();
+                                       simMatrix.resize(numGroups);
+                                       for (int m = 0; m < simMatrix.size(); m++)      {
+                                               for (int j = 0; j < simMatrix.size(); j++)      {
+                                                       simMatrix[m].push_back(0.0);
+                                               }
+                                       }
+                               
+                                       for (int k = 0; k < thisLookup.size(); k++) { 
+                                               for (int l = k; l < thisLookup.size(); l++) {
+                                                       if (k != l) { //we dont need to similiarity of a groups to itself
+                                                               //get estimated similarity between 2 groups
+                                                               
+                                                               subset.clear(); //clear out old pair of sharedrabunds
+                                                               //add new pair of sharedrabunds
+                                                               subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]); 
+                                                               
+                                                               data = matrixCalculators[i]->getValues(subset); //saves the calculator outputs
+                                                               //save values in similarity matrix
+                                                               simMatrix[k][l] = 1.0 - data[0];  //convert similiarity to distance
+                                                               simMatrix[l][k] = 1.0 - data[0];  //convert similiarity to distance
+                                                       }
+                                               }
+                                       }
+                                       
+                                       exportFileName = getRootName(globaldata->inputFileName) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".dist";
+                                       openOutputFile(exportFileName, out);
+                                       printSims(out);
+                                       out.close();
+                                       
+                               }
+
+       
+               
+       }
+       catch(exception& e) {
+               cout << "Standard Error: " << e.what() << " has occurred in the MatrixOutputCommand class Function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+       catch(...) {
+               cout << "An unknown error has occurred in the MatrixOutputCommand class function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }               
+}
+/***********************************************************/
+