__main__.py 16 KB
Newer Older
Yan's avatar
Yan committed
1
2
3
4
5
#!/usr/bin/env python3

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.widgets import SpanSelector
Yan's avatar
Yan committed
6
from matplotlib.gridspec import GridSpec
Yan's avatar
Yan committed
7
8
from PyQt5 import QtCore
from PyQt5 import QtWidgets
Yan's avatar
Yan committed
9
from PyQt5 import QtGui
10
from PyQt5 import QtPrintSupport
Yan's avatar
Yan committed
11
from rawprasslib import load_raw
Yan's avatar
Yan committed
12
from rawprasslib import rawprasslib
Yan's avatar
Yan committed
13
from prasopes.predictmz import predict as getmzpattern
Yan's avatar
Yan committed
14
15
16
17
18
try:
    from rawautoparams import load_params
    autoparams = True
except:
    autoparams = False
Yan's avatar
Yan committed
19
import numpy as np
Yan's avatar
Yan committed
20
import prasopes.config as cf
21
import prasopes.datatools as dt
Yan's avatar
Yan committed
22
import prasopes.drltools as drl
Yan's avatar
Yan committed
23
import prasopes.filetools as ft
Yan's avatar
Yan committed
24
25
import prasopes.graphtools as gt
import prasopes.imagetools as imgt
Yan's avatar
Yan committed
26
import prasopes.zcetools as zce
Yan's avatar
Yan committed
27
import prasopes.tangoicons
Yan's avatar
Yan committed
28
29
30
import sys
import matplotlib
import logging
3Yan's avatar
3Yan committed
31
import os.path
Yan's avatar
Yan committed
32
33
34
matplotlib.use("Qt5Agg")


35
36
37
38
class update_signal(QtCore.QObject):
    signal = QtCore.pyqtSignal()


39
40
41
42
43
44
45
46
47
48
class QStatusBarLogger(logging.Handler):
    def __init__(self, parent=None):
        super().__init__()
        self.statusBar = QtWidgets.QStatusBar(parent)

    def emit(self, record):
        msg = self.format(record)
        self.statusBar.showMessage(msg)


49
def load_file(parent, chrom_plot, spc, d_set, ms_s, fn, chrom_s,
50
              update, settings, filename=None):
Yan's avatar
Yan committed
51
    """populates dataset and plots it"""
3Yan's avatar
3Yan committed
52
53
54
    directory=settings.value("open_folder")
    if fn[0] is not None:
        directory=fn[0]
Yan's avatar
Yan committed
55
56
    if filename == None:
        filename = QtWidgets.QFileDialog.getOpenFileName(
3Yan's avatar
3Yan committed
57
58
59
            caption="Open spectrum",
            directory=directory,
            filter="Finnigan RAW files (*.raw, *.RAW)")[0]
3Yan's avatar
3Yan committed
60
    if filename is not '' and os.path.isfile(filename):
Yan's avatar
Yan committed
61
        try:
3Yan's avatar
3Yan committed
62
            d_set.clear()
Yan's avatar
Yan committed
63
            chrom_s['timesarg'].clear()
3Yan's avatar
3Yan committed
64
65
            [d_set.append(dict(chrom_dat=i[0], masses=i[1], matrix=i[2])) for i
                    in load_raw(filename, settings.value("tmp_location"))]
Yan's avatar
Yan committed
66
67
68
69
70
71
72
        except rawprasslib.ParsingException as pex:
            QtWidgets.QMessageBox.critical(
                parent, "Opening of the file has failed!",
                "File is incompatible with the rawprasslib, "
                "canceling request!\n\n"
                "Error message:\n{}".format(pex.args[0]))
            return
