Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
144 lines (111 loc) · 3.81 KB
/
Copy pathmain.py
File metadata and controls
144 lines (111 loc) · 3.81 KB
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
142
143
144
#!/usr/bin/env python3
"""
Genetic Analysis Desktop Application
Main entry point for the application. Provides upload of 23andMe data,
matching against GWAS Catalog, and visualization of genetic impact scores.
Usage:
python main.py
Requirements:
- Python 3.9+
- PyQt6
- pandas
- numpy
Setup:
1. pip install -r requirements.txt
2. python database/setup_database.py
3. python main.py
"""
import sys
import os
import traceback
from datetime import datetime
# Ensure the application directory is in the path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from PyQt6.QtWidgets import QApplication, QMessageBox
from PyQt6.QtCore import Qt, qInstallMessageHandler, QtMsgType
from frontend.main_window import MainWindow
from utils.logging_config import setup_logging, get_logger, log_error, ERROR_LOG_FILE
from database.setup_database import verify_database, create_database
from config import DATABASE_PATH
def qt_message_handler(mode, context, message):
"""
Custom Qt message handler to log Qt warnings and errors.
"""
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S,%f')[:-3]
if mode == QtMsgType.QtWarningMsg:
level = "QT_WARNING"
elif mode == QtMsgType.QtCriticalMsg:
level = "QT_CRITICAL"
elif mode == QtMsgType.QtFatalMsg:
level = "QT_FATAL"
else:
return # Don't log debug/info messages
log_msg = f"{timestamp} - {level} - {message}"
if context.file:
log_msg += f" ({context.file}:{context.line})"
log_msg += "\n"
try:
with open(ERROR_LOG_FILE, 'a') as f:
f.write(log_msg)
except Exception:
pass
def ensure_database() -> bool:
"""
Ensure the database exists, create if necessary.
Returns:
bool: True if database is available.
"""
if verify_database(DATABASE_PATH):
return True
print("Database not found. Creating...")
return create_database(DATABASE_PATH)
def main() -> int:
"""
Main entry point for the application.
Returns:
int: Exit code (0 for success).
"""
# Setup logging (this also sets up global exception handler)
setup_logging()
logger = get_logger(__name__)
# Install Qt message handler
qInstallMessageHandler(qt_message_handler)
logger.info("Starting Genetic Analysis Application")
try:
# Ensure database exists
if not ensure_database():
logger.error("Failed to initialize database")
log_error("Failed to initialize database")
print("Error: Could not initialize database. Exiting.")
return 1
# Create Qt application
app = QApplication(sys.argv)
app.setApplicationName("Genetic Analysis")
app.setApplicationVersion("1.0.0")
# Enable high DPI support
app.setHighDpiScaleFactorRoundingPolicy(
Qt.HighDpiScaleFactorRoundingPolicy.PassThrough
)
# Create and show main window
window = MainWindow()
window.show()
logger.info("Application window displayed")
# Run event loop
return app.exec()
except Exception as e:
# Log any unhandled exception during startup
log_error(f"Fatal error during application startup: {e}", e)
logger.critical(f"Fatal error: {e}", exc_info=True)
# Try to show error dialog
try:
app = QApplication.instance() or QApplication(sys.argv)
QMessageBox.critical(
None,
"Fatal Error",
f"A fatal error occurred:\n\n{str(e)}\n\nCheck error.log for details."
)
except Exception:
pass
return 1
if __name__ == '__main__':
sys.exit(main())