Skip to content

Commit

Permalink
use mz_read to parse precursor info
Browse files Browse the repository at this point in the history
  • Loading branch information
paretje committed Aug 13, 2024
1 parent a22de42 commit 86da407
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn get_precursor_info(spectrum_path: String) -> PyResult<HashMap<String, Pre

let precursors = match file_type {
SpectrumFileType::MascotGenericFormat | SpectrumFileType::MzML => {
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),
Expand Down
28 changes: 4 additions & 24 deletions src/parse_mzdata.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -51,32 +48,15 @@ impl From<mzdata::spectrum::MultiLayerSpectrum> for MS2Spectrum {
/// Parse precursor info from spectrum files with mzdata
pub fn parse_precursor_info(
spectrum_path: &str,
file_type: SpectrumFileType,
) -> Result<HashMap<String, Precursor>, 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::<HashMap<String, Precursor>>()),

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::<HashMap<String, Precursor>>()),

_ => Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Unsupported file type for mzdata",
)),
}
.collect::<HashMap<String, Precursor>>()
})
}

/// Read MS2 spectra from spectrum files with mzdata
Expand Down

0 comments on commit 86da407

Please sign in to comment.