Skip to content

Commit

Permalink
Merge pull request #213 from compomics/tdf_support2
Browse files Browse the repository at this point in the history
Add tdf support via timsrust_pyo3
  • Loading branch information
Cajac102 authored Feb 9, 2024
2 parents 37c0cc1 + e9bd5c0 commit a184674
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
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

0 comments on commit a184674

Please sign in to comment.