]> git.donarmstrong.com Git - mothur.git/blob - setdircommand.cpp
fixed bug with shhh.flow from file path name in write functions, added "smart" featur...
[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                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
63                 
64                 else {
65                         vector<string> myArray = setParameters();
66                         
67                         OptionParser parser(option);
68                         map<string, string> parameters = parser.getParameters();
69                         
70                         ValidParameters validParameter;
71                         //check to make sure all parameters are valid for command
72                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
73                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
74                         }
75                 
76                         output = validParameter.validFile(parameters, "output", false);                 
77                         if (output == "not found") {  output = "";  } 
78                         
79                         input = validParameter.validFile(parameters, "input", false);                   
80                         if (input == "not found") {  input = "";  }
81                         
82                         tempdefault = validParameter.validFile(parameters, "tempdefault", false);                       
83                         if (tempdefault == "not found") {  tempdefault = "";  }
84                                 
85                         if ((input == "") && (output == "") && (tempdefault == "")) {   
86                                 m->mothurOut("You must provide either an input, output or tempdefault for the set.outdir command."); m->mothurOutEndLine(); abort = true;
87                         }
88                 }
89         }
90         catch(exception& e) {
91                 m->errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
92                 exit(1);
93         }
94 }
95 //**********************************************************************************************************************
96
97 int SetDirectoryCommand::execute(){
98         try {
99                 
100                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
101                 
102                 commandFactory = CommandFactory::getInstance();
103                 
104                 string tag = "";
105 #ifdef USE_MPI
106                 int pid; 
107                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
108                 
109                 tag = toString(pid);
110 #endif
111                 
112                 m->mothurOut("Mothur's directories:"); m->mothurOutEndLine();
113                 
114                 //redirect output
115                 if ((output == "clear") || (output == "")) {  output = "";  commandFactory->setOutputDirectory(output);  }
116                 else if (output == "default") { 
117                         string exepath = m->argv;
118                         output = exepath.substr(0, (exepath.find_last_of('m')));
119                         
120                         m->mothurOut("outputDir=" + output); m->mothurOutEndLine();  
121                         commandFactory->setOutputDirectory(output);
122                 }else {
123                         //add / to name if needed
124                         string lastChar = output.substr(output.length()-1);
125                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
126                                 if (lastChar != "/") { output += "/"; }
127                         #else
128                                 if (lastChar != "\\") { output += "\\"; }       
129                         #endif
130                         
131                         //test to make sure directory exists
132                         output = m->getFullPathName(output);
133                         string outTemp = output + tag + "temp";
134                         ofstream out;
135                         out.open(outTemp.c_str(), ios::trunc);
136                         if(!out) {
137                                 m->mothurOut(output + " directory does not exist or is not writable."); m->mothurOutEndLine(); 
138                         }else{
139                                 out.close();
140                                 m->mothurRemove(outTemp);
141                                 m->mothurOut("outputDir=" + output); m->mothurOutEndLine();  
142                                 commandFactory->setOutputDirectory(output);
143                         }
144                 }
145                 
146                 //redirect input
147                 if ((input == "clear") || (input == "")) {  input = "";  commandFactory->setInputDirectory(input);  }
148                 else if (input == "default") { 
149                         string exepath = m->argv;
150                         input = exepath.substr(0, (exepath.find_last_of('m')));
151                         
152                         m->mothurOut("inputDir=" + input); m->mothurOutEndLine();  
153                         commandFactory->setInputDirectory(input);
154                 }else {
155                         //add / to name if needed
156                         string lastChar = input.substr(input.length()-1);
157                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
158                                 if (lastChar != "/") { input += "/"; }
159                         #else
160                                 if (lastChar != "\\") { input += "\\"; }        
161                         #endif
162                         
163                         //test to make sure directory exists
164                         input = m->getFullPathName(input);
165                         string inTemp = input + tag + "temp";
166                         ofstream in;
167                         in.open(inTemp.c_str(), ios::trunc);
168                         if(!in) {
169                                 m->mothurOut(input + " directory does not exist or is not writable."); m->mothurOutEndLine(); 
170                         }else{
171                                 in.close();
172                                 m->mothurRemove(inTemp);
173                                 m->mothurOut("inputDir=" + input); m->mothurOutEndLine();  
174                                 commandFactory->setInputDirectory(input); 
175                         }
176                 }
177                 
178                 //set default
179                 if (tempdefault == "clear") {  
180                         #ifdef MOTHUR_FILES
181                                 string temp = MOTHUR_FILES; 
182                                 m->mothurOut("tempDefault=" + temp); m->mothurOutEndLine();  
183                                 m->setDefaultPath(temp);
184                         #else
185                                 string temp = ""; 
186                                 m->mothurOut("No default directory defined at compile time."); m->mothurOutEndLine();  
187                                 m->setDefaultPath(temp);
188                         #endif
189                 }else if (tempdefault == "") {  //do nothing
190                 }else if (tempdefault == "default") { 
191                         string exepath = m->argv;
192                         tempdefault = exepath.substr(0, (exepath.find_last_of('m')));
193                         
194                         m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();  
195                         m->setDefaultPath(tempdefault);
196                 }else {
197                         //add / to name if needed
198                         string lastChar = tempdefault.substr(tempdefault.length()-1);
199                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
200                                 if (lastChar != "/") { tempdefault += "/"; }
201                         #else
202                                 if (lastChar != "\\") { tempdefault += "\\"; }  
203                         #endif
204                         
205                         //test to make sure directory exists
206                         tempdefault = m->getFullPathName(tempdefault);
207                         string inTemp = tempdefault + tag + "temp";
208                         ofstream in;
209                         in.open(inTemp.c_str(), ios::trunc);
210                         if(!in) {
211                                 m->mothurOut(tempdefault + " directory does not exist or is not writable."); m->mothurOutEndLine(); 
212                         }else{
213                                 in.close();
214                                 m->mothurRemove(inTemp);
215                                 m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();  
216                                 m->setDefaultPath(tempdefault);
217                         }
218                 }
219
220                 return 0;
221         }
222         catch(exception& e) {
223                 m->errorOut(e, "SetDirectoryCommand", "execute");
224                 exit(1);
225         }
226 }
227 //**********************************************************************************************************************/