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); 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();
187 for (map<string, vector<string> >::iterator it = thisOutputTypes.begin(); it != thisOutputTypes.end(); it++) {
188 if (currentTypes.count((it->first)) != 0) { //do we save this type
189 //if yes whats its tag
190 map<string, string>::iterator itCurrentFiles = currentFiles.find(it->first);
191 string thisTypesCurrentFile = "";
192 if (itCurrentFiles != currentFiles.end()) { thisTypesCurrentFile = itCurrentFiles->second; }
194 string tag = command->getOutputFileNameTag(it->first, thisTypesCurrentFile); //pass it "fasta" and the current fasta file name. some commands use the current name to get the extension, the others wont care.
195 //search for the tag in the list of output files
196 for (int h = 0; h < theseOutputNames.size(); h++) {
197 string ending = theseOutputNames[h].substr(theseOutputNames[h].length()-tag.length(), tag.length());
198 if (ending == tag) { //if it's there and this is a type we save a current version of, save it
199 if ((it->first == "column") || (it->first == "phylip")) { //check for format
200 string RippedName = "";
201 bool foundDot = false;
202 for (int i = theseOutputNames[h].length()-1; i >= 0; i--) {
203 if (foundDot && (theseOutputNames[h][i] != '.')) { RippedName = theseOutputNames[h][i] + RippedName; }
204 else if (foundDot && (theseOutputNames[h][i] == '.')) { break; }
205 else if (!foundDot && (theseOutputNames[h][i] == '.')) { foundDot = true; }
207 if ((RippedName == "phylip") || (RippedName == "lt") || (RippedName == "square")) { currentFiles["phylip"] = theseOutputNames[h]; }
208 else { currentFiles["column"] = theseOutputNames[h]; }
209 }else { currentFiles[it->first] = theseOutputNames[h]; }
219 if (m->control_pressed) { return 0; }
222 string inputString = "";
223 for (map<string, string>::iterator it = currentFiles.begin(); it != currentFiles.end(); it++) { inputString += it->first + "=" + it->second + ","; }
225 if (inputString != "") {
226 inputString = inputString.substr(0, inputString.length()-1);
227 m->mothurOutEndLine();
228 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
229 m->mothurOut("Running command: set.current(" + inputString + ")"); m->mothurOutEndLine();
230 m->mothurCalling = true;
232 Command* currentCommand = new SetCurrentCommand(inputString);
233 currentCommand->execute();
235 delete currentCommand;
236 m->mothurCalling = false;
237 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
244 catch(exception& e) {
245 m->errorOut(e, "LoadLogfileCommand", "execute");
249 //**********************************************************************************************************************