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;
184 mout->clearAllGroups();
185 mout->Treenames.clear();
187 mout->saveNextLabel = "";
188 mout->printedHeaders = false;
189 mout->commandInputsConvertError = false;
190 mout->currentBinLabels.clear();
191 mout->binLabelsInFile.clear();
193 Command* command = cFactory->getCommand(commandName, options);
194 if (mout->commandInputsConvertError) { quitCommandCalled = 2; }
195 else { quitCommandCalled = command->execute(); }
197 //if we aborted command
198 if (quitCommandCalled == 2) { mout->mothurOut("[ERROR]: did not complete " + commandName + "."); mout->mothurOutEndLine(); }
200 mout->control_pressed = 0;
201 mout->executing = false;
207 mout->mothurOut("Invalid.");
208 mout->mothurOutEndLine();
213 catch(exception& e) {
214 mout->errorOut(e, "InteractEngine", "getInput");
218 /***********************************************************************/
219 string Engine::getCommand() {
222 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
224 char* nextCommand = NULL;
225 nextCommand = readline("mothur > ");
227 if(nextCommand != NULL) { add_history(nextCommand); }
228 else{ //^D causes null string and we want it to quit mothur
229 nextCommand = strdup("quit");
230 mout->mothurOut(nextCommand);
233 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
236 string nextCommand = "";
237 mout->mothurOut("mothur > ");
238 getline(cin, nextCommand);
239 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
244 string nextCommand = "";
246 mout->mothurOut("mothur > ");
247 getline(cin, nextCommand);
248 mout->mothurOutJustToLog(toString(nextCommand));
255 catch(exception& e) {
256 mout->errorOut(e, "Engine", "getCommand");
260 /***********************************************************************/
261 //This function opens the batchfile to be used by BatchEngine::getInput.
262 BatchEngine::BatchEngine(string path, string batchFileName){
265 openedBatch = mout->openInputFile(batchFileName, inputBatchFile);
267 string temppath = path.substr(0, (path.find_last_of("othur")-5));
269 //this will happen if you set the path variable to contain mothur's exe location
270 if (temppath == "") { path = findMothursPath(); }
275 catch(exception& e) {
276 mout->errorOut(e, "BatchEngine", "BatchEngine");
281 /***********************************************************************/
283 BatchEngine::~BatchEngine(){ }
285 /***********************************************************************/
286 //This Function allows the user to run a batchfile containing several commands on Dotur
287 bool BatchEngine::getInput(){
289 //check if this is a valid batchfile
290 if (openedBatch == 1) {
291 mout->mothurOut("unable to open batchfile");
292 mout->mothurOutEndLine();
297 string commandName = "";
300 //CommandFactory cFactory;
301 int quitCommandCalled = 0;
303 while(quitCommandCalled != 1){
308 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
309 MPI_Comm_size(MPI_COMM_WORLD, &processors);
315 input = getNextCommand(inputBatchFile);
320 for(int i = 1; i < processors; i++) {
321 int length = input.length();
322 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
323 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
328 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
330 char* tempBuf = new char[length];
331 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
334 if (input.length() > length) { input = input.substr(0, length); }
343 if (input[0] != '#') {
345 mout->mothurOutEndLine();
346 mout->mothurOut("mothur > " + input);
347 mout->mothurOutEndLine();
349 if (mout->control_pressed) { input = "quit()"; }
351 //allow user to omit the () on the quit command
352 if (input == "quit") { input = "quit()"; }
354 CommandOptionParser parser(input);
355 commandName = parser.getCommandString();
356 options = parser.getOptionString();
358 if (commandName != "") {
359 mout->executing = true;
362 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
364 //cout << pid << " is here " << commandName << '\t' << count << endl;
365 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
367 //executes valid command
368 mout->runParse = true;
370 mout->clearAllGroups();
371 mout->Treenames.clear();
373 mout->saveNextLabel = "";
374 mout->printedHeaders = false;
375 mout->commandInputsConvertError = false;
376 mout->currentBinLabels.clear();
377 mout->binLabelsInFile.clear();
380 Command* command = cFactory->getCommand(commandName, options);
381 if (mout->commandInputsConvertError) { quitCommandCalled = 2; }
382 else { quitCommandCalled = command->execute(); }
384 //if we aborted command
385 if (quitCommandCalled == 2) { mout->mothurOut("[ERROR]: did not complete " + commandName + "."); mout->mothurOutEndLine(); }
387 mout->control_pressed = 0;
388 mout->executing = false;
394 mout->mothurOut("Invalid.");
395 mout->mothurOutEndLine();
399 mout->gobble(inputBatchFile);
402 inputBatchFile.close();
405 catch(exception& e) {
406 mout->errorOut(e, "BatchEngine", "getInput");
410 /***********************************************************************/
411 string BatchEngine::getNextCommand(ifstream& inputBatchFile) {
414 string nextcommand = "";
416 if (inputBatchFile.eof()) { nextcommand = "quit()"; }
417 else { nextcommand = mout->getline(inputBatchFile); }
421 catch(exception& e) {
422 mout->errorOut(e, "BatchEngine", "getNextCommand");
427 /***********************************************************************/
428 /***********************************************************************/
429 //This function opens the batchfile to be used by BatchEngine::getInput.
430 ScriptEngine::ScriptEngine(string path, string commandString){
434 listOfCommands = commandString.substr(1, (commandString.length()-1));
436 string temppath = path.substr(0, (path.find_last_of("othur")-5));
438 //this will happen if you set the path variable to contain mothur's exe location
439 if (temppath == "") { path = findMothursPath(); }
444 catch(exception& e) {
445 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
450 /***********************************************************************/
452 ScriptEngine::~ScriptEngine(){ }
454 /***********************************************************************/
455 //This Function allows the user to run a batchfile containing several commands on mothur
456 bool ScriptEngine::getInput(){
460 string commandName = "";
464 //CommandFactory cFactory;
465 int quitCommandCalled = 0;
467 while(quitCommandCalled != 1){
472 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
473 MPI_Comm_size(MPI_COMM_WORLD, &processors);
479 input = getNextCommand(listOfCommands);
481 if (input == "") { input = "quit()"; }
484 if ((input.find("quit") != string::npos) || (input.find("set.logfile") != string::npos)) {}
485 else if ((input.find("get.current") != string::npos) && (!mout->hasCurrentFiles())) {}
486 else { mout->mothurOutEndLine(); mout->mothurOut("mothur > " + input); mout->mothurOutEndLine(); }
488 mout->mothurOutEndLine(); mout->mothurOut("mothur > " + input); mout->mothurOutEndLine();
493 for(int i = 1; i < processors; i++) {
494 int length = input.length();
495 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
496 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
501 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
503 char* tempBuf = new char[length];
504 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
507 if (input.length() > length) { input = input.substr(0, length); }
515 if (mout->control_pressed) { input = "quit()"; }
517 //allow user to omit the () on the quit command
518 if (input == "quit") { input = "quit()"; }
520 CommandOptionParser parser(input);
521 commandName = parser.getCommandString();
522 options = parser.getOptionString();
524 if (commandName != "") {
525 mout->executing = true;
527 int pid, numProcesses;
529 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
530 MPI_Comm_size(MPI_COMM_WORLD, &numProcesses);
532 //cout << pid << " is here " << commandName << endl;
533 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
534 //cout << pid << " is in execute" << endl;
536 //executes valid command
537 mout->runParse = true;
539 mout->clearAllGroups();
540 mout->Treenames.clear();
542 mout->saveNextLabel = "";
543 mout->printedHeaders = false;
544 mout->commandInputsConvertError = false;
545 mout->currentBinLabels.clear();
546 mout->binLabelsInFile.clear();
548 Command* command = cFactory->getCommand(commandName, options);
549 if (mout->commandInputsConvertError) { quitCommandCalled = 2; }
550 else { quitCommandCalled = command->execute(); }
552 //if we aborted command
553 if (quitCommandCalled == 2) { mout->mothurOut("[ERROR]: did not complete " + commandName + "."); mout->mothurOutEndLine(); }
555 mout->control_pressed = 0;
556 mout->executing = false;
559 //cout << pid << " is done in execute" << endl;
563 mout->mothurOut("Invalid.");
564 mout->mothurOutEndLine();
572 catch(exception& e) {
573 mout->errorOut(e, "ScriptEngine", "getInput");
577 /***********************************************************************/
578 string ScriptEngine::getNextCommand(string& commandString) {
581 string nextcommand = "";
583 bool ignoreSemiColons = false;
585 //go through string until you reach ; or end
586 while (count < commandString.length()) {
588 //you want to ignore any ; until you reach the next '
589 if ((commandString[count] == '\'') && (!ignoreSemiColons)) { ignoreSemiColons = true; }
590 else if ((commandString[count] == '\'') && (ignoreSemiColons)) { ignoreSemiColons = false; }
592 if ((commandString[count] == ';') && (!ignoreSemiColons)) { break; }
593 else { nextcommand += commandString[count]; }
598 //if you are not at the end
599 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
600 else { commandString = ""; }
603 //get rid of spaces in between commands if any
604 if (commandString.length() > 0) {
605 while (commandString[0] == ' ') {
606 commandString = commandString.substr(1,commandString.length());
607 if (commandString.length() == 0) { break; }
613 catch(exception& e) {
614 mout->errorOut(e, "ScriptEngine", "getNextCommand");
618 /***********************************************************************/