]> git.donarmstrong.com Git - mothur.git/blobdiff - mergefilecommand.cpp
changing command name classify.shared to classifyrf.shared
[mothur.git] / mergefilecommand.cpp
index 3342c489d9f54a55abda3bac79b60511402eaf4c..643cf287bb9aded0847390aeea1091c12e95dd74 100644 (file)
@@ -9,20 +9,63 @@
 
 #include "mergefilecommand.h"
 
+//**********************************************************************************************************************
+vector<string> MergeFileCommand::setParameters(){      
+       try {
+               CommandParameter pinput("input", "String", "", "", "", "", "","",false,true,true); parameters.push_back(pinput);
+               CommandParameter poutput("output", "String", "", "", "", "", "","",false,true,true); parameters.push_back(poutput);
+               CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
+               CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
+               
+               vector<string> myArray;
+               for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "MergeFileCommand", "setParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+string MergeFileCommand::getHelpString(){      
+       try {
+               string helpString = "";
+               helpString += "The merge.file command takes a list of files separated by dashes and merges them into one file."; 
+               helpString += "The merge.file command parameters are input and output."; 
+               helpString += "Example merge.file(input=small.fasta-large.fasta, output=all.fasta).";
+               helpString += "Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputFileName).\n";
+               return helpString;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "MergeFileCommand", "getHelpString");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+MergeFileCommand::MergeFileCommand(){  
+       try {
+               abort = true; calledHelp = true; 
+               setParameters();
+               vector<string> tempOutNames;
+               outputTypes["merge"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************
 
-MergeFileCommand::MergeFileCommand(string option){
+MergeFileCommand::MergeFileCommand(string option)  {
        try {
-               abort = false;
+               abort = false; calledHelp = false;   
                
                if(option == "help") {
                        help();
-                       abort = true; 
-               }
+                       abort = true; calledHelp = true;
+               }else if(option == "citation") { citation(); abort = true; calledHelp = true;}
                else {
-                       //valid paramters for this command
-                       string Array[] =  {"input", "output"};
-                       vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+                       vector<string> myArray = setParameters();
                        
                        OptionParser parser(option);
                        map<string,string> parameters = parser.getParameters();
@@ -34,88 +77,73 @@ MergeFileCommand::MergeFileCommand(string option){
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["merge"] = tempOutNames;
+                       
+                       //if the user changes the input directory command factory will send this info to us in the output parameter 
+                       string inputDir = validParameter.validFile(parameters, "inputdir", false);              
+                       if (inputDir == "not found"){   inputDir = "";          }
+                       
                        string fileList = validParameter.validFile(parameters, "input", false);                 
-                       if(fileList == "not found") { cout << "you must enter two or more file names" << endl;  abort=true;  }
-                       else{   splitAtDash(fileList, fileNames);       }
+                       if(fileList == "not found") { m->mothurOut("you must enter two or more file names"); m->mothurOutEndLine();  abort=true;  }
+                       else{   m->splitAtDash(fileList, fileNames);    }
+                       
+                       //if the user changes the output directory command factory will send this info to us in the output parameter 
+                       string outputDir = validParameter.validFile(parameters, "outputdir", false);            if (outputDir == "not found")   {       outputDir = "";         }
+                       
                        
                        numInputFiles = fileNames.size();
                        ifstream testFile;
                        if(numInputFiles == 0){
-                               cout << "you must enter two or more file names and you entered " << fileNames.size() <<  " file names" << endl;
+                               m->mothurOut("you must enter two or more file names and you entered " + toString(fileNames.size()) +  " file names"); m->mothurOutEndLine();
                                abort=true;  
                        }
                        else{
                                for(int i=0;i<numInputFiles;i++){
-                                       if(openInputFile(fileNames[i], testFile)){      abort = true;   }
+                                       if (inputDir != "") {
+                                               string path = m->hasPath(fileNames[i]);
+                                               //if the user has not given a path then, add inputdir. else leave path alone.
+                                               if (path == "") {       fileNames[i] = inputDir + fileNames[i];         }
+                                       }
+                                       
+                                       if(m->openInputFile(fileNames[i], testFile)){   abort = true;   }
                                        testFile.close();
                                }
                        }   
                        
                        outputFileName = validParameter.validFile(parameters, "output", false);                 
-                       if (outputFileName == "not found") { cout << "you must enter an output file name" << endl;  abort=true;  }
+                       if (outputFileName == "not found") { m->mothurOut("you must enter an output file name"); m->mothurOutEndLine();  abort=true;  }
+                       else if (outputDir != "") { outputFileName = outputDir + m->getSimpleName(outputFileName);  }
                }
                        
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the MergeFileCommand class Function MergeFileCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the MergeFileCommand class function MergeFileCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
-
-//**********************************************************************************************************************
-
-MergeFileCommand::~MergeFileCommand()  {       /*      do nothing      */      }
-
 //**********************************************************************************************************************
 
 int MergeFileCommand::execute(){
        try {
-               if (abort == true) {    return 0;       }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
-               ofstream outputFile;
-               openOutputFile(outputFileName, outputFile);
+               m->mothurRemove(outputFileName);
+               for(int i=0;i<numInputFiles;i++){  m->appendFiles(fileNames[i], outputFileName);  }
                
-               ifstream inputFile;
-               char c;
-               for(int i=0;i<numInputFiles;i++){
-                       openInputFile(fileNames[i], inputFile);
-                       
-                       while(!inputFile.eof()){        c = inputFile.get(); outputFile << c;   }
-                       
-                       inputFile.close();
-               }
-               return 0;
-       }
-       catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       catch(...) {
-               cout << "An unknown error has occurred in the BinSeqCommand class function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
-       
-}
-
-//**********************************************************************************************************************
+               if (m->control_pressed) {  m->mothurRemove(outputFileName); return 0;  }
+               
+               m->mothurOutEndLine();
+               m->mothurOut("Output File Names: "); m->mothurOutEndLine();
+               m->mothurOut(outputFileName); m->mothurOutEndLine();    outputNames.push_back(outputFileName); outputTypes["merge"].push_back(outputFileName);
+               m->mothurOutEndLine();
 
-void MergeFileCommand::help(){
-       try {
-               cout << "The merge.file command..." << endl;
+               return 0;
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the MergeFileCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "MergeFileCommand", "execute");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the MergeFileCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
-       
 }
-
 //**********************************************************************************************************************