]> git.donarmstrong.com Git - mothur.git/blob - setdircommand.cpp
added getCommandInfoCommand for gui
[mothur.git] / setdircommand.cpp
1 /*
2  *  setoutdircommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 1/21/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "setdircommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> SetDirectoryCommand::setParameters(){    
14         try {
15                 CommandParameter ptempdefault("tempdefault", "String", "", "", "", "", "",false,false); parameters.push_back(ptempdefault);     
16                 CommandParameter pinput("input", "String", "", "", "", "", "",false,false); parameters.push_back(pinput);
17                 CommandParameter poutput("output", "String", "", "", "", "", "",false,false); parameters.push_back(poutput);
18                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
19                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
20                 
21                 vector<string> myArray;
22                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
23                 return myArray;
24         }
25         catch(exception& e) {
26                 m->errorOut(e, "SetDirectoryCommand", "setParameters");
27                 exit(1);
28         }
29 }
30 //**********************************************************************************************************************
31 string SetDirectoryCommand::getHelpString(){    
32         try {
33                 string helpString = "";
34                 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";
35                 helpString += "The set.dir command can also be used to specify the directory where your input files are located, the directory must exist.\n";
36                 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";
37                 helpString += "The set.dir command parameters are input, output and tempdefault and one is required.\n";
38                 helpString += "To return the output to the same directory as the input files you may enter: output=clear.\n";
39                 helpString += "To return the input to the current working directory you may enter: input=clear.\n";
40                 helpString += "To set the output to the directory where mothur.exe is located you may enter: output=default.\n";
41                 helpString += "To set the input to the directory where mothur.exe is located you may enter: input=default.\n";
42                 helpString += "To return the tempdefault to the default you provided at compile time you may enter: tempdefault=clear.\n";
43                 helpString += "To set the tempdefault to the directory where mothur.exe is located you may enter: tempdefault=default.\n";
44                 helpString += "The set.dir command should be in the following format: set.dir(output=yourOutputDirectory, input=yourInputDirectory, tempdefault=yourTempDefault).\n";
45                 helpString += "Example set.outdir(output=/Users/lab/desktop/outputs, input=/Users/lab/desktop/inputs).\n";
46                 helpString += "Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputDirectory).\n";
47                 return helpString;
48         }
49         catch(exception& e) {
50                 m->errorOut(e, "SetDirectoryCommand", "getHelpString");
51                 exit(1);
52         }
53 }
54 //**********************************************************************************************************************
55
56 SetDirectoryCommand::SetDirectoryCommand(string option)  {
57         try {
58                 abort = false; calledHelp = false;   
59                 
60                 //allow user to run help
61                 if(option == "help") { help(); abort = true; calledHelp = true; }
62                 
63                 else {
64                         vector<string> myArray = setParameters();
65                         
66                         OptionParser parser(option);
67                         map<string, string> parameters = parser.getParameters();
68                         
69                         ValidParameters validParameter;
70                         //check to make sure all parameters are valid for command
71                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
72                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
73                         }
74                 
75                         output = validParameter.validFile(parameters, "output", false);                 
76                         if (output == "not found") {  output = "";  } 
77                         
78                         input = validParameter.validFile(parameters, "input", false);                   
79                         if (input == "not found") {  input = "";  }
80                         
81                         tempdefault = validParameter.validFile(parameters, "tempdefault", false);                       
82                         if (tempdefault == "not found") {  tempdefault = "";  }
83                                 
84                         if ((input == "") && (output == "") && (tempdefault == "")) {   
85                                 m->mothurOut("You must provide either an input, output or tempdefault for the set.outdir command."); m->mothurOutEndLine(); abort = true;
86                         }
87                 }
88         }
89         catch(exception& e) {
90                 m->errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
91                 exit(1);
92         }
93 }
94 //**********************************************************************************************************************
95
96 int SetDirectoryCommand::execute(){
97         try {
98                 
99                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
100                 
101                 commandFactory = CommandFactory::getInstance();
102                 
103                 //redirect output
104                 if ((output == "clear") || (output == "")) {  output = "";  commandFactory->setOutputDirectory(output);  }
105                 else if (output == "default") { 
106                         string exepath = m->argv;
107                         output = exepath.substr(0, (exepath.find_last_of('m')));
108                         
109                         m->mothurOut("Changing output directory to " + output); m->mothurOutEndLine();  
110                         commandFactory->setOutputDirectory(output);
111                 }else {
112                         //add / to name if needed
113                         string lastChar = output.substr(output.length()-1);
114                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
115                                 if (lastChar != "/") { output += "/"; }
116                         #else
117                                 if (lastChar != "\\") { output += "\\"; }       
118                         #endif
119                         
120                         //test to make sure directory exists
121                         output = m->getFullPathName(output);
122                         string outTemp = output + "temp";
123                         ofstream out;
124                         out.open(outTemp.c_str(), ios::trunc);
125                         if(!out) {
126                                 m->mothurOut(output + " directory does not exist or is not writable."); m->mothurOutEndLine(); 
127                         }else{
128                                 out.close();
129                                 remove(outTemp.c_str());
130                                 m->mothurOut("Changing output directory to " + output); m->mothurOutEndLine();  
131                                 commandFactory->setOutputDirectory(output);
132                         }
133                 }
134                 
135                 //redirect input
136                 if ((input == "clear") || (input == "")) {  input = "";  commandFactory->setInputDirectory(input);  }
137                 else if (input == "default") { 
138                         string exepath = m->argv;
139                         input = exepath.substr(0, (exepath.find_last_of('m')));
140                         
141                         m->mothurOut("Changing input directory to " + input); m->mothurOutEndLine();  
142                         commandFactory->setInputDirectory(input);
143                 }else {
144                         //add / to name if needed
145                         string lastChar = input.substr(input.length()-1);
146                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
147                                 if (lastChar != "/") { input += "/"; }
148                         #else
149                                 if (lastChar != "\\") { input += "\\"; }        
150                         #endif
151                         
152                         //test to make sure directory exists
153                         input = m->getFullPathName(input);
154                         string inTemp = input + "temp";
155                         ofstream in;
156                         in.open(inTemp.c_str(), ios::trunc);
157                         if(!in) {
158                                 m->mothurOut(input + " directory does not exist or is not writable."); m->mothurOutEndLine(); 
159                         }else{
160                                 in.close();
161                                 remove(inTemp.c_str());
162                                 m->mothurOut("Changing input directory to " + input); m->mothurOutEndLine();  
163                                 commandFactory->setInputDirectory(input); 
164                         }
165                 }
166                 
167                 //set default
168                 if (tempdefault == "clear") {  
169                         #ifdef MOTHUR_FILES
170                                 string temp = MOTHUR_FILES; 
171                                 m->mothurOut("Resetting default directory to " + temp); m->mothurOutEndLine();  
172                                 m->setDefaultPath(temp);
173                         #else
174                                 string temp = ""; 
175                                 m->mothurOut("No default directory defined at compile time."); m->mothurOutEndLine();  
176                                 m->setDefaultPath(temp);
177                         #endif
178                 }else if (tempdefault == "") {  //do nothing
179                 }else if (tempdefault == "default") { 
180                         string exepath = m->argv;
181                         tempdefault = exepath.substr(0, (exepath.find_last_of('m')));
182                         
183                         m->mothurOut("Changing default directory to " + tempdefault); m->mothurOutEndLine();  
184                         m->setDefaultPath(tempdefault);
185                 }else {
186                         //add / to name if needed
187                         string lastChar = tempdefault.substr(tempdefault.length()-1);
188                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
189                                 if (lastChar != "/") { tempdefault += "/"; }
190                         #else
191                                 if (lastChar != "\\") { tempdefault += "\\"; }  
192                         #endif
193                         
194                         m->mothurOut("Changing default directory to " + tempdefault); m->mothurOutEndLine();  
195                         m->setDefaultPath(tempdefault);
196                 }
197
198                 return 0;
199         }
200         catch(exception& e) {
201                 m->errorOut(e, "SetDirectoryCommand", "execute");
202                 exit(1);
203         }
204 }
205 //**********************************************************************************************************************/