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

from PyQt5 import QtCore
from PyQt5 import QtWidgets
Yan's avatar
Yan committed
5
from PyQt5 import QtGui
6
from PyQt5 import QtPrintSupport
Yan's avatar
Yan committed
7
from rawprasslib import load_raw
Yan's avatar
Yan committed
8
from rawprasslib import rawprasslib
Yan's avatar
Yan committed
9
from prasopes.predictmz import predict as getmzpattern
Yan's avatar
Yan committed
10
11
12
try:
    from rawautoparams import load_params
    autoparams = True
13
except ImportError:
Yan's avatar
Yan committed
14
    autoparams = False
Yan's avatar
Yan committed
15
import numpy as np
Yan's avatar
Yan committed
16
import prasopes.config as cf
17
import prasopes.datatools as dt
Yan's avatar
Yan committed
18
import prasopes.drltools as drl
Yan's avatar
Yan committed
19
import prasopes.filetools as ft
Yan's avatar
Yan committed
20
21
import prasopes.graphtools as gt
import prasopes.imagetools as imgt
Yan's avatar
Yan committed
22
import prasopes.zcetools as zce
Yan's avatar
Yan committed
23
import prasopes.docks as docks
Yan's avatar
Yan committed
24
import prasopes.tangoicons
Yan's avatar
Yan committed
25
26
import sys
import logging
3Yan's avatar
3Yan committed
27
import os.path
Yan's avatar
Yan committed
28
29


30
31
32
33
class update_signal(QtCore.QObject):
    signal = QtCore.pyqtSignal()


34
35
36
37
class QStatusBarLogger(logging.Handler):
    def __init__(self, parent=None):
        super().__init__()
        self.statusBar = QtWidgets.QStatusBar(parent)
Yan's avatar
Yan committed
38
39
        self.trigger = update_signal()
        self.msg = str("")
40
41

    def emit(self, record):
Yan's avatar
Yan committed
42
43
        self.msg = self.format(record)
        self.trigger.signal.emit()
44
45


46
def load_file(parent, augCanvas, update, settings, loadthread, filename=None):
Yan's avatar
Yan committed
47
    """populates dataset and plots it"""
48
49
50
    directory = augCanvas.filename or settings.value("open_folder")
    filename = filename or QtWidgets.QFileDialog.getOpenFileName(
            caption="Open spectrum", directory=directory,
3Yan's avatar
3Yan committed
51
            filter="Finnigan RAW files (*.raw, *.RAW)")[0]
52
53
    if filename != '' and os.path.isfile(filename)\
            and not os.path.isdir(filename):
Yan's avatar
Yan committed
54
55
        error = update_signal()
        errormsg = []
56

57
        def runfnc():
Yan's avatar
Yan committed
58
            try:
Yan's avatar
Yan committed
59
                [i.clear() for i in (
60
61
                    augCanvas.ds, augCanvas.chrom['timesarg'],
                    augCanvas.ms['params'], augCanvas.ms['headers'])]
62
63
                [augCanvas.ds.append(dict(
                    chrom_dat=i[0], masses=i[1], matrix=i[2]))
64
                 for i in load_raw(filename, settings.value("tmp_location"))]
65
            except rawprasslib.ParsingException as pex:
Yan's avatar
Yan committed
66
67
                errormsg.append("Opening of the file has failed!")
                errormsg.append(
68
69
                    "File is incompatible with the rawprasslib, "
                    "canceling request!\n\n"
Yan's avatar
Yan committed
70
                    "Error message:\n{}".format(pex.args[0]))
Yan's avatar
Yan committed
71
                error.signal.emit()
72
                return
73
            if autoparams:
74
                try:
75
76
                    (augCanvas.ms['params'], rawheaders,
                        augCanvas.chrom['machtype']) = load_params(filename)
77
                    segments = [len(subset['chrom_dat'][0])
78
                                for subset in augCanvas.ds]
79
                    indicies = [sum(segments[:i+1])
80
                                for i in range(len(segments))]
81
82
                    augCanvas.ms['headers'] = np.split(
                            rawheaders, indicies)[:-1]
83
                except Exception as pex:
84
85
                    errormsg.append(
                            "File is incompatible with the rawautoparams,")