Yan's avatar
Yan committed
73
74
75
76
77
78
79
80
81
82
83
84
85
        [i.clear() for i in (ms_s['params'],ms_s['headers'])]
        if autoparams == True:
            try:
                ms_s['params'], rawheaders = load_params(filename)
                segments = [len(subset['chrom_dat'][0]) for subset in d_set]
                indicies = [sum(segments[:i+1]) for i in range(len(segments))]
                ms_s['headers'] = np.split(rawheaders, indicies)[:-1]
            except Exception as pex:
                QtWidgets.QMessageBox.critical(
                    parent, "Parameters readout has failed!",
                    "File is incompatible with the rawautoparams, "
                    "no parameters loaded!\n\n"
                    "Error message:\n{}".format(pex.args[0]))
Yan's avatar
Yan committed
86
        gt.populate(chrom_plot, spc, d_set, ms_s, chrom_s)
3Yan's avatar
3Yan committed
87
        fn[0] = filename
88
89
        parent.setWindowTitle("Prasopes - {}".format(
            os.path.basename(filename)))
90
        update.signal.emit()
3Yan's avatar
3Yan committed
91

Yan's avatar
Yan committed
92

Yan's avatar
Yan committed
93
94
def print_graph(data_set, mass_spec, chrom_spec, spect, fn):
    def printimage(printdevice, img):
Yan's avatar
Yan committed
95
        printer.setResolution(600)
Yan's avatar
Yan committed
96
        painter = QtGui.QPainter(printdevice)
Yan's avatar
Yan committed
97
        painter.drawImage(0,0,img)
Yan's avatar
Yan committed
98
99
        painter.end()
    #TODO: substitute the QPrintPreviewDialog with QPrintPreviewWidget
100
101
    printPreview = QtPrintSupport.QPrintPreviewDialog()
    printer = printPreview.printer()
Yan's avatar
Yan committed
102
103
    printer.setPageSize(printer.A5)
    printer.setDuplex(printer.DuplexNone)
Yan's avatar
Yan committed
104
    image = imgt.paint_image(mass_spec, spect, printer)
Yan's avatar
Yan committed
105
    printPreview.paintRequested.connect(lambda:
Yan's avatar
Yan committed
106
                                        printimage(printer, image))
107
108
109
    printPreview.exec()


Yan's avatar
Yan committed
110
111
def update_spectrum(chromatogram, spect, ds, ms, fn, chrom, config):
    if fn[0] is not None:
Yan's avatar
Yan committed
112
        slims = [spect.get_xlim(), spect.get_ylim()]
3Yan's avatar
3Yan committed
113
114
115
        ds.clear()
        [ds.append(dict(chrom_dat=i[0], masses=i[1], matrix=i[2])) for i
                in load_raw(fn[0], config.value("tmp_location"))]
Yan's avatar
Yan committed
116
117
118
        gt.populate(chromatogram, spect, ds, ms, chrom)
        spect.set_xlim(slims[0])
        spect.set_ylim(slims[1])
Yan's avatar
Yan committed
119
        gt.ann_spec(spect, ms)
Yan's avatar
Yan committed
120
121
122
        spect.get_figure().canvas.draw()


123
def dropped(event, parent, chromatogram, spectrum, ds, ms, filename,
3Yan's avatar
3Yan committed
124
            chrom, update, config):
125
    dropurl = event.mimeData().urls()[0].toLocalFile()
126
    load_file(parent, chromatogram, spectrum, ds, ms, filename,
3Yan's avatar
3Yan committed
127
              chrom, update, config, filename=dropurl)
Yan's avatar
Yan committed
128
129
130
131


def drag_entered(event):
    if event.mimeData().hasUrls() and event.mimeData().urls()[0]\
132
        .toLocalFile().lower().endswith('.raw'):
Yan's avatar
Yan committed
133
134
135
        event.accept()


Yan's avatar
Yan committed
136
137
138
139
140
141
142
143
144
145
146
147
148
def predictmz(form, chromatogram, spect, ds, ms, chrom):
    text = form.text()
    if text == "":
        ms["predict"] = None
        return
    slims = [spect.get_xlim(), spect.get_ylim()]
    ms["predict"] = getmzpattern(text)
    gt.populate(chromatogram, spect, ds, ms, chrom)
    spect.set_xlim(slims[0])
    spect.set_ylim(slims[1])
    spect.get_figure().canvas.draw()


