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->currentBinLabels.clear();
190 mout->binLabelsInFile.clear();
192 Command* command = cFactory->getCommand(commandName, options);
193 quitCommandCalled = command->execute();
195 //if we aborted command
196 if (quitCommandCalled == 2) { mout->mothurOut("[ERROR]: did not complete " + commandName + "."); mout->mothurOutEndLine(); }
198 mout->control_pressed = 0;
199 mout->executing = false;
205 mout->mothurOut("Invalid.");
206 mout->mothurOutEndLine();
211 catch(exception& e) {
212 mout->errorOut(e, "InteractEngine", "getInput");
216 /***********************************************************************/
217 string Engine::getCommand() {
220 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
222 char* nextCommand = NULL;
223 nextCommand = readline("mothur > ");
225 if(nextCommand != NULL) { add_history(nextCommand); }
226 else{ //^D causes null string and we want it to quit mothur
227 nextCommand = strdup("quit");
228 mout->mothurOut(nextCommand);
231 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
234 string nextCommand = "";
235 mout->mothurOut("mothur > ");
236 getline(cin, nextCommand);
237 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
242 string nextCommand = "";
244 mout->mothurOut("mothur > ");
245 getline(cin, nextCommand);
246 mout->mothurOutJustToLog(toString(nextCommand));
253 catch(exception& e) {
254 mout->errorOut(e, "Engine", "getCommand");
258 /***********************************************************************/
259 //This function opens the batchfile to be used by BatchEngine::getInput.
260 BatchEngine::BatchEngine(string path, string batchFileName){
263 openedBatch = mout->openInputFile(batchFileName, inputBatchFile);
265 string temppath = path.substr(0, (path.find_last_of("othur")-5));
267 //this will happen if you set the path variable to contain mothur's exe location
268 if (temppath == "") { path = findMothursPath(); }
273 catch(exception& e) {
274 mout->errorOut(e, "BatchEngine", "BatchEngine");
279 /***********************************************************************/
281 BatchEngine::~BatchEngine(){ }
283 /***********************************************************************/
284 //This Function allows the user to run a batchfile containing several commands on Dotur
285 bool BatchEngine::getInput(){
287 //check if this is a valid batchfile
288 if (openedBatch == 1) {
289 mout->mothurOut("unable to open batchfile");
290 mout->mothurOutEndLine();
295 string commandName = "";
298 //CommandFactory cFactory;
299 int quitCommandCalled = 0;
301 while(quitCommandCalled != 1){
306 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
307 MPI_Comm_size(MPI_COMM_WORLD, &processors);
313 input = getNextCommand(inputBatchFile);
318 for(int i = 1; i < processors; i++) {
319 int length = input.length();
320 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
321 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
326 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
328 char* tempBuf = new char[length];
329 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
332 if (input.length() > length) { input = input.substr(0, length); }
341 if (input[0] != '#') {
343 mout->mothurOutEndLine();
344 mout->mothurOut("mothur > " + input);
345 mout->mothurOutEndLine();
347 if (mout->control_pressed) { input = "quit()"; }
349 //allow user to omit the () on the quit command
350 if (input == "quit") { input = "quit()"; }
352 CommandOptionParser parser(input);
353 commandName = parser.getCommandString();
354 options = parser.getOptionString();
356 if (commandName != "") {
357 mout->executing = true;
360 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
362 //cout << pid << " is here " << commandName << '\t' << count << endl;
363 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
365 //executes valid command
366 mout->runParse = true;
368 mout->clearAllGroups();
369 mout->Treenames.clear();
371 mout->saveNextLabel = "";
372 mout->printedHeaders = false;
373 mout->currentBinLabels.clear();
374 mout->binLabelsInFile.clear();
377 Command* command = cFactory->getCommand(commandName, options);
378 quitCommandCalled = command->execute();
380 //if we aborted command
381 if (quitCommandCalled == 2) { mout->mothurOut("[ERROR]: did not complete " + commandName + "."); mout->mothurOutEndLine(); }
383 mout->control_pressed = 0;
384 mout->executing = false;
390 mout->mothurOut("Invalid.");
391 mout->mothurOutEndLine();
395 mout->gobble(inputBatchFile);
398 inputBatchFile.close();
401 catch(exception& e) {
402 mout->errorOut(e, "BatchEngine", "getInput");
406 /***********************************************************************/
407 string BatchEngine::getNextCommand(ifstream& inputBatchFile) {
410 string nextcommand = "";
412 if (inputBatchFile.eof()) { nextcommand = "quit()"; }
413 else { nextcommand = mout->getline(inputBatchFile); }
417 catch(exception& e) {
418 mout->errorOut(e, "BatchEngine", "getNextCommand");
423 /***********************************************************************/
424 /***********************************************************************/
425 //This function opens the batchfile to be used by BatchEngine::getInput.
426 ScriptEngine::ScriptEngine(string path, string commandString){
430 listOfCommands = commandString.substr(1, (commandString.length()-1));
432 string temppath = path.substr(0, (path.find_last_of("othur")-5));
434 //this will happen if you set the path variable to contain mothur's exe location
435 if (temppath == "") { path = findMothursPath(); }
440 catch(exception& e) {
441 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
446 /***********************************************************************/
448 ScriptEngine::~ScriptEngine(){ }
450 /***********************************************************************/
451 //This Function allows the user to run a batchfile containing several commands on mothur
452 bool ScriptEngine::getInput(){
456 string commandName = "";
460 //CommandFactory cFactory;
461 int quitCommandCalled = 0;
463 while(quitCommandCalled != 1){
468 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
469 MPI_Comm_size(MPI_COMM_WORLD, &processors);
475 input = getNextCommand(listOfCommands);
477 if (input == "") { input = "quit()"; }
480 if ((input.find("quit") != string::npos) || (input.find("set.logfile") != string::npos)) {}
481 else if ((input.find("get.current") != string::npos) && (!mout->hasCurrentFiles())) {}
482 else { mout->mothurOutEndLine(); mout->mothurOut("mothur > " + input); mout->mothurOutEndLine(); }
484 mout->mothurOutEndLine(); mout->mothurOut("mothur > " + input); mout->mothurOutEndLine();
489 for(int i = 1; i < processors; i++) {
490 int length = input.length();
491 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
492 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
497 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
499 char* tempBuf = new char[length];
500 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
503 if (input.length() > length) { input = input.substr(0, length); }
511 if (mout->control_pressed) { input = "quit()"; }
513 //allow user to omit the () on the quit command
514 if (input == "quit") { input = "quit()"; }
516 CommandOptionParser parser(input);
517 commandName = parser.getCommandString();
518 options = parser.getOptionString();
520 if (commandName != "") {
521 mout->executing = true;
523 int pid, numProcesses;
525 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
526 MPI_Comm_size(MPI_COMM_WORLD, &numProcesses);
528 //cout << pid << " is here " << commandName << endl;
529 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
530 //cout << pid << " is in execute" << endl;
532 //executes valid command
533 mout->runParse = true;
535 mout->clearAllGroups();
536 mout->Treenames.clear();
538 mout->saveNextLabel = "";
539 mout->printedHeaders = false;
540 mout->currentBinLabels.clear();
541 mout->binLabelsInFile.clear();
544 Command* command = cFactory->getCommand(commandName, options);
545 quitCommandCalled = command->execute();
547 //if we aborted command
548 if (quitCommandCalled == 2) { mout->mothurOut("[ERROR]: did not complete " + commandName + "."); mout->mothurOutEndLine(); }
550 mout->control_pressed = 0;
551 mout->executing = false;
554 //cout << pid << " is done in execute" << endl;
558 mout->mothurOut("Invalid.");
559 mout->mothurOutEndLine();
567 catch(exception& e) {
568 mout->errorOut(e, "ScriptEngine", "getInput");
572 /***********************************************************************/
573 string ScriptEngine::getNextCommand(string& commandString) {
576 string nextcommand = "";
578 bool ignoreSemiColons = false;
580 //go through string until you reach ; or end
581 while (count < commandString.length()) {
583 //you want to ignore any ; until you reach the next '
584 if ((commandString[count] == '\'') && (!ignoreSemiColons)) { ignoreSemiColons = true; }
585 else if ((commandString[count] == '\'') && (ignoreSemiColons)) { ignoreSemiColons = false; }
587 if ((commandString[count] == ';') && (!ignoreSemiColons)) { break; }
588 else { nextcommand += commandString[count]; }
593 //if you are not at the end
594 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
595 else { commandString = ""; }
598 //get rid of spaces in between commands if any
599 if (commandString.length() > 0) {
600 while (commandString[0] == ' ') {
601 commandString = commandString.substr(1,commandString.length());
602 if (commandString.length() == 0) { break; }
608 catch(exception& e) {
609 mout->errorOut(e, "ScriptEngine", "getNextCommand");
613 /***********************************************************************/