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); parameters.push_back(pinput);
16 CommandParameter poutput("output", "String", "", "", "", "", "",false,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");
45 //**********************************************************************************************************************
46 MergeFileCommand::MergeFileCommand(){
48 abort = true; calledHelp = true;
50 vector<string> tempOutNames;
51 outputTypes["merge"] = tempOutNames;
54 m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
58 //**********************************************************************************************************************
60 MergeFileCommand::MergeFileCommand(string option) {
62 abort = false; calledHelp = false;
64 if(option == "help") {
66 abort = true; calledHelp = true;
67 }else if(option == "citation") { citation(); abort = true; calledHelp = true;}
69 vector<string> myArray = setParameters();
71 OptionParser parser(option);
72 map<string,string> parameters = parser.getParameters();
74 ValidParameters validParameter;
76 //check to make sure all parameters are valid for command
77 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
78 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
81 //initialize outputTypes
82 vector<string> tempOutNames;
83 outputTypes["merge"] = tempOutNames;
85 //if the user changes the input directory command factory will send this info to us in the output parameter
86 string inputDir = validParameter.validFile(parameters, "inputdir", false);
87 if (inputDir == "not found"){ inputDir = ""; }
89 string fileList = validParameter.validFile(parameters, "input", false);
90 if(fileList == "not found") { m->mothurOut("you must enter two or more file names"); m->mothurOutEndLine(); abort=true; }
91 else{ m->splitAtDash(fileList, fileNames); }
93 //if the user changes the output directory command factory will send this info to us in the output parameter
94 string outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found") { outputDir = ""; }
97 numInputFiles = fileNames.size();
99 if(numInputFiles == 0){
100 m->mothurOut("you must enter two or more file names and you entered " + toString(fileNames.size()) + " file names"); m->mothurOutEndLine();
104 for(int i=0;i<numInputFiles;i++){
105 if (inputDir != "") {
106 string path = m->hasPath(fileNames[i]);
107 //if the user has not given a path then, add inputdir. else leave path alone.
108 if (path == "") { fileNames[i] = inputDir + fileNames[i]; }
111 if(m->openInputFile(fileNames[i], testFile)){ abort = true; }
116 outputFileName = validParameter.validFile(parameters, "output", false);
117 if (outputFileName == "not found") { m->mothurOut("you must enter an output file name"); m->mothurOutEndLine(); abort=true; }
118 else if (outputDir != "") { outputFileName = outputDir + m->getSimpleName(outputFileName); }
122 catch(exception& e) {
123 m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
127 //**********************************************************************************************************************
129 int MergeFileCommand::execute(){
131 if (abort == true) { if (calledHelp) { return 0; } return 2; }
134 m->openOutputFile(outputFileName, outputFile);
137 for(int i=0;i<numInputFiles;i++){
138 ifstream inputFile; //declaration must be inside for loop of windows throws an error
140 m->openInputFile(fileNames[i], inputFile);
142 while(!inputFile.eof()){
143 if (m->control_pressed) { outputTypes.clear(); inputFile.close(); outputFile.close(); m->mothurRemove(outputFileName); return 0; }
147 if (int(c) != -1) { outputFile << c; }
155 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outputFileName); return 0; }
157 m->mothurOutEndLine();
158 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
159 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["merge"].push_back(outputFileName);
160 m->mothurOutEndLine();
164 catch(exception& e) {
165 m->errorOut(e, "MergeFileCommand", "execute");
169 //**********************************************************************************************************************