3Yan's avatar
3Yan committed
149
150
151
152
153
154
def oddeven_changed(chromatogram, spectrum, ds, ms, filename, chrom, config,
        oddevenact):
    config.setValue("view/oddeven", oddevenact.isChecked())
    update_spectrum(chromatogram, spectrum, ds, ms, filename, chrom, config)


Yan's avatar
Yan committed
155
156
157
def key_pressed(event, chrom, spect, ds, ms_ds, fn, chrom_ds, config):
    if event.key() == QtCore.Qt.Key_F5:
        update_spectrum(chrom, spect, ds, ms_ds, fn, chrom_ds, config)
158
159
160
161
162
163
    if event.key() == QtCore.Qt.Key_C:
        if event.modifiers().__int__() == QtCore.Qt.ControlModifier:
            imgt.clip_spect_img(ms_ds, spect)
        if event.modifiers().__int__() == QtCore.Qt.ControlModifier+\
                                          QtCore.Qt.ShiftModifier:
            dt.clip_spectstr(spect, chrom_ds, fn)
Yan's avatar
Yan committed
164
165
    if event.key() in (QtCore.Qt.Key_Left, QtCore.Qt.Key_Right):
        gt.shift_times(event, spect, chrom, ds, ms_ds, chrom_ds)
Yan's avatar
Yan committed
166
167


Yan's avatar
Yan committed
168
169
170
171
172
def about(parent):
    """constructs window with "about" info"""
    QtWidgets.QMessageBox.information(
            parent, "About Prasopes",
            "Prasopes Finnigan raw file viewer\n\n"
Yan's avatar
Yan committed
173
            "Version: 0.0.11 (alpha)")
Yan's avatar
Yan committed
174
175


Yan's avatar
Yan committed
176
def main():
177
178
    app = QtWidgets.QApplication(sys.argv)

3Yan's avatar
3Yan committed
179
180
    ds = []
    ms = dict(annotation=[], name="Spectrum", xlabel="m/z",
Yan's avatar
Yan committed
181
182
              ylabel="ion count", xtics=20, predict=None,
              params=[], headers=[])
183
    chrom = dict(x=[0], y=[0], t_start=None, t_end=None,
Yan's avatar
Yan committed
184
                 name="Chromatogram", xlabel="time(min)",
3Yan's avatar
3Yan committed
185
                 ylabel="total ion count", timesarg=[])
Yan's avatar
Yan committed
186
    filename = [None]
3Yan's avatar
3Yan committed
187
    drlcache = [None, None]
188
    update = update_signal()
Yan's avatar
Yan committed
189

Yan's avatar
Yan committed
190
191
    config = cf.settings()

192
193
    barHandler = QStatusBarLogger()

Yan's avatar
Yan committed
194
    p_logger = logging.getLogger('parseLogger')
Yan's avatar
Yan committed
195
    drl_logger = logging.getLogger('drlLogger')
196
    zce_logger = logging.getLogger('zceLogger')
Yan's avatar
Yan committed
197
    logging.basicConfig()
198
199
    #p_logger.setLevel("WARN")
    p_logger.setLevel("DEBUG")
Yan's avatar
Yan committed
200
201
    #drl_logger.setLevel("INFO")
    drl_logger.setLevel("DEBUG")
202
    zce_logger.setLevel("DEBUG")
203
204
    p_logger.addHandler(barHandler)
    zce_logger.addHandler(barHandler)
Yan's avatar
Yan committed
205

Yan's avatar
Yan committed
206
    graph = Figure(figsize=(5, 4), dpi=100, facecolor="None")
Yan's avatar
Yan committed
207
208
209
    grid = graph.add_gridspec(2, 1)
    chromatogram = graph.add_subplot(grid[0,0], facecolor=(1, 1, 1, 0.8))
    spectrum = graph.add_subplot(grid[1,0], facecolor=(1, 1, 1, 0.8))
