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::getValidParameters(){
15 string Array[] = {"input", "output","outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "MergeFileCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 MergeFileCommand::MergeFileCommand(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["merge"] = tempOutNames;
33 m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
37 //**********************************************************************************************************************
38 vector<string> MergeFileCommand::getRequiredParameters(){
40 string Array[] = {"input","output"};
41 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
45 m->errorOut(e, "MergeFileCommand", "getRequiredParameters");
49 //**********************************************************************************************************************
50 vector<string> MergeFileCommand::getRequiredFiles(){
52 vector<string> myArray;
56 m->errorOut(e, "MergeFileCommand", "getRequiredFiles");
60 //**********************************************************************************************************************
62 MergeFileCommand::MergeFileCommand(string option) {
66 if(option == "help") {
71 //valid paramters for this command
72 string Array[] = {"input", "output","outputdir","inputdir"};
73 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75 OptionParser parser(option);
76 map<string,string> parameters = parser.getParameters();
78 ValidParameters validParameter;
80 //check to make sure all parameters are valid for command
81 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
82 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
85 //initialize outputTypes
86 vector<string> tempOutNames;
87 outputTypes["merge"] = tempOutNames;
89 //if the user changes the input directory command factory will send this info to us in the output parameter
90 string inputDir = validParameter.validFile(parameters, "inputdir", false);
91 if (inputDir == "not found"){ inputDir = ""; }
93 string fileList = validParameter.validFile(parameters, "input", false);
94 if(fileList == "not found") { m->mothurOut("you must enter two or more file names"); m->mothurOutEndLine(); abort=true; }
95 else{ m->splitAtDash(fileList, fileNames); }
97 //if the user changes the output directory command factory will send this info to us in the output parameter
98 string outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found") { outputDir = ""; }
101 numInputFiles = fileNames.size();
103 if(numInputFiles == 0){
104 m->mothurOut("you must enter two or more file names and you entered " + toString(fileNames.size()) + " file names"); m->mothurOutEndLine();
108 for(int i=0;i<numInputFiles;i++){
109 if (inputDir != "") {
110 string path = m->hasPath(fileNames[i]);
111 //if the user has not given a path then, add inputdir. else leave path alone.
112 if (path == "") { fileNames[i] = inputDir + fileNames[i]; }
115 if(m->openInputFile(fileNames[i], testFile)){ abort = true; }
120 outputFileName = validParameter.validFile(parameters, "output", false);
121 if (outputFileName == "not found") { m->mothurOut("you must enter an output file name"); m->mothurOutEndLine(); abort=true; }
122 else if (outputDir != "") { outputFileName = outputDir + m->getSimpleName(outputFileName); }
126 catch(exception& e) {
127 m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
132 //**********************************************************************************************************************
134 MergeFileCommand::~MergeFileCommand() { /* do nothing */ }
136 //**********************************************************************************************************************
138 int MergeFileCommand::execute(){
140 if (abort == true) { return 0; }
143 m->openOutputFile(outputFileName, outputFile);
146 for(int i=0;i<numInputFiles;i++){
147 ifstream inputFile; //declaration must be inside for loop of windows throws an error
149 m->openInputFile(fileNames[i], inputFile);
151 while(!inputFile.eof()){
152 if (m->control_pressed) { outputTypes.clear(); inputFile.close(); outputFile.close(); remove(outputFileName.c_str()); return 0; }
156 if (int(c) != -1) { outputFile << c; }
164 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); return 0; }
166 m->mothurOutEndLine();
167 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
168 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["merge"].push_back(outputFileName);
169 m->mothurOutEndLine();
173 catch(exception& e) {
174 m->errorOut(e, "MergeFileCommand", "execute");
179 //**********************************************************************************************************************
181 void MergeFileCommand::help(){
183 m->mothurOut("The merge.file command..."); m->mothurOutEndLine();
185 catch(exception& e) {
186 m->errorOut(e, "MergeFileCommand", "help");
191 //**********************************************************************************************************************