Commit 5e852242 authored by Yan's avatar Yan
Browse files

update annotation during panning and zooming

parent ad6b4461
......@@ -14,8 +14,8 @@ import logging
matplotlib.use("Qt5Agg")
def zoom_factory(ax, base_scale, das=None):
def zoom_fun(event, das):
def zoom_factory(ax, base_scale, plot=None, an=None):
def zoom_fun(event, plot, an):
if event.inaxes == ax:
if event.button == 'up':
#zoom in
......@@ -38,37 +38,44 @@ def zoom_factory(ax, base_scale, das=None):
xright = ax.get_xlim()[1] - data
ax.set_xlim([data - xleft*scale_factor,
data + xright*scale_factor])
if an != None:
if type(plot['x']) != type(None):
annSpectr(event.inaxes,plot['x'], plot['y'], an)
ax.figure.canvas.draw()
fig = ax.get_figure() # get the figure of interest
# attach the call back
fig.canvas.mpl_connect('scroll_event', lambda event: zoom_fun(event, das))
fig.canvas.mpl_connect('scroll_event', lambda event: zoom_fun(event, plot, an))
# return the function
return zoom_fun
def pan_factory(ax,ds):
def pan_factory(ax, plot=None, an=None):
def pan_fun(event):
if event.button == 1 and event.inaxes == ax:
ax.start_pan(event.x, event.y, event.button)
id_drag = fig.canvas.mpl_connect('motion_notify_event', drag_fun)
id_release = fig.canvas.mpl_connect('button_release_event',
lambda action: drag_end(action, id_drag, id_release))
lambda action: drag_end(action, id_drag, id_release, plot, an))
def drag_fun(event):
ax.drag_pan(1, 'x', event.x, event.y)
ax.figure.canvas.draw()
def drag_end(event, id_drag, id_release):
def drag_end(event, id_drag, id_release, plot, an):
if event.button == 1:
fig.canvas.mpl_disconnect(id_drag)
fig.canvas.mpl_disconnect(id_release)
if an != None:
if type(plot['x']) != type(None):
annSpectr(event.inaxes,plot['x'], plot['y'], an)
ax.figure.canvas.draw()
fig = ax.get_figure()
fig.canvas.mpl_connect('button_press_event', pan_fun)
def picktimes(xmin, xmax, mplspectrum, dataset, mplchromatogram, an):
def picktimes(xmin, xmax, mplspectrum, dataset, mplchromatogram, an, ms_s):
start_scan = 0
end_scan = -1
for i, j in enumerate(dataset['chromdat'][0, :]):
......@@ -77,20 +84,26 @@ def picktimes(xmin, xmax, mplspectrum, dataset, mplchromatogram, an):
if j > xmax and end_scan == -1:
end_scan = i
mplspectrum.clear()
spectrPlot(spectrum, dataset['masses'], np.mean(dataset['matrix'][start_scan:end_scan], axis=0), an)
an.clear()
ms_s['x'] = dataset['masses']
ms_s['y'] = np.mean(dataset['matrix'][start_scan:end_scan], axis=0)
spectrPlot(spectrum, ms['x'], ms['y'], an)
mplchromatogram.clear()
chromPlot(mplchromatogram, dataset['chromdat'][0, :], dataset['chromdat'][1, :])
mplchromatogram.plot(dataset['chromdat'][0, start_scan:end_scan], dataset['chromdat'][1, start_scan:end_scan], 'b.')
def populate(mplchromatogram, mplspectrum, dataset, timeSel,an):
def updateSpanSelector(chromatogram, spectrum, dataset):
timeselec = SpanSelector(chromatogram, lambda xmin, xmax: picktimes(xmin, xmax, spectrum, dataset, chromatogram, an), 'horizontal', useblit=True, rectprops=dict(alpha=0.15, facecolor='purple'),button=3)
def populate(mplchromatogram, mplspectrum, dataset, timeSel,an, ms_s):
an.clear()
def updateSpanSelector(chromatogram, spectrum, dataset, ms_s):
timeselec = SpanSelector(chromatogram, lambda xmin, xmax: picktimes(xmin, xmax, spectrum, dataset, chromatogram, an, ms_s), 'horizontal', useblit=True, rectprops=dict(alpha=0.15, facecolor='purple'),button=3)
return timeselec
mplspectrum.clear()
mplchromatogram.clear()
spectrPlot(mplspectrum,dataset['masses'], np.mean(dataset['matrix'], axis=0),an)
ms_s['x'] = dataset['masses']
ms_s['y'] = np.mean(dataset['matrix'], axis=0)
spectrPlot(mplspectrum,ms_s['x'], ms_s['y'], an)
chromPlot(mplchromatogram, dataset['chromdat'][0,:],dataset['chromdat'][1,:])
timeSel[0] = updateSpanSelector(mplchromatogram, mplspectrum, ds)
timeSel[0] = updateSpanSelector(mplchromatogram, mplspectrum, ds, ms_s)
mplchromatogram.figure.canvas.draw()
mplspectrum.figure.canvas.draw()
......@@ -98,24 +111,33 @@ def annSpectr(spectrum,mass,intensity,an):
def sub_peaks(peakz,mass,intensity,x,y):
gp=[]
sp=[]
for g in peakz[0]:
for g in peakz:#[0]:
if gp == [] or (abs(max(intensity[gp]) - intensity[g])*10 < y and abs(mass[gp[np.argmax(intensity[gp])]] - mass[g])*20 < x ):
gp.append(g)
else:
sp.append(gp[np.argmax(intensity[gp])])
gp = [g]
if g == peakz[0][-1]:
if g == peakz[-1]:#[0][-1]:
sp.append(gp[np.argmax(intensity[gp])])
return sp
peaks = find_peaks(intensity, height=spectrum.get_ylim()[1]/100)
borders=spectrum.get_xlim()
start = np.argmax(mass>borders[0])
end = np.argmax(mass>borders[1])
if end == 0:
end = np.argmax(mass)
#peaks = find_peaks(intensity, height=spectrum.get_ylim()[1]/100)
peaks = find_peaks(intensity[start:end], height=spectrum.get_ylim()[1]/100)
peaks = peaks[0] + start
sp = sub_peaks(peaks,mass,intensity,spectrum.get_xlim()[1]-spectrum.get_xlim()[0],spectrum.get_ylim()[1]-spectrum.get_ylim()[0])
for j in an:
j.remove()
an.clear()
an = []
for i in sp:
an.append(spectrum.annotate('{0:.2f}'.format(mass[i]), xy=(mass[i],intensity[i]),textcoords='data'))
def spectrPlot(spetrum,mass,intensity, an):
spectrum.plot(mass,intensity)
spectrum.set_title("Spectrum:", loc = "right")
......@@ -133,17 +155,19 @@ def chromPlot(chromatogram,times,tic):
chromatogram.set_ylabel("total ion count")
chromatogram.ticklabel_format(scilimits=(0,0),axis='y')
def openFile(cromatogram, spectrum, ds, timeSel,an):
def openFile(cromatogram, spectrum, ds, timeSel, an, ms_s):
filename=QtWidgets.QFileDialog.getOpenFileName(caption = "Open spectrum", filter="Finnigan RAW files (*.raw, *.RAW)")[0]
if filename is '':
return
ds['chromdat'],ds['masses'],ds['matrix'] = load_raw(filename)
populate(chromatogram, spectrum, ds, timeSel,an)
populate(chromatogram, spectrum, ds, timeSel, an, ms_s)
if __name__=="__main__":
#ds for data_set
ds = dict(chromdat=None,masses=None,matrix=None)
#mass spectrometry set
ms = dict(x=None,y=None)
nlogger = logging.getLogger('parseLogger')
logging.basicConfig()
......@@ -164,17 +188,17 @@ if __name__=="__main__":
timeSelector=[None]
annotation=[]
pan_factory(chromatogram,ds)
pan_factory(chromatogram)
zoom_factory(chromatogram, 1.15)
pan_factory(spectrum,ds)
zoom_factory(spectrum, 1.15, ds)
pan_factory(spectrum, ms, annotation)
zoom_factory(spectrum, 1.15, ms, annotation)
app = QtWidgets.QApplication(sys.argv)
main_window = QtWidgets.QMainWindow()
file_menu = QtWidgets.QMenu('&File',main_window)
main_window.menuBar().addMenu(file_menu)
file_menu.addAction('&Open..', lambda: openFile(chromatogram,spectrum, ds, timeSelector,annotation), QtCore.Qt.CTRL + QtCore.Qt.Key_O)
file_menu.addAction('&Open..', lambda: openFile(chromatogram,spectrum, ds, timeSelector,annotation, ms), QtCore.Qt.CTRL + QtCore.Qt.Key_O)
file_menu.addAction('&Quit', main_window.close, QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
main_widget = QtWidgets.QWidget(main_window)
......@@ -187,7 +211,7 @@ if __name__=="__main__":
try:
rawfile=sys.argv[1]
ds['chromdat'],ds['masses'],ds['matrix'] = load_raw(rawfile)
populate(chromatogram, spectrum, ds, timeSelector,annotation)
populate(chromatogram, spectrum, ds, timeSelector,annotation, ms)
except:
spectrPlot(spectrum, [0], [0], annotation)
chromPlot(chromatogram, [0], [0])
......
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