Yan's avatar
Yan committed
210
211
212
213
214
215
216
217
218
    graph.tight_layout()
    mpl_canvas = FigureCanvas(graph)
    mpl_canvas.setStyleSheet("background-color:transparent;")
    mpl_canvas.setAutoFillBackground(False)
    
    gt.pan_factory(chromatogram)
    gt.zoom_factory(chromatogram, 1.15)
    gt.pan_factory(spectrum, ms)
    gt.zoom_factory(spectrum, 1.15, ms)
219
    mass_selector = SpanSelector(
Yan's avatar
Yan committed
220
        spectrum, lambda x_min, x_max: gt.pick_masses(
221
222
223
            x_min, x_max, spectrum, ms), 'horizontal', minspan=0.01,
        useblit=True, rectprops=dict(alpha=0.15, facecolor='purple'),
        button=3)
224
    time_selector = SpanSelector(
Yan's avatar
Yan committed
225
226
227
228
229
         chromatogram, lambda x_min, x_max: gt.pick_times(
             x_min, x_max, spectrum, ds, chromatogram, ms, chrom), 
         'horizontal', useblit=True, rectprops=dict(
             alpha=0.15, facecolor='purple'), button=3)

Yan's avatar
Yan committed
230
    main_window = QtWidgets.QMainWindow(windowTitle="Prasopes")
Yan's avatar
Yan committed
231

Yan's avatar
Yan committed
232
233
234
    if QtGui.QIcon.themeName() is "":
        QtGui.QIcon.setThemeName("TangoMFK")

Yan's avatar
Yan committed
235
    openact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
236
        "document-open"), "&Open...", None)
Yan's avatar
Yan committed
237
238
    openact.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_O)
    openact.triggered.connect(lambda: load_file(
239
        main_window, chromatogram, spectrum, ds, ms, filename, chrom,
240
        update, config))
Yan's avatar
Yan committed
241
    exportact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
242
        "document-save-as"), "&Export...", None)
Yan's avatar
Yan committed
243
244
    exportact.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_E)
    exportact.triggered.connect(lambda: ft.export_dial(
245
        spectrum, chrom, filename, main_window))
246
247
248
    printact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
        "document-print"), "&Print", None)
    printact.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_P)
Yan's avatar
Yan committed
249
250
    printact.triggered.connect(lambda: print_graph(
        ds, ms, chrom, spectrum, filename))
Yan's avatar
Yan committed
251
    settingsact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
252
        "preferences-system"), "&Settings...", None)
Yan's avatar
Yan committed
253
254
    settingsact.triggered.connect(lambda: cf.dial(main_window))
    quitact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
255
        "application-exit"), "&Quit", None)
Yan's avatar
Yan committed
256
257
258
    quitact.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
    quitact.triggered.connect(main_window.close)
    zceact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
259
        "applications-utilities"), "&TSQ zce...", None)
Yan's avatar
Yan committed
260
261
    zceact.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_T)
    zceact.triggered.connect(lambda: zce.dialog(
262
        main_window, ds, filename, update))
Yan's avatar
Yan committed
263
    drlact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
264
        "applications-utilities"), "&DRL...", None)
Yan's avatar
Yan committed
265
266
    drlact.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_D)
    drlact.triggered.connect(lambda: drl.main_window(
267
        main_window, ds, filename, drlcache, update))
Yan's avatar
Yan committed
268
269
    aboutact = QtWidgets.QAction("&About Prasopes", None)
    aboutact.triggered.connect(lambda: about(main_window))
Yan's avatar
Yan committed
270
    autozoomy = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
271
272
273
274
        "zoom-original"), "Auto Zoom Y", None, checkable=True,
        checked=config.value("view/autozoomy", type=bool))
    autozoomy.triggered.connect(lambda: config.setValue(
                                "view/autozoomy", autozoomy.isChecked()))
