-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgui.py
451 lines (338 loc) · 17.9 KB
/
gui.py
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
from PyQt5 import QtWidgets
from PyQt5.QtCore import QObject, QThread, pyqtSignal, Qt # https://realpython.com/python-pyqt-qthread/
import sys
import logging
import time
import multiprocessing
from logging.handlers import QueueListener
import job_pool.job_pool as pool
import simsi_transfer.main as simsi_transfer
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def run_simsi_transfer(mq_txt_dir, raw_dir, output_dir, metafile_path, tmt_params, other_params, extra_params):
parameters = []
if mq_txt_dir:
parameters.extend(['--mq_txt_folder', mq_txt_dir])
if raw_dir:
parameters.extend(['--raw_folder', raw_dir])
if output_dir:
parameters.extend(['--output_folder', output_dir])
if metafile_path:
parameters.extend(['--meta_input_file', metafile_path])
if tmt_params:
parameters.extend(tmt_params)
if other_params:
parameters.extend(other_params)
if extra_params:
parameters.extend(extra_params.split())
try:
simsi_transfer.main(parameters)
# logger.info(parameters)
except SystemExit as e:
logger.info(f"Error while running SIMSI-Transfer, exited with error code {e}.")
except Exception as e:
logger.info(f"Error while running SIMSI-Transfer: {e}")
# https://stackoverflow.com/questions/28655198/best-way-to-display-logs-in-pyqt#60528393
class QTextEditLogger(logging.Handler, QObject):
appendPlainText = pyqtSignal(str)
def __init__(self, parent):
# initialize logging.Handler
super().__init__()
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
formatter.converter = time.gmtime
self.setFormatter(formatter)
# initialize QObject
QObject.__init__(self)
self.widget = QtWidgets.QPlainTextEdit(parent)
self.widget.setReadOnly(True)
self.appendPlainText.connect(self.widget.appendPlainText)
def emit(self, record):
self.appendPlainText.emit(self.format(record))
# https://stackoverflow.com/questions/53288877/python-multiprocessing-sending-child-process-logging-to-gui-running-in-parent
class LogEmitter(QObject):
sigLog = pyqtSignal(str)
class LogHandler(logging.Handler):
def __init__(self):
super().__init__()
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
formatter.converter = time.gmtime
self.setFormatter(formatter)
self.emitter = LogEmitter()
def emit(self, record):
msg = self.format(record)
self.emitter.sigLog.emit(msg)
class FileSelect(QtWidgets.QWidget):
def __init__(self, file_type, file_extensions, file_hint='', folder_select=False, *args, **kwargs):
super().__init__(*args, **kwargs)
self.file_type = file_type
self.file_hint = file_hint
self.file_extensions = file_extensions
self.label_text = f"Select {self.file_type} file"
if folder_select:
self.label_text = f"Select {self.file_type} folder"
self.file_hint_text = ""
if len(file_hint) > 0:
self.file_hint_text = f'<br><font color="grey">{self.file_hint}</font>'
self.label = QtWidgets.QLabel(self.label_text + self.file_hint_text)
self.hbox_layout = QtWidgets.QHBoxLayout()
self.hbox_layout.setContentsMargins(0, 0, 0, 0)
self.line_edit = QtWidgets.QLineEdit()
self.browse_button = QtWidgets.QPushButton("Browse")
if folder_select:
self.browse_button.clicked.connect(self.select_dir)
else:
self.browse_button.clicked.connect(self.select_file)
self.hbox_layout.addWidget(self.line_edit, stretch=1)
self.hbox_layout.addWidget(self.browse_button)
self.setLayout(self.hbox_layout)
def select_file(self):
filename, _ = QtWidgets.QFileDialog.getOpenFileName(self, self.label_text, '', self.file_extensions)
self.line_edit.setText(filename)
def select_dir(self):
output_dir = QtWidgets.QFileDialog.getExistingDirectory(self, self.label_text, '',
QtWidgets.QFileDialog.ShowDirsOnly)
self.line_edit.setText(output_dir)
def get_file(self):
return self.line_edit.text()
def setButtonsEnabled(self, enable):
self.browse_button.setEnabled(enable)
class TMTGroup(QtWidgets.QGroupBox):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.tmt_group_layout = QtWidgets.QGridLayout()
self.setLayout(self.tmt_group_layout)
self.ms_level_label = QtWidgets.QLabel("TMT MS level")
# self.ms_level_label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
self.ms_level_select = QtWidgets.QComboBox()
self.ms_level_select.addItems(['MS2', 'MS3'])
self.ms_level_select.setCurrentText('MS3')
self.requantify_label = QtWidgets.QLabel("Requantify TMT?")
self.requantify_checkbox = QtWidgets.QCheckBox()
self.requantify_checkbox.setChecked(True)
self.tmt_group_layout.addWidget(self.ms_level_label, 0, 0)
self.tmt_group_layout.addWidget(self.ms_level_select, 0, 1)
self.tmt_group_layout.addWidget(self.requantify_label, 0, 3)
self.tmt_group_layout.addWidget(self.requantify_checkbox, 0, 4)
for col in range(5):
self.tmt_group_layout.setColumnStretch(col, 1)
def get_params(self):
returnval = ["--tmt_ms_level", str(self.ms_level_select.currentText()).lower()]
if self.requantify_checkbox.isChecked():
returnval.append("--tmt_requantify")
return returnval
class ParameterGroup(QtWidgets.QGroupBox):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.parameter_group_layout = QtWidgets.QGridLayout()
self.setLayout(self.parameter_group_layout)
self.filter_decoy_label = QtWidgets.QLabel("Remove decoys")
self.filter_decoy_checkbox = QtWidgets.QCheckBox()
# self.plotting_columns_label = QtWidgets.QLabel("Keep plotting columns")
# self.plotting_columns_checkbox = QtWidgets.QCheckBox()
self.ambiguity_label = QtWidgets.QLabel("PSM ambiguity")
self.ambiguity_select = QtWidgets.QComboBox()
self.ambiguity_select.addItems(['majority', 'all', 'none'])
self.ambiguity_select.setCurrentText('majority')
self.threads_label = QtWidgets.QLabel("CPU threads")
self.threads_spinbox = QtWidgets.QSpinBox()
self.threads_spinbox.setValue(1)
self.threads_spinbox.setRange(1, 100)
self.stringency_label = QtWidgets.QLabel("MaRaCluster stringencies")
self.stringency_line = QtWidgets.QLineEdit('10, 15, 20')
self.maxPEP_label = QtWidgets.QLabel("Max. PEP [%]")
self.maxPEP_spinbox = QtWidgets.QSpinBox()
self.maxPEP_spinbox.setValue(5)
self.maxPEP_spinbox.setRange(1, 10)
self.parameter_group_layout.addWidget(self.filter_decoy_label, 0, 0)
self.parameter_group_layout.addWidget(self.filter_decoy_checkbox, 0, 1)
# self.parameter_group_layout.addWidget(self.plotting_columns_label, 0, 3)
# self.parameter_group_layout.addWidget(self.plotting_columns_checkbox, 0, 4)
self.parameter_group_layout.addWidget(self.stringency_label, 0, 3)
self.parameter_group_layout.addWidget(self.stringency_line, 0, 4)
self.parameter_group_layout.addWidget(self.ambiguity_label, 1, 0)
self.parameter_group_layout.addWidget(self.ambiguity_select, 1, 1)
self.parameter_group_layout.addWidget(self.maxPEP_label, 1, 3)
self.parameter_group_layout.addWidget(self.maxPEP_spinbox, 1, 4)
self.parameter_group_layout.addWidget(self.threads_label, 2, 0)
self.parameter_group_layout.addWidget(self.threads_spinbox, 2, 1)
for col in range(5):
self.parameter_group_layout.setColumnStretch(col, 1)
def get_params(self):
returnval = [
'--stringencies', str(self.stringency_line.text()),
'--num_threads', str(self.threads_spinbox.value()),
'--ambiguity_decision', str(self.ambiguity_select.currentText()),
'--maximum_pep', str(self.maxPEP_spinbox.value())
]
if self.filter_decoy_checkbox.isChecked():
returnval.append('--filter_decoys')
# if self.plotting_columns_checkbox.isChecked():
# returnval.append('--add_plotting_columns')
return returnval
class MainWindow(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
layout = QtWidgets.QFormLayout()
self.setWindowTitle("SIMSI-Transfer")
self.tabs = QtWidgets.QTabWidget()
self._add_single_search_input_tab()
self._add_metafile_input_tab()
layout.addRow(self.tabs)
self._add_output_dir_field(layout)
self.tmt_group = TMTGroup('TMT parameters')
self.parameter_group = ParameterGroup('SIMSI parameters')
layout.addRow(self.tmt_group)
layout.addRow(self.parameter_group)
self._add_extra_params_field(layout)
self._add_buttons(layout)
self._add_log_textarea(layout)
self.setLayout(layout)
# sets up handler that will be used by QueueListener
# which will update the LogDialoag
handler = LogHandler()
handler.emitter.sigLog.connect(self.log_text_area.widget.appendPlainText)
self.q = multiprocessing.Queue()
self.ql = QueueListener(self.q, handler)
self.ql.start()
self.pool = pool.JobPool(processes=1, warningFilter="default", queue=self.q)
self.resize(700, self.height())
def _add_buttons(self, layout):
self.help_button = QtWidgets.QPushButton("Help")
self.help_button.clicked.connect(self.run_simsi_help)
self.run_button = QtWidgets.QPushButton("Run")
self.run_button.clicked.connect(self.run_simsi_transfer)
layout.addRow(self.help_button, self.run_button)
def _add_single_search_input_tab(self):
self.singlesearch_tab = QtWidgets.QWidget()
self.singlesearch_layout = QtWidgets.QFormLayout()
self._add_mq_txt_dir_field(self.singlesearch_layout)
self._add_raw_dir_field(self.singlesearch_layout)
self.singlesearch_tab.setLayout(self.singlesearch_layout)
self.tabs.addTab(self.singlesearch_tab, "Single search input")
def _add_metafile_input_tab(self):
self.metafile_tab = QtWidgets.QWidget()
self.metafile_widget = FileSelect('metafile', 'Comma-separated file (*.txt *.csv *.tsv)')
self.metafile_layout = QtWidgets.QFormLayout()
self.metafile_layout.addRow(self.metafile_widget.label, self.metafile_widget)
self.metafile_tab.setLayout(self.metafile_layout)
self.tabs.addTab(self.metafile_tab, "Metafile input")
def _add_mq_txt_dir_field(self, layout):
# evidence.txt input
self.mq_txt_dir_label = QtWidgets.QLabel("Select MaxQuant combined/txt folder")
# self.mq_txt_dir_label.setMargin(10)
self.mq_txt_dir_widget = QtWidgets.QWidget()
self.mq_txt_dir_hbox_layout = QtWidgets.QHBoxLayout()
self.mq_txt_dir_hbox_layout.setContentsMargins(0, 0, 0, 0)
self.mq_txt_dir_line_edit = QtWidgets.QLineEdit()
self.mq_txt_dir_browse_button = QtWidgets.QPushButton("Browse")
self.mq_txt_dir_browse_button.clicked.connect(self.get_mq_txt_dir)
self.mq_txt_dir_hbox_layout.addWidget(self.mq_txt_dir_line_edit, stretch=1)
self.mq_txt_dir_hbox_layout.addWidget(self.mq_txt_dir_browse_button)
self.mq_txt_dir_widget.setLayout(self.mq_txt_dir_hbox_layout)
layout.addRow(self.mq_txt_dir_label, self.mq_txt_dir_widget)
def _add_raw_dir_field(self, layout):
# fasta file input
self.raw_dir_label = QtWidgets.QLabel("Select folder with RAW files")
# self.raw_dir_label.setMargin(10)
self.raw_dir_widget = QtWidgets.QWidget()
self.raw_dir_hbox_layout = QtWidgets.QHBoxLayout()
self.raw_dir_hbox_layout.setContentsMargins(0, 0, 0, 0)
self.raw_dir_line_edit = QtWidgets.QLineEdit()
self.raw_dir_browse_button = QtWidgets.QPushButton("Browse")
self.raw_dir_browse_button.clicked.connect(self.get_raw_dir)
self.raw_dir_hbox_layout.addWidget(self.raw_dir_line_edit, stretch=1)
self.raw_dir_hbox_layout.addWidget(self.raw_dir_browse_button)
self.raw_dir_widget.setLayout(self.raw_dir_hbox_layout)
layout.addRow(self.raw_dir_label, self.raw_dir_widget)
def _add_output_dir_field(self, layout):
# fasta file input
self.output_dir_label = QtWidgets.QLabel("Select output folder")
# self.output_dir_label.setMargin(10)
self.output_dir_widget = QtWidgets.QWidget()
self.output_dir_hbox_layout = QtWidgets.QHBoxLayout()
self.output_dir_hbox_layout.setContentsMargins(0, 0, 0, 0)
self.output_dir_line_edit = QtWidgets.QLineEdit()
self.output_dir_browse_button = QtWidgets.QPushButton("Browse")
self.output_dir_browse_button.clicked.connect(self.get_output_dir)
self.output_dir_hbox_layout.addWidget(self.output_dir_line_edit, stretch=1)
self.output_dir_hbox_layout.addWidget(self.output_dir_browse_button)
self.output_dir_widget.setLayout(self.output_dir_hbox_layout)
layout.addRow(self.output_dir_label, self.output_dir_widget)
def _add_extra_params_field(self, layout):
self.args_label = QtWidgets.QLabel("Additional parameters")
self.args_line_edit = QtWidgets.QLineEdit()
layout.addRow(self.args_label, self.args_line_edit)
def _add_log_textarea(self, layout):
self.log_text_area = QTextEditLogger(self)
self.log_text_area.setLevel(logging.INFO)
logger.addHandler(self.log_text_area)
layout.addRow(self.log_text_area.widget)
def get_mq_txt_dir(self):
mq_txt_dir = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select MaxQuant combined/txt folder', '',
QtWidgets.QFileDialog.ShowDirsOnly)
self.mq_txt_dir_line_edit.setText(mq_txt_dir)
def get_metafile_path(self):
metafile_path = QtWidgets.QFileDialog.getOpenFileName(self, 'Select path to metafile', '')
def get_raw_dir(self):
raw_dir = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select folder with RAW files', '',
QtWidgets.QFileDialog.ShowDirsOnly)
self.raw_dir_line_edit.setText(raw_dir)
def get_output_dir(self):
output_dir = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select output folder', '',
QtWidgets.QFileDialog.ShowDirsOnly)
self.output_dir_line_edit.setText(output_dir)
def set_buttons_enabled_state(self, enable):
self.mq_txt_dir_browse_button.setEnabled(enable)
self.raw_dir_browse_button.setEnabled(enable)
self.output_dir_browse_button.setEnabled(enable)
# self.run_button.setEnabled(enable)
# Cannot stop a QThread if it doesn't have an own event loop
self.run_button.clicked.disconnect()
if enable:
self.run_button.setText("Run")
self.run_button.clicked.connect(self.run_simsi_transfer)
else:
self.run_button.setText("Stop")
self.run_button.clicked.connect(self.stop_simsi_transfer)
def run_simsi_help(self):
import webbrowser
webbrowser.open('https://github.com/kusterlab/SIMSI-Transfer')
# self.pool.applyAsync(run_simsi_transfer, ('', '', '', '', [], [], '--help'), callback=self.on_simsi_finished)
def run_simsi_transfer(self):
# initialize all parameters as empty values and then override?
mq_txt_dir = ''
raw_dir = ''
metafile_path = False
if self.tabs.currentIndex() == 0:
mq_txt_dir = self.mq_txt_dir_line_edit.text()
raw_dir = self.raw_dir_line_edit.text()
else:
metafile_path = self.metafile_widget.get_file()
output_dir = self.output_dir_line_edit.text()
tmt_params = self.tmt_group.get_params()
other_params = self.parameter_group.get_params()
extra_params = self.args_line_edit.text()
self.set_buttons_enabled_state(False)
self.pool.applyAsync(run_simsi_transfer,
(mq_txt_dir, raw_dir, output_dir, metafile_path, tmt_params, other_params, extra_params),
callback=self.on_simsi_finished)
def on_simsi_finished(self, return_code):
self.set_buttons_enabled_state(True)
def stop_simsi_transfer(self):
self.pool.stopPool()
self.on_simsi_finished(-2)
logger.info("SIMSI-Transfer stopped by user")
self.pool = pool.JobPool(processes=1, warningFilter="default", queue=self.q)
def closeEvent(self, _):
self.stop_simsi_transfer()
if __name__ == '__main__':
if sys.platform.startswith('win'):
# On Windows calling this function is necessary when combined with pyinstaller: https://stackoverflow.com/questions/24944558/pyinstaller-built-windows-exe-fails-with-multiprocessing
multiprocessing.freeze_support()
else:
# On Linux calling this function is necessary when combined with pyqt: https://stackoverflow.com/questions/29556291/multiprocessing-with-qt-works-in-windows-but-not-linux
multiprocessing.set_start_method('spawn')
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec()