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