]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
added MPI to dist.seqs 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                                         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                 mout->mothurOutEndLine();
134                                                 
135         }
136         catch(exception& e) {
137                 mout->errorOut(e, "Engine", "getCommand");
138                 exit(1);
139         }
140 }
141 /***********************************************************************/
142 //This function opens the batchfile to be used by BatchEngine::getInput.
143 BatchEngine::BatchEngine(string path, string batchFileName){
144         try {
145                 globaldata = GlobalData::getInstance();
146         
147                 openedBatch = openInputFile(batchFileName, inputBatchFile);
148                 globaldata->argv = path;
149                                 
150         }
151         catch(exception& e) {
152                 mout->errorOut(e, "BatchEngine", "BatchEngine");
153                 exit(1);
154         }
155 }
156
157 /***********************************************************************/
158
159 BatchEngine::~BatchEngine(){    }
160
161 /***********************************************************************/
162 //This Function allows the user to run a batchfile containing several commands on Dotur
163 bool BatchEngine::getInput(){
164         try {
165                 //check if this is a valid batchfile
166                 if (openedBatch == 1) {  
167                         mout->mothurOut("unable to open batchfile");  
168                         mout->mothurOutEndLine();
169                         return 1; 
170                 }
171         
172                 string input = "";
173                 string commandName = "";
174                 string options = "";
175                 
176                 //CommandFactory cFactory;
177                 int quitCommandCalled = 0;
178         
179                 while(quitCommandCalled == 0){
180         
181                         if (inputBatchFile.eof()) { input = "quit()"; }
182                         else { input = getline(inputBatchFile); }
183                         
184                         if (input[0] != '#') {
185                                 
186                                 mout->mothurOutEndLine();
187                                 mout->mothurOut("mothur > " + input);
188                                 mout->mothurOutEndLine();
189                                                         
190                                 if (mout->control_pressed) { input = "quit()"; }
191                                 
192                                 //allow user to omit the () on the quit command
193                                 if (input == "quit") { input = "quit()"; }
194
195                                 CommandOptionParser parser(input);
196                                 commandName = parser.getCommandString();
197                                 options = parser.getOptionString();
198                                                                                 
199                                 if (commandName != "") {
200                                         mout->executing = true;
201                                         #ifdef USE_MPI
202                                                 int pid;
203                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
204                                         
205                                                 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
206                                         #endif
207                                         //executes valid command
208                                         Command* command = cFactory->getCommand(commandName, options);
209                                         quitCommandCalled = command->execute();
210                                         mout->control_pressed = 0;
211                                         mout->executing = false;
212                                 
213                                         #ifdef USE_MPI
214                                                 }
215                                         #endif
216                                 }else {         
217                                         mout->mothurOut("Invalid."); 
218                                         mout->mothurOutEndLine();
219                                 }
220                                 
221                         }
222                         gobble(inputBatchFile);
223                 }
224                 
225                 inputBatchFile.close();
226                 return 1;
227         }
228         catch(exception& e) {
229                 mout->errorOut(e, "BatchEngine", "getInput");
230                 exit(1);
231         }
232 }
233
234
235 /***********************************************************************/
236 /***********************************************************************/
237 //This function opens the batchfile to be used by BatchEngine::getInput.
238 ScriptEngine::ScriptEngine(string path, string commandString){
239         try {
240                 globaldata = GlobalData::getInstance();
241                 
242                 //remove quotes
243                 listOfCommands = commandString.substr(1, (commandString.length()-1));
244
245                 globaldata->argv = path;
246                                 
247         }
248         catch(exception& e) {
249                 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
250                 exit(1);
251         }
252 }
253
254 /***********************************************************************/
255
256 ScriptEngine::~ScriptEngine(){  }
257
258 /***********************************************************************/
259 //This Function allows the user to run a batchfile containing several commands on mothur
260 bool ScriptEngine::getInput(){
261         try {
262                         
263                 string input = "";
264                 string commandName = "";
265                 string options = "";
266                 
267                 
268                 //CommandFactory cFactory;
269                 int quitCommandCalled = 0;
270         
271                 while(quitCommandCalled == 0){
272                 
273                         input = getNextCommand(listOfCommands); 
274                         
275                         if (input == "") { input = "quit()"; }
276                         
277                         mout->mothurOutEndLine();
278                         mout->mothurOut("mothur > " + input);
279                         mout->mothurOutEndLine();
280                         
281                         if (mout->control_pressed) { input = "quit()"; }
282                                 
283                         //allow user to omit the () on the quit command
284                         if (input == "quit") { input = "quit()"; }
285
286                         CommandOptionParser parser(input);
287                         commandName = parser.getCommandString();
288                         options = parser.getOptionString();
289                                                                                 
290                         if (commandName != "") {
291                                 mout->executing = true;
292                                 #ifdef USE_MPI
293                                         int pid;
294                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
295                                         
296                                         if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
297                                 #endif
298                                 //executes valid command
299                                 Command* command = cFactory->getCommand(commandName, options);
300                                 quitCommandCalled = command->execute();
301                                 mout->control_pressed = 0;
302                                 mout->executing = false;
303                                 
304                                 #ifdef USE_MPI
305                                         }
306                                 #endif
307                         }else {         
308                                 mout->mothurOut("Invalid."); 
309                                 mout->mothurOutEndLine();
310                         }
311                         
312                 }
313                 
314                 return 1;
315         }
316         catch(exception& e) {
317                 mout->errorOut(e, "ScriptEngine", "getInput");
318                 exit(1);
319         }
320 }
321 /***********************************************************************/
322 string ScriptEngine::getNextCommand(string& commandString) {
323         try {
324                 string nextcommand = "";
325                 int count = 0;
326                 
327                 //go through string until you reach ; or end
328                 while (count < commandString.length()) { 
329                         
330                         if (commandString[count] == ';') {  break;   }
331                         else {          nextcommand += commandString[count];    }
332                         
333                         count++;
334                 }
335                 
336                 //if you are not at the end
337                 if (count != commandString.length())  {   commandString = commandString.substr(count+1, commandString.length());  }
338                 else { commandString = ""; }
339                                 
340                 
341                 //get rid of spaces in between commands if any
342                 if (commandString.length() > 0) {
343                         while (commandString[0] == ' ') {  
344                                 commandString = commandString.substr(1,commandString.length());
345                                 if (commandString.length() == 0) {  break;  }
346                         }
347                 }
348                                         
349                 return nextcommand;
350         }
351         catch(exception& e) {
352                 mout->errorOut(e, "ScriptEngine", "getNextCommand");
353                 exit(1);
354         }
355 }
356 /***********************************************************************/