Commit 202f986f authored by Yan's avatar Yan
Browse files

export function (2/2)

parent d96495d8
......@@ -7,6 +7,7 @@ from PyQt5 import QtCore
from PyQt5 import QtWidgets
#from rawFin2 import load_raw
from rawParse import load_raw
import os.path
import sys
import matplotlib
import numpy as np
......@@ -98,8 +99,10 @@ def pick_masses(x_min, x_max, ax, annotations, plot):
ax.figure.canvas.draw()
def pick_times(x_min, x_max, mpl_spectrum, data_set, mpl_chromatogram,
annotations, mass_spect):
annotations, mass_spect, t):
"""plot averaged spectrum of subselected part of the chromatogram"""
t['start'] = x_min
t['end'] = x_max
start_scan = None
end_scan = None
for i, times in enumerate(data_set['chrom_dat'][0, :]):
......@@ -138,13 +141,13 @@ def pick_times(x_min, x_max, mpl_spectrum, data_set, mpl_chromatogram,
'b.')
def populate(mpl_chromatogram, mpl_spectrum, data_set, time_sel, an,
mass_spect):
mass_spect, t):
"""populate the GUI plots with desired dataset"""
an.clear()
def update_span_selector(chrom, spec, d_set, ms_spec):
time_selector = SpanSelector(
chrom, lambda x_min, x_max: pick_times(
x_min, x_max,spec, d_set, chrom, an, ms_spec),
x_min, x_max,spec, d_set, chrom, an, ms_spec, t),
'horizontal',
useblit=True,
rectprops=dict(alpha=0.15, facecolor='purple'), button=3)
......@@ -156,6 +159,8 @@ def populate(mpl_chromatogram, mpl_spectrum, data_set, time_sel, an,
spectrum_plot(mpl_spectrum, mass_spect['x'], mass_spect['y'], an)
chrom_plot(mpl_chromatogram, data_set['chrom_dat'][0, :],
data_set['chrom_dat'][1, :])
t['start'] = data_set['chrom_dat'][0,0]
t['end'] = data_set['chrom_dat'][0,-1]
time_sel[0] = update_span_selector(mpl_chromatogram, mpl_spectrum,
data_set, mass_spect)
mpl_chromatogram.figure.canvas.draw()
......@@ -237,24 +242,47 @@ def chrom_plot(chrom, times, tic):
chrom.set_ylabel("total ion count")
chrom.ticklabel_format(scilimits=(0, 0), axis='y')
def open_file(chrom, spc, d_set, time_sel, an, ms_s):
def open_file(chrom, spc, d_set, time_sel, an, ms_s, t, fn):
"""Returns chromatogram, masses and ion intensities"""
filename=QtWidgets.QFileDialog.getOpenFileName(
caption = "Open spectrum",
filter="Finnigan RAW files (*.raw, *.RAW)")[0]
if filename is '':
return
fn = filename
d_set['chrom_dat'], d_set['masses'], d_set['matrix'] \
= load_raw(filename)
populate(chrom, spc, d_set, time_sel, an, ms_s)
populate(chrom, spc, d_set, time_sel, an, ms_s, t)
def export():
def export(mass_spec, t, filename):
"""exports the chromatogram into the .dat file format"""
filename=QtWidgets.QFileDialog.getSaveFileName(
if filename is None:
print("Nothing to export, cancelling request")
return
exp_f_name=QtWidgets.QFileDialog.getSaveFileName(
caption = "Export spectrum",
filter="dat table format (*.dat)")[0]
if filename is '':
if exp_f_name is '':
return
else:
if exp_f_name[-4:] != '.dat':
exp_f_name = exp_f_name + ".dat"
if os.path.isfile(exp_f_name):
quest = QtWidgets.QMessageBox.warning(
main_window , "Export spectrum",
"{} already exists.\n Do you want to replace it?".\
format(os.path.basename(exp_f_name)),
QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No)
if(hex(quest)) == "0x10000":
return
expf=open(exp_f_name,'w')
expf.write("mass ion_count\n"
"m/z\n"
"{}_{:.4}-{:.4}_minutes_of_the_aquisition\n".format(
filename,t['start'],times['end']))
for i in range(len(mass_spec['x'])):
expf.write("{} {}\n".format(mass_spec['x'][i],mass_spec['y'][i]))
expf.close
if __name__=="__main__":
......@@ -262,6 +290,8 @@ if __name__=="__main__":
ds = dict(chrom_dat=None,masses=None,matrix=None)
#mass spectrometry set
ms = dict(x=None,y=None)
times = dict(start=None,end=None)
filename = None
p_logger = logging.getLogger('parseLogger')
logging.basicConfig()
......@@ -300,9 +330,9 @@ if __name__=="__main__":
main_window.menuBar().addMenu(file_menu)
file_menu.addAction('&Open..',
lambda: open_file(chromatogram, spectrum, ds, timeSelector,
annotation, ms),
annotation, ms, times, filename),
QtCore.Qt.CTRL + QtCore.Qt.Key_O)
file_menu.addAction('&Export..', lambda: export(),
file_menu.addAction('&Export..', lambda: export(ms, times, filename),
QtCore.Qt.CTRL + QtCore.Qt.Key_E)
file_menu.addAction('&Quit', main_window.close,
QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
......@@ -318,9 +348,10 @@ if __name__=="__main__":
if len(sys.argv) == 2:
raw_file=sys.argv[1]
filename=raw_file
ds['chrom_dat'],ds['masses'],ds['matrix'] = load_raw(raw_file)
populate(chromatogram, spectrum, ds, timeSelector, annotation,
ms)
ms, times)
else:
spectrum_plot(spectrum, [0], [0], annotation)
chrom_plot(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