5 * Created by Pat Schloss on 6/14/09.
6 * Copyright 2009 Patrick D. Schloss. All rights reserved.
10 #include "mergefilecommand.h"
12 //**********************************************************************************************************************
13 vector<string> MergeFileCommand::setParameters(){
15 CommandParameter pinput("input", "String", "", "", "", "", "","",false,true,true); parameters.push_back(pinput);
16 CommandParameter poutput("output", "String", "", "", "", "", "","",false,true,true); parameters.push_back(poutput);
17 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
18 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
20 vector<string> myArray;
21 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
25 m->errorOut(e, "MergeFileCommand", "setParameters");
29 //**********************************************************************************************************************
30 string MergeFileCommand::getHelpString(){
32 string helpString = "";
33 helpString += "The merge.file command takes a list of files separated by dashes and merges them into one file.";
34 helpString += "The merge.file command parameters are input and output.";
35 helpString += "Example merge.file(input=small.fasta-large.fasta, output=all.fasta).";
36 helpString += "Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputFileName).\n";
40 m->errorOut(e, "MergeFileCommand", "getHelpString");
44 //**********************************************************************************************************************
45 MergeFileCommand::MergeFileCommand(){
47 abort = true; calledHelp = true;
49 vector<string> tempOutNames;
50 outputTypes["merge"] = tempOutNames;
53 m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
57 //**********************************************************************************************************************
59 MergeFileCommand::MergeFileCommand(string option) {
61 abort = false; calledHelp = false;
63 if(option == "help") {
65 abort = true; calledHelp = true;
66 }else if(option == "citation") { citation(); abort = true; calledHelp = true;}
68 vector<string> myArray = setParameters();
70 OptionParser parser(option);
71 map<string,string> parameters = parser.getParameters();
73 ValidParameters validParameter;
75 //check to make sure all parameters are valid for command
76 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
77 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
80 //initialize outputTypes
81 vector<string> tempOutNames;
82 outputTypes["merge"] = tempOutNames;
84 //if the user changes the input directory command factory will send this info to us in the output parameter
85 string inputDir = validParameter.validFile(parameters, "inputdir", false);
86 if (inputDir == "not found"){ inputDir = ""; }
88 string fileList = validParameter.validFile(parameters, "input", false);
89 if(fileList == "not found") { m->mothurOut("you must enter two or more file names"); m->mothurOutEndLine(); abort=true; }
90 else{ m->splitAtDash(fileList, fileNames); }
92 //if the user changes the output directory command factory will send this info to us in the output parameter
93 string outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found") { outputDir = ""; }
96 numInputFiles = fileNames.size();
98 if(numInputFiles == 0){
99 m->mothurOut("you must enter two or more file names and you entered " + toString(fileNames.size()) + " file names"); m->mothurOutEndLine();
103 for(int i=0;i<numInputFiles;i++){
104 if (inputDir != "") {
105 string path = m->hasPath(fileNames[i]);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { fileNames[i] = inputDir + fileNames[i]; }
110 if(m->openInputFile(fileNames[i], testFile)){ abort = true; }
115 outputFileName = validParameter.validFile(parameters, "output", false);
116 if (outputFileName == "not found") { m->mothurOut("you must enter an output file name"); m->mothurOutEndLine(); abort=true; }
117 else if (outputDir != "") { outputFileName = outputDir + m->getSimpleName(outputFileName); }
121 catch(exception& e) {
122 m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
126 //**********************************************************************************************************************
128 int MergeFileCommand::execute(){
130 if (abort == true) { if (calledHelp) { return 0; } return 2; }
132 m->mothurRemove(outputFileName);
133 for(int i=0;i<numInputFiles;i++){ m->appendFiles(fileNames[i], outputFileName); }
135 if (m->control_pressed) { m->mothurRemove(outputFileName); return 0; }
137 m->mothurOutEndLine();
138 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
139 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["merge"].push_back(outputFileName);
140 m->mothurOutEndLine();
144 catch(exception& e) {
145 m->errorOut(e, "MergeFileCommand", "execute");
149 //**********************************************************************************************************************