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

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.widgets import SpanSelector
from PyQt5 import QtCore
from PyQt5 import QtWidgets
Yan's avatar
Yan committed
8
from PyQt5 import QtGui
Yan's avatar
Yan committed
9
10
11
from rawprasslib import load_raw
import prasopes.graphtools as gt
import prasopes.filetools as ft
Yan's avatar
Yan committed
12
import prasopes.zcetools as zce
Yan's avatar
Yan committed
13
import prasopes.drltools as drl
Yan's avatar
Yan committed
14
import prasopes.config as cf
Yan's avatar
Yan committed
15
import prasopes.tangoicons
Yan's avatar
Yan committed
16
17
18
import sys
import matplotlib
import logging
3Yan's avatar
3Yan committed
19
import os.path
Yan's avatar
Yan committed
20
21
22
matplotlib.use("Qt5Agg")


23
24
def load_file(parent, chrom_plot, spc, d_set, ms_s, fn, chrom_s,
              settings, filename=None):
Yan's avatar
Yan committed
25
    """populates dataset and plots it"""
3Yan's avatar
3Yan committed
26
27
28
    directory=settings.value("open_folder")
    if fn[0] is not None:
        directory=fn[0]
Yan's avatar
Yan committed
29
30
    if filename == None:
        filename = QtWidgets.QFileDialog.getOpenFileName(
3Yan's avatar
3Yan committed
31
32
33
34
35
36
            caption="Open spectrum",
            directory=directory,
            filter="Finnigan RAW files (*.raw, *.RAW)")[0]
    if filename is not '':
        d_set['chrom_dat'], d_set['masses'], d_set['matrix'] \
             = load_raw(filename, settings.value("tmp_location"))
Yan's avatar
Yan committed
37
        gt.populate(chrom_plot, spc, d_set, ms_s, chrom_s)
3Yan's avatar
3Yan committed
38
        fn[0] = filename
39
40
        parent.setWindowTitle("Prasopes - {}".format(
            os.path.basename(filename)))
3Yan's avatar
3Yan committed
41

Yan's avatar
Yan committed
42

Yan's avatar
Yan committed
43
44
45
46
47
48
49
50
51
52
53
54
def update_spectrum(event, chromatogram, spect, ds,
                    ms, fn, chrom, config):
    if fn[0] is not None and event.key is "f5":
        slims = [spect.get_xlim(), spect.get_ylim()]
        ds['chrom_dat'], ds['masses'], ds['matrix'] \
            = load_raw(fn[0], config.value("tmp_location"))
        gt.populate(chromatogram, spect, ds, ms, chrom)
        spect.set_xlim(slims[0])
        spect.set_ylim(slims[1])
        spect.get_figure().canvas.draw()


55
56
def dropped(event, parent, chromatogram, spectrum, ds, ms, filename,
            chrom, config):
57
    dropurl = event.mimeData().urls()[0].toLocalFile()
58
59
    load_file(parent, chromatogram, spectrum, ds, ms, filename,
              chrom, config, filename=dropurl)
Yan's avatar
Yan committed
60
61
62
63


def drag_entered(event):
    if event.mimeData().hasUrls() and event.mimeData().urls()[0]\
64
        .toLocalFile().lower().endswith('.raw'):
Yan's avatar
Yan committed
65
66
67
        event.accept()


Yan's avatar
Yan committed
68
def main():
Yan's avatar
Yan committed
69
70
71
    # ds for data_set, ms for mass_spec_dataset, chrom for chrom_dataset
    ds = dict(chrom_dat=None, masses=None, matrix=None)
    ms = dict(x=[0], y=[0], line=None, annotation=[],
Yan's avatar
Yan committed
72
              name="Spectrum", xlabel="m/z", ylabel="ion count",
Yan's avatar
Yan committed
73
              xtics=20, autoy=True)
Yan's avatar
Yan committed
74
75
    chrom = dict(x=[0], y=[0], line=None, t_start=None, t_end=None,
                 name="Chromatogram", xlabel="time(min)",
76
                 ylabel="total ion count", timesarg=None)
Yan's avatar
Yan committed
77
    filename = [None]
3Yan's avatar
3Yan committed
78
    drlcache = [None, None]
Yan's avatar
Yan committed
79

Yan's avatar
Yan committed
80
81
    config = cf.settings()

Yan's avatar
Yan committed
82
    p_logger = logging.getLogger('parseLogger')
Yan's avatar
Yan committed
83
    drl_logger = logging.getLogger('drlLogger')
Yan's avatar
Yan committed
84
85
    logging.basicConfig()
    p_logger.setLevel("WARN")
Yan's avatar
Yan committed
86
87
    #drl_logger.setLevel("INFO")
    drl_logger.setLevel("DEBUG")
Yan's avatar
Yan committed
88

Yan's avatar
Yan committed
89
    graph = Figure(figsize=(5, 4), dpi=100, facecolor="None")
Yan's avatar
Yan committed
90
91
92
93
94
95
96
97
98
99
100
101

    chromatogram = graph.add_subplot(211, facecolor=(1, 1, 1, 0.8))
    spectrum = graph.add_subplot(212, facecolor=(1, 1, 1, 0.8))
    graph.tight_layout()

    mpl_canvas = FigureCanvas(graph)
    mpl_canvas.setStyleSheet("background-color:transparent;")
    mpl_canvas.setAutoFillBackground(False)
    mpl_canvas.setFocusPolicy(QtCore.Qt.ClickFocus)
    
    gt.pan_factory(chromatogram)
    gt.zoom_factory(chromatogram, 1.15)
102
    gt.arrow_factory(spectrum, chromatogram, ds, ms, chrom)
