]> git.donarmstrong.com Git - mothur.git/blobdiff - libshuffcommand.cpp
added set.dir command and modified commands to redirect input and output, removed...
[mothur.git] / libshuffcommand.cpp
index 49a87674d17a743e533c1bd010f6a48518e19537..cfccdf6514297fe16af59234d94cdfd758b94c43 100644 (file)
 
 //**********************************************************************************************************************
 
-LibShuffCommand::LibShuffCommand(){
+LibShuffCommand::LibShuffCommand(string option){
        try {
-               srand( (unsigned)time( NULL ) );
-               
                globaldata = GlobalData::getInstance();
-               convert(globaldata->getCutOff(), cutOff);       //get the cutoff
-               convert(globaldata->getIters(), iters);         //get the number of iterations
-               convert(globaldata->getStep(), step);           //get the step size for the discrete command
-               matrix = globaldata->gMatrix;                           //get the distance matrix
-               setGroups();                                                            //set the groups to be analyzed
+               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","outputdir","inputdir"};
+                       vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+                       
+                       OptionParser parser(option);
+                       map<string, string> parameters = parser.getParameters();
+                       
+                       ValidParameters validParameter;
+               
+                       //check to make sure all parameters are valid for command
+                       for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
+                               if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
+                       }
+                       
+                       //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 += 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;; 
+                       }
+                                               
+                       //check for optional parameter and set defaults
+                       // ...at some point should added some additional type checking...
+                       groups = validParameter.validFile(parameters, "groups", false);                 
+                       if (groups == "not found") { groups = ""; savegroups = groups; }
+                       else { 
+                               savegroups = groups;
+                               splitAtDash(groups, Groups);
+                               globaldata->Groups = Groups;
+                       }
+                               
+                       string temp;
+                       temp = validParameter.validFile(parameters, "iters", false);                            if (temp == "not found") { temp = "10000"; }
+                       convert(temp, iters); 
+                       
+                       temp = validParameter.validFile(parameters, "cutoff", false);                           if (temp == "not found") { temp = "1.0"; }
+                       convert(temp, cutOff); 
+                       
+                       temp = validParameter.validFile(parameters, "step", false);                             if (temp == "not found") { temp = "0.01"; }
+                       convert(temp, step); 
+       
+                       userform = validParameter.validFile(parameters, "form", false);                 if (userform == "not found") { userform = "integral"; }
+                       
+                       if (abort == false) {
+               
+                               matrix = globaldata->gMatrix;                           //get the distance matrix
+                               setGroups();                                                            //set the groups to be analyzed and sorts them
+       
+                               /********************************************************************************************/
+                               //this is needed because when we read the matrix we sort it into groups in alphabetical order
+                               //the rest of the command and the classes used in this command assume specific order
+                               /********************************************************************************************/
+                               matrix->setGroups(globaldata->gGroupmap->namesOfGroups);
+                               vector<int> sizes;
+                               for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {   sizes.push_back(globaldata->gGroupmap->getNumSeqs(globaldata->gGroupmap->namesOfGroups[i]));  }
+                               matrix->setSizes(sizes);
+                       
 
-               if(globaldata->getForm() == "discrete"){
-                       form = new DLibshuff(matrix, iters, step, cutOff);
-               }
-               else{
-                       form = new SLibshuff(matrix, iters, cutOff);
+                               if(userform == "discrete"){
+                                       form = new DLibshuff(matrix, iters, step, cutOff);
+                               }
+                               else{
+                                       form = new SLibshuff(matrix, iters, cutOff);
+                               }
+                       }
+                       
                }
+               
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function LibShuffCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "LibShuffCommand", "LibShuffCommand");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the LibShuffCommand class function LibShuffCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+}
+//**********************************************************************************************************************
+
+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");
+       }
+       catch(exception& e) {
+               errorOut(e, "LibShuffCommand", "help");
                exit(1);
-       }       
-                       
+       }
 }
 
 //**********************************************************************************************************************
 
 int LibShuffCommand::execute(){
        try {
-
+               
+               if (abort == true) {    return 0;       }
+       
                savedDXYValues = form->evaluateAll();
                savedMinValues = form->getSavedMins();
-               
+       
                pValueCounts.resize(numGroups);
                for(int i=0;i<numGroups;i++){
                        pValueCounts[i].assign(numGroups, 0);
@@ -67,20 +151,23 @@ int LibShuffCommand::execute(){
                for(int i=0;i<numGroups-1;i++) {
                        for(int j=i+1;j<numGroups;j++) {
                                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++) {              
-                                       form->randomizeGroups(i,j);
-                                       if(form->evaluatePair(i,j) >= savedDXYValues[i][j])     {       pValueCounts[i][j]++;   }
-                                       if(form->evaluatePair(j,i) >= savedDXYValues[j][i])     {       pValueCounts[j][i]++;   }
+                                       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]++;   }
                                        reading->update(p);                     
                                }
-                               form->resetGroup(i);
-                               form->resetGroup(j);
+                               form->resetGroup(spoti);
+                               form->resetGroup(spotj);
                        }
                }
                reading->finish();
                delete reading;
 
-               cout << endl;
+               mothurOutEndLine();
                printSummaryFile();
                printCoverageFile();
                
@@ -88,16 +175,15 @@ int LibShuffCommand::execute(){
                globaldata->Groups.clear();
                delete form;
                
+               //delete globaldata's copy of the gmatrix to free up memory
+               delete globaldata->gMatrix;  globaldata->gMatrix = NULL;
+               
                return 0;
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "LibShuffCommand", "execute");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the LibShuffCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 //**********************************************************************************************************************
@@ -106,10 +192,10 @@ void LibShuffCommand::printCoverageFile() {
        try {
 
                ofstream outCov;
-               summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.coverage";
+               summaryFile = outputDir + getRootName(getSimpleName(globaldata->getPhylipFile())) + "libshuff.coverage";
                openOutputFile(summaryFile, outCov);
                outCov.setf(ios::fixed, ios::floatfield); outCov.setf(ios::showpoint);
-               cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
+               //cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
                
                map<double,vector<int> > allDistances;
                map<double,vector<int> >::iterator it;
@@ -122,25 +208,29 @@ void LibShuffCommand::printCoverageFile() {
                        indices[i].assign(numGroups,0);
                        for(int j=0;j<numGroups;j++){
                                indices[i][j] = index++;
-                               for(int k=0;k<savedMinValues[i][j].size();k++){
-                                       if(allDistances[savedMinValues[i][j][k]].size() != 0){
-                                               allDistances[savedMinValues[i][j][k]][indices[i][j]]++;
+                               
+                               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 k=0;k<savedMinValues[spoti][spotj].size();k++){
+                                       if(allDistances[savedMinValues[spoti][spotj][k]].size() != 0){
+                                               allDistances[savedMinValues[spoti][spotj][k]][indices[i][j]]++;
                                        }
                                        else{
-                                               allDistances[savedMinValues[i][j][k]].assign(numIndices, 0);
-                                               allDistances[savedMinValues[i][j][k]][indices[i][j]] = 1;
+                                               allDistances[savedMinValues[spoti][spotj][k]].assign(numIndices, 0);
+                                               allDistances[savedMinValues[spoti][spotj][k]][indices[i][j]] = 1;
                                        }
                                }
                        }
                }
                it=allDistances.begin();
                
-               cout << setprecision(8);
+               //cout << setprecision(8);
 
                vector<int> prevRow = it->second;
                it++;
                
-               for(it;it!=allDistances.end();it++){
+               for(;it!=allDistances.end();it++){
                        for(int i=0;i<it->second.size();i++){
                                it->second[i] += prevRow[i];
                        }
@@ -175,16 +265,12 @@ void LibShuffCommand::printCoverageFile() {
                        }
                        outCov << endl;
                }
-               
+               outCov.close();
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "LibShuffCommand", "printCoverageFile");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the LibShuffCommand class function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 } 
 
 //**********************************************************************************************************************
@@ -193,47 +279,51 @@ void LibShuffCommand::printSummaryFile() {
        try {
 
                ofstream outSum;
-               summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.summary";
+               summaryFile = outputDir + getRootName(getSimpleName(globaldata->getPhylipFile())) + "libshuff.summary";
                openOutputFile(summaryFile, outSum);
 
                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();
                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++){
+                               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[i][j] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
-                                       outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[i][j] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
+                                       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();
+                                       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[i][j] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
-                                       outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[i][j] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
+                                       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();
+                                       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[j][i] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
-                                       outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[j][i] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
+                                       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();
+                                       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[j][i] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
-                                       outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[j][i] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
+                                       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();
+                                       outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
                                }
                        }
                }
                
-               
+               outSum.close();
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "LibShuffCommand", "printSummaryFile");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the LibShuffCommand class function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 } 
 
 //**********************************************************************************************************************
