]> git.donarmstrong.com Git - mothur.git/blobdiff - libshuffcommand.cpp
fixed cluster.classic and added weighted method to hcluster
[mothur.git] / libshuffcommand.cpp
index 74acd841b676acc429df6df4e303210d82ae92ba..e7fb357b9bd3028fcbd32bf388af9bbb9b4c2847 100644 (file)
 #include "slibshuff.h"
 #include "dlibshuff.h"
 
+//**********************************************************************************************************************
+vector<string> LibShuffCommand::getValidParameters(){  
+       try {
+               string Array[] =  {"iters","groups","step","form","cutoff","outputdir","inputdir"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "LibShuffCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+LibShuffCommand::LibShuffCommand(){    
+       try {
+               abort = true;
+               //initialize outputTypes
+               vector<string> tempOutNames;
+               outputTypes["coverage"] = tempOutNames;
+               outputTypes["libshuffsummary"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "LibShuffCommand", "LibShuffCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> LibShuffCommand::getRequiredParameters(){       
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "LibShuffCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> LibShuffCommand::getRequiredFiles(){    
+       try {
+               string Array[] =  {"phylip","group"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "LibShuffCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************
 
-LibShuffCommand::LibShuffCommand(string option){
+LibShuffCommand::LibShuffCommand(string option)  {
        try {
                globaldata = GlobalData::getInstance();
                abort = false;
                Groups.clear();
                
-               
                //allow user to run help
                if(option == "help") { help(); abort = true; }
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"iters","groups","step","form","cutoff"};
+                       string Array[] =  {"iters","groups","step","form","cutoff","outputdir","inputdir"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -45,9 +93,20 @@ LibShuffCommand::LibShuffCommand(string option){
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["coverage"] = tempOutNames;
+                       outputTypes["libshuffsummary"] = 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->getPhylipFile()); //if user entered a file with a path then preserve it     
+                       }
+                       
                        //make sure the user has already run the read.dist command
                        if ((globaldata->gMatrix == NULL) || (globaldata->gGroupmap == NULL)) {
-                               mothurOut("You must read in a matrix and groupfile using the read.dist command, before you use the libshuff command. "); mothurOutEndLine(); abort = true;; 
+                               m->mothurOut("You must read in a matrix and groupfile using the read.dist command, before you use the libshuff command. "); m->mothurOutEndLine(); abort = true;; 
                        }
                                                
                        //check for optional parameter and set defaults
@@ -56,7 +115,7 @@ LibShuffCommand::LibShuffCommand(string option){
                        if (groups == "not found") { groups = ""; savegroups = groups; }
                        else { 
                                savegroups = groups;
-                               splitAtDash(groups, Groups);
+                               m->splitAtDash(groups, Groups);
                                globaldata->Groups = Groups;
                        }
                                
@@ -99,7 +158,7 @@ LibShuffCommand::LibShuffCommand(string option){
                
        }
        catch(exception& e) {
-               errorOut(e, "LibShuffCommand", "LibShuffCommand");
+               m->errorOut(e, "LibShuffCommand", "LibShuffCommand");
                exit(1);
        }
 }
@@ -107,20 +166,20 @@ LibShuffCommand::LibShuffCommand(string option){
 
 void LibShuffCommand::help(){
        try {
-               mothurOut("The libshuff command can only be executed after a successful read.dist command including a groupfile.\n");
-               mothurOut("The libshuff command parameters are groups, iters, step, form and cutoff.  No parameters are required.\n");
-               mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed.  You must enter at least 2 valid groups.\n");
-               mothurOut("The group names are separated by dashes.  The iters parameter allows you to specify how many random matrices you would like compared to your matrix.\n");
-               mothurOut("The step parameter allows you to specify change in distance you would like between each output if you are using the discrete form.\n");
-               mothurOut("The form parameter allows you to specify if you would like to analyze your matrix using the discrete or integral form. Your options are integral or discrete.\n");
-               mothurOut("The libshuff command should be in the following format: libshuff(groups=yourGroups, iters=yourIters, cutOff=yourCutOff, form=yourForm, step=yourStep).\n");
-               mothurOut("Example libshuff(groups=A-B-C, iters=500, form=discrete, step=0.01, cutOff=2.0).\n");
-               mothurOut("The default value for groups is all the groups in your groupfile, iters is 10000, cutoff is 1.0, form is integral and step is 0.01.\n");
-               mothurOut("The libshuff command output two files: .coverage and .slsummary their descriptions are in the manual.\n");
-               mothurOut("Note: No spaces between parameter labels (i.e. iters), '=' and parameters (i.e.yourIters).\n\n");
+               m->mothurOut("The libshuff command can only be executed after a successful read.dist command including a groupfile.\n");
+               m->mothurOut("The libshuff command parameters are groups, iters, step, form and cutoff.  No parameters are required.\n");
+               m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed.  You must enter at least 2 valid groups.\n");
+               m->mothurOut("The group names are separated by dashes.  The iters parameter allows you to specify how many random matrices you would like compared to your matrix.\n");
+               m->mothurOut("The step parameter allows you to specify change in distance you would like between each output if you are using the discrete form.\n");
+               m->mothurOut("The form parameter allows you to specify if you would like to analyze your matrix using the discrete or integral form. Your options are integral or discrete.\n");
+               m->mothurOut("The libshuff command should be in the following format: libshuff(groups=yourGroups, iters=yourIters, cutOff=yourCutOff, form=yourForm, step=yourStep).\n");
+               m->mothurOut("Example libshuff(groups=A-B-C, iters=500, form=discrete, step=0.01, cutOff=2.0).\n");
+               m->mothurOut("The default value for groups is all the groups in your groupfile, iters is 10000, cutoff is 1.0, form is integral and step is 0.01.\n");
+               m->mothurOut("The libshuff command output two files: .coverage and .slsummary their descriptions are in the manual.\n");
+               m->mothurOut("Note: No spaces between parameter labels (i.e. iters), '=' and parameters (i.e.yourIters).\n\n");
        }
        catch(exception& e) {
-               errorOut(e, "LibShuffCommand", "help");
+               m->errorOut(e, "LibShuffCommand", "help");
                exit(1);
        }
 }
@@ -134,37 +193,53 @@ int LibShuffCommand::execute(){
        
                savedDXYValues = form->evaluateAll();
                savedMinValues = form->getSavedMins();
+               
+               if (m->control_pressed) {  delete form; globaldata->Groups.clear(); delete globaldata->gMatrix;  globaldata->gMatrix = NULL; return 0; }
        
                pValueCounts.resize(numGroups);
                for(int i=0;i<numGroups;i++){
                        pValueCounts[i].assign(numGroups, 0);
                }
-               
+       
+               if (m->control_pressed) {  outputTypes.clear(); delete form; globaldata->Groups.clear(); delete globaldata->gMatrix;  globaldata->gMatrix = NULL; return 0; }
+                               
                Progress* reading = new Progress();
                
                for(int i=0;i<numGroups-1;i++) {
                        for(int j=i+1;j<numGroups;j++) {
+                               
+                               if (m->control_pressed) {  outputTypes.clear();  delete form; globaldata->Groups.clear(); delete globaldata->gMatrix;  globaldata->gMatrix = NULL; delete reading; return 0; }
+
                                reading->newLine(groupNames[i]+'-'+groupNames[j], iters);
                                int spoti = globaldata->gGroupmap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
                                int spotj = globaldata->gGroupmap->groupIndex[groupNames[j]];
        
-                               for(int p=0;p<iters;p++) {              
+                               for(int p=0;p<iters;p++) {      
+                                       
+                                       if (m->control_pressed) {  outputTypes.clear(); delete form; globaldata->Groups.clear(); delete globaldata->gMatrix;  globaldata->gMatrix = NULL; delete reading; return 0; }
+                                       
                                        form->randomizeGroups(spoti,spotj); 
                                        if(form->evaluatePair(spoti,spotj) >= savedDXYValues[spoti][spotj])     {       pValueCounts[i][j]++;   }
                                        if(form->evaluatePair(spotj,spoti) >= savedDXYValues[spotj][spoti])     {       pValueCounts[j][i]++;   }
+                                       
+                                       if (m->control_pressed) {  outputTypes.clear(); delete form; globaldata->Groups.clear(); delete globaldata->gMatrix;  globaldata->gMatrix = NULL; delete reading; return 0; }
+                                       
                                        reading->update(p);                     
                                }
                                form->resetGroup(spoti);
                                form->resetGroup(spotj);
                        }
                }
+               
+               if (m->control_pressed) { outputTypes.clear();  delete form; globaldata->Groups.clear(); delete globaldata->gMatrix;  globaldata->gMatrix = NULL; delete reading; return 0; }
+       
                reading->finish();
                delete reading;
 
-               mothurOutEndLine();
+               m->mothurOutEndLine();
                printSummaryFile();
                printCoverageFile();
-               
+                               
                //clear out users groups
                globaldata->Groups.clear();
                delete form;
@@ -172,22 +247,31 @@ int LibShuffCommand::execute(){
                //delete globaldata's copy of the gmatrix to free up memory
                delete globaldata->gMatrix;  globaldata->gMatrix = NULL;
                
+               if (m->control_pressed) {  outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); } return 0; }
+
+               
+               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, "LibShuffCommand", "execute");
+               m->errorOut(e, "LibShuffCommand", "execute");
                exit(1);
        }
 }
 
 //**********************************************************************************************************************
 
-void LibShuffCommand::printCoverageFile() {
+int LibShuffCommand::printCoverageFile() {
        try {
 
                ofstream outCov;
-               summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.coverage";
-               openOutputFile(summaryFile, outCov);
+               summaryFile = outputDir + m->getRootName(m->getSimpleName(globaldata->getPhylipFile())) + "libshuff.coverage";
+               m->openOutputFile(summaryFile, outCov);
+               outputNames.push_back(summaryFile); outputTypes["coverage"].push_back(summaryFile);
                outCov.setf(ios::fixed, ios::floatfield); outCov.setf(ios::showpoint);
                //cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
                
@@ -207,6 +291,9 @@ void LibShuffCommand::printCoverageFile() {
                                int spotj = globaldata->gGroupmap->groupIndex[groupNames[j]];
                                
                                for(int k=0;k<savedMinValues[spoti][spotj].size();k++){
+                                       
+                                       if(m->control_pressed)  { outCov.close(); return 0; }
+                                       
                                        if(allDistances[savedMinValues[spoti][spotj][k]].size() != 0){
                                                allDistances[savedMinValues[spoti][spotj][k]][indices[i][j]]++;
                                        }
@@ -240,6 +327,7 @@ void LibShuffCommand::printCoverageFile() {
                }
                for (int i=0;i<numGroups;i++){
                        for(int j=i+1;j<numGroups;j++){
+                               if(m->control_pressed)  { outCov.close(); return 0; }
                                outCov << '\t' << groupNames[i] << '-' << groupNames[j] << '\t';
                                outCov << groupNames[j] << '-' << groupNames[i];
                        }
@@ -253,6 +341,8 @@ void LibShuffCommand::printCoverageFile() {
                        }
                        for(int i=0;i<numGroups;i++){
                                for(int j=i+1;j<numGroups;j++){
+                                       if(m->control_pressed)  { outCov.close(); return 0; }
+                                       
                                        outCov << it->second[indices[i][j]]/(float)lastRow[indices[i][j]] << '\t';
                                        outCov << it->second[indices[j][i]]/(float)lastRow[indices[j][i]] << '\t';
                                }
@@ -260,62 +350,68 @@ void LibShuffCommand::printCoverageFile() {
                        outCov << endl;
                }
                outCov.close();
+               
+               return 0;
        }
        catch(exception& e) {
-               errorOut(e, "LibShuffCommand", "printCoverageFile");
+               m->errorOut(e, "LibShuffCommand", "printCoverageFile");
                exit(1);
        }
 } 
 
 //**********************************************************************************************************************
 
-void LibShuffCommand::printSummaryFile() {
+int LibShuffCommand::printSummaryFile() {
        try {
 
                ofstream outSum;
-               summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.summary";
-               openOutputFile(summaryFile, outSum);
+               summaryFile = outputDir + m->getRootName(m->getSimpleName(globaldata->getPhylipFile())) + "libshuff.summary";
+               m->openOutputFile(summaryFile, outSum);
+               outputNames.push_back(summaryFile); outputTypes["libshuffsummary"].push_back(summaryFile);
 
                outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
                cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
                
                cout << setw(20) << left << "Comparison" << '\t' << setprecision(8) << "dCXYScore" << '\t' << "Significance" << endl;
-               mothurOutJustToLog("Comparison\tdCXYScore\tSignificance"); mothurOutEndLine();
+               m->mothurOutJustToLog("Comparison\tdCXYScore\tSignificance"); m->mothurOutEndLine();
                outSum << setw(20) << left << "Comparison" << '\t' << setprecision(8) << "dCXYScore" << '\t' << "Significance" << endl;
        
                int precision = (int)log10(iters);
                for(int i=0;i<numGroups;i++){
                        for(int j=i+1;j<numGroups;j++){
+                               if(m->control_pressed)  { outSum.close(); return 0; }
+                               
                                int spoti = globaldata->gGroupmap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
                                int spotj = globaldata->gGroupmap->groupIndex[groupNames[j]];
                                
                                if(pValueCounts[i][j]){
                                        cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
-                                       mothurOutJustToLog(groupNames[i]+"-"+groupNames[j] + "\t" + toString(savedDXYValues[spoti][spotj]) + "\t" + toString((pValueCounts[i][j]/(float)iters))); mothurOutEndLine();
+                                       m->mothurOutJustToLog(groupNames[i]+"-"+groupNames[j] + "\t" + toString(savedDXYValues[spoti][spotj]) + "\t" + toString((pValueCounts[i][j]/(float)iters))); m->mothurOutEndLine();
                                        outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
                                }
                                else{
                                        cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
-                                       mothurOutJustToLog(groupNames[i]+"-"+groupNames[j] + "\t" + toString(savedDXYValues[spoti][spotj]) + "\t" + toString((1/(float)iters))); mothurOutEndLine();
+                                       m->mothurOutJustToLog(groupNames[i]+"-"+groupNames[j] + "\t" + toString(savedDXYValues[spoti][spotj]) + "\t" + toString((1/(float)iters))); m->mothurOutEndLine();
                                        outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
                                }
                                if(pValueCounts[j][i]){
                                        cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
-                                       mothurOutJustToLog(groupNames[j]+"-"+groupNames[i] + "\t" + toString(savedDXYValues[spotj][spoti]) + "\t" + toString((pValueCounts[j][i]/(float)iters))); mothurOutEndLine();
+                                       m->mothurOutJustToLog(groupNames[j]+"-"+groupNames[i] + "\t" + toString(savedDXYValues[spotj][spoti]) + "\t" + toString((pValueCounts[j][i]/(float)iters))); m->mothurOutEndLine();
                                        outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
                                }
                                else{
                                        cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
-                                       mothurOutJustToLog(groupNames[j]+"-"+groupNames[i] + "\t" + toString(savedDXYValues[spotj][spoti]) + "\t" + toString((1/(float)iters))); mothurOutEndLine();
+                                       m->mothurOutJustToLog(groupNames[j]+"-"+groupNames[i] + "\t" + toString(savedDXYValues[spotj][spoti]) + "\t" + toString((1/(float)iters))); m->mothurOutEndLine();
                                        outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
                                }
                        }
                }
                
                outSum.close();
+               return 0;
        }
        catch(exception& e) {
-               errorOut(e, "LibShuffCommand", "printSummaryFile");
+               m->errorOut(e, "LibShuffCommand", "printSummaryFile");
                exit(1);
        }
 } 
@@ -335,7 +431,7 @@ void LibShuffCommand::setGroups() {
                                //check that groups are valid
                                for (int i = 0; i < globaldata->Groups.size(); i++) {
                                        if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
-                                               mothurOut(globaldata->Groups[i] + " is not a valid group, and will be disregarded."); mothurOutEndLine();
+                                               m->mothurOut(globaldata->Groups[i] + " is not a valid group, and will be disregarded."); m->mothurOutEndLine();
                                                // erase the invalid group from globaldata->Groups
                                                globaldata->Groups.erase(globaldata->Groups.begin()+i);
                                        }
@@ -347,7 +443,7 @@ void LibShuffCommand::setGroups() {
                                        for (int i=0; i < numGroups; i++) { 
                                                globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
                                        }
-                                       mothurOut("When using the groups parameter you must have at least 2 valid groups. I will run the command using all the groups in your groupfile."); mothurOutEndLine();
+                                       m->mothurOut("When using the groups parameter you must have at least 2 valid groups. I will run the command using all the groups in your groupfile."); m->mothurOutEndLine();
                                } else { numGroups = globaldata->Groups.size(); }
                        } else { //users wants all groups
                                numGroups = globaldata->gGroupmap->getNumGroups();
@@ -370,7 +466,7 @@ void LibShuffCommand::setGroups() {
 
        }
        catch(exception& e) {
-               errorOut(e, "LibShuffCommand", "setGroups");
+               m->errorOut(e, "LibShuffCommand", "setGroups");
                exit(1);
        }
 }