5 * Created by westcott on 1/21/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "setdircommand.h"
12 //**********************************************************************************************************************
13 vector<string> SetDirectoryCommand::setParameters(){
15 CommandParameter ptempdefault("tempdefault", "String", "", "", "", "", "",false,false); parameters.push_back(ptempdefault);
16 CommandParameter pinput("input", "String", "", "", "", "", "",false,false); parameters.push_back(pinput);
17 CommandParameter poutput("output", "String", "", "", "", "", "",false,false); parameters.push_back(poutput);
18 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
19 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
21 vector<string> myArray;
22 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
26 m->errorOut(e, "SetDirectoryCommand", "setParameters");
30 //**********************************************************************************************************************
31 string SetDirectoryCommand::getHelpString(){
33 string helpString = "";
34 helpString += "The set.dir command can be used to direct the output files generated by mothur to a specific place, the directory must exist.\n";
35 helpString += "The set.dir command can also be used to specify the directory where your input files are located, the directory must exist.\n";
36 helpString += "The set.dir command can also be used to override or set the default location mothur will look for files if it is unable to find them, the directory must exist.\n";
37 helpString += "The set.dir command parameters are input, output and tempdefault and one is required.\n";
38 helpString += "To return the output to the same directory as the input files you may enter: output=clear.\n";
39 helpString += "To return the input to the current working directory you may enter: input=clear.\n";
40 helpString += "To set the output to the directory where mothur.exe is located you may enter: output=default.\n";
41 helpString += "To set the input to the directory where mothur.exe is located you may enter: input=default.\n";
42 helpString += "To return the tempdefault to the default you provided at compile time you may enter: tempdefault=clear.\n";
43 helpString += "To set the tempdefault to the directory where mothur.exe is located you may enter: tempdefault=default.\n";
44 helpString += "The set.dir command should be in the following format: set.dir(output=yourOutputDirectory, input=yourInputDirectory, tempdefault=yourTempDefault).\n";
45 helpString += "Example set.outdir(output=/Users/lab/desktop/outputs, input=/Users/lab/desktop/inputs).\n";
46 helpString += "Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputDirectory).\n";
50 m->errorOut(e, "SetDirectoryCommand", "getHelpString");
54 //**********************************************************************************************************************
56 SetDirectoryCommand::SetDirectoryCommand(string option) {
58 abort = false; calledHelp = false;
60 //allow user to run help
61 if(option == "help") { help(); abort = true; calledHelp = true; }
64 vector<string> myArray = setParameters();
66 OptionParser parser(option);
67 map<string, string> parameters = parser.getParameters();
69 ValidParameters validParameter;
70 //check to make sure all parameters are valid for command
71 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
72 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
75 output = validParameter.validFile(parameters, "output", false);
76 if (output == "not found") { output = ""; }
78 input = validParameter.validFile(parameters, "input", false);
79 if (input == "not found") { input = ""; }
81 tempdefault = validParameter.validFile(parameters, "tempdefault", false);
82 if (tempdefault == "not found") { tempdefault = ""; }
84 if ((input == "") && (output == "") && (tempdefault == "")) {
85 m->mothurOut("You must provide either an input, output or tempdefault for the set.outdir command."); m->mothurOutEndLine(); abort = true;
90 m->errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
94 //**********************************************************************************************************************
96 int SetDirectoryCommand::execute(){
99 if (abort == true) { if (calledHelp) { return 0; } return 2; }
101 commandFactory = CommandFactory::getInstance();
104 if ((output == "clear") || (output == "")) { output = ""; commandFactory->setOutputDirectory(output); }
105 else if (output == "default") {
106 string exepath = m->argv;
107 output = exepath.substr(0, (exepath.find_last_of('m')));
109 m->mothurOut("Changing output directory to " + output); m->mothurOutEndLine();
110 commandFactory->setOutputDirectory(output);
112 //add / to name if needed
113 string lastChar = output.substr(output.length()-1);
114 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
115 if (lastChar != "/") { output += "/"; }
117 if (lastChar != "\\") { output += "\\"; }
120 //test to make sure directory exists
121 output = m->getFullPathName(output);
122 string outTemp = output + "temp";
124 out.open(outTemp.c_str(), ios::trunc);
126 m->mothurOut(output + " directory does not exist or is not writable."); m->mothurOutEndLine();
129 remove(outTemp.c_str());
130 m->mothurOut("Changing output directory to " + output); m->mothurOutEndLine();
131 commandFactory->setOutputDirectory(output);
136 if ((input == "clear") || (input == "")) { input = ""; commandFactory->setInputDirectory(input); }
137 else if (input == "default") {
138 string exepath = m->argv;
139 input = exepath.substr(0, (exepath.find_last_of('m')));
141 m->mothurOut("Changing input directory to " + input); m->mothurOutEndLine();
142 commandFactory->setInputDirectory(input);
144 //add / to name if needed
145 string lastChar = input.substr(input.length()-1);
146 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
147 if (lastChar != "/") { input += "/"; }
149 if (lastChar != "\\") { input += "\\"; }
152 //test to make sure directory exists
153 input = m->getFullPathName(input);
154 string inTemp = input + "temp";
156 in.open(inTemp.c_str(), ios::trunc);
158 m->mothurOut(input + " directory does not exist or is not writable."); m->mothurOutEndLine();
161 remove(inTemp.c_str());
162 m->mothurOut("Changing input directory to " + input); m->mothurOutEndLine();
163 commandFactory->setInputDirectory(input);
168 if (tempdefault == "clear") {
170 string temp = MOTHUR_FILES;
171 m->mothurOut("Resetting default directory to " + temp); m->mothurOutEndLine();
172 m->setDefaultPath(temp);
175 m->mothurOut("No default directory defined at compile time."); m->mothurOutEndLine();
176 m->setDefaultPath(temp);
178 }else if (tempdefault == "") { //do nothing
179 }else if (tempdefault == "default") {
180 string exepath = m->argv;
181 tempdefault = exepath.substr(0, (exepath.find_last_of('m')));
183 m->mothurOut("Changing default directory to " + tempdefault); m->mothurOutEndLine();
184 m->setDefaultPath(tempdefault);
186 //add / to name if needed
187 string lastChar = tempdefault.substr(tempdefault.length()-1);
188 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
189 if (lastChar != "/") { tempdefault += "/"; }
191 if (lastChar != "\\") { tempdefault += "\\"; }
194 m->mothurOut("Changing default directory to " + tempdefault); m->mothurOutEndLine();
195 m->setDefaultPath(tempdefault);
200 catch(exception& e) {
201 m->errorOut(e, "SetDirectoryCommand", "execute");
205 //**********************************************************************************************************************/