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; }
62 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
65 vector<string> myArray = setParameters();
67 OptionParser parser(option);
68 map<string, string> parameters = parser.getParameters();
70 ValidParameters validParameter;
71 //check to make sure all parameters are valid for command
72 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
73 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
76 output = validParameter.validFile(parameters, "output", false);
77 if (output == "not found") { output = ""; }
79 input = validParameter.validFile(parameters, "input", false);
80 if (input == "not found") { input = ""; }
82 tempdefault = validParameter.validFile(parameters, "tempdefault", false);
83 if (tempdefault == "not found") { tempdefault = ""; }
85 if ((input == "") && (output == "") && (tempdefault == "")) {
86 m->mothurOut("You must provide either an input, output or tempdefault for the set.outdir command."); m->mothurOutEndLine(); abort = true;
91 m->errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
95 //**********************************************************************************************************************
97 int SetDirectoryCommand::execute(){
100 if (abort == true) { if (calledHelp) { return 0; } return 2; }
102 commandFactory = CommandFactory::getInstance();
104 m->mothurOut("Mothur's directories:"); m->mothurOutEndLine();
107 if ((output == "clear") || (output == "")) { output = ""; commandFactory->setOutputDirectory(output); }
108 else if (output == "default") {
109 string exepath = m->argv;
110 output = exepath.substr(0, (exepath.find_last_of('m')));
112 m->mothurOut("outputDir=" + output); m->mothurOutEndLine();
113 commandFactory->setOutputDirectory(output);
115 //add / to name if needed
116 string lastChar = output.substr(output.length()-1);
117 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
118 if (lastChar != "/") { output += "/"; }
120 if (lastChar != "\\") { output += "\\"; }
123 //test to make sure directory exists
124 output = m->getFullPathName(output);
125 string outTemp = output + "temp";
127 out.open(outTemp.c_str(), ios::trunc);
129 m->mothurOut(output + " directory does not exist or is not writable."); m->mothurOutEndLine();
132 m->mothurRemove(outTemp);
133 m->mothurOut("outputDir=" + output); m->mothurOutEndLine();
134 commandFactory->setOutputDirectory(output);
139 if ((input == "clear") || (input == "")) { input = ""; commandFactory->setInputDirectory(input); }
140 else if (input == "default") {
141 string exepath = m->argv;
142 input = exepath.substr(0, (exepath.find_last_of('m')));
144 m->mothurOut("inputDir=" + input); m->mothurOutEndLine();
145 commandFactory->setInputDirectory(input);
147 //add / to name if needed
148 string lastChar = input.substr(input.length()-1);
149 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
150 if (lastChar != "/") { input += "/"; }
152 if (lastChar != "\\") { input += "\\"; }
155 //test to make sure directory exists
156 input = m->getFullPathName(input);
157 string inTemp = input + "temp";
159 in.open(inTemp.c_str(), ios::trunc);
161 m->mothurOut(input + " directory does not exist or is not writable."); m->mothurOutEndLine();
164 m->mothurRemove(inTemp);
165 m->mothurOut("inputDir=" + input); m->mothurOutEndLine();
166 commandFactory->setInputDirectory(input);
171 if (tempdefault == "clear") {
173 string temp = MOTHUR_FILES;
174 m->mothurOut("tempDefault=" + temp); m->mothurOutEndLine();
175 m->setDefaultPath(temp);
178 m->mothurOut("No default directory defined at compile time."); m->mothurOutEndLine();
179 m->setDefaultPath(temp);
181 }else if (tempdefault == "") { //do nothing
182 }else if (tempdefault == "default") {
183 string exepath = m->argv;
184 tempdefault = exepath.substr(0, (exepath.find_last_of('m')));
186 m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();
187 m->setDefaultPath(tempdefault);
189 //add / to name if needed
190 string lastChar = tempdefault.substr(tempdefault.length()-1);
191 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
192 if (lastChar != "/") { tempdefault += "/"; }
194 if (lastChar != "\\") { tempdefault += "\\"; }
197 m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();
198 m->setDefaultPath(tempdefault);
203 catch(exception& e) {
204 m->errorOut(e, "SetDirectoryCommand", "execute");
208 //**********************************************************************************************************************/