From 86da407d889c37f4b3e0aa564d7040600e70d45d Mon Sep 17 00:00:00 2001 From: Kevin Velghe Date: Tue, 13 Aug 2024 19:03:28 +0200 Subject: [PATCH] use mz_read to parse precursor info --- src/lib.rs | 2 +- src/parse_mzdata.rs | 28 ++++------------------------ 2 files changed, 5 insertions(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b78771b..f8bc25d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,7 @@ pub fn get_precursor_info(spectrum_path: String) -> PyResult { - parse_mzdata::parse_precursor_info(&spectrum_path, file_type) + parse_mzdata::parse_precursor_info(&spectrum_path) } SpectrumFileType::BrukerRaw => parse_timsrust::parse_precursor_info(&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 5717d1a..ded832d 100644 --- a/src/parse_mzdata.rs +++ b/src/parse_mzdata.rs @@ -1,11 +1,8 @@ use std::collections::HashMap; -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; use crate::precursor::Precursor; @@ -51,32 +48,15 @@ impl From for MS2Spectrum { /// Parse precursor info from spectrum files with mzdata pub fn parse_precursor_info( 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) - .filter_map(|spectrum| { - spectrum.description.precursor.as_ref()?; - Some((spectrum.description.id.clone(), Precursor::from(&spectrum))) - }) - .collect::>()), - - SpectrumFileType::MzML => Ok(MzMLReader::new(file) + mz_read!(spectrum_path.as_ref(), reader => { + reader.filter(|spectrum| spectrum.description.ms_level == 2) .filter_map(|spectrum| { - if spectrum.description.ms_level != 2 { - return None; - } spectrum.description.precursor.as_ref()?; Some((spectrum.description.id.clone(), Precursor::from(&spectrum))) }) - .collect::>()), - - _ => Err(std::io::Error::new( - std::io::ErrorKind::InvalidInput, - "Unsupported file type for mzdata", - )), - } + .collect::>() + }) } /// Read MS2 spectra from spectrum files with mzdata