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();
103 m->mothurOut("Mothur's directories:"); m->mothurOutEndLine();
106 if ((output == "clear") || (output == "")) { output = ""; commandFactory->setOutputDirectory(output); }
107 else if (output == "default") {
108 string exepath = m->argv;
109 output = exepath.substr(0, (exepath.find_last_of('m')));
111 m->mothurOut("outputDir=" + output); m->mothurOutEndLine();
112 commandFactory->setOutputDirectory(output);
114 //add / to name if needed
115 string lastChar = output.substr(output.length()-1);
116 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
117 if (lastChar != "/") { output += "/"; }
119 if (lastChar != "\\") { output += "\\"; }
122 //test to make sure directory exists
123 output = m->getFullPathName(output);
124 string outTemp = output + "temp";
126 out.open(outTemp.c_str(), ios::trunc);
128 m->mothurOut(output + " directory does not exist or is not writable."); m->mothurOutEndLine();
131 remove(outTemp.c_str());
132 m->mothurOut("outputDir=" + output); m->mothurOutEndLine();
133 commandFactory->setOutputDirectory(output);
138 if ((input == "clear") || (input == "")) { input = ""; commandFactory->setInputDirectory(input); }
139 else if (input == "default") {
140 string exepath = m->argv;
141 input = exepath.substr(0, (exepath.find_last_of('m')));
143 m->mothurOut("inputDir=" + input); m->mothurOutEndLine();
144 commandFactory->setInputDirectory(input);
146 //add / to name if needed
147 string lastChar = input.substr(input.length()-1);
148 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
149 if (lastChar != "/") { input += "/"; }
151 if (lastChar != "\\") { input += "\\"; }
154 //test to make sure directory exists
155 input = m->getFullPathName(input);
156 string inTemp = input + "temp";
158 in.open(inTemp.c_str(), ios::trunc);
160 m->mothurOut(input + " directory does not exist or is not writable."); m->mothurOutEndLine();
163 remove(inTemp.c_str());
164 m->mothurOut("inputDir=" + input); m->mothurOutEndLine();
165 commandFactory->setInputDirectory(input);
170 if (tempdefault == "clear") {
172 string temp = MOTHUR_FILES;
173 m->mothurOut("tempDefault=" + temp); m->mothurOutEndLine();
174 m->setDefaultPath(temp);
177 m->mothurOut("No default directory defined at compile time."); m->mothurOutEndLine();
178 m->setDefaultPath(temp);
180 }else if (tempdefault == "") { //do nothing
181 }else if (tempdefault == "default") {
182 string exepath = m->argv;
183 tempdefault = exepath.substr(0, (exepath.find_last_of('m')));
185 m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();
186 m->setDefaultPath(tempdefault);
188 //add / to name if needed
189 string lastChar = tempdefault.substr(tempdefault.length()-1);
190 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
191 if (lastChar != "/") { tempdefault += "/"; }
193 if (lastChar != "\\") { tempdefault += "\\"; }
196 m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();
197 m->setDefaultPath(tempdefault);
202 catch(exception& e) {
203 m->errorOut(e, "SetDirectoryCommand", "execute");
207 //**********************************************************************************************************************/