]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
started shared utilities, updates to venn and heatmap added tree.groups 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 using namespace std;
14
15 #include "engine.hpp"
16
17 /***********************************************************************/
18
19 InteractEngine::InteractEngine(){
20
21         globaldata = GlobalData::getInstance();
22
23         system("clear");
24 //      char buffer = ' ';
25 //      ifstream header("introtext.txt");
26 //      while(!header.eof()){
27 //              cout << buffer;
28 //              buffer = header.get();
29 //      }
30 }
31
32 /***********************************************************************/
33
34 InteractEngine::~InteractEngine(){
35         }
36
37 /***********************************************************************/
38 //This function allows the user to input commands one line at a time until they quit.
39 //If the command is garbage it does nothing.
40 bool InteractEngine::getInput(){
41         try {
42                 string input = "";
43                 string commandName = "";
44                 int quitCommandCalled = 0;
45                 bool errorFree;
46                 ErrorCheck* errorCheckor = new ErrorCheck();
47                 
48                 cout << "mothur v1.0" << endl;
49                 cout << "Last updated: 1/29/2009" << endl << endl;
50                 cout << "by" << endl;
51                 cout << "Patrick D. Schloss" << endl << endl;
52                 cout << "Department of Microbiology" << endl;
53                 cout << "The University of Massachusetts" << endl;
54                 cout << "pschloss@micro.umass.edu" << endl;
55                 cout << "http://schloss.micro.umass.edu/mothur" << endl << endl << endl;
56                 cout << "Distributed under the GNU General Public License" << endl << endl;
57                 cout << "Type 'help()' for information on the commands that are available" << endl << endl;
58                 cout << "Type 'quit()' to exit program" << endl;
59
60                 while(quitCommandCalled != 1){
61
62                         cout << endl << "mothur > ";
63                         getline(cin, input);
64                         if (cin.eof()) { input = "quit()"; }
65                         
66                         errorFree = errorCheckor->checkInput(input);
67                         if (errorFree == true) {
68                                 CommandOptionParser parser(input);
69                                 commandName = parser.getCommandString();
70                         
71                                 //executes valid command
72                                 CommandFactory cFactory;
73                                 Command* command = cFactory.getCommand(commandName);
74                                 quitCommandCalled = command->execute();
75                 
76                         }else {
77                                         cout << "Your input contains errors. Please try again." << endl;
78                         }
79                 }       
80                 return 1;
81         }
82         catch(exception& e) {
83                 cout << "Standard Error: " << e.what() << " has occurred in the InteractEngine class Function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
84                 exit(1);
85         }
86         catch(...) {
87                 cout << "An unknown error has occurred in the InteractEngine class function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
88                 exit(1);
89         }
90
91 }
92
93 /***********************************************************************/
94 //This function opens the batchfile to be used by BatchEngine::getInput.
95 BatchEngine::BatchEngine(string batchFileName){
96         try {
97                 globaldata = GlobalData::getInstance();
98                 openedBatch = openInputFile(batchFileName, inputBatchFile);
99
100                 system("clear");
101         
102         //      char buffer = ' ';
103         //      ifstream header("introtext.txt");
104         //      while(!header.eof()){
105         //              cout << buffer;
106         //              buffer = header.get();
107         //      }
108         }
109         catch(exception& e) {
110                 cout << "Standard Error: " << e.what() << " has occurred in the BatchEngine class Function BatchEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
111                 exit(1);
112         }
113         catch(...) {
114                 cout << "An unknown error has occurred in the BatchEngine class function BatchEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
115                 exit(1);
116         }
117 }
118
119 /***********************************************************************/
120
121 BatchEngine::~BatchEngine(){
122         }
123
124 /***********************************************************************/
125 //This Function allows the user to run a batchfile containing several commands on Dotur
126 bool BatchEngine::getInput(){
127         try {
128                 string input = "";
129                 string commandName = "";
130                 bool errorFree;
131                 ErrorCheck* errorCheckor = new ErrorCheck();
132
133                 CommandFactory cFactory;
134                 int quitCommandCalled = 0;
135         
136                 while(quitCommandCalled == 0){
137                 
138                         getline(inputBatchFile, input);
139                         if (inputBatchFile.eof()) { input = "quit()"; }
140                         
141                         cout << endl << "mothur > " << input << endl;
142                         errorFree = errorCheckor->checkInput(input);
143                         if (errorFree == true) {
144                                 CommandOptionParser parser(input);
145                                 commandName = parser.getCommandString();
146                                 ifstream filehandle;
147                 
148                                 if (openedBatch == 0) { //able to open batchfile
149                                         //executes valid command
150                                         CommandFactory cFactory;
151                                         Command* command = cFactory.getCommand(commandName);
152                                         quitCommandCalled = command->execute();
153                                 }
154                                 else {
155                                         cout << "Invalid." << endl;
156                                 }
157                         }
158                         else {
159                                 cout << "Unable to open batchfile." << endl;
160                         }
161                 }
162                 return 1;
163         }
164         catch(exception& e) {
165                 cout << "Standard Error: " << e.what() << " has occurred in the BatchEngine class Function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
166                 exit(1);
167         }
168         catch(...) {
169                 cout << "An unknown error has occurred in the BatchEngine class function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
170                 exit(1);
171         }
172 }
173
174
175 /***********************************************************************/
176