]> 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 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                 m->mothurOut("Mothur's directories:"); m->mothurOutEndLine();
105                 
106                 //redirect output
107                 if ((output == "clear") || (output == "")) {  output = "";  commandFactory->setOutputDirectory(output);  }
108                 else if (output == "default") { 
109                         string exepath = m->argv;
110                         output = exepath.substr(0, (exepath.find_last_of('m')));
111                         
112                         m->mothurOut("outputDir=" + output); m->mothurOutEndLine();  
113                         commandFactory->setOutputDirectory(output);
114                 }else {
115             if (m->dirCheck(output)) {
116                 m->mothurOut("outputDir=" + output); m->mothurOutEndLine();  
117                                 commandFactory->setOutputDirectory(output);
118             }
119                 }
120                 
121                 //redirect input
122                 if ((input == "clear") || (input == "")) {  input = "";  commandFactory->setInputDirectory(input);  }
123                 else if (input == "default") { 
124                         string exepath = m->argv;
125                         input = exepath.substr(0, (exepath.find_last_of('m')));
126                         
127                         m->mothurOut("inputDir=" + input); m->mothurOutEndLine();  
128                         commandFactory->setInputDirectory(input);
129                 }else {
130             if (m->dirCheck(input)) {
131                 m->mothurOut("inputDir=" + input); m->mothurOutEndLine();  
132                                 commandFactory->setInputDirectory(input); 
133             }
134         }
135                 
136                 //set default
137                 if (tempdefault == "clear") {  
138                         #ifdef MOTHUR_FILES
139                                 string temp = MOTHUR_FILES; 
140                                 m->mothurOut("tempDefault=" + temp); m->mothurOutEndLine();  
141                                 m->setDefaultPath(temp);
142                         #else
143                                 string temp = ""; 
144                                 m->mothurOut("No default directory defined at compile time."); m->mothurOutEndLine();  
145                                 m->setDefaultPath(temp);
146                         #endif
147                 }else if (tempdefault == "") {  //do nothing
148                 }else if (tempdefault == "default") { 
149                         string exepath = m->argv;
150                         tempdefault = exepath.substr(0, (exepath.find_last_of('m')));
151                         
152                         m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();  
153                         m->setDefaultPath(tempdefault);
154                 }else {
155             if (m->dirCheck(tempdefault)) {
156                 m->mothurOut("tempDefault=" + tempdefault); m->mothurOutEndLine();  
157                                 m->setDefaultPath(tempdefault); 
158             }
159         }
160
161                 return 0;
162         }
163         catch(exception& e) {
164                 m->errorOut(e, "SetDirectoryCommand", "execute");
165                 exit(1);
166         }
167 }
168 //**********************************************************************************************************************/