From a22de42bc93ad8438dcbb1e54da86cc22db3646d Mon Sep 17 00:00:00 2001 From: Kevin Velghe Date: Tue, 13 Aug 2024 16:42:40 +0200 Subject: [PATCH] use mz_read to read mgfs and mzmls This add support for gziped files, without the need for us to implement it. --- Cargo.toml | 1 + src/lib.rs | 2 +- src/parse_mzdata.rs | 21 +++++---------------- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d7fa121..f34b2b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ crate-type = ["cdylib"] pyo3 = "0.20.0" mzdata = "0.20.0" timsrust = "0.3.0" +mzpeaks = "0.17.0" diff --git a/src/lib.rs b/src/lib.rs index 61ee4d8..b78771b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,7 +40,7 @@ pub fn get_ms2_spectra(spectrum_path: String) -> PyResult { - parse_mzdata::read_ms2_spectra(&spectrum_path, file_type) + parse_mzdata::read_ms2_spectra(&spectrum_path) } SpectrumFileType::BrukerRaw => parse_timsrust::read_ms2_spectra(&spectrum_path), // SpectrumFileType::ThermoRaw => parse_with_mzdata_thermo(&spectrum_path, file_type), diff --git a/src/parse_mzdata.rs b/src/parse_mzdata.rs index e11c805..5717d1a 100644 --- a/src/parse_mzdata.rs +++ b/src/parse_mzdata.rs @@ -3,6 +3,7 @@ use std::fs::File; use mzdata::io::{MGFReader, MzMLReader}; use mzdata::params::ParamValue; +use mzdata::mz_read; use crate::file_types::SpectrumFileType; use crate::ms2_spectrum::MS2Spectrum; @@ -81,24 +82,12 @@ pub fn parse_precursor_info( /// Read MS2 spectra from spectrum files with mzdata pub fn read_ms2_spectra( spectrum_path: &str, - file_type: SpectrumFileType, ) -> Result, std::io::Error> { - let file = File::open(spectrum_path)?; - match file_type { - SpectrumFileType::MascotGenericFormat => Ok(MGFReader::new(file) - .map(MS2Spectrum::from) - .collect::>()), - - SpectrumFileType::MzML => Ok(MzMLReader::new(file) - .filter(|spectrum| spectrum.description.ms_level == 2) + mz_read!(spectrum_path.as_ref(), reader => { + reader.filter(|spectrum| spectrum.description.ms_level == 2) .map(MS2Spectrum::from) - .collect::>()), - - _ => Err(std::io::Error::new( - std::io::ErrorKind::InvalidInput, - "Unsupported file type for mzdata", - )), - } + .collect::>() + }) } // pub fn parse_precursor_info_thermo(