]> git.donarmstrong.com Git - mothur.git/blob - setdircommand.cpp
fixed bugs for 1.8
[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","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                         if ((input == "") && (output == "")) {  
42                                 mothurOut("You must provide either an input or output for the set.outdir command."); mothurOutEndLine(); abort = true;
43                         }
44                 }
45         }
46         catch(exception& e) {
47                 errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
48                 exit(1);
49         }
50 }
51 //**********************************************************************************************************************
52
53 void SetDirectoryCommand::help(){
54         try {
55                 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");
56                 mothurOut("The set.dir command can also be used to specify the directory where your input files are located, the directory must exist.\n");
57                 mothurOut("The set.dir command parameter is output and is required.\n");
58                 mothurOut("To return the output to the same directory as the input files you may enter: output=clear.\n");
59                 mothurOut("To return the input to the same directory as the mothur.exe you may enter: input=clear.\n");
60                 mothurOut("The set.dir command should be in the following format: set.dir(output=yourOutputDirectory, input=yourInputDirectory).\n");
61                 mothurOut("Example set.outdir(output=/Users/lab/desktop/outputs, input=/Users/lab/desktop/inputs).\n");
62                 mothurOut("Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputDirectory).\n\n");
63         }
64         catch(exception& e) {
65                 errorOut(e, "SetDirectoryCommand", "help");
66                 exit(1);
67         }
68 }
69 //**********************************************************************************************************************
70
71 SetDirectoryCommand::~SetDirectoryCommand(){}
72
73 //**********************************************************************************************************************
74
75 int SetDirectoryCommand::execute(){
76         try {
77                 
78                 if (abort == true) { return 0; }
79                 
80                 commandFactory = CommandFactory::getInstance();
81                 
82                 
83                 //redirect output
84                 if ((output == "clear") || (output == "")) {  output = "";  commandFactory->setOutputDirectory(output);  }
85                 else {
86                         //add / to name if needed
87                         string lastChar = output.substr(output.length()-1);
88                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
89                                 if (lastChar != "/") { output += "/"; }
90                         #else
91                                 if (lastChar != "\\") { output += "\\"; }       
92                         #endif
93                         
94                         //test to make sure directory exists
95                         output = getFullPathName(output);
96                         string outTemp = output + "temp";
97                         ofstream out;
98                         out.open(outTemp.c_str(), ios::trunc);
99                         if(!out) {
100                                 mothurOut(output + " directory does not exist or is not writable."); mothurOutEndLine(); 
101                         }else{
102                                 out.close();
103                                 remove(outTemp.c_str());
104                                 mothurOut("Changing output directory to " + output); mothurOutEndLine();  
105                                 commandFactory->setOutputDirectory(output);
106                         }
107                 }
108                 
109                 //redirect input
110                 if ((input == "clear") || (input == "")) {  input = "";  commandFactory->setInputDirectory(input);  }
111                 else {
112                         //add / to name if needed
113                         string lastChar = input.substr(input.length()-1);
114                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
115                                 if (lastChar != "/") { input += "/"; }
116                         #else
117                                 if (lastChar != "\\") { input += "\\"; }        
118                         #endif
119                         
120                         //test to make sure directory exists
121                         input = getFullPathName(input);
122                         string inTemp = input + "temp";
123                         ofstream in;
124                         in.open(inTemp.c_str(), ios::trunc);
125                         if(!in) {
126                                 mothurOut(input + " directory does not exist or is not writable."); mothurOutEndLine(); 
127                         }else{
128                                 in.close();
129                                 remove(inTemp.c_str());
130                                 mothurOut("Changing input directory to " + input); mothurOutEndLine();  
131                                 commandFactory->setInputDirectory(input); 
132                         }
133                 }
134
135                 return 0;
136         }
137         catch(exception& e) {
138                 errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
139                 exit(1);
140         }
141 }
142 //**********************************************************************************************************************/