]> git.donarmstrong.com Git - mothur.git/blob - setdircommand.cpp
changes for 1.12.2
[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 same directory as the mothur.exe you may enter: input=clear.\n");
64                 m->mothurOut("To return the tempdefault to the default you provided at compile time you may enter: tempdefault=clear.\n");
65                 m->mothurOut("The set.dir command should be in the following format: set.dir(output=yourOutputDirectory, input=yourInputDirectory, tempdefault=yourTempDefault).\n");
66                 m->mothurOut("Example set.outdir(output=/Users/lab/desktop/outputs, input=/Users/lab/desktop/inputs).\n");
67                 m->mothurOut("Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputDirectory).\n\n");
68         }
69         catch(exception& e) {
70                 m->errorOut(e, "SetDirectoryCommand", "help");
71                 exit(1);
72         }
73 }
74 //**********************************************************************************************************************
75
76 SetDirectoryCommand::~SetDirectoryCommand(){}
77
78 //**********************************************************************************************************************
79
80 int SetDirectoryCommand::execute(){
81         try {
82                 
83                 if (abort == true) { return 0; }
84                 
85                 commandFactory = CommandFactory::getInstance();
86                 
87                 //redirect output
88                 if ((output == "clear") || (output == "")) {  output = "";  commandFactory->setOutputDirectory(output);  }
89                 else {
90                         //add / to name if needed
91                         string lastChar = output.substr(output.length()-1);
92                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
93                                 if (lastChar != "/") { output += "/"; }
94                         #else
95                                 if (lastChar != "\\") { output += "\\"; }       
96                         #endif
97                         
98                         //test to make sure directory exists
99                         output = getFullPathName(output);
100                         string outTemp = output + "temp";
101                         ofstream out;
102                         out.open(outTemp.c_str(), ios::trunc);
103                         if(!out) {
104                                 m->mothurOut(output + " directory does not exist or is not writable."); m->mothurOutEndLine(); 
105                         }else{
106                                 out.close();
107                                 remove(outTemp.c_str());
108                                 m->mothurOut("Changing output directory to " + output); m->mothurOutEndLine();  
109                                 commandFactory->setOutputDirectory(output);
110                         }
111                 }
112                 
113                 //redirect input
114                 if ((input == "clear") || (input == "")) {  input = "";  commandFactory->setInputDirectory(input);  }
115                 else {
116                         //add / to name if needed
117                         string lastChar = input.substr(input.length()-1);
118                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
119                                 if (lastChar != "/") { input += "/"; }
120                         #else
121                                 if (lastChar != "\\") { input += "\\"; }        
122                         #endif
123                         
124                         //test to make sure directory exists
125                         input = getFullPathName(input);
126                         string inTemp = input + "temp";
127                         ofstream in;
128                         in.open(inTemp.c_str(), ios::trunc);
129                         if(!in) {
130                                 m->mothurOut(input + " directory does not exist or is not writable."); m->mothurOutEndLine(); 
131                         }else{
132                                 in.close();
133                                 remove(inTemp.c_str());
134                                 m->mothurOut("Changing input directory to " + input); m->mothurOutEndLine();  
135                                 commandFactory->setInputDirectory(input); 
136                         }
137                 }
138                 
139                 //set default
140                 if (tempdefault == "clear") {  
141                         #ifdef MOTHUR_FILES
142                                 string temp = MOTHUR_FILES; 
143                                 m->mothurOut("Resetting default directory to " + temp); m->mothurOutEndLine();  
144                                 m->setDefaultPath(temp);
145                         #else
146                                 string temp = ""; 
147                                 m->mothurOut("No default directory defined at compile time."); m->mothurOutEndLine();  
148                                 m->setDefaultPath(temp);
149                         #endif
150                 }else if (tempdefault == "") {  //do nothing
151                 }else {
152                         //add / to name if needed
153                         string lastChar = tempdefault.substr(tempdefault.length()-1);
154                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
155                                 if (lastChar != "/") { tempdefault += "/"; }
156                         #else
157                                 if (lastChar != "\\") { tempdefault += "\\"; }  
158                         #endif
159                         
160                         m->mothurOut("Changing default directory to " + tempdefault); m->mothurOutEndLine();  
161                         m->setDefaultPath(tempdefault);
162                 }
163
164                 return 0;
165         }
166         catch(exception& e) {
167                 m->errorOut(e, "SetDirectoryCommand", "execute");
168                 exit(1);
169         }
170 }
171 //**********************************************************************************************************************/