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 pdebug("debug", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pdebug);
17 CommandParameter pinput("input", "String", "", "", "", "", "",false,false); parameters.push_back(pinput);
18 CommandParameter poutput("output", "String", "", "", "", "", "",false,false); parameters.push_back(poutput);
19 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
20 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
22 vector<string> myArray;
23 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
27 m->errorOut(e, "SetDirectoryCommand", "setParameters");
31 //**********************************************************************************************************************
32 string SetDirectoryCommand::getHelpString(){
34 string helpString = "";
35 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";
36 helpString += "The set.dir command can also be used to specify the directory where your input files are located, the directory must exist.\n";
37 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";
38 helpString += "The set.dir command can also be used to run mothur in debug mode.\n";
39 helpString += "The set.dir command parameters are input, output, tempdefault and debug and one is required.\n";
40 helpString += "To run mothur in debug mode set debug=true. Default debug=false.\n";
41 helpString += "To return the output to the same directory as the input files you may enter: output=clear.\n";
42 helpString += "To return the input to the current working directory you may enter: input=clear.\n";
43 helpString += "To set the output to the directory where mothur.exe is located you may enter: output=default.\n";
44 helpString += "To set the input to the directory where mothur.exe is located you may enter: input=default.\n";
45 helpString += "To return the tempdefault to the default you provided at compile time you may enter: tempdefault=clear.\n";
46 helpString += "To set the tempdefault to the directory where mothur.exe is located you may enter: tempdefault=default.\n";
47 helpString += "The set.dir command should be in the following format: set.dir(output=yourOutputDirectory, input=yourInputDirectory, tempdefault=yourTempDefault).\n";
48 helpString += "Example set.outdir(output=/Users/lab/desktop/outputs, input=/Users/lab/desktop/inputs).\n";
49 helpString += "Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputDirectory).\n";
53 m->errorOut(e, "SetDirectoryCommand", "getHelpString");
57 //**********************************************************************************************************************
59 SetDirectoryCommand::SetDirectoryCommand(string option) {
61 abort = false; calledHelp = false;
63 //allow user to run help
64 if(option == "help") { help(); abort = true; calledHelp = true; }
65 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
68 vector<string> myArray = setParameters();
70 OptionParser parser(option);
71 map<string, string> parameters = parser.getParameters();
73 ValidParameters validParameter;
74 //check to make sure all parameters are valid for command
75 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
76 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
79 output = validParameter.validFile(parameters, "output", false);
80 if (output == "not found") { output = ""; }
82 input = validParameter.validFile(parameters, "input", false);
83 if (input == "not found") { input = ""; }
85 tempdefault = validParameter.validFile(parameters, "tempdefault", false);
86 if (tempdefault == "not found") { tempdefault = ""; }
91 string temp = validParameter.validFile(parameters, "debug", false);
92 if (temp == "not found") { debug = false; nodebug=true; }
93 else { debug = m->isTrue(temp); }
96 if (debug) { m->mothurOut("Setting [DEBUG] flag.\n"); }
98 if ((input == "") && (output == "") && (tempdefault == "") && nodebug) {
99 m->mothurOut("You must provide either an input, output, tempdefault or debug for the set.outdir command."); m->mothurOutEndLine(); abort = true;
100 }else if((input == "") && (output == "") && (tempdefault == "")) { debugOnly = true; }
103 catch(exception& e) {
104 m->errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
108 //**********************************************************************************************************************
110 int SetDirectoryCommand::execute(){
113 if (abort == true) { if (calledHelp) { return 0; } return 2; }
115 if (debugOnly) { return 0; }
117 commandFactory = CommandFactory::getInstance();
119 m->mothurOut("Mothur's directories:"); m->mothurOutEndLine();
122 if ((output == "clear") || (output == "")) { output = ""; commandFactory->setOutputDirectory(output); }
123 else if (output == "default") {
124 string exepath = m->argv;
125 output = exepath.substr(0, (exepath.find_last_of('m')));
127 m->mothurOut("outputDir=" + output); m->mothurOutEndLine();
128 commandFactory->setOutputDirectory(output);
130 if (m->dirCheck(output)) {
131 m->mothurOut("outputDir=" + output); m->mothurOutEndLine();
132 commandFactory->setOutputDirectory(output);
137 if ((input == "clear") || (input == "")) { input = ""; commandFactory->setInputDirectory(input); }
138 else if (input == "default") {
139 string exepath = m->argv;
140 input = exepath.substr(0, (exepath.find_last_of('m')));
142 m->mothurOut("inputDir=" + input); m->mothurOutEndLine();
143 commandFactory->setInputDirectory(input);
145 if (m->dirCheck(input)) {
146 m->mothurOut("inputDir=" + input); m->mothurOutEndLine();
147 commandFactory->setInputDirectory(input);
152 if (tempdefault == "clear") {
154 string temp = MOTHUR_FILES;
155 m->mothurOut("tempDefault=" + temp); m->mothurOutEndLine();
156 m->setDefaultPath(temp);
159 m->mothurOut("No default directory defined at compile time."); m->mothurOutEndLine();
160 m->setDefaultPath(temp);
162 }else if (tempdefault == "") { //do nothing
163 }else if (tempdefault == "default") {
164 string exepath = m->argv;
165 tempdefault = exepath.substr(0, (exepath.find_last_of('m')));
167 m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();
168 m->setDefaultPath(tempdefault);
170 if (m->dirCheck(tempdefault)) {
171 m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();
172 m->setDefaultPath(tempdefault);
178 catch(exception& e) {
179 m->errorOut(e, "SetDirectoryCommand", "execute");
183 //**********************************************************************************************************************/