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(){
27 abort = true; calledHelp = true;
28 vector<string> tempOutNames;
29 outputTypes["merge"] = tempOutNames;
32 m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
36 //**********************************************************************************************************************
37 vector<string> MergeFileCommand::getRequiredParameters(){
39 string Array[] = {"input","output"};
40 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
44 m->errorOut(e, "MergeFileCommand", "getRequiredParameters");
48 //**********************************************************************************************************************
49 vector<string> MergeFileCommand::getRequiredFiles(){
51 vector<string> myArray;
55 m->errorOut(e, "MergeFileCommand", "getRequiredFiles");
59 //**********************************************************************************************************************
61 MergeFileCommand::MergeFileCommand(string option) {
63 abort = false; calledHelp = false;
65 if(option == "help") {
67 abort = true; calledHelp = true;
70 //valid paramters for this command
71 string Array[] = {"input", "output","outputdir","inputdir"};
72 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
74 OptionParser parser(option);
75 map<string,string> parameters = parser.getParameters();
77 ValidParameters validParameter;
79 //check to make sure all parameters are valid for command
80 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
81 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
84 //initialize outputTypes
85 vector<string> tempOutNames;
86 outputTypes["merge"] = tempOutNames;
88 //if the user changes the input directory command factory will send this info to us in the output parameter
89 string inputDir = validParameter.validFile(parameters, "inputdir", false);
90 if (inputDir == "not found"){ inputDir = ""; }
92 string fileList = validParameter.validFile(parameters, "input", false);
93 if(fileList == "not found") { m->mothurOut("you must enter two or more file names"); m->mothurOutEndLine(); abort=true; }
94 else{ m->splitAtDash(fileList, fileNames); }
96 //if the user changes the output directory command factory will send this info to us in the output parameter
97 string outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found") { outputDir = ""; }
100 numInputFiles = fileNames.size();
102 if(numInputFiles == 0){
103 m->mothurOut("you must enter two or more file names and you entered " + toString(fileNames.size()) + " file names"); m->mothurOutEndLine();
107 for(int i=0;i<numInputFiles;i++){
108 if (inputDir != "") {
109 string path = m->hasPath(fileNames[i]);
110 //if the user has not given a path then, add inputdir. else leave path alone.
111 if (path == "") { fileNames[i] = inputDir + fileNames[i]; }
114 if(m->openInputFile(fileNames[i], testFile)){ abort = true; }
119 outputFileName = validParameter.validFile(parameters, "output", false);
120 if (outputFileName == "not found") { m->mothurOut("you must enter an output file name"); m->mothurOutEndLine(); abort=true; }
121 else if (outputDir != "") { outputFileName = outputDir + m->getSimpleName(outputFileName); }
125 catch(exception& e) {
126 m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
131 //**********************************************************************************************************************
133 MergeFileCommand::~MergeFileCommand() { /* do nothing */ }
135 //**********************************************************************************************************************
137 int MergeFileCommand::execute(){
139 if (abort == true) { if (calledHelp) { return 0; } return 2; }
142 m->openOutputFile(outputFileName, outputFile);
145 for(int i=0;i<numInputFiles;i++){
146 ifstream inputFile; //declaration must be inside for loop of windows throws an error
148 m->openInputFile(fileNames[i], inputFile);
150 while(!inputFile.eof()){
151 if (m->control_pressed) { outputTypes.clear(); inputFile.close(); outputFile.close(); remove(outputFileName.c_str()); return 0; }
155 if (int(c) != -1) { outputFile << c; }
163 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); return 0; }
165 m->mothurOutEndLine();
166 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
167 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["merge"].push_back(outputFileName);
168 m->mothurOutEndLine();
172 catch(exception& e) {
173 m->errorOut(e, "MergeFileCommand", "execute");
178 //**********************************************************************************************************************
180 void MergeFileCommand::help(){
182 m->mothurOut("The merge.file command..."); m->mothurOutEndLine();
184 catch(exception& e) {
185 m->errorOut(e, "MergeFileCommand", "help");
190 //**********************************************************************************************************************