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 pmodnames("modifynames", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(pmodnames);
18 CommandParameter pinput("input", "String", "", "", "", "", "","",false,false,true); parameters.push_back(pinput);
19 CommandParameter poutput("output", "String", "", "", "", "", "","",false,false,true); parameters.push_back(poutput);
20 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
21 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
23 vector<string> myArray;
24 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
28 m->errorOut(e, "SetDirectoryCommand", "setParameters");
32 //**********************************************************************************************************************
33 string SetDirectoryCommand::getHelpString(){
35 string helpString = "";
36 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";
37 helpString += "The set.dir command can also be used to specify the directory where your input files are located, the directory must exist.\n";
38 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";
39 helpString += "The set.dir command can also be used to run mothur in debug mode.\n";
40 helpString += "The set.dir command can also be used to set the modifynames parameter. Default=t, meaning if your sequence names contain ':' change them to '_' to avoid issues while making trees. modifynames=F will leave sequence names as they are.\n";
41 helpString += "The set.dir command parameters are input, output, tempdefault and debug and one is required.\n";
42 helpString += "To run mothur in debug mode set debug=true. Default debug=false.\n";
43 helpString += "To return the output to the same directory as the input files you may enter: output=clear.\n";
44 helpString += "To return the input to the current working directory you may enter: input=clear.\n";
45 helpString += "To set the output to the directory where mothur.exe is located you may enter: output=default.\n";
46 helpString += "To set the input to the directory where mothur.exe is located you may enter: input=default.\n";
47 helpString += "To return the tempdefault to the default you provided at compile time you may enter: tempdefault=clear.\n";
48 helpString += "To set the tempdefault to the directory where mothur.exe is located you may enter: tempdefault=default.\n";
49 helpString += "The set.dir command should be in the following format: set.dir(output=yourOutputDirectory, input=yourInputDirectory, tempdefault=yourTempDefault).\n";
50 helpString += "Example set.outdir(output=/Users/lab/desktop/outputs, input=/Users/lab/desktop/inputs).\n";
51 helpString += "Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputDirectory).\n";
55 m->errorOut(e, "SetDirectoryCommand", "getHelpString");
59 //**********************************************************************************************************************
61 SetDirectoryCommand::SetDirectoryCommand(string option) {
63 abort = false; calledHelp = false;
65 //allow user to run help
66 if(option == "help") { help(); abort = true; calledHelp = true; }
67 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
70 vector<string> myArray = setParameters();
72 OptionParser parser(option);
73 map<string, string> parameters = parser.getParameters();
75 ValidParameters validParameter;
76 //check to make sure all parameters are valid for command
77 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
78 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
81 output = validParameter.validFile(parameters, "output", false);
82 if (output == "not found") { output = ""; }
84 input = validParameter.validFile(parameters, "input", false);
85 if (input == "not found") { input = ""; }
87 tempdefault = validParameter.validFile(parameters, "tempdefault", false);
88 if (tempdefault == "not found") { tempdefault = ""; }
93 string temp = validParameter.validFile(parameters, "debug", false);
94 if (temp == "not found") { debug = false; nodebug=true; }
95 else { debug = m->isTrue(temp); }
99 temp = validParameter.validFile(parameters, "modifynames", false);
100 if (temp == "not found") { modifyNames = true; nomod=true; }
101 else { modifyNames = m->isTrue(temp); }
102 m->modifyNames = modifyNames;
104 if (debug) { m->mothurOut("Setting [DEBUG] flag.\n"); }
107 if ((input == "") && (output == "") && (tempdefault == "") && nodebug && nomod) {
108 m->mothurOut("You must provide either an input, output, tempdefault, debug or modifynames for the set.outdir command."); m->mothurOutEndLine(); abort = true;
109 }else if((input == "") && (output == "") && (tempdefault == "")) { debugOnly = true; }
112 catch(exception& e) {
113 m->errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
117 //**********************************************************************************************************************
119 int SetDirectoryCommand::execute(){
122 if (abort == true) { if (calledHelp) { return 0; } return 2; }
124 if (debugOnly) { return 0; }
126 commandFactory = CommandFactory::getInstance();
128 m->mothurOut("Mothur's directories:"); m->mothurOutEndLine();
131 if ((output == "clear") || (output == "")) { output = ""; commandFactory->setOutputDirectory(output); }
132 else if (output == "default") {
133 string exepath = m->argv;
134 output = exepath.substr(0, (exepath.find_last_of('m')));
136 m->mothurOut("outputDir=" + output); m->mothurOutEndLine();
137 commandFactory->setOutputDirectory(output);
139 if (m->dirCheck(output)) {
140 m->mothurOut("outputDir=" + output); m->mothurOutEndLine();
141 commandFactory->setOutputDirectory(output);
146 if ((input == "clear") || (input == "")) { input = ""; commandFactory->setInputDirectory(input); }
147 else if (input == "default") {
148 string exepath = m->argv;
149 input = exepath.substr(0, (exepath.find_last_of('m')));
151 m->mothurOut("inputDir=" + input); m->mothurOutEndLine();
152 commandFactory->setInputDirectory(input);
154 if (m->dirCheck(input)) {
155 m->mothurOut("inputDir=" + input); m->mothurOutEndLine();
156 commandFactory->setInputDirectory(input);
161 if (tempdefault == "clear") {
163 string temp = MOTHUR_FILES;
164 m->mothurOut("tempDefault=" + temp); m->mothurOutEndLine();
165 m->setDefaultPath(temp);
168 m->mothurOut("No default directory defined at compile time."); m->mothurOutEndLine();
169 m->setDefaultPath(temp);
171 }else if (tempdefault == "") { //do nothing
172 }else if (tempdefault == "default") {
173 string exepath = m->argv;
174 tempdefault = exepath.substr(0, (exepath.find_last_of('m')));
176 m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();
177 m->setDefaultPath(tempdefault);
179 if (m->dirCheck(tempdefault)) {
180 m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();
181 m->setDefaultPath(tempdefault);
187 catch(exception& e) {
188 m->errorOut(e, "SetDirectoryCommand", "execute");
192 //**********************************************************************************************************************/