From: westcott <westcott>
Date: Tue, 16 Jun 2009 19:01:09 +0000 (+0000)
Subject: fixed some bugs and added scriptengine
X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=786d5631d9cd5baa6ed6ef16f8b4b384cbc7470f;p=mothur.git

fixed some bugs and added scriptengine
---

diff --git a/engine.cpp b/engine.cpp
index df65678..03422e2 100644
--- a/engine.cpp
+++ b/engine.cpp
@@ -103,6 +103,7 @@ bool InteractEngine::getInput(){
 BatchEngine::BatchEngine(string path, string batchFileName){
 	try {
 		globaldata = GlobalData::getInstance();
+	
 		openedBatch = openInputFile(batchFileName, inputBatchFile);
 		globaldata->argv = path;
 
@@ -140,6 +141,7 @@ bool BatchEngine::getInput(){
 		string input = "";
 		string commandName = "";
 		string options = "";
+		//int count = 1;
 		
 		//CommandFactory cFactory;
 		int quitCommandCalled = 0;
@@ -149,6 +151,8 @@ bool BatchEngine::getInput(){
 			if (inputBatchFile.eof()) { input = "quit()"; }
 			else { getline(inputBatchFile, input); }
 			
+			//cout << "command number" << count << endl; count++;
+			
 			if (input[0] != '#') {
 			
 				cout << endl << "mothur > " << input << endl;
@@ -185,4 +189,112 @@ bool BatchEngine::getInput(){
 
 
 /***********************************************************************/
+/***********************************************************************/
+//This function opens the batchfile to be used by BatchEngine::getInput.
+ScriptEngine::ScriptEngine(string path, string commandString){
+	try {
+		globaldata = GlobalData::getInstance();
+		
+		//remove quotes
+		listOfCommands = commandString.substr(1, (commandString.length()-1));
+
+		globaldata->argv = path;
 
+		system("clear");
+	
+	}
+	catch(exception& e) {
+		cout << "Standard Error: " << e.what() << " has occurred in the ScriptEngine class Function ScriptEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+		exit(1);
+	}
+	catch(...) {
+		cout << "An unknown error has occurred in the ScriptEngine class function ScriptEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+		exit(1);
+	}
+}
+
+/***********************************************************************/
+
+ScriptEngine::~ScriptEngine(){
+	}
+
+/***********************************************************************/
+//This Function allows the user to run a batchfile containing several commands on Dotur
+bool ScriptEngine::getInput(){
+	try {
+			
+		string input = "";
+		string commandName = "";
+		string options = "";
+		//int count = 1;
+		
+		//CommandFactory cFactory;
+		int quitCommandCalled = 0;
+	
+		while(quitCommandCalled == 0){
+		
+			input = getNextCommand(listOfCommands);	
+			
+			if (input == "") { input = "quit()"; }
+			//cout << "command number" << count << endl; count++;
+			
+			cout << endl << "mothur > " << input << endl;
+				
+			//allow user to omit the () on the quit command
+			if (input == "quit") { input = "quit()"; }
+
+			CommandOptionParser parser(input);
+			commandName = parser.getCommandString();
+			options = parser.getOptionString();
+										
+			if (commandName != "") {
+
+				//executes valid command
+				CommandFactory cFactory;
+				Command* command = cFactory.getCommand(commandName, options);
+				quitCommandCalled = command->execute();
+			}else {		cout << "Invalid." << endl;		}
+			
+		}
+		
+		return 1;
+	}
+	catch(exception& e) {
+		cout << "Standard Error: " << e.what() << " has occurred in the ScriptEngine class Function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+		exit(1);
+	}
+	catch(...) {
+		cout << "An unknown error has occurred in the ScriptEngine class function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+		exit(1);
+	}
+}
+/***********************************************************************/
+string ScriptEngine::getNextCommand(string& commandString) {
+	try {
+		string nextcommand = "";
+		
+		nextcommand = commandString.substr(0,commandString.find_first_of(';'));
+
+				
+		if ((commandString.find_first_of(';')+1) <= commandString.length()) {
+			commandString = commandString.substr(commandString.find_first_of(';')+1, commandString.length());
+		}else { commandString = ""; } //you have reached the last command.
+		
+		//get rid of any extra spaces in between commands
+		//string space = " ";
+		
+		//while(commandString.at(0) == ' ')
+			//commandString = commandString.substr(1, commandString.length());
+			
+		return nextcommand;
+	}
+	catch(exception& e) {
+		cout << "Standard Error: " << e.what() << " has occurred in the ScriptEngine class Function getNextCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+		exit(1);
+	}
+	catch(...) {
+		cout << "An unknown error has occurred in the ScriptEngine class function getNextCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+		exit(1);
+	}
+}
+/***********************************************************************/
\ No newline at end of file
diff --git a/engine.hpp b/engine.hpp
index d169905..603ff0a 100644
--- a/engine.hpp
+++ b/engine.hpp
@@ -57,4 +57,18 @@ private:
 };
 
 
+class ScriptEngine : public Engine {
+public:
+	ScriptEngine(string, string);
+	~ScriptEngine();
+	virtual bool getInput();
+	int openedBatch;
+private:
+	GlobalData* globaldata;
+	string listOfCommands;
+	string getNextCommand(string&);
+
+};
+
+
 #endif
diff --git a/mothur.cpp b/mothur.cpp
index 8d0e507..59837dd 100644
--- a/mothur.cpp
+++ b/mothur.cpp
@@ -22,9 +22,16 @@ int main(int argc, char *argv[]){
 
 		Engine* mothur;
 		bool bail = 0;
+		string input;
 
 		if(argc>1){
-			mothur = new BatchEngine(argv[0], argv[1]);
+			input = argv[1];
+
+			if (input[0] == '#') {
+				mothur = new ScriptEngine(argv[0], argv[1]);
+			}else{
+				mothur = new BatchEngine(argv[0], argv[1]);
+			}
 		}
 		else{
 			mothur = new InteractEngine(argv[0]);		
diff --git a/parselistcommand.cpp b/parselistcommand.cpp
index 5de3b72..332074d 100644
--- a/parselistcommand.cpp
+++ b/parselistcommand.cpp
@@ -15,7 +15,8 @@ ParseListCommand::ParseListCommand(){
 		globaldata = GlobalData::getInstance();
 		
 		//read in group map info.
-		groupMap = globaldata->gGroupmap;
+		groupMap = new GroupMap(globaldata->getGroupFile());
+		groupMap->readMap();
 
 		//fill filehandles with neccessary ofstreams
 		int i;
diff --git a/readotucommand.cpp b/readotucommand.cpp
index ba7eae4..2df74e4 100644
--- a/readotucommand.cpp
+++ b/readotucommand.cpp
@@ -64,6 +64,7 @@ ReadOtuCommand::ReadOtuCommand(string option){
 				globaldata->setGroupFile(groupfile); 
 				groupMap = new GroupMap(groupfile);
 				groupMap->readMap();
+				globaldata->gGroupmap = groupMap;
 			}
 
 			//you are doing a list and group shared
@@ -168,10 +169,7 @@ int ReadOtuCommand::execute(){
 		
 		//read->read(&*globaldata); 
 		if (globaldata->getFormat() == "shared") {
-			groupMap->readMap();
-			
-			//if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap;  }
-			globaldata->gGroupmap = groupMap;
+			//groupMap->readMap();
 			
 			parselist = new ParseListCommand();
 			parselist->execute();
diff --git a/readtreecommand.cpp b/readtreecommand.cpp
index b030542..88e34ff 100644
--- a/readtreecommand.cpp
+++ b/readtreecommand.cpp
@@ -33,6 +33,7 @@ ReadTreeCommand::ReadTreeCommand(string option){
 				if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
 			}
 			
+			globaldata->runParse = true;
 			globaldata->newRead();
 			
 			//check for required parameters
diff --git a/tree.cpp b/tree.cpp
index 656d4cc..c078f4f 100644
--- a/tree.cpp
+++ b/tree.cpp
@@ -495,6 +495,7 @@ void Tree::createNewickFile(string f) {
 		int root = findRoot();
 		//filename = getRootName(globaldata->getTreeFile()) + "newick";
 		filename = f;
+
 		openOutputFile(filename, out);
 		
 		printBranch(root, out, "branch");
diff --git a/treegroupscommand.cpp b/treegroupscommand.cpp
index 484b553..0f106c4 100644
--- a/treegroupscommand.cpp
+++ b/treegroupscommand.cpp
@@ -66,7 +66,7 @@ TreeGroupCommand::TreeGroupCommand(string option){
 			else if (namefile == "not found") { namefile = ""; }
 			else {  globaldata->setNameFile(namefile);	}
 			
-//			format = globaldata->getFormat();
+			format = globaldata->getFormat();
 			
 			//error checking on files			
 			if ((globaldata->getSharedFile() == "") && ((phylipfile == "") && (columnfile == "")))	{ cout << "You must run the read.otu command or provide a distance file before running the tree.shared command." << endl; abort = true; }
@@ -234,6 +234,8 @@ int TreeGroupCommand::execute(){
 			
 			if (lookup.size() < 2) { cout << "You have not provided enough valid groups.  I cannot run the command." << endl; return 0; }
 		
+			globaldata->runParse = false;
+			
 			//create tree file
 			makeSimsShared();
 		}else{
@@ -269,7 +271,9 @@ int TreeGroupCommand::execute(){
 		
 			//fills globaldatas tree names
 			globaldata->Treenames = globaldata->Groups;
-
+			
+			globaldata->runParse = false;
+			
 			makeSimsDist();
 
 			//create a new filename
@@ -562,7 +566,7 @@ void TreeGroupCommand::process(vector<SharedRAbundVector*> thisLookup) {
 							}
 						}
 					}
-					
+				
 					//creates tree from similarity matrix and write out file
 					createTree();
 				}