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 m->mothurOut("You must provide either an input or output for the set.outdir command."); m->mothurOutEndLine(); abort = true;
47 m->errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
51 //**********************************************************************************************************************
53 void SetDirectoryCommand::help(){
55 m->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 m->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 m->mothurOut("The set.dir command parameter is output and is required.\n");
58 m->mothurOut("To return the output to the same directory as the input files you may enter: output=clear.\n");
59 m->mothurOut("To return the input to the same directory as the mothur.exe you may enter: input=clear.\n");
60 m->mothurOut("The set.dir command should be in the following format: set.dir(output=yourOutputDirectory, input=yourInputDirectory).\n");
61 m->mothurOut("Example set.outdir(output=/Users/lab/desktop/outputs, input=/Users/lab/desktop/inputs).\n");
62 m->mothurOut("Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputDirectory).\n\n");
65 m->errorOut(e, "SetDirectoryCommand", "help");
69 //**********************************************************************************************************************
71 SetDirectoryCommand::~SetDirectoryCommand(){}
73 //**********************************************************************************************************************
75 int SetDirectoryCommand::execute(){
78 if (abort == true) { return 0; }
80 commandFactory = CommandFactory::getInstance();
83 if ((output == "clear") || (output == "")) { output = ""; commandFactory->setOutputDirectory(output); }
85 //add / to name if needed
86 string lastChar = output.substr(output.length()-1);
87 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
88 if (lastChar != "/") { output += "/"; }
90 if (lastChar != "\\") { output += "\\"; }
93 //test to make sure directory exists
94 output = getFullPathName(output);
95 string outTemp = output + "temp";
97 out.open(outTemp.c_str(), ios::trunc);
99 m->mothurOut(output + " directory does not exist or is not writable."); m->mothurOutEndLine();
102 remove(outTemp.c_str());
103 m->mothurOut("Changing output directory to " + output); m->mothurOutEndLine();
104 commandFactory->setOutputDirectory(output);
109 if ((input == "clear") || (input == "")) { input = ""; commandFactory->setInputDirectory(input); }
111 //add / to name if needed
112 string lastChar = input.substr(input.length()-1);
113 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
114 if (lastChar != "/") { input += "/"; }
116 if (lastChar != "\\") { input += "\\"; }
119 //test to make sure directory exists
120 input = getFullPathName(input);
121 string inTemp = input + "temp";
123 in.open(inTemp.c_str(), ios::trunc);
125 m->mothurOut(input + " directory does not exist or is not writable."); m->mothurOutEndLine();
128 remove(inTemp.c_str());
129 m->mothurOut("Changing input directory to " + input); m->mothurOutEndLine();
130 commandFactory->setInputDirectory(input);
136 catch(exception& e) {
137 m->errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
141 //**********************************************************************************************************************/