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 ((input == "") && (output == "") && (tempdefault == "") && nodebug) {
97 m->mothurOut("You must provide either an input, output, tempdefault or debug for the set.outdir command."); m->mothurOutEndLine(); abort = true;
98 }else if((input == "") && (output == "") && (tempdefault == "")) { debugOnly = true; }
101 catch(exception& e) {
102 m->errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
106 //**********************************************************************************************************************
108 int SetDirectoryCommand::execute(){
111 if (abort == true) { if (calledHelp) { return 0; } return 2; }
113 if (debugOnly) { return 0; }
115 commandFactory = CommandFactory::getInstance();
117 m->mothurOut("Mothur's directories:"); m->mothurOutEndLine();
120 if ((output == "clear") || (output == "")) { output = ""; commandFactory->setOutputDirectory(output); }
121 else if (output == "default") {
122 string exepath = m->argv;
123 output = exepath.substr(0, (exepath.find_last_of('m')));
125 m->mothurOut("outputDir=" + output); m->mothurOutEndLine();
126 commandFactory->setOutputDirectory(output);
128 if (m->dirCheck(output)) {
129 m->mothurOut("outputDir=" + output); m->mothurOutEndLine();
130 commandFactory->setOutputDirectory(output);
135 if ((input == "clear") || (input == "")) { input = ""; commandFactory->setInputDirectory(input); }
136 else if (input == "default") {
137 string exepath = m->argv;
138 input = exepath.substr(0, (exepath.find_last_of('m')));
140 m->mothurOut("inputDir=" + input); m->mothurOutEndLine();
141 commandFactory->setInputDirectory(input);
143 if (m->dirCheck(input)) {
144 m->mothurOut("inputDir=" + input); m->mothurOutEndLine();
145 commandFactory->setInputDirectory(input);
150 if (tempdefault == "clear") {
152 string temp = MOTHUR_FILES;
153 m->mothurOut("tempDefault=" + temp); m->mothurOutEndLine();
154 m->setDefaultPath(temp);
157 m->mothurOut("No default directory defined at compile time."); m->mothurOutEndLine();
158 m->setDefaultPath(temp);
160 }else if (tempdefault == "") { //do nothing
161 }else if (tempdefault == "default") {
162 string exepath = m->argv;
163 tempdefault = exepath.substr(0, (exepath.find_last_of('m')));
165 m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();
166 m->setDefaultPath(tempdefault);
168 if (m->dirCheck(tempdefault)) {
169 m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();
170 m->setDefaultPath(tempdefault);
176 catch(exception& e) {
177 m->errorOut(e, "SetDirectoryCommand", "execute");
181 //**********************************************************************************************************************/