2 // loadlogfilecommand.cpp
5 // Created by Sarah Westcott on 6/13/12.
6 // Copyright (c) 2012 Schloss Lab. All rights reserved.
9 #include "loadlogfilecommand.h"
10 #include "commandoptionparser.hpp"
11 #include "commandfactory.hpp"
12 #include "setcurrentcommand.h"
14 //**********************************************************************************************************************
15 vector<string> LoadLogfileCommand::setParameters(){
17 CommandParameter plogfile("logfile", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(plogfile);
18 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
19 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
21 vector<string> myArray;
22 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
26 m->errorOut(e, "LoadLogfileCommand", "setParameters");
30 //**********************************************************************************************************************
31 string LoadLogfileCommand::getHelpString(){
33 string helpString = "";
34 helpString += "The load.logfile command extracts the current file names from a logfile.\n";
35 helpString += "The load.logfile parameter is logfile, and it is required.\n";
36 helpString += "The load.logfile command should be in the following format: \n";
37 helpString += "load.logfile(logfile=yourLogFile)\n";
41 m->errorOut(e, "LoadLogfileCommand", "getHelpString");
45 //**********************************************************************************************************************
46 LoadLogfileCommand::LoadLogfileCommand(){
48 abort = true; calledHelp = true;
52 m->errorOut(e, "LoadLogfileCommand", "LoadLogfileCommand");
56 //**********************************************************************************************************************
57 LoadLogfileCommand::LoadLogfileCommand(string option) {
59 abort = false; calledHelp = false;
61 //allow user to run help
62 if(option == "help") { help(); abort = true; calledHelp = true; }
63 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
66 //valid paramters for this command
67 vector<string> myArray = setParameters();
69 OptionParser parser(option);
70 map<string,string> parameters = parser.getParameters();
72 ValidParameters validParameter;
73 map<string,string>::iterator it;
74 //check to make sure all parameters are valid for command
75 for (it = parameters.begin(); it != parameters.end(); it++) {
76 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
79 //if the user changes the input directory command factory will send this info to us in the output parameter
80 string inputDir = validParameter.validFile(parameters, "inputdir", false);
81 if (inputDir == "not found"){ inputDir = ""; }
85 it = parameters.find("logfile");
86 //user has given a template file
87 if(it != parameters.end()){
88 path = m->hasPath(it->second);
89 //if the user has not given a path then, add inputdir. else leave path alone.
90 if (path == "") { parameters["logfile"] = inputDir + it->second; }
95 //get shared file, it is required
96 logfile = validParameter.validFile(parameters, "logfile", true);
97 if (logfile == "not open") { logfile = ""; abort = true; }
98 else if (logfile == "not found") { m->mothurOut("The logfile parameter is required."); m->mothurOutEndLine();abort = true; }
100 //if the user changes the output directory command factory will send this info to us in the output parameter
101 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
102 outputDir = m->hasPath(logfile); //if user entered a file with a path then preserve it
107 catch(exception& e) {
108 m->errorOut(e, "NewCommand", "NewCommand");
112 //**********************************************************************************************************************
114 int LoadLogfileCommand::execute(){
117 if (abort == true) { if (calledHelp) { return 0; } return 2; }
119 m->mothurOutEndLine();
120 m->mothurOut("Extracting current files names..."); m->mothurOutEndLine();
121 m->mothurOutEndLine();
123 CommandFactory* cFactory;
124 cFactory = CommandFactory::getInstance();
127 m->openInputFile(logfile, in);
129 set<string> currentTypes = m->getCurrentTypes();
130 map<string, string> currentFiles;
131 string commandName = "";
136 if (m->control_pressed) { break; }
138 if (!skip) { line = m->getline(in); m->gobble(in); }
141 //look for "mothur >"
142 int pos = line.find("mothur > "); //command line
143 int pos2 = line.find("Output File "); //indicates command completed and we can update the current file
144 int pos3 = line.find("/*****************");
146 //skipping over parts where a command runs another command
147 if (pos3 != string::npos) {
149 if (m->control_pressed) { break; }
150 line = m->getline(in); m->gobble(in);
151 int posTemp = line.find("/*****************");
152 if (posTemp != string::npos) { break; }
156 if (pos != string::npos) {
158 //extract command name and option string
159 string input = line.substr(pos+9);
160 CommandOptionParser parser(input);
161 commandName = parser.getCommandString();
162 string options = parser.getOptionString();
164 //parse out parameters in option string
165 map<string,string> parameters;
166 OptionParser optionParser(options, parameters);
168 for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
169 if (currentTypes.count((it->first)) != 0) { //if this is a type we save
170 if (it->second != "current") { currentFiles[it->first] = it->second; }//save the input file name as current
173 }else if (pos2 != string::npos) {
174 //read file output file names
175 vector<string> theseOutputNames;
177 if (m->control_pressed) { break; }
178 line = m->getline(in); m->gobble(in);
179 int pos = line.find("mothur > ");
180 if (pos != string::npos) { skip = true; break; }
181 else { theseOutputNames.push_back(line); }
183 //ask command for the output names for each type based on inputs
184 Command* command = cFactory->getCommand(commandName);
185 map<string, vector<string> > thisOutputTypes = command->getOutputFiles();
188 for (map<string, vector<string> >::iterator it = thisOutputTypes.begin(); it != thisOutputTypes.end(); it++) {
189 if (currentTypes.count((it->first)) != 0) { //do we save this type
190 //if yes whats its tag
191 map<string, string>::iterator itCurrentFiles = currentFiles.find(it->first);
192 string thisTypesCurrentFile = "";
193 if (itCurrentFiles != currentFiles.end()) { thisTypesCurrentFile = itCurrentFiles->second; }
195 //outputfilename pattern for this input type
196 string pattern = command->getOutputPattern(it->first);
197 updateCurrent(pattern, it->first, thisTypesCurrentFile, theseOutputNames, currentFiles);
199 //cout << "current=\n\n";
200 //for (map<string, string>::iterator itcc = currentFiles.begin(); itcc != currentFiles.end(); itcc++) {
201 // cout << itcc->first << '\t' << itcc->second << endl;
209 if (m->control_pressed) { return 0; }
212 string inputString = "";
213 for (map<string, string>::iterator it = currentFiles.begin(); it != currentFiles.end(); it++) { inputString += it->first + "=" + it->second + ","; }
215 if (inputString != "") {
216 inputString = inputString.substr(0, inputString.length()-1);
217 m->mothurOutEndLine();
218 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
219 m->mothurOut("Running command: set.current(" + inputString + ")"); m->mothurOutEndLine();
220 m->mothurCalling = true;
222 Command* currentCommand = new SetCurrentCommand(inputString);
223 currentCommand->execute();
225 delete currentCommand;
226 m->mothurCalling = false;
227 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
234 catch(exception& e) {
235 m->errorOut(e, "LoadLogfileCommand", "execute");
239 //**********************************************************************************************************************
241 int LoadLogfileCommand::updateCurrent(string pattern, string type, string thisTypesCurrentFile, vector<string> filenames, map<string, string>& currentFiles){
244 vector<string> patterns; m->splitAtChar(pattern, patterns, '-');
246 for (int i = 0; i < patterns.size(); i++) {
248 vector<string> peices; m->splitAtChar(patterns[i], peices, ',');
249 //cout << "patterns i = " << patterns[i] << endl;
250 if (peices.size() != 0) {
251 string tag = peices[peices.size()-1];
252 //cout << "tag = " << tag << endl;
253 if (peices[peices.size()-1] == "[extension]") { tag = m->getExtension(thisTypesCurrentFile); }
255 //search for the tag in the list of output files
256 for (int h = 0; h < filenames.size(); h++) {
258 if (m->control_pressed) { return 0; }
259 //cout << "filename h = " << filenames[h]<< endl;
260 string ending = filenames[h].substr(filenames[h].length()-tag.length(), tag.length());
261 if (ending == tag) { //if it's there and this is a type we save a current version of, save it
262 if ((type == "column") || (type == "phylip")) { //check for format
263 string RippedName = "";
264 bool foundDot = false;
265 for (int i = filenames[h].length()-1; i >= 0; i--) {
266 if (foundDot && (filenames[h][i] != '.')) { RippedName = filenames[h][i] + RippedName; }
267 else if (foundDot && (filenames[h][i] == '.')) { break; }
268 else if (!foundDot && (filenames[h][i] == '.')) { foundDot = true; }
270 if ((RippedName == "phylip") || (RippedName == "lt") || (RippedName == "square")) { currentFiles["phylip"] = filenames[h]; }
271 else { currentFiles["column"] = filenames[h]; }
272 }else { currentFiles[type] = filenames[h]; }
281 catch(exception& e) {
282 m->errorOut(e, "LoadLogfileCommand", "updateCurrent");
286 //**********************************************************************************************************************