]> git.donarmstrong.com Git - mothur.git/blob - loadlogfilecommand.cpp
added load.logfile command. changed summary.single output for subsample=t.
[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); 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                 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;  }
193                         
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; }
206                                     }
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]; }
210                                 break;
211                             }
212                         }
213                     }
214                 }
215             }
216         }
217         in.close();
218         
219         if (m->control_pressed) { return 0; }
220                 
221         //output results
222         string inputString = "";
223         for (map<string, string>::iterator it = currentFiles.begin(); it != currentFiles.end(); it++) { inputString += it->first + "=" + it->second + ","; }
224         
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;
231             
232                         Command* currentCommand = new SetCurrentCommand(inputString);
233                         currentCommand->execute();
234                         
235             delete currentCommand;
236                         m->mothurCalling = false;
237                         m->mothurOut("/******************************************/"); m->mothurOutEndLine(); 
238                         
239
240         } 
241         
242         return 0;
243     }
244         catch(exception& e) {
245                 m->errorOut(e, "LoadLogfileCommand", "execute");
246                 exit(1);
247         }
248 }
249 //**********************************************************************************************************************
250
251