5 * Created by Pat Schloss on 8/15/08.
6 * Copyright 2008 Patrick D. Schloss. All rights reserved.
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.
17 /***********************************************************************/
20 cFactory = CommandFactory::getInstance();
21 mout = MothurOut::getInstance();
24 mout->errorOut(e, "Engine", "Engine");
28 /***********************************************************************/
29 string Engine::findMothursPath(){
32 string envPath = getenv("PATH");
33 string mothurPath = "";
35 //delimiting path char
37 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
43 //break apart path variable by ':'
45 mout->splitAtChar(envPath, dirs, delim);
47 //get path related to mothur
48 for (int i = 0; i < dirs.size(); i++) {
49 //to lower so we can find it
50 string tempLower = "";
51 for (int j = 0; j < dirs[i].length(); j++) { tempLower += tolower(dirs[i][j]); }
53 //is this mothurs path?
54 if (tempLower.find("mothur") != -1) { mothurPath = dirs[i]; break; }
57 if (mothurPath != "") {
58 //add mothur so it looks like what argv would look like
59 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
60 mothurPath += "/mothur";
62 mothurPath += "\\mothur";
65 //okay mothur is not in the path, so the folder mothur is in must be in the path
66 //lets find out which one
68 //get path related to mothur
69 for (int i = 0; i < dirs.size(); i++) {
71 //is this mothurs path?
73 string tempIn = dirs[i];
74 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
79 mout->openInputFile(tempIn, in, "");
82 if (in) { in.close(); mothurPath = tempIn; break; }
90 mout->errorOut(e, "Engine", "findMothursPath");
94 /***********************************************************************/
96 InteractEngine::InteractEngine(string path){
99 string temppath = path.substr(0, (path.find_last_of("othur")-5));
101 //this will happen if you set the path variable to contain mothur's exe location
102 if (temppath == "") { path = findMothursPath(); }
107 /***********************************************************************/
109 InteractEngine::~InteractEngine(){}
111 /***********************************************************************/
112 //This function allows the user to input commands one line at a time until they quit.
113 //If the command is garbage it does nothing.
114 bool InteractEngine::getInput(){
117 string commandName = "";
119 int quitCommandCalled = 0;
121 while(quitCommandCalled != 1){
126 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
127 MPI_Comm_size(MPI_COMM_WORLD, &processors);
133 mout->mothurOutEndLine();
135 input = getCommand();
136 mout->mothurOutEndLine();
138 if (mout->control_pressed) { input = "quit()"; }
140 //allow user to omit the () on the quit command
141 if (input == "quit") { input = "quit()"; }
146 for(int i = 1; i < processors; i++) {
147 int length = input.length();
148 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
149 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
154 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
156 char* tempBuf = new char[length];
157 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
160 if (input.length() > length) { input = input.substr(0, length); }
167 CommandOptionParser parser(input);
168 commandName = parser.getCommandString();
170 options = parser.getOptionString();
172 if (commandName != "") {
173 mout->executing = true;
176 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
178 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
179 //cout << pid << " is in execute " << commandName << endl;
181 //executes valid command
182 mout->runParse = true;
183 mout->Groups.clear();
184 mout->namesOfGroups.clear();
185 mout->Treenames.clear();
187 mout->saveNextLabel = "";
188 mout->printedHeaders = false;
190 Command* command = cFactory->getCommand(commandName, options);
191 quitCommandCalled = command->execute();
193 //if we aborted command
194 if (quitCommandCalled == 2) { mout->mothurOut("[ERROR]: did not complete " + commandName + "."); mout->mothurOutEndLine(); }
196 mout->control_pressed = 0;
197 mout->executing = false;
203 mout->mothurOut("Invalid.");
204 mout->mothurOutEndLine();
209 catch(exception& e) {
210 mout->errorOut(e, "InteractEngine", "getInput");
214 /***********************************************************************/
215 string Engine::getCommand() {
218 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
220 char* nextCommand = NULL;
221 nextCommand = readline("mothur > ");
223 if(nextCommand != NULL) { add_history(nextCommand); }
224 else{ //^D causes null string and we want it to quit mothur
225 nextCommand = strdup("quit");
226 mout->mothurOut(nextCommand);
229 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
232 string nextCommand = "";
233 mout->mothurOut("mothur > ");
234 getline(cin, nextCommand);
235 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
240 string nextCommand = "";
242 mout->mothurOut("mothur > ");
243 getline(cin, nextCommand);
244 mout->mothurOutJustToLog(toString(nextCommand));
251 catch(exception& e) {
252 mout->errorOut(e, "Engine", "getCommand");
256 /***********************************************************************/
257 //This function opens the batchfile to be used by BatchEngine::getInput.
258 BatchEngine::BatchEngine(string path, string batchFileName){
261 openedBatch = mout->openInputFile(batchFileName, inputBatchFile);
263 string temppath = path.substr(0, (path.find_last_of("othur")-5));
265 //this will happen if you set the path variable to contain mothur's exe location
266 if (temppath == "") { path = findMothursPath(); }
271 catch(exception& e) {
272 mout->errorOut(e, "BatchEngine", "BatchEngine");
277 /***********************************************************************/
279 BatchEngine::~BatchEngine(){ }
281 /***********************************************************************/
282 //This Function allows the user to run a batchfile containing several commands on Dotur
283 bool BatchEngine::getInput(){
285 //check if this is a valid batchfile
286 if (openedBatch == 1) {
287 mout->mothurOut("unable to open batchfile");
288 mout->mothurOutEndLine();
293 string commandName = "";
296 //CommandFactory cFactory;
297 int quitCommandCalled = 0;
299 while(quitCommandCalled != 1){
304 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
305 MPI_Comm_size(MPI_COMM_WORLD, &processors);
311 input = getNextCommand(inputBatchFile);
316 for(int i = 1; i < processors; i++) {
317 int length = input.length();
318 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
319 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
324 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
326 char* tempBuf = new char[length];
327 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
330 if (input.length() > length) { input = input.substr(0, length); }
339 if (input[0] != '#') {
341 mout->mothurOutEndLine();
342 mout->mothurOut("mothur > " + input);
343 mout->mothurOutEndLine();
345 if (mout->control_pressed) { input = "quit()"; }
347 //allow user to omit the () on the quit command
348 if (input == "quit") { input = "quit()"; }
350 CommandOptionParser parser(input);
351 commandName = parser.getCommandString();
352 options = parser.getOptionString();
354 if (commandName != "") {
355 mout->executing = true;
358 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
360 //cout << pid << " is here " << commandName << '\t' << count << endl;
361 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
363 //executes valid command
364 mout->runParse = true;
365 mout->Groups.clear();
366 mout->namesOfGroups.clear();
367 mout->Treenames.clear();
369 mout->saveNextLabel = "";
370 mout->printedHeaders = false;
372 Command* command = cFactory->getCommand(commandName, options);
373 quitCommandCalled = command->execute();
375 //if we aborted command
376 if (quitCommandCalled == 2) { mout->mothurOut("[ERROR]: did not complete " + commandName + "."); mout->mothurOutEndLine(); }
378 mout->control_pressed = 0;
379 mout->executing = false;
385 mout->mothurOut("Invalid.");
386 mout->mothurOutEndLine();
390 mout->gobble(inputBatchFile);
393 inputBatchFile.close();
396 catch(exception& e) {
397 mout->errorOut(e, "BatchEngine", "getInput");
401 /***********************************************************************/
402 string BatchEngine::getNextCommand(ifstream& inputBatchFile) {
405 string nextcommand = "";
407 if (inputBatchFile.eof()) { nextcommand = "quit()"; }
408 else { nextcommand = mout->getline(inputBatchFile); }
412 catch(exception& e) {
413 mout->errorOut(e, "BatchEngine", "getNextCommand");
418 /***********************************************************************/
419 /***********************************************************************/
420 //This function opens the batchfile to be used by BatchEngine::getInput.
421 ScriptEngine::ScriptEngine(string path, string commandString){
425 listOfCommands = commandString.substr(1, (commandString.length()-1));
427 string temppath = path.substr(0, (path.find_last_of("othur")-5));
429 //this will happen if you set the path variable to contain mothur's exe location
430 if (temppath == "") { path = findMothursPath(); }
435 catch(exception& e) {
436 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
441 /***********************************************************************/
443 ScriptEngine::~ScriptEngine(){ }
445 /***********************************************************************/
446 //This Function allows the user to run a batchfile containing several commands on mothur
447 bool ScriptEngine::getInput(){
451 string commandName = "";
455 //CommandFactory cFactory;
456 int quitCommandCalled = 0;
458 while(quitCommandCalled != 1){
463 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
464 MPI_Comm_size(MPI_COMM_WORLD, &processors);
470 input = getNextCommand(listOfCommands);
472 if (input == "") { input = "quit()"; }
475 if ((input.find("quit") != string::npos) || (input.find("set.logfile") != string::npos)) {}
476 else if ((input.find("get.current") != string::npos) && (!mout->hasCurrentFiles())) {}
477 else { mout->mothurOutEndLine(); mout->mothurOut("mothur > " + input); mout->mothurOutEndLine(); }
479 mout->mothurOutEndLine(); mout->mothurOut("mothur > " + input); mout->mothurOutEndLine();
484 for(int i = 1; i < processors; i++) {
485 int length = input.length();
486 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
487 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
492 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
494 char* tempBuf = new char[length];
495 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
498 if (input.length() > length) { input = input.substr(0, length); }
506 if (mout->control_pressed) { input = "quit()"; }
508 //allow user to omit the () on the quit command
509 if (input == "quit") { input = "quit()"; }
511 CommandOptionParser parser(input);
512 commandName = parser.getCommandString();
513 options = parser.getOptionString();
515 if (commandName != "") {
516 mout->executing = true;
518 int pid, numProcesses;
520 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
521 MPI_Comm_size(MPI_COMM_WORLD, &numProcesses);
523 //cout << pid << " is here " << commandName << endl;
524 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
525 //cout << pid << " is in execute" << endl;
527 //executes valid command
528 mout->runParse = true;
529 mout->Groups.clear();
530 mout->namesOfGroups.clear();
531 mout->Treenames.clear();
533 mout->saveNextLabel = "";
534 mout->printedHeaders = false;
536 Command* command = cFactory->getCommand(commandName, options);
537 quitCommandCalled = command->execute();
539 //if we aborted command
540 if (quitCommandCalled == 2) { mout->mothurOut("[ERROR]: did not complete " + commandName + "."); mout->mothurOutEndLine(); }
542 mout->control_pressed = 0;
543 mout->executing = false;
546 //cout << pid << " is done in execute" << endl;
550 mout->mothurOut("Invalid.");
551 mout->mothurOutEndLine();
559 catch(exception& e) {
560 mout->errorOut(e, "ScriptEngine", "getInput");
564 /***********************************************************************/
565 string ScriptEngine::getNextCommand(string& commandString) {
568 string nextcommand = "";
570 bool ignoreSemiColons = false;
572 //go through string until you reach ; or end
573 while (count < commandString.length()) {
575 //you want to ignore any ; until you reach the next '
576 if ((commandString[count] == '\'') && (!ignoreSemiColons)) { ignoreSemiColons = true; }
577 else if ((commandString[count] == '\'') && (ignoreSemiColons)) { ignoreSemiColons = false; }
579 if ((commandString[count] == ';') && (!ignoreSemiColons)) { break; }
580 else { nextcommand += commandString[count]; }
585 //if you are not at the end
586 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
587 else { commandString = ""; }
590 //get rid of spaces in between commands if any
591 if (commandString.length() > 0) {
592 while (commandString[0] == ' ') {
593 commandString = commandString.substr(1,commandString.length());
594 if (commandString.length() == 0) { break; }
600 catch(exception& e) {
601 mout->errorOut(e, "ScriptEngine", "getNextCommand");
605 /***********************************************************************/