__main__.py 4.76 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
16
17
18
19
20
import sys
import matplotlib
import logging
matplotlib.use("Qt5Agg")


Yan's avatar
Yan committed
21
def load_file(chrom_plot, spc, d_set, ms_s, fn, chrom_s, settings):
Yan's avatar
Yan committed
22
    """populates dataset and plots it"""
Yan's avatar
Yan committed
23
    lf = ft.open_dial(d_set, settings)
Yan's avatar
Yan committed
24
25
26
    if lf is not None:
        gt.populate(chrom_plot, spc, d_set, ms_s, chrom_s)
        fn[0] = lf
Yan's avatar
Yan committed
27

Yan's avatar
Yan committed
28
29
30
31
32
33
34
35
36
37
38
39
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()


Yan's avatar
Yan committed
40

Yan's avatar
Yan committed
41
def main():
Yan's avatar
Yan committed
42
43
44
    # 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
45
46
              name="Spectrum", xlabel="m/z", ylabel="ion count",
              xtics=20)
Yan's avatar
Yan committed
47
48
49
50
    chrom = dict(x=[0], y=[0], line=None, t_start=None, t_end=None,
                 name="Chromatogram", xlabel="time(min)",
                 ylabel="total ion count")
    filename = [None]
Yan's avatar
Yan committed
51
    drlcache = [None, None, None]
Yan's avatar
Yan committed
52

Yan's avatar
Yan committed
53
54
    config = cf.settings()

Yan's avatar
Yan committed
55
56
57
58
    p_logger = logging.getLogger('parseLogger')
    logging.basicConfig()
    p_logger.setLevel("WARN")

Yan's avatar
Yan committed
59
    graph = Figure(figsize=(5, 4), dpi=100, facecolor="None")
Yan's avatar
Yan committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73

    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)
    gt.pan_factory(spectrum, ms)
    gt.zoom_factory(spectrum, 1.15, ms)
74
    mass_selector = SpanSelector(
Yan's avatar
Yan committed
75
        spectrum, lambda x_min, x_max: gt.pick_masses(
76
77
78
            x_min, x_max, spectrum, ms), 'horizontal', minspan=0.01,
        useblit=True, rectprops=dict(alpha=0.15, facecolor='purple'),
        button=3)
79
    time_selector = SpanSelector(
Yan's avatar
Yan committed
80
81
82
83
84
85
         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
86
    main_window = QtWidgets.QMainWindow(windowTitle="Prasopes")
Yan's avatar
Yan committed
87
88
89

    file_menu = QtWidgets.QMenu('&File', main_window)
    main_window.menuBar().addMenu(file_menu)
Yan's avatar
Yan committed
90
    file_menu.addAction('&Open..', lambda: load_file(
Yan's avatar
Yan committed
91
        chromatogram, spectrum, ds, ms, filename, chrom, config),
Yan's avatar
Yan committed
92
        QtCore.Qt.CTRL + QtCore.Qt.Key_O)
Yan's avatar
Yan committed
93
94
95
    file_menu.addAction('&Export..', lambda: ft.export_dial(
        ms, chrom, filename, main_window),
        QtCore.Qt.CTRL + QtCore.Qt.Key_E)
Yan's avatar
Yan committed
96
97
    file_menu.addAction('Settings', lambda: cf.dial(
        main_window))
Yan's avatar
Yan committed
98
99
    file_menu.addAction('&Quit', main_window.close,
                        QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
Yan's avatar
Yan committed
100
101
102
103
    tools_menu = QtWidgets.QMenu('&Tools', main_window)
    main_window.menuBar().addMenu(tools_menu)
    tools_menu.addAction('&TSQ zce', lambda: zce.dialog(
        main_window, ds, filename), QtCore.Qt.CTRL + QtCore.Qt.Key_T)
Yan's avatar
Yan committed
104
    tools_menu.addAction('&DRL..', lambda: drl.main_window(
Yan's avatar
Yan committed
105
        main_window, ds, filename, drlcache),
106
        QtCore.Qt.CTRL + QtCore.Qt.Key_D)
Yan's avatar
Yan committed
107
108
109
110
111
112
113
114

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

    layout = QtWidgets.QVBoxLayout(main_widget)
    layout.addWidget(mpl_canvas)
    mpl_canvas.setFocus()

Yan's avatar
Yan committed
115
116
117
118
119
    mpl_canvas.mpl_connect('key_press_event',lambda event:
        update_spectrum(event, chromatogram, spectrum, ds, ms,
                        filename, chrom, config))


Yan's avatar
Yan committed
120
121
122
    if len(sys.argv) == 2:
        raw_file = sys.argv[1]
        filename[0] = raw_file
Yan's avatar
Yan committed
123
124
        ds['chrom_dat'], ds['masses'], ds['matrix'] = load_raw(
                raw_file, config.value("tmp_location"))
Yan's avatar
Yan committed
125
126
127
128
129
130
131
        gt.populate(chromatogram, spectrum, ds, ms, chrom)
    else:
        gt.pop_plot(spectrum, ms)
        gt.pop_plot(chromatogram, chrom)

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

133

Yan's avatar
Yan committed
134
135
if __name__ == "__main__":
    main()