Yan's avatar
Yan committed
86
87
88
89
                    errormsg.append(
                            "no parameters loaded!\n\n"
                            "Error message:\n{}".format(pex.args[0]))
                    error.signal.emit()
90
91
            gt.populate(augCanvas)
            augCanvas.filename = filename
92
93
            parent.setWindowTitle("Prasopes - {}".format(
                os.path.basename(filename)))
94
95
96
            oldrecents = settings.value("recents")
            oldrecents.remove(filename) if filename in oldrecents else None
            settings.setValue("recents", [filename, *oldrecents])
97
            update.signal.emit()
98
            loadthread.quit()
99
100
        error.signal.connect(lambda: QtWidgets.QMessageBox.critical(
            parent, errormsg[0], errormsg[1]))
101
102
        loadthread.run = runfnc
        loadthread.start()
3Yan's avatar
3Yan committed
103

Yan's avatar
Yan committed
104

105
def print_graph(augCanvas):
Yan's avatar
Yan committed
106
    def printimage(printdevice, img):
Yan's avatar
Yan committed
107
        printer.setResolution(600)
Yan's avatar
Yan committed
108
        painter = QtGui.QPainter(printdevice)
Yan's avatar
Yan committed
109
110
111
112
        font = painter.font()
        linesize = printer.resolution()/15
        font.setPixelSize(linesize)
        painter.setFont(font)
113
        painter.drawImage(0, 0, img)
Yan's avatar
Yan committed
114
115
116
        offset = img.size().height()
        line = 1
        spacing = 1.5
117
118
119
120
121
        for row in range(augCanvas.paramstable.rowCount()):
            if augCanvas.paramstable.cellWidget(row, 0).checkState() == 2:
                text = augCanvas.paramstable.item(row, 1).text() +\
                       augCanvas.paramstable.item(row, 2).text()
                painter.drawText(300, int(offset+line*linesize*spacing), text)
Yan's avatar
Yan committed
122
                line += 1
Yan's avatar
Yan committed
123
        painter.end()
124
    # TODO: substitute the QPrintPreviewDialog with QPrintPreviewWidget
125
126
    printPreview = QtPrintSupport.QPrintPreviewDialog()
    printer = printPreview.printer()
Yan's avatar
Yan committed
127
128
    printer.setPageSize(printer.A5)
    printer.setDuplex(printer.DuplexNone)
129
130
    image = imgt.paint_image(augCanvas.ms, augCanvas.spectplot,
                             augCanvas.filename, printer)
Yan's avatar
Yan committed
131
    printPreview.paintRequested.connect(lambda:
Yan's avatar
Yan committed
132
                                        printimage(printer, image))
133
134
135
    printPreview.exec()


136
137
138
139
140
141
142
143
144
145
146
147
def update_spectrum(augCanvas, config):
    if augCanvas.filename:
        slims = [augCanvas.spectplot.get_xlim(),
                 augCanvas.spectplot.get_ylim()]
        augCanvas.ds.clear()
        [augCanvas.ds.append(dict(chrom_dat=i[0], masses=i[1], matrix=i[2]))
         for i in load_raw(augCanvas.filename, config.value("tmp_location"))]
        gt.populate(augCanvas)
        augCanvas.spectplot.set_xlim(slims[0])
        augCanvas.spectplot.set_ylim(slims[1])
        gt.ann_spec(augCanvas.spectplot, augCanvas.ms)
        augCanvas.draw()
Yan's avatar
Yan committed
148
149


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def update_recents(rcm, main_window, augCanvas, update, config, loadthread):
    rcm.clear()
    # Actions need to be stored somewhere. Otherwise they end up in garbage.
    rcm.actionCache = []
    for i, j in enumerate(config.value("recents"), start=1):
        rcm.actionCache.append(QtWidgets.QAction("&{}. {}".format(i, j), None))
        rcm.actionCache[-1].triggered.connect(lambda _, fn=j: load_file(
            main_window, augCanvas, update, config, loadthread, filename=fn))
    rcm.actionCache.append(rcm.addSeparator())
    rcm.actionCache.append(QtWidgets.QAction("Clear Recents", None))
    [rcm.actionCache[-1].triggered.connect(i) for i in (
         lambda: config.setValue("recents", ""), lambda: update_recents(
             rcm, main_window, augCanvas, update, config, loadthread))]
    rcm.addActions(rcm.actionCache)


