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 same directory as the mothur.exe you may enter: input=clear.\n");
64 m->mothurOut("To return the tempdefault to the default you provided at compile time you may enter: tempdefault=clear.\n");
65 m->mothurOut("The set.dir command should be in the following format: set.dir(output=yourOutputDirectory, input=yourInputDirectory, tempdefault=yourTempDefault).\n");
66 m->mothurOut("Example set.outdir(output=/Users/lab/desktop/outputs, input=/Users/lab/desktop/inputs).\n");
67 m->mothurOut("Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputDirectory).\n\n");
70 m->errorOut(e, "SetDirectoryCommand", "help");
74 //**********************************************************************************************************************
76 SetDirectoryCommand::~SetDirectoryCommand(){}
78 //**********************************************************************************************************************
80 int SetDirectoryCommand::execute(){
83 if (abort == true) { return 0; }
85 commandFactory = CommandFactory::getInstance();
88 if ((output == "clear") || (output == "")) { output = ""; commandFactory->setOutputDirectory(output); }
90 //add / to name if needed
91 string lastChar = output.substr(output.length()-1);
92 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
93 if (lastChar != "/") { output += "/"; }
95 if (lastChar != "\\") { output += "\\"; }
98 //test to make sure directory exists
99 output = getFullPathName(output);
100 string outTemp = output + "temp";
102 out.open(outTemp.c_str(), ios::trunc);
104 m->mothurOut(output + " directory does not exist or is not writable."); m->mothurOutEndLine();
107 remove(outTemp.c_str());
108 m->mothurOut("Changing output directory to " + output); m->mothurOutEndLine();
109 commandFactory->setOutputDirectory(output);
114 if ((input == "clear") || (input == "")) { input = ""; commandFactory->setInputDirectory(input); }
116 //add / to name if needed
117 string lastChar = input.substr(input.length()-1);
118 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
119 if (lastChar != "/") { input += "/"; }
121 if (lastChar != "\\") { input += "\\"; }
124 //test to make sure directory exists
125 input = getFullPathName(input);
126 string inTemp = input + "temp";
128 in.open(inTemp.c_str(), ios::trunc);
130 m->mothurOut(input + " directory does not exist or is not writable."); m->mothurOutEndLine();
133 remove(inTemp.c_str());
134 m->mothurOut("Changing input directory to " + input); m->mothurOutEndLine();
135 commandFactory->setInputDirectory(input);
140 if (tempdefault == "clear") {
142 string temp = MOTHUR_FILES;
143 m->mothurOut("Resetting default directory to " + temp); m->mothurOutEndLine();
144 m->setDefaultPath(temp);
147 m->mothurOut("No default directory defined at compile time."); m->mothurOutEndLine();
148 m->setDefaultPath(temp);
150 }else if (tempdefault == "") { //do nothing
152 //add / to name if needed
153 string lastChar = tempdefault.substr(tempdefault.length()-1);
154 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
155 if (lastChar != "/") { tempdefault += "/"; }
157 if (lastChar != "\\") { tempdefault += "\\"; }
160 //test to make sure directory exists
161 tempdefault = getFullPathName(tempdefault);
162 string inTemp = tempdefault + "temp";
164 in.open(inTemp.c_str(), ios::trunc);
166 m->mothurOut(tempdefault + " directory does not exist or is not writable."); m->mothurOutEndLine();
169 remove(inTemp.c_str());
170 m->mothurOut("Changing default directory to " + tempdefault); m->mothurOutEndLine();
171 m->setDefaultPath(tempdefault);
177 catch(exception& e) {
178 m->errorOut(e, "SetDirectoryCommand", "execute");
182 //**********************************************************************************************************************/