5 * Created by westcott on 1/21/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "setdircommand.h"
12 //**********************************************************************************************************************
14 SetDirectoryCommand::SetDirectoryCommand(string option){
18 //allow user to run help
19 if(option == "help") { help(); abort = true; }
22 //valid paramters for this command
23 string Array[] = {"output","input","outputdir","inputdir"};
24 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26 OptionParser parser(option);
27 map<string, string> parameters = parser.getParameters();
29 ValidParameters validParameter;
30 //check to make sure all parameters are valid for command
31 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
32 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
35 output = validParameter.validFile(parameters, "output", false);
36 if (output == "not found") { output = ""; }
38 input = validParameter.validFile(parameters, "input", false);
39 if (input == "not found") { input = ""; }
41 if ((input == "") && (output == "")) {
42 mothurOut("You must provide either an input or output for the set.outdir command."); mothurOutEndLine(); abort = true;
47 errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
51 //**********************************************************************************************************************
53 void SetDirectoryCommand::help(){
55 mothurOut("The set.dir command can be used to direct the output files generated by mothur to a specific place, the directory must exist.\n");
56 mothurOut("The set.dir command can also be used to specify the directory where your input files are located, the directory must exist.\n");
57 mothurOut("The set.dir command parameter is output and is required.\n");
58 mothurOut("To return the output to the same directory as the input files you may enter: output=clear.\n");
59 mothurOut("To return the input to the same directory as the mothur.exe you may enter: input=clear.\n");
60 mothurOut("The set.dir command should be in the following format: set.dir(output=yourOutputDirectory, input=yourInputDirectory).\n");
61 mothurOut("Example set.outdir(output=/Users/lab/desktop/outputs, input=/Users/lab/desktop/inputs).\n");
62 mothurOut("Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputDirectory).\n\n");
65 errorOut(e, "SetDirectoryCommand", "help");
69 //**********************************************************************************************************************
71 SetDirectoryCommand::~SetDirectoryCommand(){}
73 //**********************************************************************************************************************
75 int SetDirectoryCommand::execute(){
78 if (abort == true) { return 0; }
80 commandFactory = CommandFactory::getInstance();
84 if ((output == "clear") || (output == "")) { output = ""; commandFactory->setOutputDirectory(output); }
86 //add / to name if needed
87 string lastChar = output.substr(output.length()-1);
88 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
89 if (lastChar != "/") { output += "/"; }
91 if (lastChar != "\\") { output += "\\"; }
94 //test to make sure directory exists
95 output = getFullPathName(output);
96 string outTemp = output + "temp";
98 out.open(outTemp.c_str(), ios::trunc);
100 mothurOut(output + " directory does not exist or is not writable."); mothurOutEndLine();
103 remove(outTemp.c_str());
104 mothurOut("Changing output directory to " + output); mothurOutEndLine();
105 commandFactory->setOutputDirectory(output);
110 if ((input == "clear") || (input == "")) { input = ""; commandFactory->setInputDirectory(input); }
112 //add / to name if needed
113 string lastChar = input.substr(input.length()-1);
114 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
115 if (lastChar != "/") { input += "/"; }
117 if (lastChar != "\\") { input += "\\"; }
120 //test to make sure directory exists
121 input = getFullPathName(input);
122 string inTemp = input + "temp";
124 in.open(inTemp.c_str(), ios::trunc);
126 mothurOut(input + " directory does not exist or is not writable."); mothurOutEndLine();
129 remove(inTemp.c_str());
130 mothurOut("Changing input directory to " + input); mothurOutEndLine();
131 commandFactory->setInputDirectory(input);
137 catch(exception& e) {
138 errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
142 //**********************************************************************************************************************/