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