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::getValidParameters(){
15 string Array[] = {"output","input","tempdefault","outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "SetDirectoryCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 vector<string> SetDirectoryCommand::getRequiredParameters(){
27 vector<string> myArray;
31 m->errorOut(e, "SetDirectoryCommand", "getRequiredParameters");
35 //**********************************************************************************************************************
36 vector<string> SetDirectoryCommand::getRequiredFiles(){
38 vector<string> myArray;
42 m->errorOut(e, "SetDirectoryCommand", "getRequiredFiles");
46 //**********************************************************************************************************************
48 SetDirectoryCommand::SetDirectoryCommand(string option) {
52 //allow user to run help
53 if(option == "help") { help(); abort = true; }
56 //valid paramters for this command
57 string Array[] = {"output","input","tempdefault","outputdir","inputdir"};
58 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
60 OptionParser parser(option);
61 map<string, string> parameters = parser.getParameters();
63 ValidParameters validParameter;
64 //check to make sure all parameters are valid for command
65 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
66 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
69 output = validParameter.validFile(parameters, "output", false);
70 if (output == "not found") { output = ""; }
72 input = validParameter.validFile(parameters, "input", false);
73 if (input == "not found") { input = ""; }
75 tempdefault = validParameter.validFile(parameters, "tempdefault", false);
76 if (tempdefault == "not found") { tempdefault = ""; }
78 if ((input == "") && (output == "") && (tempdefault == "")) {
79 m->mothurOut("You must provide either an input, output or tempdefault for the set.outdir command."); m->mothurOutEndLine(); abort = true;
84 m->errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
88 //**********************************************************************************************************************
90 void SetDirectoryCommand::help(){
92 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");
93 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");
94 m->mothurOut("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");
95 m->mothurOut("The set.dir command parameters are input, output and tempdefault and one is required.\n");
96 m->mothurOut("To return the output to the same directory as the input files you may enter: output=clear.\n");
97 m->mothurOut("To return the input to the current working directory you may enter: input=clear.\n");
98 m->mothurOut("To set the output to the directory where mothur.exe is located you may enter: output=default.\n");
99 m->mothurOut("To set the input to the directory where mothur.exe is located you may enter: input=default.\n");
100 m->mothurOut("To return the tempdefault to the default you provided at compile time you may enter: tempdefault=clear.\n");
101 m->mothurOut("To set the tempdefault to the directory where mothur.exe is located you may enter: tempdefault=default.\n");
102 m->mothurOut("The set.dir command should be in the following format: set.dir(output=yourOutputDirectory, input=yourInputDirectory, tempdefault=yourTempDefault).\n");
103 m->mothurOut("Example set.outdir(output=/Users/lab/desktop/outputs, input=/Users/lab/desktop/inputs).\n");
104 m->mothurOut("Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputDirectory).\n\n");
106 catch(exception& e) {
107 m->errorOut(e, "SetDirectoryCommand", "help");
111 //**********************************************************************************************************************
113 SetDirectoryCommand::~SetDirectoryCommand(){}
115 //**********************************************************************************************************************
117 int SetDirectoryCommand::execute(){
120 if (abort == true) { return 0; }
122 commandFactory = CommandFactory::getInstance();
125 if ((output == "clear") || (output == "")) { output = ""; commandFactory->setOutputDirectory(output); }
126 else if (output == "default") {
127 GlobalData* globaldata = GlobalData::getInstance();
128 string exepath = globaldata->argv;
129 output = exepath.substr(0, (exepath.find_last_of('m')));
131 m->mothurOut("Changing output directory to " + output); m->mothurOutEndLine();
132 commandFactory->setOutputDirectory(output);
134 //add / to name if needed
135 string lastChar = output.substr(output.length()-1);
136 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
137 if (lastChar != "/") { output += "/"; }
139 if (lastChar != "\\") { output += "\\"; }
142 //test to make sure directory exists
143 output = m->getFullPathName(output);
144 string outTemp = output + "temp";
146 out.open(outTemp.c_str(), ios::trunc);
148 m->mothurOut(output + " directory does not exist or is not writable."); m->mothurOutEndLine();
151 remove(outTemp.c_str());
152 m->mothurOut("Changing output directory to " + output); m->mothurOutEndLine();
153 commandFactory->setOutputDirectory(output);
158 if ((input == "clear") || (input == "")) { input = ""; commandFactory->setInputDirectory(input); }
159 else if (input == "default") {
160 GlobalData* globaldata = GlobalData::getInstance();
161 string exepath = globaldata->argv;
162 input = exepath.substr(0, (exepath.find_last_of('m')));
164 m->mothurOut("Changing input directory to " + input); m->mothurOutEndLine();
165 commandFactory->setInputDirectory(input);
167 //add / to name if needed
168 string lastChar = input.substr(input.length()-1);
169 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
170 if (lastChar != "/") { input += "/"; }
172 if (lastChar != "\\") { input += "\\"; }
175 //test to make sure directory exists
176 input = m->getFullPathName(input);
177 string inTemp = input + "temp";
179 in.open(inTemp.c_str(), ios::trunc);
181 m->mothurOut(input + " directory does not exist or is not writable."); m->mothurOutEndLine();
184 remove(inTemp.c_str());
185 m->mothurOut("Changing input directory to " + input); m->mothurOutEndLine();
186 commandFactory->setInputDirectory(input);
191 if (tempdefault == "clear") {
193 string temp = MOTHUR_FILES;
194 m->mothurOut("Resetting default directory to " + temp); m->mothurOutEndLine();
195 m->setDefaultPath(temp);
198 m->mothurOut("No default directory defined at compile time."); m->mothurOutEndLine();
199 m->setDefaultPath(temp);
201 }else if (tempdefault == "") { //do nothing
202 }else if (tempdefault == "default") {
203 GlobalData* globaldata = GlobalData::getInstance();
204 string exepath = globaldata->argv;
205 tempdefault = exepath.substr(0, (exepath.find_last_of('m')));
207 m->mothurOut("Changing default directory to " + tempdefault); m->mothurOutEndLine();
208 m->setDefaultPath(tempdefault);
210 //add / to name if needed
211 string lastChar = tempdefault.substr(tempdefault.length()-1);
212 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
213 if (lastChar != "/") { tempdefault += "/"; }
215 if (lastChar != "\\") { tempdefault += "\\"; }
218 m->mothurOut("Changing default directory to " + tempdefault); m->mothurOutEndLine();
219 m->setDefaultPath(tempdefault);
224 catch(exception& e) {
225 m->errorOut(e, "SetDirectoryCommand", "execute");
229 //**********************************************************************************************************************/