Commit 3566136f authored by Yan's avatar Yan
Browse files

Support for timTOF - pt1

* rewritten routines to be able to work with different input formats
* opening of spectrum works
* lots of things broken on the way. Now its time to fix and cleanup
parent 2dff3afb
......@@ -24,6 +24,7 @@ import prasopes.graphtools as gt
import prasopes.imagetools as imgt
import prasopes.zcetools as zce
import prasopes.docks as docks
import prasopes.datasets as datasets
import prasopes.tangoicons
import sys
import logging
......@@ -64,12 +65,15 @@ def show_exception_and_exit(exc_type, exc_value, tb):
def load_file(parent, augCanvas, update, settings, loadthread, filename=None,
nothreading=False):
nothreading=True):
"""populates dataset and plots it"""
directory = augCanvas.filename or settings.value("open_folder")
directory = augCanvas.ds.filename\
if not (isinstance(augCanvas.ds, list))\
else settings.value("open_folder")
filename = filename or QtWidgets.QFileDialog.getOpenFileName(
caption="Open spectrum", directory=directory,
filter="Finnigan RAW files (*.raw, *.RAW)")[0]
filter="All supported formats (*.raw *.RAW *.d);;"
"Finnigan RAW files (*.raw *.RAW);;timsTOF file (*.d)")[0]
if filename != '' and os.path.isfile(filename)\
and not os.path.isdir(filename):
error = update_signal()
......@@ -77,12 +81,9 @@ def load_file(parent, augCanvas, update, settings, loadthread, filename=None,
def runfnc():
try:
[i.clear() for i in (
augCanvas.ds, augCanvas.chrom['timesarg'],
augCanvas.ms['params'], augCanvas.ms['headers'])]
[augCanvas.ds.append(dict(
chrom_dat=i[0], masses=i[1], matrix=i[2]))
for i in load_raw(filename, settings.value("tmp_location"))]
augCanvas.ds = datasets.ThermoRawDataset(filename) if\
os.path.splitext(filename)[1] in (".raw", ".RAW")\
else datasets.BrukerTimsDataset(filename)
except rawprasslib.ParsingException as pex:
errormsg.append("Opening of the file has failed!")
errormsg.append(
......@@ -91,29 +92,10 @@ def load_file(parent, augCanvas, update, settings, loadthread, filename=None,
"Error message:\n{}".format(pex.args[0]))
error.signal.emit()
return
if autoparams:
try:
(augCanvas.ms['params'], rawheaders,
augCanvas.chrom['machtype']) = load_params(
filename, settings.value("tmp_location"))
segments = [len(subset['chrom_dat'][0])
for subset in augCanvas.ds]
indicies = [sum(segments[:i+1])
for i in range(len(segments))]
augCanvas.ms['headers'] = np.split(
rawheaders, indicies)[:-1]
except Exception as pex:
errormsg.append(
"File is incompatible with the rawautoparams,")
errormsg.append(
"no parameters loaded!\n\n"
"Error message:\n{}".format(pex.args[0]))
error.signal.emit()
gt.populate(augCanvas)
augCanvas.filename = filename
oldrecents = settings.value("recents")
oldrecents.remove(filename) if filename in oldrecents else None
settings.setValue("recents", [filename, *oldrecents])
settings.setValue("recents", [filename, *oldrecents][:10])
update.signal.emit()
error.signal.connect(lambda: QtWidgets.QMessageBox.critical(
parent, errormsg[0], errormsg[1]))
......
#!/usr/bin/env python3
from rawprasslib import load_raw
from opentimspy.opentims import OpenTIMS
try:
from rawautoparams import load_params
import rawautoparams
autoparams = True
except ImportError:
autoparams = False
import pathlib
import opentims_bruker_bridge
import numpy as np
import prasopes.config as cf
import prasopes.datatools as dt
import os.path
class Dataset():
def __init__(self, rawfile):
self.filename = rawfile
self.chromatograms = []
self.dataset = []
self.timemin = -np.inf
self.timemax = np.inf
def refresh(self):
"""implement per-case"""
return None
def get_chromatograms(self):
"""implement per-case"""
raise NotImplementedError
def get_spectra(self):
"""implement per-case"""
raise NotImplementedError
class ThermoRawDataset(Dataset):
def __init__(self, rawfile):
super().__init__(rawfile)
self.params = []
self.machtype = []
self.headers = []
self.refresh()
def refresh(self):
self.dataset = load_raw(self.filename,
cf.settings().value("tmp_location"))
self.chromatograms = self.get_chromatograms()
self.timemin, self.timemax = [self.chromatograms[i][0][i]
for i in (0,-1)]
if autoparams:
try:
self.params, rawheaders, self.machtype = load_params(
self.filename, cf.settings().value("tmp_location"))
segments = [len(i[0]) for i in self.chromatograms]
indicies = [sum(segments[:i+1]) for i,j in enumerate(segments)]
self.headers = np.split(rawheaders, indicies)[:-1]
except Exception as pex:
self.params, self.machtype, self.headers = None, None, None
def get_chromatograms(self):
if cf.settings().value("view/oddeven", type=bool):
chroms = []
for i in self.dataset:
for j in (0,1):
chroms.append([i[0][ax, :][j::2] for ax in (0,1)])
else:
chroms = [i[0] for i in self.dataset]
return chroms
def get_spectra(self):
times = dt.argsubselect(np.concatenate(
[subset[0][0] for subset in self.dataset]),
self.timemin, self.timemax)
args = []
for subset in self.dataset:
goodtimes = np.where((times < len(subset[0][0]))
& ~(times < 0))[0]
args.append(times[goodtimes])
times =- len(subset[0][0])
spectra = []
for i,subset in enumerate(self.dataset):
if cf.settings().value("view/oddeven", type=bool):
for j in (0,1):
yvalz = np.mean(subset[2][args[i][j::2]], axis=0)
spectra.append([subset[1], yvalz])
else:
yvalz = np.mean(subset[2][args[i]], axis=0)
spectra.append([subset[1], yvalz])
return spectra
class BrukerTimsDataset(Dataset):
def __init__(self, rawfile):
super().__init__(rawfile)
self.refresh()
def refresh(self):
if(os.path.isdir(self.filename)):
self.dataset = OpenTIMS(pathlib.Path(self.filename))
else:
self.dataset = OpenTIMS(pathlib.Path(os.path.dirname(self.filename)))
self.chromatograms = self.get_chromatograms()
self.timemin, self.timemax = [self.chromatograms[i][0][i]
for i in (0,-1)]
def get_chromatograms(self):
keys = ('retention_time', 'intensity')
times = self.dataset.retention_times / 60
intensities = [np.sum(i['intensity']) for i in self.dataset.query_iter(
self.dataset.ms1_frames, columns=('intensity',))]
return [[times,intensities]]
def get_spectra(self):
massints = self.dataset.rt_query(self.timemin*60, self.timemax*60,
columns=('mz', 'intensity'))
sortmasses = np.sort(massints['mz'])
masssteps = sortmasses[1:] - sortmasses[:-1]
binspos = np.where(masssteps > 0.001)[0]
bins = sortmasses[:-1][binspos] + (masssteps[binspos]/2)
binpos = np.digitize(massints['mz'], bins)
masses = np.bincount(binpos, massints['mz']) / np.bincount(binpos)
ints = np.bincount(binpos, massints['intensity']) / self.dataset.max_frame
return [[masses, ints]]
......@@ -26,6 +26,7 @@ class AugFigureCanvas(FigureCanvas):
self.figure = Figure(figsize=(5, 4), dpi=100, facecolor="None",
constrained_layout=True)
super().__init__(self.figure)
self.filename = ""
self.ds = []
self.ms = dict(annotation=[], name="Spectrum", xlabel="m/z",
ylabel="ion count", xtics=20, predict=None,
......@@ -34,7 +35,6 @@ class AugFigureCanvas(FigureCanvas):
x=[0], y=[0], t_start=None, t_end=None, machtype=None,
name="Chromatogram", xlabel="time(min)", ylabel="total ion count",
timesarg=[])
self.filename = None
self.drlcache = [None, None]
grid = self.figure.add_gridspec(2, 1)
self.chromplot = self.figure.add_subplot(grid[0, 0],
......@@ -296,7 +296,10 @@ def plot_subtime(augCanvas):
def pick_times(x_min, x_max, augCanvas):
"""subselect part of the chromatogram and plot it"""
augCanvas.chrom['t_start'] = x_min
augCanvas.ds.timemin = x_min
augCanvas.ds.timemax = x_max
populate(augCanvas)
"""augCanvas.chrom['t_start'] = x_min
augCanvas.chrom['t_end'] = x_max
times = dt.argsubselect(np.concatenate(
[subset['chrom_dat'][0] for subset in augCanvas.ds]), x_min, x_max)
......@@ -307,7 +310,7 @@ def pick_times(x_min, x_max, augCanvas):
augCanvas.chrom['timesarg'].append(times[goodtimes])
times = times - len(subset['chrom_dat'][0])
update_paramstable(augCanvas)
plot_subtime(augCanvas)
plot_subtime(augCanvas)"""
def shift_times(event, augCanvas):
......@@ -408,6 +411,7 @@ def ann_spec(ms_spec, msdata, ann_limit=0.01):
def pop_plot(xdata, ydata, plot, plot_data, colornum=0, legend=None, annotate=True):
"""Define and populate plot"""
if len(xdata):
print(colornum)
plot.plot(xdata, ydata, linewidth=1, color=(
colors[colornum % len(colors)]/255), label=legend)
plot.set_title(plot_data['name'], loc="right")
......@@ -486,14 +490,18 @@ def populate(augCanvas):
[pints.extend([np.nan, 0, i]) for i in crudeints]
augCanvas.spectplot.plot(pmasses, pints, linewidth=1)
augCanvas.chrom['t_start'] = augCanvas.ds[0]['chrom_dat'][0, 0]
augCanvas.chrom['t_end'] = augCanvas.ds[-1]['chrom_dat'][0, -1]
for i, subset in enumerate(augCanvas.ds):
if len(augCanvas.ms['headers']) == len(augCanvas.ds):
legend = legendize(augCanvas.ms['headers'][i], augCanvas.chrom)
chromxy = augCanvas.ds.get_chromatograms()
msxy = augCanvas.ds.get_spectra()
for i in range(len(msxy)):
#if len(augCanvas.ds.headers) == len(augCanvas.ds.chromatograms):
if False:
legend = legendize(augCanvas.ds.headers[i], augCanvas.chrom)
else:
legend = None
if cf.settings().value("view/oddeven", type=bool):
pop_plot(msxy[i][0], msxy[i][1], augCanvas.spectplot, augCanvas.ms, i, legend)
pop_plot(chromxy[i][0], chromxy[i][1], augCanvas.chromplot,
augCanvas.chrom, i, legend)
"""if cf.settings().value("view/oddeven", type=bool):
msx = subset['masses']
for j in range(2):
msy = np.mean(subset['matrix'][j::2], axis=0)
......@@ -510,7 +518,7 @@ def populate(augCanvas):
chromy = subset['chrom_dat'][1, :]
pop_plot(msx, msy, augCanvas.spectplot, augCanvas.ms, i, legend)
pop_plot(chromx, chromy, augCanvas.chromplot, augCanvas.chrom,
i, legend)
i, legend)"""
for ax in (augCanvas.spectplot, augCanvas.chromplot):
if not augCanvas.ms['headers'] == []:
ax.legend(loc=2)
......
#!/bin/bash
prasopes ~/roithPhD/sw_modd/msresearch/bench_batch/TSQ.raw
#!/bin/bash
prasopes ~/roithPhD/sw_modd/msresearch/opentims_spektra/210211_PEG600_TIMS_Detect_Calibrated.d/210211_PEG600_TIMS_Detect_Calibrated.d
Markdown is supported
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