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