Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tdf support via timsrust_pyo3 #213

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion ms2pip/spectrum_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
from typing import Generator

import numpy as np
try:
import timsrust_pyo3 as timsrust
_has_timsrust = True
except ImportError:
_has_timsrust = False
from pyteomics import mgf, mzml

from ms2pip.exceptions import UnsupportedSpectrumFiletypeError
Expand Down Expand Up @@ -83,9 +88,33 @@ def read_mzml(spectrum_file: str) -> Generator[ObservedSpectrum, None, None]:
yield spectrum


def read_tdf(spectrum_file: str) -> Generator[ObservedSpectrum, None, None]:
"""
Read MS2 DDA spectra from .d folder.

Parameters
----------
spectrum_file
Path to .d folder.

"""
if not _has_timsrust:
raise ImportError("Optional dependency timsrust_pyo3 required for .d spectrum file support.")
reader = timsrust.TimsReader(spectrum_file)
for spectrum in reader.read_all_spectra():
spectrum = ObservedSpectrum(
identifier=spectrum.index,
mz=np.asarray(spectrum.mz_values),
intensity=np.asarray(spectrum.intensities),
precursor_mz=spectrum.precursor.mz,
precursor_charge=spectrum.precursor.charge
)
yield spectrum


def read_spectrum_file(spectrum_file: str) -> Generator[ObservedSpectrum, None, None]:
"""
Read MS2 spectra from MGF or mzML file; inferring the type from the filename extension.
Read MS2 spectra from MGF or mzML file or .d folder; inferring the type from the filename extension.

Parameters
----------
Expand All @@ -100,5 +129,8 @@ def read_spectrum_file(spectrum_file: str) -> Generator[ObservedSpectrum, None,
elif filetype == ".mgf":
for spectrum in read_mgf(spectrum_file):
yield spectrum
elif filetype == ".d":
for spectrum in read_tdf(spectrum_file):
yield spectrum
else:
raise UnsupportedSpectrumFiletypeError(filetype)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ dependencies = [

[project.optional-dependencies]
plotting = ["matplotlib>=3.0", "spectrum-utils>=0.4"]
tdf = ["timsrust_pyo3@git+https://github.com/jspaezp/timsrust_pyo3#egg=0f40c31"]
dev = ["black", "isort>5", "pytest"]
docs = [
"sphinx",
Expand Down
Loading