File: mothur.cpp

package info (click to toggle)
mothur 1.48.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,692 kB
  • sloc: cpp: 161,866; makefile: 122; sh: 31
file content (141 lines) | stat: -rwxr-xr-x 5,137 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 *  interface.cpp
 *  
 *
 *  Created by Pat Schloss on 8/14/08.
 *  Copyright 2008 Patrick D. Schloss. All rights reserved.
 *
 */

#include "batchengine.hpp"
#include "scriptengine.hpp"
#include "interactengine.hpp"
#include "mothurout.h"

/**************************************************************************************************/

CommandFactory* CommandFactory::_uniqueInstance = 0;
MothurOut* MothurOut::_uniqueInstance = 0;
CurrentFile* CurrentFile::instance = 0;
/***********************************************************************/
volatile int ctrlc_pressed = 0;
void ctrlc_handler ( int sig ) {
	MothurOut* m = MothurOut::getInstance();
    ctrlc_pressed = 1;
	m->setControl_pressed(ctrlc_pressed);
	
	if (m->getExecuting()) { //if mid command quit execution, else quit mothur
        m->mothurOut("\nquitting command...\n");
	}else{
		m->mothurOut("quitting mothur\n");
		exit(1);
	}
}
/***********************************************************************/
int main(int argc, char *argv[], char *envp[]){
	MothurOut* m = MothurOut::getInstance();
	try {
        CurrentFile* current = CurrentFile::getInstance();
        Utils util;
        bool createLogFile = true;
        
		signal(SIGINT, ctrlc_handler );
        
        string mothurVersion, releaseDate, OS;
        vector<string> defaultPath, toolsPath;
        util.mothurInitialPrep(defaultPath, toolsPath, mothurVersion, releaseDate, OS);
        
        current->setReleaseDate(releaseDate);
        current->setVersion(mothurVersion);
		
		#ifdef MOTHUR_FILES
			current->setDefaultPath(defaultPath);
		#endif
        
        #ifdef MOTHUR_TOOLS
            current->setToolsPath(toolsPath);
        #endif
        
		if (argc>1) {
            if (argc > 2) { //is one of these -q for quiet mode?
                if (argc > 3) { m->mothurOut("[ERROR]: mothur only allows command inputs and the -q command line options.\n  i.e. ./mothur \"#summary.seqs(fasta=final.fasta);\" -q\n or ./mothur -q \"#summary.seqs(fasta=final.fasta);\"\n"); return 0; }
                else {
                    string argv1 = argv[1];
                    string argv2 = argv[2];
                    if ((argv1 == "--quiet") || (argv1 == "-q")) {
                        m->setQuietMode(true);
                        argv[1] = argv[2];
                    }else if ((argv2 == "--quiet") || (argv2 == "-q")) {
                         m->setQuietMode(true);
                    }else {
                        m->mothurOut("[ERROR]: mothur only allows command inputs and the -q command line options.\n");
                        m->mothurOut("[ERROR]: Unrecognized options: " + argv1 + " " + argv2 + "\n");
                        return 0;
                    }
                }
            }
		}
        
        map<string, string> environmentalVariables;
        for (char **env = envp; *env != 0; env++){
            string thisEvn = *env;
            string key, value; value = thisEvn;
            util.splitAtEquals(key, value);
            
            map<string, string>::iterator it = environmentalVariables.find(key);
            if (it == environmentalVariables.end())     { environmentalVariables[key] = value;  }
            else                                        { it->second = value;                   }
            //m->mothurOut("[DEBUG]: Setting environment variable " + key + " to " + value + "\n"); 
            if (m->getDebug()) { m->mothurOut("[DEBUG]: Setting environment variable " + key + " to " + value + "\n"); }
        }
        
		Engine* mothur = nullptr;
		bool bail = false;
		string input;
 
		if(argc>1){
			input = argv[1];
			if (input[0] == '#') {
				m->mothurOut("Script Mode\n\n");
				mothur = new ScriptEngine(argv[0], argv[1], environmentalVariables);
			}else if ((input == "--version") || (input == "-v")) {
				cout << (OS + "\nMothur version=" + mothurVersion + "\nRelease Date=" + releaseDate + "\n\n"); return 0;
            }else if ((input == "--help") || (input == "-h")) {
                createLogFile = false;
                m->mothurOut("Script Mode\n\n");

                string helpQuit = "#help();quit();";
               
                argv[1] = util.mothurConvert(helpQuit);
                mothur = new ScriptEngine(argv[0], argv[1], environmentalVariables);
			}else{
				m->mothurOut("Batch Mode\n\n");
                mothur = new BatchEngine(argv[0], argv[1], environmentalVariables);
			}
		}else{
			m->mothurOut("Interactive Mode\n\n");
            mothur = new InteractEngine(argv[0], environmentalVariables);
		}
		
		while(!bail)	{   bail = mothur->getInput();	}
		
		string newlogFileName = mothur->getLogFileName();
		        
        if (!createLogFile) { util.mothurRemove(newlogFileName); }
				
		if (mothur != nullptr) { delete mothur; }
        
        int returnCode = 0;
        if (m->getNumErrors() != 0) { returnCode = 1; }
        m->closeLog();
        
		return returnCode;
	}
	catch(exception& e) {
		m->errorOut(e, "mothur", "main");
		exit(1);
	}
}

/**************************************************************************************************/