]> git.donarmstrong.com Git - mothur.git/blob - setdircommand.cpp
Merge remote-tracking branch 'mothur/master'
[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 pdebug("debug", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pdebug);
17                 CommandParameter pinput("input", "String", "", "", "", "", "",false,false); parameters.push_back(pinput);
18                 CommandParameter poutput("output", "String", "", "", "", "", "",false,false); parameters.push_back(poutput);
19                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
20                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
21                 
22                 vector<string> myArray;
23                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
24                 return myArray;
25         }
26         catch(exception& e) {
27                 m->errorOut(e, "SetDirectoryCommand", "setParameters");
28                 exit(1);
29         }
30 }
31 //**********************************************************************************************************************
32 string SetDirectoryCommand::getHelpString(){    
33         try {
34                 string helpString = "";
35                 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";
36                 helpString += "The set.dir command can also be used to specify the directory where your input files are located, the directory must exist.\n";
37                 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";
38         helpString += "The set.dir command can also be used to run mothur in debug mode.\n";
39                 helpString += "The set.dir command parameters are input, output, tempdefault and debug and one is required.\n";
40         helpString += "To run mothur in debug mode set debug=true. Default debug=false.\n";
41                 helpString += "To return the output to the same directory as the input files you may enter: output=clear.\n";
42                 helpString += "To return the input to the current working directory you may enter: input=clear.\n";
43                 helpString += "To set the output to the directory where mothur.exe is located you may enter: output=default.\n";
44                 helpString += "To set the input to the directory where mothur.exe is located you may enter: input=default.\n";
45                 helpString += "To return the tempdefault to the default you provided at compile time you may enter: tempdefault=clear.\n";
46                 helpString += "To set the tempdefault to the directory where mothur.exe is located you may enter: tempdefault=default.\n";
47                 helpString += "The set.dir command should be in the following format: set.dir(output=yourOutputDirectory, input=yourInputDirectory, tempdefault=yourTempDefault).\n";
48                 helpString += "Example set.outdir(output=/Users/lab/desktop/outputs, input=/Users/lab/desktop/inputs).\n";
49                 helpString += "Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputDirectory).\n";
50                 return helpString;
51         }
52         catch(exception& e) {
53                 m->errorOut(e, "SetDirectoryCommand", "getHelpString");
54                 exit(1);
55         }
56 }
57 //**********************************************************************************************************************
58
59 SetDirectoryCommand::SetDirectoryCommand(string option)  {
60         try {
61                 abort = false; calledHelp = false;   
62                 
63                 //allow user to run help
64                 if(option == "help") { help(); abort = true; calledHelp = true; }
65                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
66                 
67                 else {
68                         vector<string> myArray = setParameters();
69                         
70                         OptionParser parser(option);
71                         map<string, string> parameters = parser.getParameters();
72                         
73                         ValidParameters validParameter;
74                         //check to make sure all parameters are valid for command
75                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
76                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
77                         }
78                 
79                         output = validParameter.validFile(parameters, "output", false);                 
80                         if (output == "not found") {  output = "";  } 
81                         
82                         input = validParameter.validFile(parameters, "input", false);                   
83                         if (input == "not found") {  input = "";  }
84                         
85                         tempdefault = validParameter.validFile(parameters, "tempdefault", false);                       
86                         if (tempdefault == "not found") {  tempdefault = "";  }
87             
88             bool debug = false;
89             bool nodebug = false;
90             debugOnly = false;
91             string temp = validParameter.validFile(parameters, "debug", false);                 
92                         if (temp == "not found") {  debug = false;  nodebug=true; }
93             else {  debug = m->isTrue(temp); }
94             m->debug = debug;
95                                 
96                         if ((input == "") && (output == "") && (tempdefault == "") && nodebug) {        
97                                 m->mothurOut("You must provide either an input, output, tempdefault or debug for the set.outdir command."); m->mothurOutEndLine(); abort = true;
98                         }else if((input == "") && (output == "") && (tempdefault == "")) { debugOnly = true; }
99                 }
100         }
101         catch(exception& e) {
102                 m->errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand");
103                 exit(1);
104         }
105 }
106 //**********************************************************************************************************************
107
108 int SetDirectoryCommand::execute(){
109         try {
110                 
111                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
112                 
113         if (debugOnly) { return 0; }
114         
115                 commandFactory = CommandFactory::getInstance();
116                 
117                 m->mothurOut("Mothur's directories:"); m->mothurOutEndLine();
118                 
119                 //redirect output
120                 if ((output == "clear") || (output == "")) {  output = "";  commandFactory->setOutputDirectory(output);  }
121                 else if (output == "default") { 
122                         string exepath = m->argv;
123                         output = exepath.substr(0, (exepath.find_last_of('m')));
124                         
125                         m->mothurOut("outputDir=" + output); m->mothurOutEndLine();  
126                         commandFactory->setOutputDirectory(output);
127                 }else {
128             if (m->dirCheck(output)) {
129                 m->mothurOut("outputDir=" + output); m->mothurOutEndLine();  
130                                 commandFactory->setOutputDirectory(output);
131             }
132                 }
133                 
134                 //redirect input
135                 if ((input == "clear") || (input == "")) {  input = "";  commandFactory->setInputDirectory(input);  }
136                 else if (input == "default") { 
137                         string exepath = m->argv;
138                         input = exepath.substr(0, (exepath.find_last_of('m')));
139                         
140                         m->mothurOut("inputDir=" + input); m->mothurOutEndLine();  
141                         commandFactory->setInputDirectory(input);
142                 }else {
143             if (m->dirCheck(input)) {
144                 m->mothurOut("inputDir=" + input); m->mothurOutEndLine();  
145                                 commandFactory->setInputDirectory(input); 
146             }
147         }
148                 
149                 //set default
150                 if (tempdefault == "clear") {  
151                         #ifdef MOTHUR_FILES
152                                 string temp = MOTHUR_FILES; 
153                                 m->mothurOut("tempDefault=" + temp); m->mothurOutEndLine();  
154                                 m->setDefaultPath(temp);
155                         #else
156                                 string temp = ""; 
157                                 m->mothurOut("No default directory defined at compile time."); m->mothurOutEndLine();  
158                                 m->setDefaultPath(temp);
159                         #endif
160                 }else if (tempdefault == "") {  //do nothing
161                 }else if (tempdefault == "default") { 
162                         string exepath = m->argv;
163                         tempdefault = exepath.substr(0, (exepath.find_last_of('m')));
164                         
165                         m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();  
166                         m->setDefaultPath(tempdefault);
167                 }else {
168             if (m->dirCheck(tempdefault)) {
169                 m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();  
170                                 m->setDefaultPath(tempdefault); 
171             }
172         }
173
174                 return 0;
175         }
176         catch(exception& e) {
177                 m->errorOut(e, "SetDirectoryCommand", "execute");
178                 exit(1);
179         }
180 }
181 //**********************************************************************************************************************/