]> git.donarmstrong.com Git - mothur.git/blob - mothur.cpp
fixes while testing 1.12.0
[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 = 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                 //header
101                 m->mothurOut("mothur v.1.12.0");
102                 m->mothurOutEndLine();          
103                 m->mothurOut("Last updated: 7/23/2010");
104                 m->mothurOutEndLine();  
105                 m->mothurOutEndLine();          
106                 m->mothurOut("by");
107                 m->mothurOutEndLine();          
108                 m->mothurOut("Patrick D. Schloss");
109                 m->mothurOutEndLine();
110                 m->mothurOutEndLine();                  
111                 m->mothurOut("Department of Microbiology & Immunology");
112                 m->mothurOutEndLine();  
113                 m->mothurOut("University of Michigan");
114                 m->mothurOutEndLine();                  
115                 m->mothurOut("pschloss@umich.edu");
116                 m->mothurOutEndLine();          
117                 m->mothurOut("http://www.mothur.org");
118                 m->mothurOutEndLine();
119                 m->mothurOutEndLine();
120                 m->mothurOut("When using, please cite:");
121                 m->mothurOutEndLine();
122                 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.");
123                 m->mothurOutEndLine();  
124                 m->mothurOutEndLine();          
125                 m->mothurOut("Distributed under the GNU General Public License");
126                 m->mothurOutEndLine();
127                 m->mothurOutEndLine();                  
128                 m->mothurOut("Type 'help()' for information on the commands that are available");
129                 m->mothurOutEndLine();
130                 m->mothurOutEndLine();                  
131                 m->mothurOut("Type 'quit()' to exit program");
132                 m->mothurOutEndLine();  
133                 
134                 #ifdef USE_MPI
135                         m->mothurOutJustToLog("Using MPI\tversion ");
136                         int version, subversion;
137                         MPI_Get_version(&version, &subversion);
138                         m->mothurOutJustToLog(toString(version) + "." + toString(subversion) + "\n");
139                 #endif
140                 
141                 //srand(54321);
142                 srand( (unsigned)time( NULL ) );
143                 
144                 Engine* mothur;
145                 bool bail = 0;
146                 string input;
147
148                 if(argc>1){
149                         input = argv[1];
150
151                         if (input[0] == '#') {
152                                 m->mothurOutJustToLog("Script Mode");
153                                 m->mothurOutEndLine(); m->mothurOutEndLine();
154
155                                 mothur = new ScriptEngine(argv[0], argv[1]);
156                         }else{
157                                 m->mothurOutJustToLog("Batch Mode");
158                                 m->mothurOutEndLine(); m->mothurOutEndLine();
159                                 
160                                 mothur = new BatchEngine(argv[0], argv[1]);
161                         }
162                 }
163                 else{
164                         m->mothurOutJustToLog("Interactive Mode");
165                         m->mothurOutEndLine(); m->mothurOutEndLine();
166                         
167                         mothur = new InteractEngine(argv[0]);   
168                 }
169                 
170                 while(bail == 0)        {       bail = mothur->getInput();      }
171                 
172                 //closes logfile so we can rename
173                 m->closeLog();
174                 
175                 string outputDir = mothur->getOutputDir();
176                 string tempLog = mothur->getLogFileName();
177                 bool append = mothur->getAppend();
178                 
179                 string newlogFileName;
180                 if (tempLog != "") {
181                         newlogFileName = outputDir + tempLog;
182                         
183                         if (!append) {  
184                                 //need this because m->mothurOut makes the logfile, but doesn't know where to put it
185                                 rename(logFileName.c_str(), newlogFileName.c_str()); //logfile with timestamp
186
187                         }else {
188                                 ofstream outNewLog;
189                                 openOutputFileAppend(newlogFileName, outNewLog);
190                                 outNewLog << endl << endl << "*********************************************************************************" << endl << endl;
191                                 outNewLog.close();
192                                 
193                                 appendFiles(logFileName, newlogFileName);
194                                 remove(logFileName.c_str());
195                         }
196                 }else{  
197                         newlogFileName = outputDir + logFileName;
198                         //need this because m->mothurOut makes the logfile, but doesn't know where to put it
199                         rename(logFileName.c_str(), newlogFileName.c_str()); //logfile with timestamp
200                 }
201                 
202                                 
203                 delete mothur;
204                 
205                 #ifdef USE_MPI
206                         MPI_Finalize();
207                 #endif
208                 
209                 return 0;
210         }
211         catch(exception& e) {
212                 m->errorOut(e, "mothur", "main");
213                 exit(1);
214         }
215 }
216
217 /**************************************************************************************************/
218