]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
changed the daughterLevels in classifiers summary file to reflect the classifications...
[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
14
15 #include "engine.hpp"
16
17 /***********************************************************************/
18 Engine::Engine(){
19         try {
20                 cFactory = CommandFactory::getInstance();
21                 mout = MothurOut::getInstance();
22         }
23         catch(exception& e) {
24                 mout->errorOut(e, "Engine", "Engine");
25                 exit(1);
26         }
27 }
28
29 /***********************************************************************/
30
31 InteractEngine::InteractEngine(string path){
32
33         globaldata = GlobalData::getInstance();
34         globaldata->argv = path;
35         
36 }
37
38 /***********************************************************************/
39
40 InteractEngine::~InteractEngine(){}
41
42 /***********************************************************************/
43 //This function allows the user to input commands one line at a time until they quit.
44 //If the command is garbage it does nothing.
45 bool InteractEngine::getInput(){
46         try {
47                 string input = "";
48                 string commandName = "";
49                 string options = "";
50                 int quitCommandCalled = 0;
51                 
52                 while(quitCommandCalled != 1){
53
54                         mout->mothurOutEndLine();
55                         
56                         input = getCommand();   
57                         mout->mothurOutEndLine();       
58                         
59                         if (mout->control_pressed) { input = "quit()"; }
60                         
61                         //allow user to omit the () on the quit command
62                         if (input == "quit") { input = "quit()"; }
63                         
64                         CommandOptionParser parser(input);
65                         commandName = parser.getCommandString();
66         
67                         options = parser.getOptionString();
68                         
69                         if (commandName != "") {
70                                         mout->executing = true;
71                                         #ifdef USE_MPI
72                                                 int pid;
73                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
74  
75 cout << pid << " is here " << commandName << endl;
76                                                 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
77                                         #endif
78                                         //executes valid command
79                                         Command* command = cFactory->getCommand(commandName, options);
80                                         quitCommandCalled = command->execute();
81                                         mout->control_pressed = 0;
82                                         mout->executing = false;
83                                                                                 
84                                         #ifdef USE_MPI
85                                                 }
86                                         #endif
87                                 }else {         
88                                         mout->mothurOut("Invalid."); 
89                                         mout->mothurOutEndLine();
90                                 }
91                 }       
92                 return 1;
93         }
94         catch(exception& e) {
95                 mout->errorOut(e, "InteractEngine", "getInput");
96                 exit(1);
97         }
98 }
99 /***********************************************************************/
100 string Engine::getCommand()  {
101         try {
102                 #ifdef USE_MPI
103                         MPI_Barrier(MPI_COMM_WORLD);
104                 #endif
105         
106                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
107                         #ifdef USE_READLINE
108                                 char* nextCommand = NULL;
109                                 nextCommand = readline("mothur > ");
110                                 
111                                 if(nextCommand != NULL) {  add_history(nextCommand);  } 
112                                 else{ //^D causes null string and we want it to quit mothur
113                                         nextCommand = "quit"; 
114                                         mout->mothurOut(nextCommand);
115                                 }       
116                                 
117                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
118                                 return nextCommand;
119                         #else
120                                 string nextCommand = "";
121                                 mout->mothurOut("mothur > ");
122                                 getline(cin, nextCommand);
123                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
124                                 
125                                 return nextCommand;
126                         #endif
127                 #else
128                                 string nextCommand = "";
129                                 
130                                 mout->mothurOut("mothur > ");
131                                 getline(cin, nextCommand);
132                                 mout->mothurOutJustToLog(toString(nextCommand));
133                                 
134                                 return nextCommand;
135                 #endif
136         
137                                                 
138         }
139         catch(exception& e) {
140                 mout->errorOut(e, "Engine", "getCommand");
141                 exit(1);
142         }
143 }
144 /***********************************************************************/
145 //This function opens the batchfile to be used by BatchEngine::getInput.
146 BatchEngine::BatchEngine(string path, string batchFileName){
147         try {
148                 globaldata = GlobalData::getInstance();
149         
150                 openedBatch = openInputFile(batchFileName, inputBatchFile);
151                 globaldata->argv = path;
152                                 
153         }
154         catch(exception& e) {
155                 mout->errorOut(e, "BatchEngine", "BatchEngine");
156                 exit(1);
157         }
158 }
159
160 /***********************************************************************/
161
162 BatchEngine::~BatchEngine(){    }
163
164 /***********************************************************************/
165 //This Function allows the user to run a batchfile containing several commands on Dotur
166 bool BatchEngine::getInput(){
167         try {
168                 //check if this is a valid batchfile
169                 if (openedBatch == 1) {  
170                         mout->mothurOut("unable to open batchfile");  
171                         mout->mothurOutEndLine();
172                         return 1; 
173                 }
174         
175                 string input = "";
176                 string commandName = "";
177                 string options = "";
178                 
179                 //CommandFactory cFactory;
180                 int quitCommandCalled = 0;
181             int count = 0;
182                 while(quitCommandCalled == 0){
183         
184                         input = getNextCommand(inputBatchFile);
185                         count++;
186                         
187                         if (input[0] != '#') {
188                                 
189                                 mout->mothurOutEndLine();
190                                 mout->mothurOut("mothur > " + input);
191                                 mout->mothurOutEndLine();
192                                                         
193                                 if (mout->control_pressed) { input = "quit()"; }
194                                 
195                                 //allow user to omit the () on the quit command
196                                 if (input == "quit") { input = "quit()"; }
197
198                                 CommandOptionParser parser(input);
199                                 commandName = parser.getCommandString();
200                                 options = parser.getOptionString();
201                                                                                 
202                                 if (commandName != "") {
203                                         mout->executing = true;
204                                         #ifdef USE_MPI
205                                                 int pid;
206                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
207                                                 
208 cout << pid << " is here " << commandName << '\t' << count << endl;
209                                                 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
210                                         #endif
211                                         //executes valid command
212                                         Command* command = cFactory->getCommand(commandName, options);
213                                         quitCommandCalled = command->execute();
214                                         mout->control_pressed = 0;
215                                         mout->executing = false;
216                                                                                 
217                                         #ifdef USE_MPI
218                                                 }
219                                         #endif
220                                 }else {         
221                                         mout->mothurOut("Invalid."); 
222                                         mout->mothurOutEndLine();
223                                 }
224                                 
225                         }
226                         gobble(inputBatchFile);
227                 }
228                 
229                 inputBatchFile.close();
230                 return 1;
231         }
232         catch(exception& e) {
233                 mout->errorOut(e, "BatchEngine", "getInput");
234                 exit(1);
235         }
236 }
237 /***********************************************************************/
238 string BatchEngine::getNextCommand(ifstream& inputBatchFile) {
239         try {
240                 
241                 #ifdef USE_MPI
242                         int err = MPI_Barrier(MPI_COMM_WORLD);
243 //cout << "barrier = " << err << '\t' << MPI_SUCCESS << endl;
244                 #endif
245                 
246                 string nextcommand = "";
247                 
248                 if (inputBatchFile.eof()) { nextcommand = "quit()"; }
249                 else { nextcommand = getline(inputBatchFile); }
250                 
251                 return nextcommand;
252         }
253         catch(exception& e) {
254                 mout->errorOut(e, "BatchEngine", "getNextCommand");
255                 exit(1);
256         }
257 }
258
259 /***********************************************************************/
260 /***********************************************************************/
261 //This function opens the batchfile to be used by BatchEngine::getInput.
262 ScriptEngine::ScriptEngine(string path, string commandString){
263         try {
264                 globaldata = GlobalData::getInstance();
265                 
266                 //remove quotes
267                 listOfCommands = commandString.substr(1, (commandString.length()-1));
268
269                 globaldata->argv = path;
270                                 
271         }
272         catch(exception& e) {
273                 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
274                 exit(1);
275         }
276 }
277
278 /***********************************************************************/
279
280 ScriptEngine::~ScriptEngine(){  }
281
282 /***********************************************************************/
283 //This Function allows the user to run a batchfile containing several commands on mothur
284 bool ScriptEngine::getInput(){
285         try {
286                         
287                 string input = "";
288                 string commandName = "";
289                 string options = "";
290                 
291                 
292                 //CommandFactory cFactory;
293                 int quitCommandCalled = 0;
294         
295                 while(quitCommandCalled == 0){
296                 
297                         input = getNextCommand(listOfCommands); 
298                         
299                         if (input == "") { input = "quit()"; }
300                         
301                         mout->mothurOutEndLine();
302                         mout->mothurOut("mothur > " + input);
303                         mout->mothurOutEndLine();
304                         
305                         if (mout->control_pressed) { input = "quit()"; }
306                                 
307                         //allow user to omit the () on the quit command
308                         if (input == "quit") { input = "quit()"; }
309
310                         CommandOptionParser parser(input);
311                         commandName = parser.getCommandString();
312                         options = parser.getOptionString();
313                                                                                 
314                         if (commandName != "") {
315                                         mout->executing = true;
316                                         #ifdef USE_MPI
317                                                 int pid, numProcesses;
318                                                 MPI_Status status; 
319                                                 
320                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
321                                                 MPI_Comm_size(MPI_COMM_WORLD, &numProcesses); 
322                                         
323 cout << pid << " is here " << commandName  << endl;
324                                                 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
325                                                         cout << pid << " is in execute" << endl;        
326                                         #endif
327                                         //executes valid command
328                                         Command* command = cFactory->getCommand(commandName, options);
329                                         quitCommandCalled = command->execute();
330                                         mout->control_pressed = 0;
331                                         mout->executing = false;
332                                                                         
333                                         #ifdef USE_MPI
334                                         cout << pid << " is done in execute" << endl;
335                                                 }
336                                         #endif
337                                 }else {         
338                                         mout->mothurOut("Invalid."); 
339                                         mout->mothurOutEndLine();
340                                 }
341
342                         
343                 }
344                 
345                 return 1;
346         }
347         catch(exception& e) {
348                 mout->errorOut(e, "ScriptEngine", "getInput");
349                 exit(1);
350         }
351 }
352 /***********************************************************************/
353 string ScriptEngine::getNextCommand(string& commandString) {
354         try {
355                 
356                 #ifdef USE_MPI
357                         MPI_Barrier(MPI_COMM_WORLD);
358                 #endif
359                 
360                 string nextcommand = "";
361                 int count = 0;
362                 
363                 //go through string until you reach ; or end
364                 while (count < commandString.length()) { 
365                         
366                         if (commandString[count] == ';') {  break;   }
367                         else {          nextcommand += commandString[count];    }
368                         
369                         count++;
370                 }
371                 
372                 //if you are not at the end
373                 if (count != commandString.length())  {   commandString = commandString.substr(count+1, commandString.length());  }
374                 else { commandString = ""; }
375                                 
376                 
377                 //get rid of spaces in between commands if any
378                 if (commandString.length() > 0) {
379                         while (commandString[0] == ' ') {  
380                                 commandString = commandString.substr(1,commandString.length());
381                                 if (commandString.length() == 0) {  break;  }
382                         }
383                 }
384                 
385                 return nextcommand;
386         }
387         catch(exception& e) {
388                 mout->errorOut(e, "ScriptEngine", "getNextCommand");
389                 exit(1);
390         }
391 }
392 /***********************************************************************/