166
def dropped(event, parent, augCanvas, update, config, loadthread):
167
    dropurl = event.mimeData().urls()[0].toLocalFile()
168
    load_file(parent, augCanvas, update, config, loadthread, filename=dropurl)
Yan's avatar
Yan committed
169
170
171
172


def drag_entered(event):
    if event.mimeData().hasUrls() and event.mimeData().urls()[0]\
173
            .toLocalFile().lower().endswith('.raw'):
Yan's avatar
Yan committed
174
175
176
        event.accept()


177
def predictmz(form, augCanvas):
Yan's avatar
Yan committed
178
179
    text = form.text()
    if text == "":
180
        augCanvas.ms["predict"] = None
Yan's avatar
Yan committed
181
        return
182
183
184
185
186
187
188
    slims = [augCanvas.spectplot.get_xlim(),
             augCanvas.spectplot.get_ylim()]
    augCanvas.ms["predict"] = getmzpattern(text)
    gt.populate(augCanvas)
    augCanvas.spectplot.set_xlim(slims[0])
    augCanvas.spectplot.set_ylim(slims[1])
    augCanvas.draw()
Yan's avatar
Yan committed
189
190


191
def oddeven_changed(augCanvas, config, oddevenact):
3Yan's avatar
3Yan committed
192
    config.setValue("view/oddeven", oddevenact.isChecked())
193
    update_spectrum(augCanvas, config)
3Yan's avatar
3Yan committed
194
195


196
def key_pressed(event, augCanvas, config):
Yan's avatar
Yan committed
197
    if event.key() == QtCore.Qt.Key_F5:
198
        update_spectrum(augCanvas, config)
199
200
    if event.key() == QtCore.Qt.Key_C:
        if event.modifiers().__int__() == QtCore.Qt.ControlModifier:
201
202
203
204
205
            if augCanvas.paramstable.underMouse():
                dt.clip_tablestr(augCanvas)
            else:
                imgt.clip_spect_img(augCanvas.ms, augCanvas.spectplot,
                                    augCanvas.filename)
206
207
        if event.modifiers().__int__() == QtCore.Qt.ControlModifier + \
                QtCore.Qt.ShiftModifier:
208
            dt.clip_spectstr(augCanvas)
Yan's avatar
Yan committed
209
    if event.key() in (QtCore.Qt.Key_Left, QtCore.Qt.Key_Right):
210
        gt.shift_times(event, augCanvas)
Yan's avatar
Yan committed
211
212


Yan's avatar
Yan committed
213
214
215
216
217
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
218
            "Version: 0.0.13 (alpha)")
Yan's avatar
Yan committed
219
220


Yan's avatar
Yan committed
221
def main():
222
    app = QtWidgets.QApplication(sys.argv)
223
    loadthread = QtCore.QThread()
224

225
    augCanvas = gt.AugFigureCanvas()
226
    update = update_signal()
Yan's avatar
Yan committed
227

Yan's avatar
Yan committed
228
229
    config = cf.settings()

230
    barHandler = QStatusBarLogger()
231
232
    barHandler.trigger.signal.connect(
        lambda: barHandler.statusBar.showMessage(barHandler.msg))
233

Yan's avatar
Yan committed
234
    p_logger = logging.getLogger('parseLogger')
235
    params_logger = logging.getLogger('acqLogLogger')
Yan's avatar
Yan committed
236
    drl_logger = logging.getLogger('drlLogger')
237
    zce_logger = logging.getLogger('zceLogger')
Yan's avatar
Yan committed
238
    logging.basicConfig()
239
    # p_logger.setLevel("WARN")
240
    p_logger.setLevel("DEBUG")
241
    # drl_logger.setLevel("INFO")
Yan's avatar
Yan committed
242
    drl_logger.setLevel("DEBUG")
243
    zce_logger.setLevel("DEBUG")
244
    # params_logger.setLevel("DEBUG")
245
246
    p_logger.addHandler(barHandler)
    zce_logger.addHandler(barHandler)
247
248
    params_logger.addHandler(barHandler)
    barHandler.setLevel("DEBUG")
Yan's avatar
Yan committed
249

Yan's avatar
Yan committed
250
    main_window = QtWidgets.QMainWindow(windowTitle="Prasopes")
