-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
197 lines (166 loc) · 7.65 KB
/
gui.py
File metadata and controls
197 lines (166 loc) · 7.65 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# GUI logic for the Central Dogma Simulator
from PyQt5.QtWidgets import QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QMessageBox, QTabWidget, QTextEdit, QFileDialog
# Import processing functions and utilities
from dna_processing import transcribe_dna_to_mrna, translate_mrna_to_protein
from visualization import draw_dna
from mutations import mutate_dna
from utils import validate_dna_sequence, validate_mrna_sequence, generate_random_dna
# Tab 1: DNA Input Interface
class DNAInputTab(QWidget):
def __init__(self, transcription_tab):
super().__init__()
self.transcription_tab = transcription_tab # Link to transcription tab for data handoff
# Set up layout and widgets
layout = QVBoxLayout()
self.label = QLabel("Enter non-template DNA sequence:")
self.dna_input = QTextEdit()
self.validate_button = QPushButton("Validate DNA and Draw")
self.load_button = QPushButton("Load DNA from File")
self.generate_button = QPushButton("Generate Random DNA")
self.output_label = QLabel("")
# Connect buttons to their handlers
self.validate_button.clicked.connect(self.validate_and_draw)
self.load_button.clicked.connect(self.load_from_file)
self.generate_button.clicked.connect(self.handle_generate_random_dna)
# Add widgets to layout
layout.addWidget(self.label)
layout.addWidget(self.dna_input)
layout.addWidget(self.validate_button)
layout.addWidget(self.load_button)
layout.addWidget(self.generate_button)
layout.addWidget(self.output_label)
self.setLayout(layout)
# Pre-fill with random DNA sequence at startup
long_dna = generate_random_dna(100)
self.dna_input.setPlainText(long_dna)
def handle_generate_random_dna(self):
# Generates and displays a new random DNA sequence
random_sequence = generate_random_dna(100)
self.dna_input.setPlainText(random_sequence)
def load_from_file(self):
# Loads DNA sequence from a text file and validates it
file_path, _ = QFileDialog.getOpenFileName(self, "Open DNA Text File", "", "Text Files (*.txt);;All Files (*)")
if file_path:
try:
with open(file_path, 'r') as file:
dna = file.read().strip().upper()
if all(base in "ATCG" for base in dna) and dna:
self.dna_input.setText(dna)
self.output_label.setText("Valid DNA loaded. Drawing...")
draw_dna(dna)
self.transcription_tab.set_dna_input(dna)
else:
self.output_label.setText("Invalid DNA sequence in file! Use only A, T, C, G.")
except Exception as e:
QMessageBox.critical(self, "File Error", f"Could not read file: {e}")
def validate_and_draw(self):
# Validates the entered DNA and updates the transcription tab
dna = self.dna_input.toPlainText().strip().upper()
if validate_dna_sequence(dna):
self.output_label.setText("Valid DNA sequence. Drawing...")
draw_dna(dna)
self.transcription_tab.set_dna_input(dna)
else:
self.output_label.setText("Invalid DNA sequence! Use only A, T, C, G.")
# Tab 2: Transcription Interface
class TranscriptionTab(QWidget):
def __init__(self, translation_tab):
super().__init__()
self.translation_tab = translation_tab # Link to translation tab for data handoff
# Set up widgets and layout
layout = QVBoxLayout()
self.input_label = QLabel("DNA (non-template) → mRNA")
self.dna_input = QLineEdit()
self.transcribe_button = QPushButton("Transcribe")
self.result_label = QLabel("")
# Connect button to action
self.transcribe_button.clicked.connect(self.transcribe)
# Add widgets to layout
layout.addWidget(self.input_label)
layout.addWidget(self.dna_input)
layout.addWidget(self.transcribe_button)
layout.addWidget(self.result_label)
self.setLayout(layout)
def transcribe(self):
# Transcribes DNA to mRNA and sends it to the translation tab
dna = self.dna_input.text().upper()
if validate_dna_sequence(dna):
mrna = transcribe_dna_to_mrna(dna)
self.result_label.setText(f"mRNA: {mrna}")
self.translation_tab.set_mrna_input(mrna)
else:
self.result_label.setText("Invalid DNA sequence!")
def set_dna_input(self, dna):
# Allows the DNA input to be set programmatically (from DNAInputTab)
self.dna_input.setText(dna)
# Tab 3: Translation Interface
class TranslationTab(QWidget):
def __init__(self):
super().__init__()
# Set up widgets and layout
layout = QVBoxLayout()
self.input_label = QLabel("mRNA → Protein")
self.mrna_input = QLineEdit()
self.translate_button = QPushButton("Translate")
self.result_area = QTextEdit()
self.result_area.setReadOnly(True)
# Connect button to action
self.translate_button.clicked.connect(self.translate)
# Add widgets to layout
layout.addWidget(self.input_label)
layout.addWidget(self.mrna_input)
layout.addWidget(self.translate_button)
layout.addWidget(self.result_area)
self.setLayout(layout)
def translate(self):
# Translates mRNA into a protein sequence
mrna = self.mrna_input.text().upper()
if validate_mrna_sequence(mrna):
protein_sequence = translate_mrna_to_protein(mrna)
self.result_area.setText("\n".join(protein_sequence))
else:
self.result_area.setText("Invalid mRNA sequence!")
def set_mrna_input(self, mrna):
# Allows mRNA input to be set programmatically (from TranscriptionTab)
self.mrna_input.setText(mrna)
# Tab 4: Mutation Interface
class MutationTab(QWidget):
def __init__(self):
super().__init__()
# Set up widgets and layout
layout = QVBoxLayout()
self.label = QLabel("Enter DNA sequence:")
self.dna_input = QLineEdit()
self.mutation_label = QLabel("Mutation type:")
self.mutation_type = QLineEdit()
self.mutation_type.setPlaceholderText("e.g., substitution, deletion, insertion")
self.mutate_button = QPushButton("Mutate!")
self.result_area = QTextEdit()
self.result_area.setReadOnly(True)
# Connect button to mutation logic
self.mutate_button.clicked.connect(self.perform_mutation)
# Add widgets to layout
layout.addWidget(self.label)
layout.addWidget(self.dna_input)
layout.addWidget(self.mutation_label)
layout.addWidget(self.mutation_type)
layout.addWidget(self.mutate_button)
layout.addWidget(self.result_area)
self.setLayout(layout)
def perform_mutation(self):
# Applies the specified mutation to the DNA and displays results
dna = self.dna_input.text().upper()
mutation = self.mutation_type.text().lower()
if validate_dna_sequence(dna):
mutated_dna, consequence = mutate_dna(dna, mutation)
mrna = transcribe_dna_to_mrna(mutated_dna)
protein = translate_mrna_to_protein(mrna)
result = (
f"Mutated DNA: {mutated_dna}\n\n"
f"mRNA: {mrna}\n\n"
f"Protein Translation:\n" + "\n".join(protein) +
f"\n\nConsequence: {consequence}"
)
self.result_area.setText(result)
else:
self.result_area.setText("Invalid DNA sequence!")