]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
changed groupfile in classify.seqs to reflect multiple fasta files
[mothur.git] / engine.cpp
1 /*
2  *  engine.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 8/15/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
8  *  There's a TON of duplicated code between InteractEngine and BatchEngine
9  *  I couldn't figure out how to transition between ifstream (batch) and cin (interact)
10  *  Fix later, don't have time now.
11  *
12  */
13
14
15 #include "engine.hpp"
16
17 /***********************************************************************/
18 Engine::Engine(){
19         try {
20                 cFactory = CommandFactory::getInstance();
21                 mout = MothurOut::getInstance();
22         }
23         catch(exception& e) {
24                 mout->errorOut(e, "Engine", "Engine");
25                 exit(1);
26         }
27 }
28
29 /***********************************************************************/
30
31 InteractEngine::InteractEngine(string path){
32
33         globaldata = GlobalData::getInstance();
34         globaldata->argv = path;
35         
36 }
37
38 /***********************************************************************/
39
40 InteractEngine::~InteractEngine(){}
41
42 /***********************************************************************/
43 //This function allows the user to input commands one line at a time until they quit.
44 //If the command is garbage it does nothing.
45 bool InteractEngine::getInput(){
46         try {
47                 string input = "";
48                 string commandName = "";
49                 string options = "";
50                 int quitCommandCalled = 0;
51                 
52                 while(quitCommandCalled != 1){
53
54                         mout->mothurOutEndLine();
55                         
56                         input = getCommand();   
57                         mout->mothurOutEndLine();       
58                         
59                         if (mout->control_pressed) { input = "quit()"; }
60                         
61                         //allow user to omit the () on the quit command
62                         if (input == "quit") { input = "quit()"; }
63                         
64                         CommandOptionParser parser(input);
65                         commandName = parser.getCommandString();
66         
67                         options = parser.getOptionString();
68                         
69                         if (commandName != "") {
70                                 mout->executing = true;
71                                 
72                                 #ifdef USE_MPI
73                                         int pid;
74                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
75                                         
76                                         if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
77                                 #endif
78                                 //executes valid command
79                                 Command* command = cFactory->getCommand(commandName, options);
80                                 quitCommandCalled = command->execute();
81                                 mout->control_pressed = 0;
82                                 mout->executing = false;
83                                 
84                                 #ifdef USE_MPI
85                                         }
86                                 #endif
87                         }else {
88                                 mout->mothurOut("Your input contains errors. Please try again."); 
89                                 mout->mothurOutEndLine();
90                         }
91                 }       
92                 return 1;
93         }
94         catch(exception& e) {
95                 mout->errorOut(e, "InteractEngine", "getInput");
96                 exit(1);
97         }
98 }
99 /***********************************************************************/
100 string Engine::getCommand()  {
101         try {
102                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
103                         #ifdef USE_READLINE
104                                 char* nextCommand = NULL;
105                                 nextCommand = readline("mothur > ");
106                                 
107                                 if(nextCommand != NULL) {  add_history(nextCommand);  } 
108                                 else{ //^D causes null string and we want it to quit mothur
109                                         nextCommand = "quit"; 
110                                         mout->mothurOut(nextCommand);
111                                 }       
112                                 
113                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
114                                 return nextCommand;
115                         #else
116                                 string nextCommand = "";
117                                 mout->mothurOut("mothur > ");
118                                 getline(cin, nextCommand);
119                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
120                                 
121                                 return nextCommand;
122                         #endif
123                 #else
124                                 string nextCommand = "";
125                                 
126                                 mout->mothurOut("mothur > ");
127                                 getline(cin, nextCommand);
128                                 mout->mothurOutJustToLog(toString(nextCommand));
129                                 
130                                 return nextCommand;
131                 #endif
132         
133                                                 
134         }
135         catch(exception& e) {
136                 mout->errorOut(e, "Engine", "getCommand");
137                 exit(1);
138         }
139 }
140 /***********************************************************************/
141 //This function opens the batchfile to be used by BatchEngine::getInput.
142 BatchEngine::BatchEngine(string path, string batchFileName){
143         try {
144                 globaldata = GlobalData::getInstance();
145         
146                 openedBatch = openInputFile(batchFileName, inputBatchFile);
147                 globaldata->argv = path;
148                                 
149         }
150         catch(exception& e) {
151                 mout->errorOut(e, "BatchEngine", "BatchEngine");
152                 exit(1);
153         }
154 }
155
156 /***********************************************************************/
157
158 BatchEngine::~BatchEngine(){    }
159
160 /***********************************************************************/
161 //This Function allows the user to run a batchfile containing several commands on Dotur
162 bool BatchEngine::getInput(){
163         try {
164                 //check if this is a valid batchfile
165                 if (openedBatch == 1) {  
166                         mout->mothurOut("unable to open batchfile");  
167                         mout->mothurOutEndLine();
168                         return 1; 
169                 }
170         
171                 string input = "";
172                 string commandName = "";
173                 string options = "";
174                 
175                 //CommandFactory cFactory;
176                 int quitCommandCalled = 0;
177         
178                 while(quitCommandCalled == 0){
179         
180                         if (inputBatchFile.eof()) { input = "quit()"; }
181                         else { input = getline(inputBatchFile); }
182                         
183                         if (input[0] != '#') {
184                                 
185                                 mout->mothurOutEndLine();
186                                 mout->mothurOut("mothur > " + input);
187                                 mout->mothurOutEndLine();
188                                                         
189                                 if (mout->control_pressed) { input = "quit()"; }
190                                 
191                                 //allow user to omit the () on the quit command
192                                 if (input == "quit") { input = "quit()"; }
193
194                                 CommandOptionParser parser(input);
195                                 commandName = parser.getCommandString();
196                                 options = parser.getOptionString();
197                                                                                 
198                                 if (commandName != "") {
199                                         mout->executing = true;
200                                         #ifdef USE_MPI
201                                                 int pid;
202                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
203                                         
204                                                 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
205                                         #endif
206                                         //executes valid command
207                                         Command* command = cFactory->getCommand(commandName, options);
208                                         quitCommandCalled = command->execute();
209                                         mout->control_pressed = 0;
210                                         mout->executing = false;
211                                 
212                                         #ifdef USE_MPI
213                                                 }
214                                         #endif
215                                 }else {         
216                                         mout->mothurOut("Invalid."); 
217                                         mout->mothurOutEndLine();
218                                 }
219                                 
220                         }
221                         gobble(inputBatchFile);
222                 }
223                 
224                 inputBatchFile.close();
225                 return 1;
226         }
227         catch(exception& e) {
228                 mout->errorOut(e, "BatchEngine", "getInput");
229                 exit(1);
230         }
231 }
232
233
234 /***********************************************************************/
235 /***********************************************************************/
236 //This function opens the batchfile to be used by BatchEngine::getInput.
237 ScriptEngine::ScriptEngine(string path, string commandString){
238         try {
239                 globaldata = GlobalData::getInstance();
240                 
241                 //remove quotes
242                 listOfCommands = commandString.substr(1, (commandString.length()-1));
243
244                 globaldata->argv = path;
245                                 
246         }
247         catch(exception& e) {
248                 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
249                 exit(1);
250         }
251 }
252
253 /***********************************************************************/
254
255 ScriptEngine::~ScriptEngine(){  }
256
257 /***********************************************************************/
258 //This Function allows the user to run a batchfile containing several commands on mothur
259 bool ScriptEngine::getInput(){
260         try {
261                         
262                 string input = "";
263                 string commandName = "";
264                 string options = "";
265                 
266                 
267                 //CommandFactory cFactory;
268                 int quitCommandCalled = 0;
269         
270                 while(quitCommandCalled == 0){
271                 
272                         input = getNextCommand(listOfCommands); 
273                         
274                         if (input == "") { input = "quit()"; }
275                         
276                         mout->mothurOutEndLine();
277                         mout->mothurOut("mothur > " + input);
278                         mout->mothurOutEndLine();
279                         
280                         if (mout->control_pressed) { input = "quit()"; }
281                                 
282                         //allow user to omit the () on the quit command
283                         if (input == "quit") { input = "quit()"; }
284
285                         CommandOptionParser parser(input);
286                         commandName = parser.getCommandString();
287                         options = parser.getOptionString();
288                                                                                 
289                         if (commandName != "") {
290                                 mout->executing = true;
291                                 #ifdef USE_MPI
292                                         int pid;
293                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
294                                         
295                                         if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
296                                 #endif
297                                 //executes valid command
298                                 Command* command = cFactory->getCommand(commandName, options);
299                                 quitCommandCalled = command->execute();
300                                 mout->control_pressed = 0;
301                                 mout->executing = false;
302                                 
303                                 #ifdef USE_MPI
304                                         }
305                                 #endif
306                         }else {         
307                                 mout->mothurOut("Invalid."); 
308                                 mout->mothurOutEndLine();
309                         }
310                         
311                 }
312                 
313                 return 1;
314         }
315         catch(exception& e) {
316                 mout->errorOut(e, "ScriptEngine", "getInput");
317                 exit(1);
318         }
319 }
320 /***********************************************************************/
321 string ScriptEngine::getNextCommand(string& commandString) {
322         try {
323                 string nextcommand = "";
324                 int count = 0;
325                 
326                 //go through string until you reach ; or end
327                 while (count < commandString.length()) { 
328                         
329                         if (commandString[count] == ';') {  break;   }
330                         else {          nextcommand += commandString[count];    }
331                         
332                         count++;
333                 }
334                 
335                 //if you are not at the end
336                 if (count != commandString.length())  {   commandString = commandString.substr(count+1, commandString.length());  }
337                 else { commandString = ""; }
338                                 
339                 
340                 //get rid of spaces in between commands if any
341                 if (commandString.length() > 0) {
342                         while (commandString[0] == ' ') {  
343                                 commandString = commandString.substr(1,commandString.length());
344                                 if (commandString.length() == 0) {  break;  }
345                         }
346                 }
347                                         
348                 return nextcommand;
349         }
350         catch(exception& e) {
351                 mout->errorOut(e, "ScriptEngine", "getNextCommand");
352                 exit(1);
353         }
354 }
355 /***********************************************************************/