]> git.donarmstrong.com Git - mothur.git/blobdiff - matrixoutputcommand.cpp
added pipeline commands which involved change to command factory and command class...
[mothur.git] / matrixoutputcommand.cpp
index 5069e0f07684391033c7c1e2b7595dca9b18e30f..020ccff57c813168625132e83c9c198eb113a121 100644 (file)
 #include "sharedmorisitahorn.h"
 #include "sharedbraycurtis.h"
 
-
+//**********************************************************************************************************************
+vector<string> MatrixOutputCommand::getValidParameters(){      
+       try {
+               string Array[] =  {"label","calc","groups","outputdir","inputdir", "output"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "MatrixOutputCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+MatrixOutputCommand::MatrixOutputCommand(){    
+       try {
+               //initialize outputTypes
+               vector<string> tempOutNames;
+               outputTypes["phylip"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> MatrixOutputCommand::getRequiredParameters(){   
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "MatrixOutputCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> MatrixOutputCommand::getRequiredFiles(){        
+       try {
+               string Array[] =  {"shared"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "MatrixOutputCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************
 
-MatrixOutputCommand::MatrixOutputCommand(string option){
+MatrixOutputCommand::MatrixOutputCommand(string option)  {
        try {
                globaldata = GlobalData::getInstance();
                abort = false;
@@ -36,7 +82,7 @@ MatrixOutputCommand::MatrixOutputCommand(string option){
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"label","calc","groups"};
+                       string Array[] =  {"label","calc","groups","outputdir","inputdir", "output"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -49,10 +95,20 @@ MatrixOutputCommand::MatrixOutputCommand(string option){
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["phylip"] = tempOutNames;
+                       
+                       //if the user changes the output directory command factory will send this info to us in the output parameter 
+                       outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
+                               outputDir = ""; 
+                               outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it       
+                       }
+                       
                        //make sure the user has already run the read.otu command
                        if (globaldata->getSharedFile() == "") {
-                               if (globaldata->getListFile() == "") { mothurOut("You must read a list and a group, or a shared before you can use the dist.shared command."); mothurOutEndLine(); abort = true; }
-                               else if (globaldata->getGroupFile() == "") { mothurOut("You must read a list and a group, or a shared before you can use the dist.shared command."); mothurOutEndLine(); abort = true; }
+                               if (globaldata->getListFile() == "") { m->mothurOut("You must read a list and a group, or a shared before you can use the dist.shared command."); m->mothurOutEndLine(); abort = true; }
+                               else if (globaldata->getGroupFile() == "") { m->mothurOut("You must read a list and a group, or a shared before you can use the dist.shared command."); m->mothurOutEndLine(); abort = true; }
                        }
                        
                        //check for optional parameter and set defaults
@@ -60,10 +116,13 @@ MatrixOutputCommand::MatrixOutputCommand(string option){
                        label = validParameter.validFile(parameters, "label", false);                   
                        if (label == "not found") { label = ""; }
                        else { 
-                               if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
+                               if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
                                else { allLines = 1;  }
                        }
                        
+                       output = validParameter.validFile(parameters, "output", false);         if(output == "not found"){      output = "lt"; }
+                       if ((output != "lt") && (output != "square")) { m->mothurOut(output + " is not a valid output form. Options are lt and square. I will use lt."); m->mothurOutEndLine(); output = "lt"; }
+                       
                        //if the user has not specified any labels use the ones from read.otu
                        if (label == "") {  
                                allLines = globaldata->allLines; 
@@ -73,7 +132,7 @@ MatrixOutputCommand::MatrixOutputCommand(string option){
                        groups = validParameter.validFile(parameters, "groups", false);                 
                        if (groups == "not found") { groups = ""; }
                        else { 
-                               splitAtDash(groups, Groups);
+                               m->splitAtDash(groups, Groups);
                                globaldata->Groups = Groups;
                        }
                                
@@ -82,7 +141,7 @@ MatrixOutputCommand::MatrixOutputCommand(string option){
                        else { 
                                 if (calc == "default")  {  calc = "jclass-thetayc";  }
                        }
-                       splitAtDash(calc, Estimators);
+                       m->splitAtDash(calc, Estimators);
 
                        if (abort == false) {
                        
@@ -120,7 +179,7 @@ MatrixOutputCommand::MatrixOutputCommand(string option){
                
        }
        catch(exception& e) {
-               errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
+               m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
                exit(1);
        }
 }
@@ -129,20 +188,21 @@ MatrixOutputCommand::MatrixOutputCommand(string option){
 
 void MatrixOutputCommand::help(){
        try {
-               mothurOut("The dist.shared command can only be executed after a successful read.otu command.\n");
-               mothurOut("The dist.shared command parameters are groups, calc and label.  No parameters are required.\n");
-               mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n");
-               mothurOut("The group names are separated by dashes. The label parameter allows you to select what distance levels you would like distance matrices created for, and is also separated by dashes.\n");
-               mothurOut("The dist.shared command should be in the following format: dist.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n");
-               mothurOut("Example dist.shared(groups=A-B-C, calc=jabund-sorabund).\n");
-               mothurOut("The default value for groups is all the groups in your groupfile.\n");
-               mothurOut("The default value for calc is jclass and thetayc.\n");
+               m->mothurOut("The dist.shared command can only be executed after a successful read.otu command.\n");
+               m->mothurOut("The dist.shared command parameters are groups, calc, output and label.  No parameters are required.\n");
+               m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n");
+               m->mothurOut("The group names are separated by dashes. The label parameter allows you to select what distance levels you would like distance matrices created for, and is also separated by dashes.\n");
+               m->mothurOut("The dist.shared command should be in the following format: dist.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n");
+               m->mothurOut("The output parameter allows you to specify format of your distance matrix. Options are lt, and square. The default is lt.\n");
+               m->mothurOut("Example dist.shared(groups=A-B-C, calc=jabund-sorabund).\n");
+               m->mothurOut("The default value for groups is all the groups in your groupfile.\n");
+               m->mothurOut("The default value for calc is jclass and thetayc.\n");
                validCalculator->printCalc("matrix", cout);
-               mothurOut("The dist.shared command outputs a .dist file for each calculator you specify at each distance you choose.\n");
-               mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
+               m->mothurOut("The dist.shared command outputs a .dist file for each calculator you specify at each distance you choose.\n");
+               m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
        }
        catch(exception& e) {
-               errorOut(e, "MatrixOutputCommand", "help");
+               m->errorOut(e, "MatrixOutputCommand", "help");
                exit(1);
        }
 }
@@ -166,7 +226,7 @@ int MatrixOutputCommand::execute(){
                if (abort == true) {    return 0;       }
                        
                //if the users entered no valid calculators don't execute command
-               if (matrixCalculators.size() == 0) { mothurOut("No valid calculators."); mothurOutEndLine();  return 0; }
+               if (matrixCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine();  return 0; }
 
                //you have groups
                read = new ReadOTUFile(globaldata->inputFileName);      
@@ -180,28 +240,32 @@ int MatrixOutputCommand::execute(){
                set<string> processedLabels;
                set<string> userLabels = labels;
                                        
-               if (lookup.size() < 2) { mothurOut("You have not provided enough valid groups.  I cannot run the command."); mothurOutEndLine(); return 0;}
+               if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups.  I cannot run the command."); m->mothurOutEndLine(); return 0;}
                
                numGroups = lookup.size();
+               
+               if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } globaldata->Groups.clear(); return 0;  }
                                
                //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))) {
                
+                       if (m->control_pressed) { outputTypes.clear(); delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str()); } globaldata->Groups.clear(); return 0;  }
+               
                        if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
-                               mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
+                               m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
                                process(lookup);
                                
                                processedLabels.insert(lookup[0]->getLabel());
                                userLabels.erase(lookup[0]->getLabel());
                        }
                        
-                       if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
+                       if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
                                string saveLabel = lookup[0]->getLabel();
                                
                                for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
                                lookup = input->getSharedRAbundVectors(lastLabel);
 
-                               mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
+                               m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
                                process(lookup);
                                
                                processedLabels.insert(lookup[0]->getLabel());
@@ -218,37 +282,48 @@ int MatrixOutputCommand::execute(){
                        lookup = input->getSharedRAbundVectors();
                }
                
+               if (m->control_pressed) { outputTypes.clear(); delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str()); } globaldata->Groups.clear(); 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++) {  
-                       mothurOut("Your file does not include the label " + *it);  
+                       m->mothurOut("Your file does not include the label " + *it);  
                        if (processedLabels.count(lastLabel) != 1) {
-                               mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
+                               m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
                                needToRun = true;
                        }else {
-                               mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
+                               m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
                        }
                }
                
+               if (m->control_pressed) { outputTypes.clear(); delete read; delete input; globaldata->ginput = NULL;  for (int i = 0; i < outputNames.size(); i++) {    remove(outputNames[i].c_str()); } globaldata->Groups.clear(); return 0;  }
+
                //run last label if you need to
                if (needToRun == true)  {
                        for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {  delete lookup[i]; }  } 
                        lookup = input->getSharedRAbundVectors(lastLabel);
 
-                       mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
+                       m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
                        process(lookup);
                        for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
                }
                
-                               
+               if (m->control_pressed) { outputTypes.clear();  delete read; delete input; globaldata->ginput = NULL;  for (int i = 0; i < outputNames.size(); i++) {   remove(outputNames[i].c_str()); } globaldata->Groups.clear(); return 0;  }
+               
                //reset groups parameter
                globaldata->Groups.clear();  
+               
+               m->mothurOutEndLine();
+               m->mothurOut("Output File Names: "); m->mothurOutEndLine();
+               for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
+               m->mothurOutEndLine();
+
 
                return 0;
        }
        catch(exception& e) {
-               errorOut(e, "MatrixOutputCommand", "execute");
+               m->errorOut(e, "MatrixOutputCommand", "execute");
                exit(1);
        }
 }
@@ -256,25 +331,36 @@ int MatrixOutputCommand::execute(){
 void MatrixOutputCommand::printSims(ostream& out) {
        try {
                
-               //output column headers
+               out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
+               
+               //output num seqs
                out << simMatrix.size() << endl;
                
-               for (int m = 0; m < simMatrix.size(); m++)      {
-                       out << lookup[m]->getGroup() << '\t';
-                       for (int n = 0; n < m; n++)     {
-                               out << simMatrix[m][n] << '\t'; 
+               if (output == "lt") {
+                       for (int m = 0; m < simMatrix.size(); m++)      {
+                               out << lookup[m]->getGroup() << '\t';
+                               for (int n = 0; n < m; n++)     {
+                                       out << simMatrix[m][n] << '\t'; 
+                               }
+                               out << endl;
+                       }
+               }else{
+                       for (int m = 0; m < simMatrix.size(); m++)      {
+                               out << lookup[m]->getGroup() << '\t';
+                               for (int n = 0; n < simMatrix[m].size(); n++)   {
+                                       out << simMatrix[m][n] << '\t'; 
+                               }
+                               out << endl;
                        }
-                       out << endl;
                }
-
        }
        catch(exception& e) {
-               errorOut(e, "MatrixOutputCommand", "printSims");
+               m->errorOut(e, "MatrixOutputCommand", "printSims");
                exit(1);
        }
 }
 /***********************************************************/
-void MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
+int MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
        try {
        
                                EstOutput data;
@@ -286,9 +372,9 @@ void MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
                                        //initialize simMatrix
                                        simMatrix.clear();
                                        simMatrix.resize(numGroups);
-                                       for (int m = 0; m < simMatrix.size(); m++)      {
+                                       for (int p = 0; p < simMatrix.size(); p++)      {
                                                for (int j = 0; j < simMatrix.size(); j++)      {
-                                                       simMatrix[m].push_back(0.0);
+                                                       simMatrix[p].push_back(0.0);
                                                }
                                        }
                                
@@ -297,6 +383,8 @@ void MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
                                                        if (k != l) { //we dont need to similiarity of a groups to itself
                                                                //get estimated similarity between 2 groups
                                                                
+                                                               if (m->control_pressed) { return 0; }
+                                                               
                                                                subset.clear(); //clear out old pair of sharedrabunds
                                                                //add new pair of sharedrabunds
                                                                subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]); 
@@ -309,18 +397,20 @@ void MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
                                                }
                                        }
                                        
-                                       exportFileName = getRootName(globaldata->inputFileName) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".dist";
-                                       openOutputFile(exportFileName, out);
+                                       exportFileName = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + "." + output + ".dist";
+                                       m->openOutputFile(exportFileName, out);
+                                       outputNames.push_back(exportFileName); outputTypes["phylip"].push_back(exportFileName);
+                                       
                                        printSims(out);
                                        out.close();
                                        
                                }
 
-       
+                               return 0;
                
        }
        catch(exception& e) {
-               errorOut(e, "MatrixOutputCommand", "process");
+               m->errorOut(e, "MatrixOutputCommand", "process");
                exit(1);
        }
 }