Yan's avatar
Yan committed
251

252
    if QtGui.QIcon.themeName() == "":
Yan's avatar
Yan committed
253
254
        QtGui.QIcon.setThemeName("TangoMFK")

Yan's avatar
Yan committed
255
256
257
    consoledock = docks.consoleDockWidget(
            locals(), "&Console", "view/consolevisible")
    treedock = docks.treeDockWidget(
258
259
            "&File browser", "view/filebrowservisible", update, load_file,
            main_window, augCanvas, config, loadthread)
Yan's avatar
Yan committed
260
    paramsdock = docks.AugDock("Acquisition parameters", "&Acq parameters",
261
                               "view/acqparvisible")
262
263
    update.signal.connect(lambda: gt.update_paramstable(augCanvas))
    paramsdock.setWidget(augCanvas.paramstable)
Yan's avatar
Yan committed
264

Yan's avatar
Yan committed
265
    openact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
266
        "document-open"), "&Open...", None)
Yan's avatar
Yan committed
267
268
    openact.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_O)
    openact.triggered.connect(lambda: load_file(
269
        main_window, augCanvas, update, config, loadthread))
Yan's avatar
Yan committed
270
    exportact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
271
        "document-save-as"), "&Export...", None)
Yan's avatar
Yan committed
272
273
    exportact.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_E)
    exportact.triggered.connect(lambda: ft.export_dial(
274
        augCanvas, main_window))
275
276
277
    printact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
        "document-print"), "&Print", None)
    printact.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_P)
278
    printact.triggered.connect(lambda: print_graph(augCanvas))
Yan's avatar
Yan committed
279
    settingsact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
280
        "preferences-system"), "&Settings...", None)
Yan's avatar
Yan committed
281
282
    settingsact.triggered.connect(lambda: cf.dial(main_window))
    quitact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
283
        "application-exit"), "&Quit", None)
Yan's avatar
Yan committed
284
285
286
    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
287
        "applications-utilities"), "&TSQ zce...", None)
Yan's avatar
Yan committed
288
289
    zceact.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_T)
    zceact.triggered.connect(lambda: zce.dialog(
290
        main_window, augCanvas, update))
Yan's avatar
Yan committed
291
    drlact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
292
        "applications-utilities"), "&DRL...", None)
Yan's avatar
Yan committed
293
294
    drlact.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_D)
    drlact.triggered.connect(lambda: drl.main_window(
295
        main_window, augCanvas, update))
Yan's avatar
Yan committed
296
297
    aboutact = QtWidgets.QAction("&About Prasopes", None)
    aboutact.triggered.connect(lambda: about(main_window))
Yan's avatar
Yan committed
298
    autozoomy = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
299
300
301
302
        "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()))
Yan's avatar
Yan committed
303
    autozoomy.triggered.connect(lambda: gt.autozoomy(augCanvas.spectplot))
304
    intensitiesact = QtWidgets.QAction(
Yan's avatar
Yan committed
305
306
        "&Show intensities", None, checkable=True,
        checked=config.value("view/intensities", type=bool))
307
308
    intensitiesact.triggered.connect(lambda: config.setValue(
        "view/intensities", intensitiesact.isChecked()))
Yan's avatar
Yan committed
309
310
    intensitiesact.triggered.connect(lambda: gt.ann_spec(
        augCanvas.spectplot, augCanvas.ms))
311
    intensitiesact.triggered.connect(lambda: augCanvas.draw())
312
    oddevenact = QtWidgets.QAction(
Yan's avatar
Yan committed
313
314
            "&Odd / even", None, checkable=True,
            checked=config.value("view/oddeven", type=bool))
315
316
    oddevenact.triggered.connect(
        lambda: oddeven_changed(augCanvas, config, oddevenact))
Yan's avatar
Yan committed
317

Yan's avatar
Yan committed
318
319
    predictform = QtWidgets.QLineEdit(maximumWidth=150)
    predictform.editingFinished.connect(lambda: predictmz(
320
        predictform, augCanvas))
Yan's avatar
Yan committed
321

322
323
324
325
326
327
    recents_menu = QtWidgets.QMenu('Open &Recent', main_window)
    update_recents(recents_menu, main_window, augCanvas,
                   update, config, loadthread)
    update.signal.connect(lambda: update_recents(
        recents_menu, main_window, augCanvas, update, config, loadthread))

