]> git.donarmstrong.com Git - mothur.git/blob - loadlogfilecommand.cpp
changed random forest output filename
[mothur.git] / loadlogfilecommand.cpp
1 //
2 //  loadlogfilecommand.cpp
3 //  Mothur
4 //
5 //  Created by Sarah Westcott on 6/13/12.
6 //  Copyright (c) 2012 Schloss Lab. All rights reserved.
7 //
8
9 #include "loadlogfilecommand.h"
10 #include "commandoptionparser.hpp"
11 #include "commandfactory.hpp"
12 #include "setcurrentcommand.h"
13
14 //**********************************************************************************************************************
15 vector<string> LoadLogfileCommand::setParameters(){     
16         try {
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);
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, "LoadLogfileCommand", "setParameters");
27                 exit(1);
28         }
29 }
30 //**********************************************************************************************************************
31 string LoadLogfileCommand::getHelpString(){     
32         try {
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";
38                 return helpString;
39         }
40         catch(exception& e) {
41                 m->errorOut(e, "LoadLogfileCommand", "getHelpString");
42                 exit(1);
43         }
44 }
45 //**********************************************************************************************************************
46 LoadLogfileCommand::LoadLogfileCommand(){       
47         try {
48                 abort = true; calledHelp = true;
49                 setParameters();
50         }
51         catch(exception& e) {
52                 m->errorOut(e, "LoadLogfileCommand", "LoadLogfileCommand");
53                 exit(1);
54         }
55 }
56 //**********************************************************************************************************************
57 LoadLogfileCommand::LoadLogfileCommand(string option)  {
58         try {
59                 abort = false; calledHelp = false;   
60                 
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;}
64                 
65                 else {
66                         //valid paramters for this command
67                         vector<string> myArray = setParameters();
68                         
69                         OptionParser parser(option);
70                         map<string,string> parameters = parser.getParameters();
71                         
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;  }
77                         }
78             
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 = "";          }
82                         else {
83                 
84                 string path;
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;          }
91                                 }
92                                 
93             }
94                         
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;  }
99             
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  
103                         }
104         }
105                 
106         }
107         catch(exception& e) {
108                 m->errorOut(e, "NewCommand", "NewCommand");
109                 exit(1);
110         }
111 }
112 //**********************************************************************************************************************
113
114 int LoadLogfileCommand::execute(){
115         try {
116                 
117                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
118         
119         m->mothurOutEndLine(); 
120         m->mothurOut("Extracting current files names..."); m->mothurOutEndLine(); 
121         m->mothurOutEndLine(); 
122         
123         CommandFactory* cFactory;
124         cFactory = CommandFactory::getInstance();
125         
126         ifstream in;
127         m->openInputFile(logfile, in);
128         
129         set<string> currentTypes = m->getCurrentTypes();
130         map<string, string> currentFiles;
131         string commandName = "";
132         bool skip = false;
133         string line = "";
134         
135         while (!in.eof()) {
136             if (m->control_pressed) { break; }
137
138             if (!skip) {  line = m->getline(in); m->gobble(in); }
139             m->gobble(in);
140              
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("/*****************"); 
145             
146             //skipping over parts where a command runs another command
147             if (pos3 != string::npos) {
148                 while (!in.eof()) {
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; }
153                 }
154             }
155             
156             if (pos != string::npos) { 
157                 skip=false;
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();
163                 
164                 //parse out parameters in option string
165                 map<string,string> parameters;
166                 OptionParser optionParser(options, parameters);
167                 
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
171                     }
172                 }
173             }else if (pos2 != string::npos) { 
174                 //read file output file names
175                 vector<string> theseOutputNames;
176                 while (!in.eof()) {
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);  }
182                 }
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();
186                 
187                 
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;  }
194                         
195                         //outputfilename pattern for this input type
196                         string pattern = command->getOutputPattern(it->first);
197                         updateCurrent(pattern, it->first, thisTypesCurrentFile, theseOutputNames, currentFiles);
198                         
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;
202                        // }
203                     }
204                 }
205             }
206         }
207         in.close();
208         
209         if (m->control_pressed) { return 0; }
210                 
211         //output results
212         string inputString = "";
213         for (map<string, string>::iterator it = currentFiles.begin(); it != currentFiles.end(); it++) { inputString += it->first + "=" + it->second + ","; }
214         
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;
221             
222                         Command* currentCommand = new SetCurrentCommand(inputString);
223                         currentCommand->execute();
224                         
225             delete currentCommand;
226                         m->mothurCalling = false;
227                         m->mothurOut("/******************************************/"); m->mothurOutEndLine(); 
228                         
229
230         } 
231         
232         return 0;
233     }
234         catch(exception& e) {
235                 m->errorOut(e, "LoadLogfileCommand", "execute");
236                 exit(1);
237         }
238 }
239 //**********************************************************************************************************************
240
241 int LoadLogfileCommand::updateCurrent(string pattern, string type, string thisTypesCurrentFile, vector<string> filenames, map<string, string>& currentFiles){
242         try {
243         
244         vector<string> patterns; m->splitAtChar(pattern, patterns, '-');
245         
246         for (int i = 0; i < patterns.size(); i++) {
247             
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);  }
254                
255                 //search for the tag in the list of output files
256                 for (int h = 0; h < filenames.size(); h++) {
257                     
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; }
269                             }
270                             if ((RippedName == "phylip") || (RippedName == "lt") || (RippedName == "square"))  {  currentFiles["phylip"] = filenames[h];  }
271                             else {  currentFiles["column"] = filenames[h]; }
272                         }else {  currentFiles[type] = filenames[h]; }
273                         break;
274                     }
275                 }
276             }
277         }
278         
279         return 0;
280     }
281         catch(exception& e) {
282                 m->errorOut(e, "LoadLogfileCommand", "updateCurrent");
283                 exit(1);
284         }
285 }
286 //**********************************************************************************************************************
287
288