Yan's avatar
Yan committed
103
104
    gt.pan_factory(spectrum, ms)
    gt.zoom_factory(spectrum, 1.15, ms)
105
    mass_selector = SpanSelector(
Yan's avatar
Yan committed
106
        spectrum, lambda x_min, x_max: gt.pick_masses(
107
108
109
            x_min, x_max, spectrum, ms), 'horizontal', minspan=0.01,
        useblit=True, rectprops=dict(alpha=0.15, facecolor='purple'),
        button=3)
110
    time_selector = SpanSelector(
Yan's avatar
Yan committed
111
112
113
114
115
116
         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)

    app = QtWidgets.QApplication(sys.argv)
Yan's avatar
Yan committed
117
    main_window = QtWidgets.QMainWindow(windowTitle="Prasopes")
Yan's avatar
Yan committed
118

Yan's avatar
Yan committed
119
120
121
    if QtGui.QIcon.themeName() is "":
        QtGui.QIcon.setThemeName("TangoMFK")

Yan's avatar
Yan committed
122
    openact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
123
        "document-open"), "&Open...", None)
Yan's avatar
Yan committed
124
125
    openact.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_O)
    openact.triggered.connect(lambda: load_file(
126
127
        main_window, chromatogram, spectrum, ds, ms, filename, chrom,
        config))
Yan's avatar
Yan committed
128
    exportact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
129
        "document-save-as"), "&Export...", None)
Yan's avatar
Yan committed
130
131
132
133
    exportact.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_E)
    exportact.triggered.connect(lambda: ft.export_dial(
        ms, chrom, filename, main_window))
    settingsact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
134
        "preferences-system"), "&Settings...", None)
Yan's avatar
Yan committed
135
136
    settingsact.triggered.connect(lambda: cf.dial(main_window))
    quitact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
137
        "application-exit"), "&Quit", None)
Yan's avatar
Yan committed
138
139
140
    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
141
        "applications-utilities"), "&TSQ zce...", None)
Yan's avatar
Yan committed
142
143
144
145
    zceact.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_T)
    zceact.triggered.connect(lambda: zce.dialog(
        main_window, ds, filename))
    drlact = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
146
        "applications-utilities"), "&DRL...", None)
Yan's avatar
Yan committed
147
148
149
    drlact.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_D)
    drlact.triggered.connect(lambda: drl.main_window(
        main_window, ds, filename, drlcache))
Yan's avatar
Yan committed
150
    autozoomy = QtWidgets.QAction(QtGui.QIcon.fromTheme(
Yan's avatar
Yan committed
151
        "zoom-original"), "Auto Zoom Y", None)
Yan's avatar
Yan committed
152
153
154
155
    autozoomy.setCheckable(True)
    autozoomy.setChecked(True)
    autozoomy.triggered.connect(lambda: gt.autozoomy(
        autozoomy.isChecked(), spectrum, ms))
Yan's avatar
Yan committed
156

Yan's avatar
Yan committed
157
158
    file_menu = QtWidgets.QMenu('&File', main_window)
    main_window.menuBar().addMenu(file_menu)
Yan's avatar
Yan committed
159
160
161
162
163
164
    file_menu.addAction(openact)
    file_menu.addAction(exportact)
    file_menu.addSeparator()
    file_menu.addAction(settingsact)
    file_menu.addSeparator()
    file_menu.addAction(quitact)
Yan's avatar
Yan committed
165
166
    tools_menu = QtWidgets.QMenu('&Tools', main_window)
    main_window.menuBar().addMenu(tools_menu)
Yan's avatar
Yan committed
167
168
    tools_menu.addAction(zceact)
    tools_menu.addAction(drlact)
Yan's avatar
Yan committed
169
170
171
172

    main_widget = QtWidgets.QWidget(main_window)
    main_window.setCentralWidget(main_widget)

Yan's avatar
Yan committed
173
174
175
176
177
    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
178

Yan's avatar
Yan committed
179
180
181
182
183
    toolBar.addAction(openact)
    toolBar.addAction(exportact)
    toolBar.addSeparator()
    toolBar.addAction(zceact)
    toolBar.addAction(drlact)
Yan's avatar
Yan committed
184
185
    toolBar.addSeparator()
    toolBar.addAction(autozoomy)
Yan's avatar
Yan committed
186

Yan's avatar
Yan committed
187
    layout = QtWidgets.QVBoxLayout(main_widget)
Yan's avatar
Yan committed
188
    layout.addWidget(toolBar)
Yan's avatar
Yan committed
189
190
191
    layout.addWidget(mpl_canvas)
    mpl_canvas.setFocus()

192
    mpl_canvas.mpl_connect('key_press_event', lambda event:
Yan's avatar
Yan committed
193
194
        update_spectrum(event, chromatogram, spectrum, ds, ms,
                        filename, chrom, config))
Yan's avatar
Yan committed
195
196
    main_window.dragEnterEvent = lambda event: drag_entered(event)
    main_window.dropEvent = lambda event: dropped(
197
198
        event, main_window, chromatogram, spectrum, ds, ms, filename,
        chrom, config)
Yan's avatar
Yan committed
199
    main_window.setAcceptDrops(True)
Yan's avatar
Yan committed
200

Yan's avatar
Yan committed
201
    if len(sys.argv) == 2:
202
203
        load_file(main_window, chromatogram, spectrum, ds, ms,
                  filename, chrom, config, filename=sys.argv[1])
Yan's avatar
Yan committed
204
205
206
207
208
209
    else:
        gt.pop_plot(spectrum, ms)
        gt.pop_plot(chromatogram, chrom)

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

211

Yan's avatar
Yan committed
212
213
if __name__ == "__main__":
    main()