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