Commit c0580d1f authored by Yan's avatar Yan
Browse files

Added support for odd/even acquisition method

* rewritten populate and plot_subtime functions in graphtools
* rewritten autozoomy function
* slightly modified the update_spectrum function in __main__
* rewritted the peak picking function
* removed debug prints from the last commit
parent a0e12f8c
......@@ -57,9 +57,9 @@ def load_file(parent, chrom_plot, spc, d_set, ms_s, fn, chrom_s,
update.signal.emit()
def update_spectrum(event, chromatogram, spect, ds,
def update_spectrum(condition, chromatogram, spect, ds,
ms, fn, chrom, config):
if fn[0] is not None and event.key is "f5":
if fn[0] is not None and condition:
slims = [spect.get_xlim(), spect.get_ylim()]
ds['chrom_dat'], ds['masses'], ds['matrix'] \
= load_raw(fn[0], config.value("tmp_location"))
......@@ -106,10 +106,10 @@ def about(parent):
def main():
# 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=[],
ms = dict(x=[0], y=[0], annotation=[],
name="Spectrum", xlabel="m/z", ylabel="ion count",
xtics=20, predict=None)
chrom = dict(x=[0], y=[0], line=None, t_start=None, t_end=None,
chrom = dict(x=[0], y=[0], t_start=None, t_end=None,
name="Chromatogram", xlabel="time(min)",
ylabel="total ion count", timesarg=None)
filename = [None]
......@@ -194,14 +194,21 @@ def main():
checked=config.value("view/autozoomy", type=bool))
autozoomy.triggered.connect(lambda: config.setValue(
"view/autozoomy", autozoomy.isChecked()))
autozoomy.triggered.connect(lambda: gt.autozoomy(spectrum, ms))
intensities = QtWidgets.QAction(
"&Show intensities", None, checkable=True,
checked=config.value("view/intensities", type=bool))
intensities.triggered.connect(lambda: config.setValue(
"view/intensities", intensities.isChecked()))
intensities.triggered.connect(lambda: gt.ann_spec(spectrum, ms))
intensities.triggered.connect(lambda: spectrum.figure.canvas.draw())
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: config.setValue(
"view/oddeven", oddevenact.isChecked()))
oddevenact.triggered.connect(lambda:
update_spectrum(True, chromatogram, spectrum, ds, ms,
filename, chrom, config))
filebrowseract = QtWidgets.QAction(
"&File browser", None, checkable=True,
checked=config.value("view/filebrowservisible", type=bool))
......@@ -231,7 +238,9 @@ def main():
view_menu = QtWidgets.QMenu('&View', main_window)
view_menu.addAction(filebrowseract)
view_menu.addAction(autozoomy)
view_menu.addAction(intensities)
view_menu.addAction(intensitiesact)
view_menu.addSeparator()
view_menu.addAction(oddevenact)
main_window.menuBar().addMenu(view_menu)
help_menu = QtWidgets.QMenu('&Help', main_window)
main_window.menuBar().addMenu(help_menu)
......@@ -303,7 +312,7 @@ def main():
mpl_canvas.mpl_connect('key_press_event', lambda event:
update_spectrum(event, chromatogram, spectrum, ds, ms,
update_spectrum(event.key == "f5", chromatogram, spectrum, ds, ms,
filename, chrom, config))
main_window.dragEnterEvent = lambda event: drag_entered(event)
main_window.dropEvent = lambda event: dropped(
......
......@@ -9,7 +9,8 @@ def settings():
settings = QtCore.QSettings("Yansoft","Prasopes")
defvals = {("view/autozoomy", True),
("view/filebrowservisible", True),
("view/intensities", False)}
("view/intensities", False),
("view/oddeven", False)}
[settings.setValue(i[0], i[1]) for i in defvals if settings.value(i[0]) == None]
return settings
......
......@@ -87,7 +87,7 @@ def pan_factory(axis, plot=None):
def pick_masses(x_min, x_max, ms_spec, msdata):
"""zoom the spectrum in x axis by mass range"""
ms_spec.set_xlim(x_min, x_max)
autozoomy(ms_spec, msdata)
autozoomy(ms_spec)
ann_spec(ms_spec, msdata)
......@@ -95,23 +95,43 @@ def plot_subtime(mpl_spectrum, mpl_chromatogram, data_set, mass_spect,
chrom_spect):
"""plot averaged spectrum of subselected part of the chromatogram"""
slims = [mpl_spectrum.get_xlim(), mpl_spectrum.get_ylim()]
mpl_spectrum.clear()
mass_spect['annotation'].clear()
mass_spect['x'] = data_set['masses']
mass_spect['y'] = np.mean(
data_set['matrix'][chrom_spect['timesarg']], axis=0)
pop_plot(mpl_spectrum, mass_spect)
mpl_spectrum.clear()
mpl_chromatogram.clear()
if cf.settings().value("view/oddeven", type=bool):
for i in ((mpl_spectrum, mass_spect), (mpl_chromatogram, chrom_spect)):
for j in range(2):
spect = i[1].copy()
if i[1] == mass_spect:
spect['y'] = np.mean(
data_set['matrix'][chrom_spect['timesarg'][j::2]],
axis=0)
else:
spect['x'], spect['y'] = [i[1][k[0]][k[1]::2] for k in
(('x',j),('y',j))]
pop_plot(i[0], spect)
styles = ("blue", "orange")
for i,j in enumerate(styles):
dots_x = data_set['chrom_dat'][0, chrom_spect['timesarg']][i::2]
dots_y = data_set['chrom_dat'][1, chrom_spect['timesarg']][i::2]
mpl_chromatogram.plot(dots_x, dots_y, linestyle=None,
marker='.', color=j)
else:
pop_plot(mpl_spectrum, mass_spect)
pop_plot(mpl_chromatogram, chrom_spect)
dots_x = data_set['chrom_dat'][0, chrom_spect['timesarg']]
dots_y = data_set['chrom_dat'][1, chrom_spect['timesarg']]
mpl_chromatogram.plot(dots_x, dots_y, 'b.')
mpl_spectrum.set_xlim(slims[0])
if not cf.settings().value("view/autozoomy", type=bool):
mpl_spectrum.set_ylim(slims[1])
else:
autozoomy(mpl_spectrum, mass_spect)
mpl_chromatogram.clear()
pop_plot(mpl_chromatogram, chrom_spect)
dots_x = data_set['chrom_dat'][0, chrom_spect['timesarg']]
dots_y = data_set['chrom_dat'][1, chrom_spect['timesarg']]
mpl_chromatogram.plot(dots_x, dots_y, 'b.')
autozoomy(mpl_spectrum)
ann_spec(mpl_spectrum, mass_spect)
mpl_chromatogram.get_figure().canvas.draw()
......@@ -145,13 +165,14 @@ def arrow_factory(spectrum, chromatogram, ds, mass_spect,
fig.canvas.mpl_connect('key_press_event', lambda event: arrow_fun(
event, spectrum, chromatogram, ds, mass_spect, chrom_spect))
def autozoomy(ms_spec, msdata):
def autozoomy(ms_spec):
if cf.settings().value("view/autozoomy", type=bool):
ms_spec.autoscale(True, 'y')
ymin = -0.01
argvis = dt.argsubselect(msdata['x'], *ms_spec.get_xlim())
if not np.array_equal(msdata['y'], [0]):
ymax = max(msdata['y'][argvis])
xdata = ms_spec.lines[0].get_data()[0]
argvis = dt.argsubselect(xdata, *ms_spec.get_xlim())
if not np.array_equal(xdata, [0]):
ymax = np.max([ydata.get_data()[1][argvis] for ydata in ms_spec.lines])
ms_spec.set_ylim(ymax*ymin,
ymax*1.1)
ms_spec.figure.canvas.draw()
......@@ -165,38 +186,35 @@ def ann_spec(ms_spec, msdata, ann_limit=0.01):
local maximas from the array by forcycling. Local maximas are then
reduced to a representation of the important ones by the sub_peaks
function"""
def sub_peaks(peakz, x_value, y_value, xrange, yrange, coef_x=10,
coef_y=10):
def sub_peaks(peakz, xrange, yrange, coef_x=10, coef_y=10):
"""Returns reasonable subselection of local maximas"""
sort_peaks = sorted(
peakz, key=lambda h: y_value[h], reverse=True)
subtracted_peaks = []
sort_peaks = np.sort(np.array(peakz, dtype=[
('x', float),('y', float)]), order='y')[::-1]
red_x = xrange / coef_x
red_y = yrange / coef_y
for peak in sort_peaks:
add = True
yv = y_value[peak]
xv = x_value[peak]
for u in subtracted_peaks:
if abs(yv - y_value[u]) < red_y \
and abs(xv - x_value[u]) < red_x:
add = False
if add:
subtracted_peaks.append(peak)
return subtracted_peaks
argvis = dt.argsubselect(msdata['x'], *ms_spec.get_xlim())
big_peaks = np.array([], dtype=[('x', float),('y', float)])
for peak in np.nditer(sort_peaks):
if not np.any((abs(peak['y'] - big_peaks['y']) < red_y) \
& (abs(peak['x'] - big_peaks['x']) < red_x)):
big_peaks = np.append(big_peaks,peak)
return big_peaks
xdata = ms_spec.lines[0].get_data()[0]
ydata = [j.get_data()[1] for j in ms_spec.lines]
if np.array_equal(xdata, [0]):
return
argvis = dt.argsubselect(xdata, *ms_spec.get_xlim())
#remove tails which cannot be evaluated as maximas
argvis = argvis[np.where((argvis != 0)
& (argvis != (len(msdata['x'])-1)))]
& (argvis != (len(xdata)-1)))]
lim = ms_spec.get_ylim()[1] * ann_limit
peaks = []
for i in argvis:
if msdata['y'][i] > lim and msdata['y'][i] > max(
msdata['y'][i-1], msdata['y'][i+1]):
peaks.append(i)
s_peaks = sub_peaks(peaks, msdata['x'], msdata['y'],
np.diff(ms_spec.get_xlim()),
for yline in ydata:
if yline[i] > lim and yline[i] > max(yline[i-1], yline[i+1]):
peaks.append((xdata[i],yline[i]))
s_peaks = sub_peaks(peaks, np.diff(ms_spec.get_xlim()),
np.diff(ms_spec.get_ylim()))
# delete objects from the spectra
......@@ -205,21 +223,18 @@ def ann_spec(ms_spec, msdata, ann_limit=0.01):
# remove them from tracking
msdata['annotation'].clear()
for i in s_peaks:
print(cf.settings().value("view/intensities", type=bool))
for peak in s_peaks:
if cf.settings().value("view/intensities", type=bool):
annotation = '{0:.2f}\n{1: .2e}'.format(msdata['x'][i],
msdata['y'][i])
print("ifim")
annotation = '{0:.2f}\n{1: .2e}'.format(peak['x'], peak['y'])
else:
annotation = '{0:.2f}'.format(msdata['x'][i])
print("elsim")
annotation = '{0:.2f}'.format(peak['x'])
msdata['annotation'].append(ms_spec.annotate(annotation,
xy=(msdata['x'][i], msdata['y'][i]), textcoords='data'))
xy=(peak['x'], peak['y']), textcoords='data'))
def pop_plot(plot, plot_data):
"""Define and populate plot"""
plot_data['line'] = plot.plot(plot_data['x'], plot_data['y'], linewidth=1)
plot.plot(plot_data['x'], plot_data['y'], linewidth=1)
plot.set_title(plot_data['name'], loc="right")
plot.set_xlabel(plot_data['xlabel'])
plot.set_ylabel(plot_data['ylabel'])
......@@ -245,7 +260,6 @@ def populate(mpl_chromatogram, mpl_spectrum, data_set,
mass_spect['x'] = data_set['masses']
mass_spect['y'] = np.mean(data_set['matrix'], axis=0)
pop_plot(mpl_spectrum, mass_spect)
if mass_spect['predict'] != None:
maxm = np.argmax(mass_spect['predict'][1]) + mass_spect['predict'][0]
maxseek = dt.argsubselect(mass_spect['x'], maxm-.5, maxm+.5)
......@@ -263,5 +277,17 @@ def populate(mpl_chromatogram, mpl_spectrum, data_set,
chrom_spect['y'] = data_set['chrom_dat'][1, :]
chrom_spect['t_start'] = data_set['chrom_dat'][0, 0]
chrom_spect['t_end'] = data_set['chrom_dat'][0, -1]
pop_plot(mpl_chromatogram, chrom_spect)
for i in ((mpl_spectrum, mass_spect), (mpl_chromatogram, chrom_spect)):
if cf.settings().value("view/oddeven", type=bool):
for j in range(2):
spect = i[1].copy()
if i[1] == mass_spect:
spect['y'] = np.mean(data_set['matrix'][j::2], axis=0)
else:
spect['x'], spect['y'] = [i[1][k[0]][k[1]::2] for k in
(('x',j),('y',j))]
pop_plot(i[0], spect)
else:
pop_plot(*i)
mpl_chromatogram.figure.canvas.draw()
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