275
276
277
278
279
280
281
282
283
284
285
    autozoomy.triggered.connect(lambda: gt.autozoomy(spectrum))
    intensitiesact = QtWidgets.QAction("&Show intensities", None,
            checkable=True, checked=config.value("view/intensities",
                type=bool))
    intensitiesact.triggered.connect(lambda: config.setValue(
        "view/intensities", intensitiesact.isChecked()))
    intensitiesact.triggered.connect(lambda: gt.ann_spec(spectrum, ms))
    intensitiesact.triggered.connect(lambda: spectrum.figure.canvas.draw())
    oddevenact = QtWidgets.QAction("&Odd / even", None, checkable=True,
            checked=config.value("view/oddeven", type=bool))
    oddevenact.triggered.connect(lambda:
3Yan's avatar
3Yan committed
286
287
        oddeven_changed(chromatogram, spectrum, ds, ms, filename, chrom,
            config, oddevenact))
Yan's avatar
Yan committed
288
289
290
291
292
    filebrowseract = QtWidgets.QAction(
            "&File browser", None, checkable=True,
            checked=config.value("view/filebrowservisible", type=bool))
    filebrowseract.triggered.connect(lambda: config.setValue(
        "view/filebrowservisible", filebrowseract.isChecked()))
293
294
295
    filebrowseract.triggered.connect(
        lambda: treedock.show() if filebrowseract.isChecked() == True
                else treedock.hide())
Yan's avatar
Yan committed
296

Yan's avatar
Yan committed
297
298
299
300
    predictform = QtWidgets.QLineEdit(maximumWidth=150)
    predictform.editingFinished.connect(lambda: predictmz(
        predictform, chromatogram, spectrum, ds, ms, chrom))

Yan's avatar
Yan committed
301
302
    file_menu = QtWidgets.QMenu('&File', main_window)
    main_window.menuBar().addMenu(file_menu)
Yan's avatar
Yan committed
303
304
305
    file_menu.addAction(openact)
    file_menu.addAction(exportact)
    file_menu.addSeparator()
306
307
    file_menu.addAction(printact)
    file_menu.addSeparator()
Yan's avatar
Yan committed
308
309
310
    file_menu.addAction(settingsact)
    file_menu.addSeparator()
    file_menu.addAction(quitact)
Yan's avatar
Yan committed
311
312
    tools_menu = QtWidgets.QMenu('&Tools', main_window)
    main_window.menuBar().addMenu(tools_menu)
Yan's avatar
Yan committed
313
314
    tools_menu.addAction(zceact)
    tools_menu.addAction(drlact)
315
    tools_menu.addSeparator()
Yan's avatar
Yan committed
316
317
318
    view_menu = QtWidgets.QMenu('&View', main_window)
    view_menu.addAction(filebrowseract)
    view_menu.addAction(autozoomy)
319
320
321
    view_menu.addAction(intensitiesact)
    view_menu.addSeparator()
    view_menu.addAction(oddevenact)
Yan's avatar
Yan committed
322
    main_window.menuBar().addMenu(view_menu)
Yan's avatar
Yan committed
323
324
325
    help_menu = QtWidgets.QMenu('&Help', main_window)
    main_window.menuBar().addMenu(help_menu)
    help_menu.addAction(aboutact)
Yan's avatar
Yan committed
326

327
    main_window.setCentralWidget(mpl_canvas)
Yan's avatar
Yan committed
328

Yan's avatar
Yan committed
329
330
331
332
333
    toolBar = QtWidgets.QToolBar(main_window)
    toolBar.setAllowedAreas(QtCore.Qt.TopToolBarArea)
    toolBar.setFloatable(False)
    toolBar.setMovable(False)
    toolBar.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)
Yan's avatar
Yan committed
334

Yan's avatar
Yan committed
335
336
337
    toolBar.addAction(openact)
    toolBar.addAction(exportact)
    toolBar.addSeparator()
Yan's avatar
Yan committed
338
339
340
    toolBar.addWidget(QtWidgets.QLabel("Predict Formula:"))
    toolBar.addWidget(predictform)
    toolBar.addSeparator()
Yan's avatar
Yan committed
341
342
    toolBar.addAction(zceact)
    toolBar.addAction(drlact)
