Commit 4f3a3efa authored by 3Yan's avatar 3Yan
Browse files

Small bugfix in main, add_parent_ion working in DRL!

* still preliminary - code is messy there
parent 5d5f2112
......@@ -53,12 +53,12 @@ def main():
gt.zoom_factory(chromatogram, 1.15)
gt.pan_factory(spectrum, ms)
gt.zoom_factory(spectrum, 1.15, ms)
gt.mass_selector = SpanSelector(
mass_selector = SpanSelector(
spectrum, lambda x_min, x_max: gt.pick_masses(
x_min, x_max, spectrum, ms),
'horizontal', useblit=True, rectprops=dict(
alpha=0.15, facecolor='purple'), button=3)
gt.time_selector = SpanSelector(
time_selector = SpanSelector(
chromatogram, lambda x_min, x_max: gt.pick_times(
x_min, x_max, spectrum, ds, chromatogram, ms, chrom),
'horizontal', useblit=True, rectprops=dict(
......@@ -82,7 +82,8 @@ def main():
tools_menu.addAction('&TSQ zce', lambda: zce.dialog(
main_window, ds, filename), QtCore.Qt.CTRL + QtCore.Qt.Key_T)
tools_menu.addAction('&DRL..', lambda: drl.dialog(
main_window, ds, filename), QtCore.Qt.CTRL + QtCore.Qt.Key_D)
main_window, ds, filename, mass_selector, spectrum),
QtCore.Qt.CTRL + QtCore.Qt.Key_D)
main_widget = QtWidgets.QWidget(main_window)
main_window.setCentralWidget(main_widget)
......
......@@ -5,6 +5,7 @@ from matplotlib.figure import Figure
from matplotlib.widgets import SpanSelector
from PyQt5 import QtCore
from PyQt5 import QtWidgets
import prasopes.graphtools as gt
import sys
import matplotlib
import numpy as np
......@@ -19,8 +20,168 @@ class HBar(QtWidgets.QFrame):
self.setFrameShape(QtWidgets.QFrame.HLine)
def add_parent(parent_widget, mass_selector, spectrum, ds, parenttable):
"""add parent ion to the table"""
ap_dial = QtWidgets.QDialog(parent_widget, windowTitle='Add Ion')
parent_widget.finished.connect(ap_dial.close)
ion = dict(x=[0], y=[0], line=None, name="", xlabel="m/z",
ylabel="ion count", annotation=[])
ion_graph = Figure(figsize=(3, 1.5), dpi=100, facecolor="None")
ionspect = ion_graph.add_subplot(111, facecolor=(1, 1, 1, 0.8))
graph_canvas = FigureCanvas(ion_graph)
ion_graph.tight_layout()
graph_canvas.setStyleSheet("background-color:transparent;")
graph_canvas.setAutoFillBackground(False)
graph_canvas.setFocusPolicy(QtCore.Qt.ClickFocus)
gt.pop_plot(ionspect, ion)
def end_bindings(bindings, axis):
fig = axis.get_figure()
for i in bindings:
axis.canvas.mpl_disconnect(i)
return
def update_fields(x_min, x_max, startw, endw):
endw.setValue(x_max)
startw.setValue(x_min)
def update_parent(start_mz, end_mz, spectrum, ion_set, dataset):
start = start_mz.value()
end = end_mz.value()
if start > end:
start = end_mz.value()
end = start_mz.value()
start_mz.setValue(start)
end_mz.setValue(end)
#TODO: maybe find a way how this should be called only once
#TODO: make the function more economical both in CPU load and in linecount
start_mass = None
end_mass = None
for i, mass in enumerate(dataset['masses'][0:]):
if mass > start and start_mass is None:
start_mass = i
if mass > end and end_mass is None:
end_mass = i
if end_mass is None:
end_mass = len(dataset['masses'])
if start_mass is None:
start_mass = len(dataset['masses'])-1
if start_mass == end_mass:
if start_mass == 0:
end_mass = 1
else:
start_mass -= 1
end_mass = start_mass + 2
spectrum.clear()
gt.pop_plot(spectrum, ion_set)
yshape = np.mean(dataset['matrix'], axis=0)
dots_x = dataset['masses']
dots_y = yshape
full_x = dataset['masses'][start_mass:end_mass]
full_y = yshape[start_mass:end_mass]
spectrum.plot(dots_x, dots_y, ':', color='gray')
spectrum.plot(full_x, full_y, 'r')
xex = (dataset['masses'][end_mass-1]-dataset['masses'][start_mass])*0.25
spectrum.set_xlim(dataset['masses'][start_mass]-xex,dataset['masses'][end_mass-1]+xex)
ymax = max(yshape[start_mass:end_mass])
spectrum.set_ylim(ymax*-0.1, ymax*1.1)
spectrum.figure.canvas.draw()
def activate_altselector(event, mass_selector, picker):
if event.key == 'shift':
mass_selector.active = False
picker.active = True
def deactivate_altselector(event, mass_selector, picker):
if event.name == 'motion_notify_event' \
or (event.name == 'key_release_event'
and event.key == 'shift'):
mass_selector.active = True
picker.active = False
def add_ion(start_mz, end_mz, table, dialog):
newrow = table.rowCount()
table.setRowCount(newrow+1)
col2 = QtWidgets.QTableWidgetItem()
col2.setText("bagr")
table.setCellWidget(newrow,1,start_mz)
table.setCellWidget(newrow,2,end_mz)
dialog.close()
ax = spectrum.get_figure()
bindings = []
bindings.append(ax.canvas.mpl_connect('key_press_event', lambda
event: activate_altselector(event, mass_selector, picker)))
bindings.append(ax.canvas.mpl_connect('key_release_event', lambda
event: deactivate_altselector(event, mass_selector, picker)))
# TODO: somehow manage if user enters with shift pressed, but
# figure_enter_event cannot handle this :( and with QT it is PIA
bindings.append(ax.canvas.mpl_connect('figure_leave_event', lambda
event: deactivate_altselector(event, mass_selector, picker)))
ap_dial.finished.connect(lambda: end_bindings(bindings, ax))
start_mz = QtWidgets.QDoubleSpinBox(maximum=999999)
start_mz.valueChanged.connect(lambda: update_parent(
start_mz, end_mz, ionspect, ion, ds))
end_mz = QtWidgets.QDoubleSpinBox(maximum=999999)
end_mz.valueChanged.connect(lambda: update_parent(
start_mz, end_mz, ionspect, ion, ds))
submit = QtWidgets.QPushButton("&Submit")
submit.clicked.connect(lambda: add_ion(start_mz, end_mz, parenttable, ap_dial))
cancel = QtWidgets.QPushButton("Cancel")
cancel.clicked.connect(ap_dial.close)
main_layout = QtWidgets.QVBoxLayout(ap_dial)
data_layout = QtWidgets.QHBoxLayout()
butt_layout = QtWidgets.QHBoxLayout()
butt_layout.addStretch(1)
butt_layout.addWidget(submit)
butt_layout.addWidget(cancel)
data_layout.addWidget(QtWidgets.QLabel("start:"))
data_layout.addWidget(start_mz)
data_layout.addWidget(QtWidgets.QLabel("m/z"))
data_layout.addSpacing(20)
data_layout.addWidget(QtWidgets.QLabel("end:"))
data_layout.addWidget(end_mz)
data_layout.addWidget(QtWidgets.QLabel("m/z"))
data_layout.addStretch(0)
main_layout.addWidget(QtWidgets.QLabel("Press shift in "
"the chromatogram window and \n"
"select the ion range by the right mouse button\n"
"or fill the columns manually"))
main_layout.addWidget(graph_canvas, 1)
main_layout.addLayout(data_layout)
main_layout.addLayout(butt_layout)
picker = SpanSelector(spectrum, lambda x_min, x_max:
update_fields(x_min, x_max, start_mz, end_mz),
'horizontal', useblit=True, rectprops=dict(
alpha=0.15, facecolor='orange'), button=3)
picker.active = False
ap_dial.show()
def iontable(labels, exp_col):
"""creates a table for ions"""
def remove_rows(table):
# TODO: maybe better selection in future, but this works for now
rows = reversed(list(set(
map(lambda index: index.row(), table.selectedIndexes()))))
for row in rows:
table.removeRow(row)
table = QtWidgets.QTableWidget(columnCount=len(labels))
table.setSizePolicy(QtWidgets.QSizePolicy.Fixed,
QtWidgets.QSizePolicy.Expanding)
......@@ -35,6 +196,7 @@ def iontable(labels, exp_col):
table.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
table_add = QtWidgets.QPushButton("Add")
table_rem = QtWidgets.QPushButton("Remove")
table_rem.clicked.connect(lambda: remove_rows(table))
table_butlayout = QtWidgets.QHBoxLayout()
table_butlayout.addWidget(table_add)
table_butlayout.addWidget(table_rem)
......@@ -42,8 +204,9 @@ def iontable(labels, exp_col):
return table, table_butlayout, table_add, table_rem
def dialog(parent, ds, filename):
def dialog(parent, ds, filename, mass_selector, spectrum):
"""constructs a dialog window"""
dial_widget = QtWidgets.QDialog(
parent, windowTitle='Delayed reactant labelling')
......@@ -53,15 +216,19 @@ def dialog(parent, ds, filename):
graph_canvas.setStyleSheet("background-color:transparent;")
graph_canvas.setAutoFillBackground(False)
drl_load = QtWidgets.QPushButton("Load")
drl_save = QtWidgets.QPushButton("Save")
drl_export = QtWidgets.QPushButton("Export")
close = QtWidgets.QPushButton("Close")
drl_load = QtWidgets.QPushButton("&Load")
drl_save = QtWidgets.QPushButton("&Save")
drl_export = QtWidgets.QPushButton("&Export")
close = QtWidgets.QPushButton("&Close")
close.clicked.connect(dial_widget.close)
# pt = parenttable
pt, pt_butlayout, pt_add, pt_rem = iontable(
["Name", "start (m/z)", "end (m/z)", "profile"], 2)
pt_add.setText("&Add")
pt_add.clicked.connect(lambda: add_parent(
dial_widget, mass_selector, spectrum, ds, pt))
# dt = daughtertable
dt, dt_butlayout, dt_add, dt_rem = iontable(
["Name", "start (m/z)", "end (m/z)", "corr to"], 2)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment