]> git.donarmstrong.com Git - mothur.git/blob - mothur.cpp
added compile time default path
[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 "globaldata.hpp"
13 #include "mothurout.h"
14
15 /**************************************************************************************************/
16
17 GlobalData* GlobalData::_uniqueInstance = 0;
18 CommandFactory* CommandFactory::_uniqueInstance = 0;
19 MothurOut* MothurOut::_uniqueInstance = 0;
20
21 /***********************************************************************/
22 volatile int ctrlc_pressed = 0;
23 void ctrlc_handler ( int sig ) {
24         MothurOut* m = MothurOut::getInstance();
25     ctrlc_pressed = 1;
26         m->control_pressed = ctrlc_pressed;
27         
28         if (m->executing) { //if mid command quit execution, else quit mothur
29                 m->mothurOutEndLine(); m->mothurOut("quitting command...");  m->mothurOutEndLine();
30         }else{
31                 m->mothurOut("quitting mothur");  m->mothurOutEndLine();
32                 exit(1);
33         }
34 }
35 /***********************************************************************/
36 int main(int argc, char *argv[]){
37         MothurOut* m = MothurOut::getInstance();
38         try {
39                 
40                 signal(SIGINT, ctrlc_handler );
41                                 
42                 time_t ltime = time(NULL); /* calendar time */  
43                 string logFileName = "mothur." + toString(ltime) + ".logfile";
44                 
45                 #ifdef USE_MPI
46                         MPI_Init(&argc, &argv); 
47                 #endif
48
49                 m->setFileName(logFileName);
50                 
51                                 
52                 //version
53                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
54                         system("clear");
55                         #if defined (__APPLE__) || (__MACH__)
56                                 m->mothurOutJustToLog("Mac version");
57                                 m->mothurOutEndLine(); m->mothurOutEndLine();
58                         #else
59                                 m->mothurOutJustToLog("Linux version");
60                                 m->mothurOutEndLine(); m->mothurOutEndLine();
61                         #endif
62
63                 #else
64                         system("CLS");
65                         m->mothurOutJustToLog("Windows version");
66                         m->mothurOutEndLine(); m->mothurOutEndLine();
67                 #endif          
68                 
69                 #ifdef USE_READLINE
70                         m->mothurOutJustToLog("Using ReadLine");
71                         m->mothurOutEndLine(); m->mothurOutEndLine();
72                 #endif
73                 
74                 #ifdef MOTHUR_FILES
75                         string temp = MOTHUR_FILES; 
76                         m->setDefaultPath(temp);
77                         m->mothurOutJustToLog("Using default file location " + temp);
78                         m->mothurOutEndLine(); m->mothurOutEndLine();
79                 #endif
80                 
81                 #ifdef BIT_VERSION
82                         m->mothurOutJustToLog("Running 64Bit Version");
83                         m->mothurOutEndLine(); m->mothurOutEndLine();
84                 #else
85                         m->mothurOutJustToLog("Running 32Bit Version");
86                         m->mothurOutEndLine(); m->mothurOutEndLine();
87                 #endif
88                 
89                 //header
90                 m->mothurOut("mothur v.1.11.0");
91                 m->mothurOutEndLine();          
92                 m->mothurOut("Last updated: 6/18/2010");
93                 m->mothurOutEndLine();  
94                 m->mothurOutEndLine();          
95                 m->mothurOut("by");
96                 m->mothurOutEndLine();          
97                 m->mothurOut("Patrick D. Schloss");
98                 m->mothurOutEndLine();
99                 m->mothurOutEndLine();                  
100                 m->mothurOut("Department of Microbiology & Immunology");
101                 m->mothurOutEndLine();  
102                 m->mothurOut("University of Michigan");
103                 m->mothurOutEndLine();                  
104                 m->mothurOut("pschloss@umich.edu");
105                 m->mothurOutEndLine();          
106                 m->mothurOut("http://www.mothur.org");
107                 m->mothurOutEndLine();
108                 m->mothurOutEndLine();
109                 m->mothurOut("When using, please cite:");
110                 m->mothurOutEndLine();
111                 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.");
112                 m->mothurOutEndLine();  
113                 m->mothurOutEndLine();          
114                 m->mothurOut("Distributed under the GNU General Public License");
115                 m->mothurOutEndLine();
116                 m->mothurOutEndLine();                  
117                 m->mothurOut("Type 'help()' for information on the commands that are available");
118                 m->mothurOutEndLine();
119                 m->mothurOutEndLine();                  
120                 m->mothurOut("Type 'quit()' to exit program");
121                 m->mothurOutEndLine();  
122                 
123                 #ifdef USE_MPI
124                         m->mothurOutJustToLog("Using MPI\tversion ");
125                         int version, subversion;
126                         MPI_Get_version(&version, &subversion);
127                         m->mothurOutJustToLog(toString(version) + "." + toString(subversion) + "\n");
128                 #endif
129                 
130                 //srand(54321);
131                 srand( (unsigned)time( NULL ) );
132                 
133                 Engine* mothur;
134                 bool bail = 0;
135                 string input;
136
137                 if(argc>1){
138                         input = argv[1];
139
140                         if (input[0] == '#') {
141                                 m->mothurOutJustToLog("Script Mode");
142                                 m->mothurOutEndLine(); m->mothurOutEndLine();
143
144                                 mothur = new ScriptEngine(argv[0], argv[1]);
145                         }else{
146                                 m->mothurOutJustToLog("Batch Mode");
147                                 m->mothurOutEndLine(); m->mothurOutEndLine();
148                                 
149                                 mothur = new BatchEngine(argv[0], argv[1]);
150                         }
151                 }
152                 else{
153                         m->mothurOutJustToLog("Interactive Mode");
154                         m->mothurOutEndLine(); m->mothurOutEndLine();
155                         
156                         mothur = new InteractEngine(argv[0]);   
157                 }
158                 
159                 while(bail == 0)        {       bail = mothur->getInput();      }
160                 
161                 //closes logfile so we can rename
162                 m->closeLog();
163                 
164                 string outputDir = mothur->getOutputDir();
165                 string tempLog = mothur->getLogFileName();
166                 bool append = mothur->getAppend();
167                 
168                 string newlogFileName;
169                 if (tempLog != "") {
170                         newlogFileName = outputDir + tempLog;
171                         
172                         if (!append) {  
173                                 //need this because m->mothurOut makes the logfile, but doesn't know where to put it
174                                 rename(logFileName.c_str(), newlogFileName.c_str()); //logfile with timestamp
175
176                         }else {
177                                 ofstream outNewLog;
178                                 openOutputFileAppend(newlogFileName, outNewLog);
179                                 outNewLog << endl << endl << "*********************************************************************************" << endl << endl;
180                                 outNewLog.close();
181                                 
182                                 appendFiles(logFileName, newlogFileName);
183                                 remove(logFileName.c_str());
184                         }
185                 }else{  
186                         newlogFileName = outputDir + logFileName;
187                         //need this because m->mothurOut makes the logfile, but doesn't know where to put it
188                         rename(logFileName.c_str(), newlogFileName.c_str()); //logfile with timestamp
189                 }
190                 
191                                 
192                 delete mothur;
193                 
194                 #ifdef USE_MPI
195                         MPI_Finalize();
196                 #endif
197                 
198                 return 0;
199         }
200         catch(exception& e) {
201                 m->errorOut(e, "mothur", "main");
202                 exit(1);
203         }
204 }
205
206 /**************************************************************************************************/
207