Yan's avatar
Yan committed
343
344
    toolBar.addSeparator()
    toolBar.addAction(autozoomy)
Yan's avatar
Yan committed
345

346
    #fileModel gots stuck sometimes, better give it a separate thread
347
    #it complains at the start, but works and I did not found better solution
3Yan's avatar
3Yan committed
348
    fileModel = QtWidgets.QFileSystemModel()
349
350
    treedockThread = QtCore.QThread()
    treedockThread.start()
351
    fileModel.setRootPath('')
352
    fileModel.moveToThread(treedockThread)
353
    activeDir = fileModel.index(config.value("open_folder"))
3Yan's avatar
3Yan committed
354
355
356
357
    treeview = QtWidgets.QTreeView()
    treeview.setModel(fileModel)
    treeview.setCurrentIndex(activeDir)
    treeview.expand(activeDir)
358
359
    dirview = QtWidgets.QListView()
    dirview.setModel(fileModel)
3Yan's avatar
3Yan committed
360
    treedock = QtWidgets.QDockWidget()
Yan's avatar
Yan committed
361
362
    if not config.value("view/filebrowservisible",type=bool):
        treedock.hide()
363
364
365
366
    treedock.setWidget(QtWidgets.QWidget())
    treedock_layout = QtWidgets.QVBoxLayout(treedock.widget())
    treedock_layout.addWidget(treeview)
    treedock_layout.addWidget(dirview)
367
368
369
    update.signal.connect(lambda: treeview.setCurrentIndex(fileModel.index(
                                  filename[0])))
    update.signal.connect(lambda: dirview.setRootIndex(fileModel.index(
3Yan's avatar
3Yan committed
370
            os.path.dirname(os.path.realpath(filename[0])))))
371
    clickload = lambda index: load_file(
3Yan's avatar
3Yan committed
372
        main_window, chromatogram, spectrum, ds, ms, filename, chrom,
373
374
375
376
        update, config, filename=fileModel.filePath(index))
    for i in [treeview.doubleClicked, dirview.doubleClicked, dirview.clicked]:
        i.connect(clickload)

377
378
379
380
381
382
    def closeOverride(event, dock, action):
        action.setChecked(False)
        dock.hide()
        event.ignore()
    treedock.closeEvent = lambda event: closeOverride(
            event, treedock, filebrowseract)
Yan's avatar
Yan committed
383
    main_window.closeEvent = lambda event: treedockThread.quit()
Yan's avatar
Yan committed
384
385
    main_window.dragEnterEvent = lambda event: drag_entered(event)
    main_window.dropEvent = lambda event: dropped(
386
        event, main_window, chromatogram, spectrum, ds, ms, filename,
3Yan's avatar
3Yan committed
387
        chrom, update, config)
Yan's avatar
Yan committed
388
    main_window.setAcceptDrops(True)
Yan's avatar
Yan committed
389
390
    main_window.keyPressEvent = lambda event: key_pressed(event, chromatogram,
            spectrum, ds, ms, filename, chrom, config)
Yan's avatar
Yan committed
391

392
393
    main_window.addToolBar(QtCore.Qt.TopToolBarArea, toolBar)
    main_window.addDockWidget(QtCore.Qt.LeftDockWidgetArea, treedock)
394
    main_window.setStatusBar(barHandler.statusBar)
395
396
397

    main_window.setFocus()

Yan's avatar
Yan committed
398
    if len(sys.argv) == 2:
399
        load_file(main_window, chromatogram, spectrum, ds, ms,
3Yan's avatar
3Yan committed
400
                  filename, chrom, update, config, filename=sys.argv[1])
Yan's avatar
Yan committed
401
    else:
3Yan's avatar
3Yan committed
402
403
        gt.pop_plot(0, 0, spectrum, ms)
        gt.pop_plot(0, 0, chromatogram, chrom)
Yan's avatar
Yan committed
404
405
406

    main_window.show()
    sys.exit(app.exec_())
Yan's avatar
Yan committed
407

408

Yan's avatar
Yan committed
409
410
if __name__ == "__main__":
    main()