]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
added cluster.split command
[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                                         MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
77                                 
78                                         if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
79                                 #endif
80                                 //executes valid command
81                                 Command* command = cFactory->getCommand(commandName, options);
82                                 quitCommandCalled = command->execute();
83                                 mout->control_pressed = 0;
84                                 mout->executing = false;
85                                 
86                                 #ifdef USE_MPI
87                                         }
88                                 #endif
89                         }else {
90                                 mout->mothurOut("Your input contains errors. Please try again."); 
91                                 mout->mothurOutEndLine();
92                         }
93                 }       
94                 return 1;
95         }
96         catch(exception& e) {
97                 mout->errorOut(e, "InteractEngine", "getInput");
98                 exit(1);
99         }
100 }
101 /***********************************************************************/
102 string Engine::getCommand()  {
103         try {
104                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
105                         #ifdef USE_READLINE
106                                 char* nextCommand = NULL;
107                                 nextCommand = readline("mothur > ");
108                                 
109                                 if(nextCommand != NULL) {  add_history(nextCommand);  } 
110                                 else{ //^D causes null string and we want it to quit mothur
111                                         nextCommand = "quit"; 
112                                         mout->mothurOut(nextCommand);
113                                 }       
114                                 
115                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
116                                 return nextCommand;
117                         #else
118                                 string nextCommand = "";
119                                 mout->mothurOut("mothur > ");
120                                 getline(cin, nextCommand);
121                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
122                                 
123                                 return nextCommand;
124                         #endif
125                 #else
126                                 string nextCommand = "";
127                                 
128                                 mout->mothurOut("mothur > ");
129                                 getline(cin, nextCommand);
130                                 mout->mothurOutJustToLog(toString(nextCommand));
131                                 
132                                 return nextCommand;
133                 #endif
134         
135                                                 
136         }
137         catch(exception& e) {
138                 mout->errorOut(e, "Engine", "getCommand");
139                 exit(1);
140         }
141 }
142 /***********************************************************************/
143 //This function opens the batchfile to be used by BatchEngine::getInput.
144 BatchEngine::BatchEngine(string path, string batchFileName){
145         try {
146                 globaldata = GlobalData::getInstance();
147         
148                 openedBatch = openInputFile(batchFileName, inputBatchFile);
149                 globaldata->argv = path;
150                                 
151         }
152         catch(exception& e) {
153                 mout->errorOut(e, "BatchEngine", "BatchEngine");
154                 exit(1);
155         }
156 }
157
158 /***********************************************************************/
159
160 BatchEngine::~BatchEngine(){    }
161
162 /***********************************************************************/
163 //This Function allows the user to run a batchfile containing several commands on Dotur
164 bool BatchEngine::getInput(){
165         try {
166                 //check if this is a valid batchfile
167                 if (openedBatch == 1) {  
168                         mout->mothurOut("unable to open batchfile");  
169                         mout->mothurOutEndLine();
170                         return 1; 
171                 }
172         
173                 string input = "";
174                 string commandName = "";
175                 string options = "";
176                 
177                 //CommandFactory cFactory;
178                 int quitCommandCalled = 0;
179         
180                 while(quitCommandCalled == 0){
181         
182                         if (inputBatchFile.eof()) { input = "quit()"; }
183                         else { input = getline(inputBatchFile); }
184                         
185                         if (input[0] != '#') {
186                                 
187                                 mout->mothurOutEndLine();
188                                 mout->mothurOut("mothur > " + input);
189                                 mout->mothurOutEndLine();
190                                                         
191                                 if (mout->control_pressed) { input = "quit()"; }
192                                 
193                                 //allow user to omit the () on the quit command
194                                 if (input == "quit") { input = "quit()"; }
195
196                                 CommandOptionParser parser(input);
197                                 commandName = parser.getCommandString();
198                                 options = parser.getOptionString();
199                                                                                 
200                                 if (commandName != "") {
201                                         mout->executing = true;
202                                         #ifdef USE_MPI
203                                                 int pid;
204                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
205 cout << pid << " is waiting " << commandName << endl;                                           
206                                                 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
207 cout << pid << " is here " << commandName << endl;
208                                                 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
209                                         #endif
210                                         //executes valid command
211                                         Command* command = cFactory->getCommand(commandName, options);
212                                         quitCommandCalled = command->execute();
213                                         mout->control_pressed = 0;
214                                         mout->executing = false;
215                                 
216                                         #ifdef USE_MPI
217                                                 }
218                                         #endif
219                                 }else {         
220                                         mout->mothurOut("Invalid."); 
221                                         mout->mothurOutEndLine();
222                                 }
223                                 
224                         }
225                         gobble(inputBatchFile);
226                 }
227                 
228                 inputBatchFile.close();
229                 return 1;
230         }
231         catch(exception& e) {
232                 mout->errorOut(e, "BatchEngine", "getInput");
233                 exit(1);
234         }
235 }
236
237
238 /***********************************************************************/
239 /***********************************************************************/
240 //This function opens the batchfile to be used by BatchEngine::getInput.
241 ScriptEngine::ScriptEngine(string path, string commandString){
242         try {
243                 globaldata = GlobalData::getInstance();
244                 
245                 //remove quotes
246                 listOfCommands = commandString.substr(1, (commandString.length()-1));
247
248                 globaldata->argv = path;
249                                 
250         }
251         catch(exception& e) {
252                 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
253                 exit(1);
254         }
255 }
256
257 /***********************************************************************/
258
259 ScriptEngine::~ScriptEngine(){  }
260
261 /***********************************************************************/
262 //This Function allows the user to run a batchfile containing several commands on mothur
263 bool ScriptEngine::getInput(){
264         try {
265                         
266                 string input = "";
267                 string commandName = "";
268                 string options = "";
269                 
270                 
271                 //CommandFactory cFactory;
272                 int quitCommandCalled = 0;
273         
274                 while(quitCommandCalled == 0){
275                 
276                         input = getNextCommand(listOfCommands); 
277                         
278                         if (input == "") { input = "quit()"; }
279                         
280                         mout->mothurOutEndLine();
281                         mout->mothurOut("mothur > " + input);
282                         mout->mothurOutEndLine();
283                         
284                         if (mout->control_pressed) { input = "quit()"; }
285                                 
286                         //allow user to omit the () on the quit command
287                         if (input == "quit") { input = "quit()"; }
288
289                         CommandOptionParser parser(input);
290                         commandName = parser.getCommandString();
291                         options = parser.getOptionString();
292                                                                                 
293                         if (commandName != "") {
294                                 mout->executing = true;
295                                 #ifdef USE_MPI
296                                         int pid;
297                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
298                                         
299                                         MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
300                                         
301                                         if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
302                                 #endif
303                                 //executes valid command
304                                 Command* command = cFactory->getCommand(commandName, options);
305                                 quitCommandCalled = command->execute();
306                                 mout->control_pressed = 0;
307                                 mout->executing = false;
308                                 
309                                 #ifdef USE_MPI
310                                         }
311                                 #endif
312                         }else {         
313                                 mout->mothurOut("Invalid."); 
314                                 mout->mothurOutEndLine();
315                         }
316                         
317                 }
318                 
319                 return 1;
320         }
321         catch(exception& e) {
322                 mout->errorOut(e, "ScriptEngine", "getInput");
323                 exit(1);
324         }
325 }
326 /***********************************************************************/
327 string ScriptEngine::getNextCommand(string& commandString) {
328         try {
329                 string nextcommand = "";
330                 int count = 0;
331                 
332                 //go through string until you reach ; or end
333                 while (count < commandString.length()) { 
334                         
335                         if (commandString[count] == ';') {  break;   }
336                         else {          nextcommand += commandString[count];    }
337                         
338                         count++;
339                 }
340                 
341                 //if you are not at the end
342                 if (count != commandString.length())  {   commandString = commandString.substr(count+1, commandString.length());  }
343                 else { commandString = ""; }
344                                 
345                 
346                 //get rid of spaces in between commands if any
347                 if (commandString.length() > 0) {
348                         while (commandString[0] == ' ') {  
349                                 commandString = commandString.substr(1,commandString.length());
350                                 if (commandString.length() == 0) {  break;  }
351                         }
352                 }
353                                         
354                 return nextcommand;
355         }
356         catch(exception& e) {
357                 mout->errorOut(e, "ScriptEngine", "getNextCommand");
358                 exit(1);
359         }
360 }
361 /***********************************************************************/