]> git.donarmstrong.com Git - mothur.git/blob - setdircommand.cpp
1.18.0
[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                 m->mothurOut("Mothur's directories:"); m->mothurOutEndLine();
104                 
105                 //redirect output
106                 if ((output == "clear") || (output == "")) {  output = "";  commandFactory->setOutputDirectory(output);  }
107                 else if (output == "default") { 
108                         string exepath = m->argv;
109                         output = exepath.substr(0, (exepath.find_last_of('m')));
110                         
111                         m->mothurOut("outputDir=" + output); m->mothurOutEndLine();  
112                         commandFactory->setOutputDirectory(output);
113                 }else {
114                         //add / to name if needed
115                         string lastChar = output.substr(output.length()-1);
116                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
117                                 if (lastChar != "/") { output += "/"; }
118                         #else
119                                 if (lastChar != "\\") { output += "\\"; }       
120                         #endif
121                         
122                         //test to make sure directory exists
123                         output = m->getFullPathName(output);
124                         string outTemp = output + "temp";
125                         ofstream out;
126                         out.open(outTemp.c_str(), ios::trunc);
127                         if(!out) {
128                                 m->mothurOut(output + " directory does not exist or is not writable."); m->mothurOutEndLine(); 
129                         }else{
130                                 out.close();
131                                 remove(outTemp.c_str());
132                                 m->mothurOut("outputDir=" + output); m->mothurOutEndLine();  
133                                 commandFactory->setOutputDirectory(output);
134                         }
135                 }
136                 
137                 //redirect input
138                 if ((input == "clear") || (input == "")) {  input = "";  commandFactory->setInputDirectory(input);  }
139                 else if (input == "default") { 
140                         string exepath = m->argv;
141                         input = exepath.substr(0, (exepath.find_last_of('m')));
142                         
143                         m->mothurOut("inputDir=" + input); m->mothurOutEndLine();  
144                         commandFactory->setInputDirectory(input);
145                 }else {
146                         //add / to name if needed
147                         string lastChar = input.substr(input.length()-1);
148                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
149                                 if (lastChar != "/") { input += "/"; }
150                         #else
151                                 if (lastChar != "\\") { input += "\\"; }        
152                         #endif
153                         
154                         //test to make sure directory exists
155                         input = m->getFullPathName(input);
156                         string inTemp = input + "temp";
157                         ofstream in;
158                         in.open(inTemp.c_str(), ios::trunc);
159                         if(!in) {
160                                 m->mothurOut(input + " directory does not exist or is not writable."); m->mothurOutEndLine(); 
161                         }else{
162                                 in.close();
163                                 remove(inTemp.c_str());
164                                 m->mothurOut("inputDir=" + input); m->mothurOutEndLine();  
165                                 commandFactory->setInputDirectory(input); 
166                         }
167                 }
168                 
169                 //set default
170                 if (tempdefault == "clear") {  
171                         #ifdef MOTHUR_FILES
172                                 string temp = MOTHUR_FILES; 
173                                 m->mothurOut("tempDefault=" + temp); m->mothurOutEndLine();  
174                                 m->setDefaultPath(temp);
175                         #else
176                                 string temp = ""; 
177                                 m->mothurOut("No default directory defined at compile time."); m->mothurOutEndLine();  
178                                 m->setDefaultPath(temp);
179                         #endif
180                 }else if (tempdefault == "") {  //do nothing
181                 }else if (tempdefault == "default") { 
182                         string exepath = m->argv;
183                         tempdefault = exepath.substr(0, (exepath.find_last_of('m')));
184                         
185                         m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();  
186                         m->setDefaultPath(tempdefault);
187                 }else {
188                         //add / to name if needed
189                         string lastChar = tempdefault.substr(tempdefault.length()-1);
190                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
191                                 if (lastChar != "/") { tempdefault += "/"; }
192                         #else
193                                 if (lastChar != "\\") { tempdefault += "\\"; }  
194                         #endif
195                         
196                         m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();  
197                         m->setDefaultPath(tempdefault);
198                 }
199
200                 return 0;
201         }
202         catch(exception& e) {
203                 m->errorOut(e, "SetDirectoryCommand", "execute");
204                 exit(1);
205         }
206 }
207 //**********************************************************************************************************************/