@@ -247,13 +337,13 @@ void LibShuffCommand::setGroups() {
                                globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
                        }
                } else {
-                       if (globaldata->getGroups() != "all") {
+                       if (savegroups != "all") {
                                //check that groups are valid
                                for (int i = 0; i < globaldata->Groups.size(); i++) {
                                        if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
-                                               cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
+                                               mothurOut(globaldata->Groups[i] + " is not a valid group, and will be disregarded."); mothurOutEndLine();
                                                // erase the invalid group from globaldata->Groups
-                                               globaldata->Groups.erase (globaldata->Groups.begin()+i);
+                                               globaldata->Groups.erase(globaldata->Groups.begin()+i);
                                        }
                                }
                        
@@ -263,7 +353,7 @@ void LibShuffCommand::setGroups() {
                                        for (int i=0; i < numGroups; i++) { 
                                                globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
                                        }
-                                       cout << "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." << endl; 
+                                       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();
                                } else { numGroups = globaldata->Groups.size(); }
                        } else { //users wants all groups
                                numGroups = globaldata->gGroupmap->getNumGroups();
@@ -279,23 +369,14 @@ void LibShuffCommand::setGroups() {
                
                //sort
                sort(globaldata->gGroupmap->namesOfGroups.begin(), globaldata->gGroupmap->namesOfGroups.end());
+               
+               for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {  globaldata->gGroupmap->groupIndex[globaldata->gGroupmap->namesOfGroups[i]] = i;  }
 
                groupNames = globaldata->Groups;
 
-               // number of comparisons i.e. with groups A,B,C = AA, AB, AC, BA, BB, BC...;
-//             for (int i=0; i<numGroups; i++) { 
-//                     for (int l = 0; l < numGroups; l++) {
-//                             //set group comparison labels
-//                             groupComb.push_back(globaldata->Groups[i] + "-" + globaldata->Groups[l]);
-//                     }
-//             }
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       catch(...) {
-               cout << "An unknown error has occurred in the LibShuffCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "LibShuffCommand", "setGroups");
                exit(1);
        }
 }