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