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