5 * Created by westcott on 1/21/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "setdircommand.h"
12 //**********************************************************************************************************************
14 SetDirectoryCommand::SetDirectoryCommand(string option) {
18 //allow user to run help
19 if(option == "help") { help(); abort = true; }
22 //valid paramters for this command
23 string Array[] = {"output","input","tempdefault","outputdir","inputdir"};
24 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26 OptionParser parser(option);
27 map<string, string> parameters = parser.getParameters();
29 ValidParameters validParameter;
30 //check to make sure all parameters are valid for command
31 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
32 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
35 output = validParameter.validFile(parameters, "output", false);
36 if (output == "not found") { output = ""; }
38 input = validParameter.validFile(parameters, "input", false);
39 if (input == "not found") { input = ""; }
41 tempdefault = validParameter.validFile(parameters, "tempdefault", false);
42 if (tempdefault == "not found") { tempdefault = ""; }
44 if ((input == "") && (output == "") && (tempdefault == "")) {
45 m->mothurOut("You must provide either an input, output or tempdefault for the set.outdir command."); m->mothurOutEndLine(); abort = true;
50 m->errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
54 //**********************************************************************************************************************
56 void SetDirectoryCommand::help(){
58 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");
59 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");
60 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");
61 m->mothurOut("The set.dir command parameters are input, output and tempdefault and one is required.\n");
62 m->mothurOut("To return the output to the same directory as the input files you may enter: output=clear.\n");
63 m->mothurOut("To return the input to the current working directory you may enter: input=clear.\n");
64 m->mothurOut("To set the output to the directory where mothur.exe is located you may enter: output=default.\n");
65 m->mothurOut("To set the input to the directory where mothur.exe is located you may enter: input=default.\n");
66 m->mothurOut("To return the tempdefault to the default you provided at compile time you may enter: tempdefault=clear.\n");
67 m->mothurOut("To set the tempdefault to the directory where mothur.exe is located you may enter: tempdefault=default.\n");
68 m->mothurOut("The set.dir command should be in the following format: set.dir(output=yourOutputDirectory, input=yourInputDirectory, tempdefault=yourTempDefault).\n");
69 m->mothurOut("Example set.outdir(output=/Users/lab/desktop/outputs, input=/Users/lab/desktop/inputs).\n");
70 m->mothurOut("Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputDirectory).\n\n");
73 m->errorOut(e, "SetDirectoryCommand", "help");
77 //**********************************************************************************************************************
79 SetDirectoryCommand::~SetDirectoryCommand(){}
81 //**********************************************************************************************************************
83 int SetDirectoryCommand::execute(){
86 if (abort == true) { return 0; }
88 commandFactory = CommandFactory::getInstance();
91 if ((output == "clear") || (output == "")) { output = ""; commandFactory->setOutputDirectory(output); }
92 else if (output == "default") {
93 GlobalData* globaldata = GlobalData::getInstance();
94 string exepath = globaldata->argv;
95 output = exepath.substr(0, (exepath.find_last_of('m')));
97 m->mothurOut("Changing output directory to " + output); m->mothurOutEndLine();
98 commandFactory->setOutputDirectory(output);
100 //add / to name if needed
101 string lastChar = output.substr(output.length()-1);
102 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
103 if (lastChar != "/") { output += "/"; }
105 if (lastChar != "\\") { output += "\\"; }
108 //test to make sure directory exists
109 output = getFullPathName(output);
110 string outTemp = output + "temp";
112 out.open(outTemp.c_str(), ios::trunc);
114 m->mothurOut(output + " directory does not exist or is not writable."); m->mothurOutEndLine();
117 remove(outTemp.c_str());
118 m->mothurOut("Changing output directory to " + output); m->mothurOutEndLine();
119 commandFactory->setOutputDirectory(output);
124 if ((input == "clear") || (input == "")) { input = ""; commandFactory->setInputDirectory(input); }
125 else if (input == "default") {
126 GlobalData* globaldata = GlobalData::getInstance();
127 string exepath = globaldata->argv;
128 input = exepath.substr(0, (exepath.find_last_of('m')));
130 m->mothurOut("Changing input directory to " + input); m->mothurOutEndLine();
131 commandFactory->setInputDirectory(input);
133 //add / to name if needed
134 string lastChar = input.substr(input.length()-1);
135 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
136 if (lastChar != "/") { input += "/"; }
138 if (lastChar != "\\") { input += "\\"; }
141 //test to make sure directory exists
142 input = getFullPathName(input);
143 string inTemp = input + "temp";
145 in.open(inTemp.c_str(), ios::trunc);
147 m->mothurOut(input + " directory does not exist or is not writable."); m->mothurOutEndLine();
150 remove(inTemp.c_str());
151 m->mothurOut("Changing input directory to " + input); m->mothurOutEndLine();
152 commandFactory->setInputDirectory(input);
157 if (tempdefault == "clear") {
159 string temp = MOTHUR_FILES;
160 m->mothurOut("Resetting default directory to " + temp); m->mothurOutEndLine();
161 m->setDefaultPath(temp);
164 m->mothurOut("No default directory defined at compile time."); m->mothurOutEndLine();
165 m->setDefaultPath(temp);
167 }else if (tempdefault == "") { //do nothing
168 }else if (tempdefault == "default") {
169 GlobalData* globaldata = GlobalData::getInstance();
170 string exepath = globaldata->argv;
171 tempdefault = exepath.substr(0, (exepath.find_last_of('m')));
173 m->mothurOut("Changing default directory to " + tempdefault); m->mothurOutEndLine();
174 m->setDefaultPath(tempdefault);
176 //add / to name if needed
177 string lastChar = tempdefault.substr(tempdefault.length()-1);
178 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
179 if (lastChar != "/") { tempdefault += "/"; }
181 if (lastChar != "\\") { tempdefault += "\\"; }
184 m->mothurOut("Changing default directory to " + tempdefault); m->mothurOutEndLine();
185 m->setDefaultPath(tempdefault);
190 catch(exception& e) {
191 m->errorOut(e, "SetDirectoryCommand", "execute");
195 //**********************************************************************************************************************/