Yan's avatar
Yan committed
328
329
    file_menu = QtWidgets.QMenu('&File', main_window)
    main_window.menuBar().addMenu(file_menu)
Yan's avatar
Yan committed
330
    file_menu.addAction(openact)
331
    file_menu.addMenu(recents_menu)
Yan's avatar
Yan committed
332
333
    file_menu.addAction(exportact)
    file_menu.addSeparator()
334
335
    file_menu.addAction(printact)
    file_menu.addSeparator()
Yan's avatar
Yan committed
336
337
338
    file_menu.addAction(settingsact)
    file_menu.addSeparator()
    file_menu.addAction(quitact)
Yan's avatar
Yan committed
339
340
    tools_menu = QtWidgets.QMenu('&Tools', main_window)
    main_window.menuBar().addMenu(tools_menu)
Yan's avatar
Yan committed
341
342
    tools_menu.addAction(zceact)
    tools_menu.addAction(drlact)
343
    tools_menu.addSeparator()
Yan's avatar
Yan committed
344
    view_menu = QtWidgets.QMenu('&View', main_window)
345
346
    [view_menu.addAction(i.action) for i in
     (treedock, paramsdock, consoledock)]
Yan's avatar
Yan committed
347
    [view_menu.addAction(i) for i in (autozoomy, intensitiesact)]
348
349
    view_menu.addSeparator()
    view_menu.addAction(oddevenact)
Yan's avatar
Yan committed
350
    main_window.menuBar().addMenu(view_menu)
Yan's avatar
Yan committed
351
352
353
    help_menu = QtWidgets.QMenu('&Help', main_window)
    main_window.menuBar().addMenu(help_menu)
    help_menu.addAction(aboutact)
Yan's avatar
Yan committed
354

355
    main_window.setCentralWidget(augCanvas)
Yan's avatar
Yan committed
356

Yan's avatar
Yan committed
357
358
359
360
361
    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
362

Yan's avatar
Yan committed
363
364
365
    toolBar.addAction(openact)
    toolBar.addAction(exportact)
    toolBar.addSeparator()
Yan's avatar
Yan committed
366
367
368
    toolBar.addWidget(QtWidgets.QLabel("Predict Formula:"))
    toolBar.addWidget(predictform)
    toolBar.addSeparator()
Yan's avatar
Yan committed
369
370
    toolBar.addAction(zceact)
    toolBar.addAction(drlact)
Yan's avatar
Yan committed
371
372
    toolBar.addSeparator()
    toolBar.addAction(autozoomy)
Yan's avatar
Yan committed
373

Yan's avatar
Yan committed
374
375
    main_window.dragEnterEvent = lambda event: drag_entered(event)
    main_window.dropEvent = lambda event: dropped(
376
        event, main_window, augCanvas, update, config, loadthread)
Yan's avatar
Yan committed
377
    main_window.setAcceptDrops(True)
378
379
    main_window.keyPressEvent = lambda event: key_pressed(
            event, augCanvas, config)
Yan's avatar
Yan committed
380

381
382
    main_window.addToolBar(QtCore.Qt.TopToolBarArea, toolBar)
    main_window.addDockWidget(QtCore.Qt.LeftDockWidgetArea, treedock)
Yan's avatar
Yan committed
383
    main_window.addDockWidget(QtCore.Qt.RightDockWidgetArea, paramsdock)
Yan's avatar
Yan committed
384
    main_window.addDockWidget(QtCore.Qt.RightDockWidgetArea, consoledock)
385
    main_window.setStatusBar(barHandler.statusBar)
386
387
388

    main_window.setFocus()

Yan's avatar
Yan committed
389
    if len(sys.argv) == 2:
390
        load_file(main_window, augCanvas, update, config, loadthread,
391
                  filename=sys.argv[1])
Yan's avatar
Yan committed
392
    else:
393
394
        gt.pop_plot(0, 0, augCanvas.spectplot, augCanvas.ms)
        gt.pop_plot(0, 0, augCanvas.chromplot, augCanvas.chrom)
Yan's avatar
Yan committed
395
396
397

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

399

Yan's avatar
Yan committed
400
401
if __name__ == "__main__":
    main()