]> git.donarmstrong.com Git - mothur.git/blob - mothur.cpp
minor bug in chimera maligner
[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 outputHeader = true;
82                 if (argc>1) {
83                         string guiInput = argv[1];
84                         if (guiInput[0] == '+') { outputHeader = false; }
85                         if (guiInput[0] == '-') { outputHeader = false; }
86                 }
87                 
88                 if (outputHeader)  {
89                         //version
90                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
91                                 #if defined (__APPLE__) || (__MACH__)
92                                         m->mothurOutJustToLog("Mac version");
93                                         m->mothurOutEndLine(); m->mothurOutEndLine();
94                                 #else
95                                         m->mothurOutJustToLog("Linux version");
96                                         m->mothurOutEndLine(); m->mothurOutEndLine();
97                                 #endif
98
99                         #else
100                                 m->mothurOutJustToLog("Windows version");
101                                 m->mothurOutEndLine(); m->mothurOutEndLine();
102                         #endif          
103                         
104                         #ifdef USE_READLINE
105                                 m->mothurOutJustToLog("Using ReadLine");
106                                 m->mothurOutEndLine(); m->mothurOutEndLine();
107                         #endif
108                         
109                         #ifdef MOTHUR_FILES
110                                 m->mothurOutJustToLog("Using default file location " + temp);
111                                 m->mothurOutEndLine(); m->mothurOutEndLine();
112                         #endif
113                         
114                         #ifdef BIT_VERSION
115                                 m->mothurOutJustToLog("Running 64Bit Version");
116                                 m->mothurOutEndLine(); m->mothurOutEndLine();
117                         #else
118                                 m->mothurOutJustToLog("Running 32Bit Version");
119                                 m->mothurOutEndLine(); m->mothurOutEndLine();
120                         #endif
121                         
122                         //header
123                         m->mothurOut("mothur v." + mothurVersion);
124                         m->mothurOutEndLine();          
125                         m->mothurOut("Last updated: " + releaseDate);
126                         m->mothurOutEndLine();  
127                         m->mothurOutEndLine();          
128                         m->mothurOut("by");
129                         m->mothurOutEndLine();          
130                         m->mothurOut("Patrick D. Schloss");
131                         m->mothurOutEndLine();
132                         m->mothurOutEndLine();                  
133                         m->mothurOut("Department of Microbiology & Immunology");
134                         m->mothurOutEndLine();  
135                         m->mothurOut("University of Michigan");
136                         m->mothurOutEndLine();                  
137                         m->mothurOut("pschloss@umich.edu");
138                         m->mothurOutEndLine();          
139                         m->mothurOut("http://www.mothur.org");
140                         m->mothurOutEndLine();
141                         m->mothurOutEndLine();
142                         m->mothurOut("When using, please cite:");
143                         m->mothurOutEndLine();
144                         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.");
145                         m->mothurOutEndLine();  
146                         m->mothurOutEndLine();          
147                         m->mothurOut("Distributed under the GNU General Public License");
148                         m->mothurOutEndLine();
149                         m->mothurOutEndLine();                  
150                         m->mothurOut("Type 'help()' for information on the commands that are available");
151                         m->mothurOutEndLine();
152                         m->mothurOutEndLine();                  
153                         m->mothurOut("Type 'quit()' to exit program");
154                         m->mothurOutEndLine();  
155                         
156                         #ifdef USE_MPI
157                                 m->mothurOutJustToLog("Using MPI\tversion ");
158                                 m->mothurOutJustToLog(toString(version) + "." + toString(subversion) + "\n");
159                         #endif
160                 }
161                 
162                 //srand(54321);
163                 srand( (unsigned)time( NULL ) );
164                 
165                 Engine* mothur = NULL;
166                 bool bail = 0;
167                 string input;
168  
169                 if(argc>1){
170                         input = argv[1];
171                         //m->mothurOut("input = " + input); m->mothurOutEndLine();
172
173                         if (input[0] == '#') {
174                                 m->mothurOutJustToLog("Script Mode");
175                                 m->mothurOutEndLine(); m->mothurOutEndLine();
176
177                                 mothur = new ScriptEngine(argv[0], argv[1]);
178                         }else if (input[0] == '+') {
179                                         mothur = new ScriptEngine(argv[0], argv[1]);
180                                         m->gui = true;
181                         }else if (input == "-version") {
182                                 m->mothurOut("Mothur version=" + mothurVersion + "\nRelease Date=" + releaseDate); m->mothurOutEndLine(); m->mothurOutEndLine(); m->closeLog();
183                                 #ifdef USE_MPI
184                                         MPI_Finalize();
185                                 #endif
186                                 return 0;
187                         }else{
188                                 m->mothurOutJustToLog("Batch Mode");
189                                 m->mothurOutEndLine(); m->mothurOutEndLine();
190                                 
191                                 mothur = new BatchEngine(argv[0], argv[1]);
192                         }
193                 }
194                 else{
195                         m->mothurOutJustToLog("Interactive Mode");
196                         m->mothurOutEndLine(); m->mothurOutEndLine();
197                         
198                         mothur = new InteractEngine(argv[0]);   
199                 }
200                 
201                 while(bail == 0)        {       bail = mothur->getInput();      }
202                 
203                 //closes logfile so we can rename
204                 m->closeLog();
205                 
206                 string outputDir = mothur->getOutputDir();
207                 string tempLog = mothur->getLogFileName();
208                 bool append = mothur->getAppend();
209                 
210                 string newlogFileName;
211                 if (tempLog != "") {
212                         newlogFileName = outputDir + tempLog;
213                         
214                         if (!append) {  
215                                 //need this because m->mothurOut makes the logfile, but doesn't know where to put it
216                                 rename(logFileName.c_str(), newlogFileName.c_str()); //logfile with timestamp
217
218                         }else {
219                                 ofstream outNewLog;
220                                 m->openOutputFileAppend(newlogFileName, outNewLog);
221                                 
222                                 if (!m->gui) {
223                                         outNewLog << endl << endl << "*********************************************************************************" << endl << endl;
224                                 }else {
225                                         outNewLog << endl;
226                                 }
227                                 outNewLog.close();
228                                 
229                                 m->appendFiles(logFileName, newlogFileName);
230                                 remove(logFileName.c_str());
231                         }
232                 }else{  
233                         newlogFileName = outputDir + logFileName;
234                         //need this because m->mothurOut makes the logfile, but doesn't know where to put it
235                         rename(logFileName.c_str(), newlogFileName.c_str()); //logfile with timestamp
236                 }
237                 
238                                 
239                 if (mothur != NULL) { delete mothur; }
240                 
241                 #ifdef USE_MPI
242                         MPI_Finalize();
243                 #endif
244                 
245                 return 0;
246         }
247         catch(exception& e) {
248                 m->errorOut(e, "mothur", "main");
249                 exit(1);
250         }
251 }
252
253 /**************************************************************************************************/
254