]> git.donarmstrong.com Git - mothur.git/blob - mothur.cpp
1.18.1
[mothur.git] / mothur.cpp
1 /*
2  *  interface.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 8/14/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
8  */
9  
10 #include "mothur.h"
11 #include "engine.hpp"
12 #include "mothurout.h"
13
14 /**************************************************************************************************/
15
16 CommandFactory* CommandFactory::_uniqueInstance = 0;
17 MothurOut* MothurOut::_uniqueInstance = 0;
18
19 /***********************************************************************/
20 volatile int ctrlc_pressed = 0;
21 void ctrlc_handler ( int sig ) {
22         MothurOut* m = MothurOut::getInstance();
23     ctrlc_pressed = 1;
24         m->control_pressed = ctrlc_pressed;
25         
26         if (m->executing) { //if mid command quit execution, else quit mothur
27                 m->mothurOutEndLine(); m->mothurOut("quitting command...");  m->mothurOutEndLine();
28         }else{
29                 m->mothurOut("quitting mothur");  m->mothurOutEndLine();
30                 exit(1);
31         }
32 }
33 /***********************************************************************/
34 int main(int argc, char *argv[]){
35         MothurOut* m = MothurOut::getInstance();
36         try {   
37                 signal(SIGINT, ctrlc_handler );
38                                 
39                 time_t ltime = time(NULL); /* calendar time */  
40                 string logFileName = "mothur." + toString(ltime) + ".logfile";
41                 
42                 #ifdef USE_MPI
43                         MPI_Init(&argc, &argv); 
44                 #endif
45
46                 m->setFileName(logFileName);
47                 
48                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
49                         system("clear");
50                 #else
51                         system("CLS");
52                 #endif
53                 
54                 #ifdef MOTHUR_FILES
55                         string temp = MOTHUR_FILES; 
56                 
57                         //add / to name if needed
58                         string lastChar = temp.substr(temp.length()-1);
59                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
60                                 if (lastChar != "/") { temp += "/"; }
61                         #else
62                                 if (lastChar != "\\") { temp += "\\"; } 
63                         #endif
64                 
65                         temp = m->getFullPathName(temp);
66                         m->setDefaultPath(temp);
67                 #endif
68                 
69                 #ifdef USE_MPI
70                         int version, subversion;
71                         MPI_Get_version(&version, &subversion);
72                 #endif
73                 
74                 //get releaseDate from Make
75                 string releaseDate = RELEASE_DATE; 
76                 string mothurVersion = VERSION; 
77                 m->setReleaseDate(releaseDate);
78                 m->setVersion(mothurVersion);
79                 
80                 //will make the gui output "pretty"
81                 bool guiMode = false;
82                 if (argc>1) {
83                         string guiInput = argv[1];
84                         if (guiInput[0] == '+') { guiMode = true; }
85                 }
86                 
87                 if (!guiMode) {
88                         //version
89                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
90                                 #if defined (__APPLE__) || (__MACH__)
91                                         m->mothurOutJustToLog("Mac version");
92                                         m->mothurOutEndLine(); m->mothurOutEndLine();
93                                 #else
94                                         m->mothurOutJustToLog("Linux version");
95                                         m->mothurOutEndLine(); m->mothurOutEndLine();
96                                 #endif
97
98                         #else
99                                 m->mothurOutJustToLog("Windows version");
100                                 m->mothurOutEndLine(); m->mothurOutEndLine();
101                         #endif          
102                         
103                         #ifdef USE_READLINE
104                                 m->mothurOutJustToLog("Using ReadLine");
105                                 m->mothurOutEndLine(); m->mothurOutEndLine();
106                         #endif
107                         
108                         #ifdef MOTHUR_FILES
109                                 m->mothurOutJustToLog("Using default file location " + temp);
110                                 m->mothurOutEndLine(); m->mothurOutEndLine();
111                         #endif
112                         
113                         #ifdef BIT_VERSION
114                                 m->mothurOutJustToLog("Running 64Bit Version");
115                                 m->mothurOutEndLine(); m->mothurOutEndLine();
116                         #else
117                                 m->mothurOutJustToLog("Running 32Bit Version");
118                                 m->mothurOutEndLine(); m->mothurOutEndLine();
119                         #endif
120                         
121                         //header
122                         m->mothurOut("mothur v." + mothurVersion);
123                         m->mothurOutEndLine();          
124                         m->mothurOut("Last updated: " + releaseDate);
125                         m->mothurOutEndLine();  
126                         m->mothurOutEndLine();          
127                         m->mothurOut("by");
128                         m->mothurOutEndLine();          
129                         m->mothurOut("Patrick D. Schloss");
130                         m->mothurOutEndLine();
131                         m->mothurOutEndLine();                  
132                         m->mothurOut("Department of Microbiology & Immunology");
133                         m->mothurOutEndLine();  
134                         m->mothurOut("University of Michigan");
135                         m->mothurOutEndLine();                  
136                         m->mothurOut("pschloss@umich.edu");
137                         m->mothurOutEndLine();          
138                         m->mothurOut("http://www.mothur.org");
139                         m->mothurOutEndLine();
140                         m->mothurOutEndLine();
141                         m->mothurOut("When using, please cite:");
142                         m->mothurOutEndLine();
143                         m->mothurOut("Schloss, P.D., et al., Introducing mothur: Open-source, platform-independent, community-supported software for describing and comparing microbial communities. Appl Environ Microbiol, 2009. 75(23):7537-41.");
144                         m->mothurOutEndLine();  
145                         m->mothurOutEndLine();          
146                         m->mothurOut("Distributed under the GNU General Public License");
147                         m->mothurOutEndLine();
148                         m->mothurOutEndLine();                  
149                         m->mothurOut("Type 'help()' for information on the commands that are available");
150                         m->mothurOutEndLine();
151                         m->mothurOutEndLine();                  
152                         m->mothurOut("Type 'quit()' to exit program");
153                         m->mothurOutEndLine();  
154                         
155                         #ifdef USE_MPI
156                                 m->mothurOutJustToLog("Using MPI\tversion ");
157                                 m->mothurOutJustToLog(toString(version) + "." + toString(subversion) + "\n");
158                         #endif
159                 }
160                 
161                 //srand(54321);
162                 srand( (unsigned)time( NULL ) );
163                 
164                 Engine* mothur;
165                 bool bail = 0;
166                 string input;
167  
168                 if(argc>1){
169                         input = argv[1];
170                         //m->mothurOut("input = " + input); m->mothurOutEndLine();
171
172                         if (input[0] == '#') {
173                                 m->mothurOutJustToLog("Script Mode");
174                                 m->mothurOutEndLine(); m->mothurOutEndLine();
175
176                                 mothur = new ScriptEngine(argv[0], argv[1]);
177                         }else if (input[0] == '+') {
178                                         mothur = new ScriptEngine(argv[0], argv[1]);
179                                         m->gui = true;
180                         }else{
181                                 m->mothurOutJustToLog("Batch Mode");
182                                 m->mothurOutEndLine(); m->mothurOutEndLine();
183                                 
184                                 mothur = new BatchEngine(argv[0], argv[1]);
185                         }
186                 }
187                 else{
188                         m->mothurOutJustToLog("Interactive Mode");
189                         m->mothurOutEndLine(); m->mothurOutEndLine();
190                         
191                         mothur = new InteractEngine(argv[0]);   
192                 }
193                 
194                 while(bail == 0)        {       bail = mothur->getInput();      }
195                 
196                 //closes logfile so we can rename
197                 m->closeLog();
198                 
199                 string outputDir = mothur->getOutputDir();
200                 string tempLog = mothur->getLogFileName();
201                 bool append = mothur->getAppend();
202                 
203                 string newlogFileName;
204                 if (tempLog != "") {
205                         newlogFileName = outputDir + tempLog;
206                         
207                         if (!append) {  
208                                 //need this because m->mothurOut makes the logfile, but doesn't know where to put it
209                                 rename(logFileName.c_str(), newlogFileName.c_str()); //logfile with timestamp
210
211                         }else {
212                                 ofstream outNewLog;
213                                 m->openOutputFileAppend(newlogFileName, outNewLog);
214                                 
215                                 if (!m->gui) {
216                                         outNewLog << endl << endl << "*********************************************************************************" << endl << endl;
217                                 }else {
218                                         outNewLog << endl;
219                                 }
220                                 outNewLog.close();
221                                 
222                                 m->appendFiles(logFileName, newlogFileName);
223                                 remove(logFileName.c_str());
224                         }
225                 }else{  
226                         newlogFileName = outputDir + logFileName;
227                         //need this because m->mothurOut makes the logfile, but doesn't know where to put it
228                         rename(logFileName.c_str(), newlogFileName.c_str()); //logfile with timestamp
229                 }
230                 
231                                 
232                 delete mothur;
233                 
234                 #ifdef USE_MPI
235                         MPI_Finalize();
236                 #endif
237                 
238                 return 0;
239         }
240         catch(exception& e) {
241                 m->errorOut(e, "mothur", "main");
242                 exit(1);
243         }
244 }
245
246 /**************************************************************************************************/
247