Commit d42e1cf4 authored by Yan's avatar Yan
Browse files

Created project tree

parent 40f00d32
#!/usr/bin/env python3
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.widgets import SpanSelector
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from rawParse import load_raw
import graph_tools as gt
import file_tools as ft
import os.path
import sys
import matplotlib
import numpy as np
import logging
matplotlib.use("Qt5Agg")
if __name__ == "__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=[],
name="Spectrum", xlabel="m/z", ylabel="ion count")
chrom = dict(x=[0], y=[0], line=None, t_start=None, t_end=None,
name="Chromatogram", xlabel="time(min)",
ylabel="total ion count")
filename = [None]
p_logger = logging.getLogger('parseLogger')
logging.basicConfig()
p_logger.setLevel("WARN")
graph = Figure(figsize=(5, 4), dpi=100)
graph.patch.set_facecolor("None")
chromatogram = graph.add_subplot(211, facecolor=(1, 1, 1, 0.8))
spectrum = graph.add_subplot(212, facecolor=(1, 1, 1, 0.8))
graph.tight_layout()
mpl_canvas = FigureCanvas(graph)
mpl_canvas.setStyleSheet("background-color:transparent;")
mpl_canvas.setAutoFillBackground(False)
mpl_canvas.setFocusPolicy(QtCore.Qt.ClickFocus)
gt.pan_factory(chromatogram)
gt.zoom_factory(chromatogram, 1.15)
gt.pan_factory(spectrum, ms)
gt.zoom_factory(spectrum, 1.15, ms)
gt.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(
chromatogram, lambda x_min, x_max: gt.pick_times(
x_min, x_max, spectrum, ds, chromatogram, ms, chrom),
'horizontal', useblit=True, rectprops=dict(
alpha=0.15, facecolor='purple'), button=3)
app = QtWidgets.QApplication(sys.argv)
main_window = QtWidgets.QMainWindow()
main_window.setWindowTitle("Prasopes")
file_menu = QtWidgets.QMenu('&File', main_window)
main_window.menuBar().addMenu(file_menu)
file_menu.addAction('&Open..', lambda: ft.open_file(
chromatogram, spectrum, ds, ms, filename, chrom),
QtCore.Qt.CTRL + QtCore.Qt.Key_O)
file_menu.addAction('&Export..', lambda: ft.export(
ms, chrom, filename), QtCore.Qt.CTRL + QtCore.Qt.Key_E)
file_menu.addAction('&Quit', main_window.close,
QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
main_widget = QtWidgets.QWidget(main_window)
main_window.setCentralWidget(main_widget)
layout = QtWidgets.QVBoxLayout(main_widget)
layout.addWidget(mpl_canvas)
mpl_canvas.setFocus()
if len(sys.argv) == 2:
raw_file = sys.argv[1]
filename[0] = raw_file
ds['chrom_dat'], ds['masses'], ds['matrix'] = load_raw(raw_file)
gt.populate(chromatogram, spectrum, ds, ms, chrom)
else:
gt.pop_plot(spectrum, ms)
gt.pop_plot(chromatogram, chrom)
main_window.show()
sys.exit(app.exec_())
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from rawParse import load_raw
import os.path
import logging
import graph_tools as gt
def open_file(chrom_plot, spc, d_set, ms_s, fn, chrom_s):
"""Returns chromatogram, masses and ion intensities"""
fn[0] = QtWidgets.QFileDialog.getOpenFileName(
caption="Open spectrum",
filter="Finnigan RAW files (*.raw, *.RAW)")[0]
if fn[0] is not'':
d_set['chrom_dat'], d_set['masses'], d_set['matrix'] \
= load_raw(fn[0])
gt.populate(chrom_plot, spc, d_set, ms_s, chrom_s)
def export(mass_spec, chrom_spec, fn):
"""exports the chromatogram into the .dat file format"""
if fn[0] is None:
QtWidgets.QMessageBox.warning(
main_window, "Export spectrum",
"Nothing to export, cancelling request")
return
exp_f_name = QtWidgets.QFileDialog.getSaveFileName(
caption="Export spectrum",
filter="dat table format (*.dat)")[0]
if exp_f_name is not '':
# fix for Qt5 "Feature"
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(
os.path.basename(fn[0]),
chrom_spec['t_start'], chrom_spec['t_end']))
for i in range(len(mass_spec['x'])):
expf.write("{} {}\n".format(
mass_spec['x'][i], mass_spec['y'][i]))
expf.close()
#!/usr/bin/env python3
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.widgets import SpanSelector
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from rawParse import load_raw
import os.path
import sys
import matplotlib
import numpy as np
import logging
matplotlib.use("Qt5Agg")
import file_tools as ft
def zoom_factory(axis, base_scale, plot_data=dict()):
"""returns zooming functionality to axis"""
......@@ -141,27 +131,6 @@ def pick_times(x_min, x_max, mpl_spectrum, data_set, mpl_chromatogram,
dots_y = data_set['chrom_dat'][1, start_scan:end_scan]
mpl_chromatogram.plot(dots_x, dots_y, 'b.')
def populate(mpl_chromatogram, mpl_spectrum, data_set,
mass_spect, chrom_spect):
"""populate the GUI plots with desired dataset"""
mass_spect['annotation'].clear()
mpl_spectrum.clear()
mpl_chromatogram.clear()
mass_spect['x'] = data_set['masses']
mass_spect['y'] = np.mean(data_set['matrix'], axis=0)
pop_plot(mpl_spectrum, mass_spect)
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]
pop_plot(mpl_chromatogram, chrom_spect)
mpl_chromatogram.figure.canvas.draw()
def ann_spec(ms_spec, msdata, ann_limit=0.01):
"""annotate spectrum
......@@ -220,7 +189,6 @@ def ann_spec(ms_spec, msdata, ann_limit=0.01):
'{0:.2f}'.format(msdata['x'][i]), xy=(msdata['x'][i], msdata['y'][i]),
textcoords='data'))
def pop_plot(plot, plot_data):
"""Define and populate plot"""
plot_data['line'] = plot.plot(plot_data['x'], plot_data['y'])
......@@ -233,121 +201,21 @@ def pop_plot(plot, plot_data):
if "annotation" in plot_data:
ann_spec(plot, plot_data)
def populate(mpl_chromatogram, mpl_spectrum, data_set,
mass_spect, chrom_spect):
"""populate the GUI plots with desired dataset"""
mass_spect['annotation'].clear()
mpl_spectrum.clear()
mpl_chromatogram.clear()
def open_file(chrom_plot, spc, d_set, ms_s, fn, chrom_s):
"""Returns chromatogram, masses and ion intensities"""
fn[0] = QtWidgets.QFileDialog.getOpenFileName(
caption="Open spectrum",
filter="Finnigan RAW files (*.raw, *.RAW)")[0]
if fn[0] is not'':
d_set['chrom_dat'], d_set['masses'], d_set['matrix'] \
= load_raw(fn[0])
populate(chrom_plot, spc, d_set, ms_s, chrom_s)
def export(mass_spec, chrom_spec, fn):
"""exports the chromatogram into the .dat file format"""
if fn[0] is None:
QtWidgets.QMessageBox.warning(
main_window, "Export spectrum",
"Nothing to export, cancelling request")
return
exp_f_name = QtWidgets.QFileDialog.getSaveFileName(
caption="Export spectrum",
filter="dat table format (*.dat)")[0]
if exp_f_name is not '':
# fix for Qt5 "Feature"
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(
os.path.basename(fn[0]),
chrom_spec['t_start'], chrom_spec['t_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__":
# 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=[],
name="Spectrum", xlabel="m/z", ylabel="ion count")
chrom = dict(x=[0], y=[0], line=None, t_start=None, t_end=None,
name="Chromatogram", xlabel="time(min)",
ylabel="total ion count")
filename = [None]
p_logger = logging.getLogger('parseLogger')
logging.basicConfig()
p_logger.setLevel("WARN")
graph = Figure(figsize=(5, 4), dpi=100)
graph.patch.set_facecolor("None")
chromatogram = graph.add_subplot(211, facecolor=(1, 1, 1, 0.8))
spectrum = graph.add_subplot(212, facecolor=(1, 1, 1, 0.8))
graph.tight_layout()
mpl_canvas = FigureCanvas(graph)
mpl_canvas.setStyleSheet("background-color:transparent;")
mpl_canvas.setAutoFillBackground(False)
mpl_canvas.setFocusPolicy(QtCore.Qt.ClickFocus)
pan_factory(chromatogram)
zoom_factory(chromatogram, 1.15)
pan_factory(spectrum, ms)
zoom_factory(spectrum, 1.15, ms)
mass_selector = SpanSelector(
spectrum, lambda x_min, x_max: pick_masses(
x_min, x_max, spectrum, ms),
'horizontal', useblit=True, rectprops=dict(
alpha=0.15, facecolor='purple'), button=3)
time_selector = SpanSelector(
chromatogram, lambda x_min, x_max: pick_times(
x_min, x_max, spectrum, ds, chromatogram, ms, chrom),
'horizontal', useblit=True, rectprops=dict(
alpha=0.15, facecolor='purple'), button=3)
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: open_file(
chromatogram, spectrum, ds, ms, filename, chrom),
QtCore.Qt.CTRL + QtCore.Qt.Key_O)
file_menu.addAction('&Export..', lambda: export(
ms, chrom, filename), QtCore.Qt.CTRL + QtCore.Qt.Key_E)
file_menu.addAction('&Quit', main_window.close,
QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
main_widget = QtWidgets.QWidget(main_window)
main_window.setCentralWidget(main_widget)
layout = QtWidgets.QVBoxLayout(main_widget)
layout.addWidget(mpl_canvas)
mpl_canvas.setFocus()
if len(sys.argv) == 2:
raw_file = sys.argv[1]
filename[0] = raw_file
ds['chrom_dat'], ds['masses'], ds['matrix'] = load_raw(raw_file)
populate(chromatogram, spectrum, ds, ms, chrom)
else:
pop_plot(spectrum, ms)
pop_plot(chromatogram, chrom)
mass_spect['x'] = data_set['masses']
mass_spect['y'] = np.mean(data_set['matrix'], axis=0)
pop_plot(mpl_spectrum, mass_spect)
mpl_spectrum.figure.canvas.draw()
main_window.show()
sys.exit(app.exec_())
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]
pop_plot(mpl_chromatogram, chrom_spect)
mpl_chromatogram.figure.canvas.draw()
from setuptools import setup
with open("README.md", "r") as fh:
long_description = fh.read()
setup(
name="prasopes",
version="0.0.1",
author="Jan Zelenka",
author_email="3yanyanyan@gmail.com",
description="Thermo/Finnigan .raw file viewer based on rawprasslib",
long_description=long_description,
url="https://gitlab.science.ru.nl/jzelenka/prasopes",
packages=['prasopes'],
classifiers=[
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
"Development Status :: 3-Alpha",
"Intended Audience :: Science/Research",
"Topic :: Scientific/Engineering :: Chemistry",
],
install_requires=['numpy', 'PyQt5-sip', 'matplotlib', 'rawprasslib']
)
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