Commit 2a0f8818 authored by 3Yan's avatar 3Yan Committed by Yan
Browse files

Parameters change support - pt1

* annotation, plotting of parts of chromatogram and zooming seems to work
* odd/even is partially broken
* arrow moving of chromatogram sector seems broken
* drl definitely broken hard
* zce probably broken
* almost not tested
parent 86db23e4
......@@ -43,8 +43,9 @@ def load_file(parent, chrom_plot, spc, d_set, ms_s, fn, chrom_s,
filter="Finnigan RAW files (*.raw, *.RAW)")[0]
if filename is not '' and os.path.isfile(filename):
try:
d_set['chrom_dat'], d_set['masses'], d_set['matrix'] \
= load_raw(filename, settings.value("tmp_location"))
d_set.clear()
[d_set.append(dict(chrom_dat=i[0], masses=i[1], matrix=i[2])) for i
in load_raw(filename, settings.value("tmp_location"))]
except rawprasslib.ParsingException as pex:
QtWidgets.QMessageBox.critical(
parent, "Opening of the file has failed!",
......@@ -80,8 +81,9 @@ def print_graph(data_set, mass_spec, chrom_spec, spect, fn):
def update_spectrum(chromatogram, spect, ds, ms, fn, chrom, config):
if fn[0] is not None:
slims = [spect.get_xlim(), spect.get_ylim()]
ds['chrom_dat'], ds['masses'], ds['matrix'] \
= load_raw(fn[0], config.value("tmp_location"))
ds.clear()
[ds.append(dict(chrom_dat=i[0], masses=i[1], matrix=i[2])) for i
in 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])
......@@ -115,6 +117,12 @@ def predictmz(form, chromatogram, spect, ds, ms, chrom):
spect.get_figure().canvas.draw()
def oddeven_changed(chromatogram, spectrum, ds, ms, filename, chrom, config,
oddevenact):
config.setValue("view/oddeven", oddevenact.isChecked())
update_spectrum(chromatogram, spectrum, ds, ms, filename, chrom, config)
def key_pressed(event, chrom, spect, ds, ms_ds, fn, chrom_ds, config):
if event.key() == QtCore.Qt.Key_F5:
update_spectrum(chrom, spect, ds, ms_ds, fn, chrom_ds, config)
......@@ -132,14 +140,12 @@ 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], annotation=[],
name="Spectrum", xlabel="m/z", ylabel="ion count",
xtics=20, predict=None)
ds = []
ms = dict(annotation=[], name="Spectrum", xlabel="m/z",
ylabel="ion count", xtics=20, predict=None)
chrom = dict(x=[0], y=[0], t_start=None, t_end=None,
name="Chromatogram", xlabel="time(min)",
ylabel="total ion count", timesarg=None)
ylabel="total ion count", timesarg=[])
filename = [None]
drlcache = [None, None]
update = update_signal()
......@@ -236,11 +242,9 @@ def main():
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(chromatogram, spectrum, ds, ms, filename, chrom,
config))
oddeven_changed(chromatogram, spectrum, ds, ms, filename, chrom,
config, oddevenact))
filebrowseract = QtWidgets.QAction(
"&File browser", None, checkable=True,
checked=config.value("view/filebrowservisible", type=bool))
......@@ -357,8 +361,8 @@ def main():
load_file(main_window, chromatogram, spectrum, ds, ms,
filename, chrom, update, config, filename=sys.argv[1])
else:
gt.pop_plot(spectrum, ms)
gt.pop_plot(chromatogram, chrom)
gt.pop_plot(0, 0, spectrum, ms)
gt.pop_plot(0, 0, chromatogram, chrom)
main_window.show()
sys.exit(app.exec_())
......
......@@ -9,5 +9,5 @@ def argsubselect(array, minimum, maximum):
args = np.where((array >= bounds[0]) & (array <= bounds[1]))[0]
if len(args) == 0:
aver = (minimum+maximum)/2
args = [(np.abs(array - aver)).argmin()]
args = np.array([(np.abs(array - aver)).argmin()])
return args
......@@ -136,10 +136,6 @@ def update_drlspectrum(ds, drls, drlspectrum):
# Do not do anything when the data set is not populated.
if isinstance(ds['masses'], type(None)):
return
colors = np.array([[0, 0, 0], [255, 0, 0], [0, 255, 0], [0, 0, 255],
[0, 200, 255], [255, 200, 0], [255, 100, 0],
[200, 50, 0], [255, 0, 200], [0, 100, 0],
[0, 100, 255], [100, 100, 100]])
names, times, intensities = get_daughterset(ds, drls)
drlspectrum.clear()
......@@ -155,7 +151,7 @@ def update_drlspectrum(ds, drls, drlspectrum):
if drls['dt'].cellWidget(row, 0).checkState() == 2:
drls['dt'].blockSignals(True)
drls['dt'].item(row, 0).setBackground(QtGui.QBrush(
QtGui.QColor(*colors[row % len(colors)], alpha=50)))
QtGui.QColor(*gt.colors[row % len(gt.colors)], alpha=50)))
drls['dt'].blockSignals(False)
label = " {}".format(drls['pt'].item(row, 0).text())
intensity = intensities[i]
......@@ -165,7 +161,7 @@ def update_drlspectrum(ds, drls, drlspectrum):
intensities, 0), np.nextafter(
0, 1, dtype=np.float32), None), dtype=np.float64)
drlspectrum.plot(times, intensity, label=label,
color=(colors[row % len(colors)] / 255))
color=(gt.colors[row % len(gt.colors)] / 255))
i += 1
else:
drls['dt'].item(row, 0).setBackground(QtGui.QBrush())
......
......@@ -5,6 +5,12 @@ from PyQt5 import QtWidgets
from PyQt5 import QtCore
colors = np.array([[0, 0, 0], [255, 0, 0], [0, 255, 0], [0, 0, 255],
[0, 200, 255], [255, 200, 0], [255, 100, 0],
[200, 50, 0], [255, 0, 200], [0, 100, 0],
[0, 100, 255], [100, 100, 100]])
def zoom_factory(axis, base_scale, plot_data=None):
"""returns zooming functionality to axis"""
def zoom_fun(event, pd, ax, scale):
......@@ -93,48 +99,59 @@ def pick_masses(x_min, x_max, ms_spec, msdata):
ann_spec(ms_spec, msdata)
def plot_subtime(mpl_spectrum, mpl_chromatogram, data_set, mass_spect,
chrom_spect):
def plot_subtime(mpl_spectrum, mpl_chromatogram, ds, ms_data, chrom_data):
"""plot averaged spectrum of subselected part of the chromatogram"""
slims = [mpl_spectrum.get_xlim(), mpl_spectrum.get_ylim()]
mass_spect['annotation'].clear()
mass_spect['x'] = data_set['masses']
mass_spect['y'] = np.mean(
data_set['matrix'][chrom_spect['timesarg']], axis=0)
chlims = [mpl_chromatogram.get_xlim(), mpl_chromatogram.get_ylim()]
ms_data['annotation'].clear()
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 i,subset in enumerate(ds):
if cf.settings().value("view/oddeven", type=bool):
for i in range(2):
msx = subset['masses'][i::2]
msy = np.mean(subset['matrix'], axis=0)[i::2]
print("panic")
"""for i in ((mpl_spectrum, ms_data), (mpl_chromatogram, chrom_data)):
for j in range(2):
spect = i[1].copy()
if i[1] == mass_spect:
if i[1] == ms_data:
spect['y'] = np.mean(
data_set['matrix'][chrom_spect['timesarg'][j::2]],
ds['matrix'][chrom_data['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]
styles = ("blue", "orange")
for i,j in enumerate(styles):
dots_x = ds['chrom_dat'][0, chrom_data['timesarg']][i::2]
dots_y = ds['chrom_dat'][1, chrom_data['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.')
marker='.', color=j)"""
else:
chromx = subset['chrom_dat'][0, :]
chromy = subset['chrom_dat'][1, :]
pop_plot(chromx, chromy, mpl_chromatogram, chrom_data, colornum=i)
if not np.array_equal(chrom_data['timesarg'][i], []):
ms_x = subset['masses']
ms_y = np.mean(subset['matrix'][chrom_data['timesarg'][i]],
axis=0)
pop_plot(ms_x, ms_y, mpl_spectrum, ms_data, colornum=i)
dots_x = subset['chrom_dat'][0, chrom_data['timesarg'][i]]
dots_y = subset['chrom_dat'][1, chrom_data['timesarg'][i]]
mpl_chromatogram.plot(dots_x, dots_y, '.', color=(
colors[i % len(colors)]/255))
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)
ann_spec(mpl_spectrum, mass_spect)
ann_spec(mpl_spectrum, ms_data)
mpl_chromatogram.set_ylim(chlims[1])
mpl_chromatogram.set_xlim(chlims[0])
mpl_chromatogram.get_figure().canvas.draw()
......@@ -143,8 +160,13 @@ def pick_times(x_min, x_max, mpl_spectrum, data_set, mpl_chromatogram,
"""subselect part of the chromatogram and plot it"""
chrom_spect['t_start'] = x_min
chrom_spect['t_end'] = x_max
times = data_set['chrom_dat'][0, :]
chrom_spect['timesarg'] = dt.argsubselect(times, x_min, x_max)
times = dt.argsubselect(np.concatenate(
[subset['chrom_dat'][0] for subset in data_set]), x_min, x_max)
chrom_spect['timesarg'].clear()
for subset in data_set:
goodtimes = np.where((times < len(subset['chrom_dat'][0])) & ~(times < 0))[0]
chrom_spect['timesarg'].append(times[goodtimes])
times = times - len(subset['chrom_dat'][0])
plot_subtime(mpl_spectrum, mpl_chromatogram, data_set, mass_spect,
chrom_spect)
......@@ -168,17 +190,16 @@ def arrow_factory(spectrum, chromatogram, ds, mass_spect,
event, spectrum, chromatogram, ds, mass_spect, chrom_spect))
def autozoomy(ms_spec):
if cf.settings().value("view/autozoomy", type=bool):
if cf.settings().value("view/autozoomy", type=bool) and not (
np.array_equal(ms_spec.lines[0].get_xdata(), [0]) and
len(ms_spec.lines) == 1):
ms_spec.autoscale(True, 'y')
ymin = -0.01
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()
gap = 0.01
ymax = np.max([np.max(line.get_data()[1][dt.argsubselect(
line.get_data()[0], *ms_spec.get_xlim())])
for line in ms_spec.lines])
ms_spec.set_ylim(-ymax*gap, ymax*1.1)
ms_spec.figure.canvas.draw()
def ann_spec(ms_spec, msdata, ann_limit=0.01):
"""annotate spectrum
......@@ -196,27 +217,23 @@ def ann_spec(ms_spec, msdata, ann_limit=0.01):
red_x = xrange / coef_x
red_y = yrange / coef_y
big_peaks = np.array([], dtype=[('x', float),('y', float)])
for peak in np.nditer(sort_peaks):
for peak in np.nditer(sort_peaks, flags=["zerosize_ok"]):
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[-1].get_data()[0]
ydata = [j.get_data()[1] for j in ms_spec.lines\
if len(j.get_data()[1]) == len(xdata)]
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(xdata)-1)))]
lim = ms_spec.get_ylim()[1] * ann_limit
peaks = []
for i in argvis:
for yline in ydata:
if yline[i] > lim and yline[i] > max(yline[i-1], yline[i+1]):
peaks.append((xdata[i],yline[i]))
for line in ms_spec.lines:
xdata, ydata = line.get_data()
argvis = dt.argsubselect(xdata, *ms_spec.get_xlim())
#remove tails which cannot be evaluated as maximas
argvis = argvis[np.where((argvis != 0)
& (argvis != (len(xdata)-1)))]
lim = ms_spec.get_ylim()[1] * ann_limit
for i in argvis:
if ydata[i] > lim and ydata[i] > max(ydata[i-1], ydata[i+1]):
peaks.append((xdata[i],ydata[i]))
s_peaks = sub_peaks(peaks, np.diff(ms_spec.get_xlim()),
np.diff(ms_spec.get_ylim()))
......@@ -235,9 +252,10 @@ def ann_spec(ms_spec, msdata, ann_limit=0.01):
xy=(peak['x'], peak['y']), textcoords='data'))
def pop_plot(plot, plot_data):
def pop_plot(xdata, ydata, plot, plot_data, colornum=0):
"""Define and populate plot"""
plot.plot(plot_data['x'], plot_data['y'], linewidth=1)
plot.plot(xdata, ydata, linewidth=1, color=
(colors[colornum % len(colors)]/255))
plot.set_title(plot_data['name'], loc="right")
plot.set_xlabel(plot_data['xlabel'])
plot.set_ylabel(plot_data['ylabel'])
......@@ -253,45 +271,46 @@ def pop_plot(plot, plot_data):
def populate(mpl_chromatogram, mpl_spectrum, data_set,
mass_spect, chrom_spect):
ms_data, chrom_data):
"""populate the GUI plots with desired dataset"""
if isinstance(data_set['masses'], type(None)):
if np.array_equal(data_set, []):
return
mass_spect['annotation'].clear()
ms_data['annotation'].clear()
mpl_spectrum.clear()
mpl_chromatogram.clear()
mass_spect['x'] = data_set['masses']
mass_spect['y'] = np.mean(data_set['matrix'], axis=0)
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)
maxpos = maxseek[np.argmax(mass_spect['y'][maxseek])]
crudeints = mass_spect['predict'][1] * mass_spect['y'][maxpos]
crudemasses = (np.arange(len(mass_spect['predict'][1])) +
mass_spect['x'][maxpos])
if ms_data['predict'] != None:
maxm = np.argmax(ms_data['predict'][1]) + ms_data['predict'][0]
maxseek = dt.argsubselect(linex, maxm-.5, maxm+.5)
maxpos = maxseek[np.argmax(liney[maxseek])]
crudeints = ms_data['predict'][1] * ms_data['y'][maxpos]
crudemasses = (np.arange(len(ms_data['predict'][1])) +
linex[maxpos])
pmasses, pints = [], []
[pmasses.extend([np.nan,i,i]) for i in crudemasses]
[pints.extend([np.nan,0,i]) for i in crudeints]
mpl_spectrum.plot(pmasses,pints, linewidth=1)
mpl_spectrum.figure.canvas.draw()
chrom_spect['x'] = data_set['chrom_dat'][0, :]
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]
for i in ((mpl_spectrum, mass_spect), (mpl_chromatogram, chrom_spect)):
chrom_data['t_start'] = data_set[0]['chrom_dat'][0, 0]
chrom_data['t_end'] = data_set[-1]['chrom_dat'][0, -1]
for i,subset in enumerate(data_set):
if cf.settings().value("view/oddeven", type=bool):
msx = subset['masses']
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)
autozoomy(mpl_spectrum)
msy = np.mean(subset['matrix'][j::2], axis=0)
chromx = subset['chrom_dat'][0, :][j::2]
chromy = subset['chrom_dat'][1, :][j::2]
pop_plot(msx, msy, mpl_spectrum, ms_data, i*2+j)
pop_plot(chromx, chromy, mpl_chromatogram, chrom_data, i*2+j)
else:
pop_plot(*i)
mpl_chromatogram.figure.canvas.draw()
msx = subset['masses']
msy = np.mean(subset['matrix'], axis=0)
chromx = subset['chrom_dat'][0, :]
chromy = subset['chrom_dat'][1, :]
pop_plot(msx, msy, mpl_spectrum, ms_data, i)
pop_plot(chromx, chromy, mpl_chromatogram, chrom_data, i)
for ax in (mpl_spectrum, mpl_chromatogram):
ax.autoscale(True)
ax.set_ylim(ax.get_ylim()[1]*-0.01, ax.get_ylim()[1]*1.1)
ax.figure.canvas.draw()
return
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