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