Skip to content

Commit

Permalink
Update to timsrust 0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfG committed Dec 7, 2024
1 parent c02400f commit e5ad3d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ thermo = ["mzdata/thermo"]

[dependencies]
mzdata = "0.39.0"
timsrust = "0.3.0"
timsrust = "0.4.1"

[dependencies.pyo3]
version = "0.23.3"
Expand Down
51 changes: 26 additions & 25 deletions src/parse_timsrust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::collections::HashMap;
use crate::ms2_spectrum::MS2Spectrum;
use crate::precursor::Precursor;

impl From<timsrust::ms_data::Precursor> for Precursor {
fn from(precursor: timsrust::ms_data::Precursor) -> Self {
impl From<timsrust::Precursor> for Precursor {
fn from(precursor: timsrust::Precursor) -> Self {
Precursor {
mz: precursor.mz,
rt: precursor.rt,
Expand All @@ -15,8 +15,8 @@ impl From<timsrust::ms_data::Precursor> for Precursor {
}
}

impl From<timsrust::ms_data::Spectrum> for MS2Spectrum {
fn from(spectrum: timsrust::ms_data::Spectrum) -> Self {
impl From<timsrust::Spectrum> for MS2Spectrum {
fn from(spectrum: timsrust::Spectrum) -> Self {
MS2Spectrum::new(
spectrum.index.to_string(),
spectrum.mz_values.iter().map(|mz| *mz as f32).collect(),
Expand All @@ -25,7 +25,7 @@ impl From<timsrust::ms_data::Spectrum> for MS2Spectrum {
.iter()
.map(|intensity| *intensity as f32)
.collect(),
Some(Precursor::from(spectrum.precursor)),
spectrum.precursor.map(Precursor::from),
)
}
}
Expand All @@ -34,35 +34,36 @@ impl From<timsrust::ms_data::Spectrum> for MS2Spectrum {
pub fn parse_precursor_info(
spectrum_path: &str,
) -> Result<HashMap<String, Precursor>, std::io::Error> {
let reader = timsrust::FileReader::new(spectrum_path)
let reader = timsrust::readers::SpectrumReader::new(spectrum_path)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;

Ok(reader
.read_all_spectra()
let spectra = reader
.get_all()
.into_iter()
.filter(|spectrum| {
matches!(
spectrum.precursor,
timsrust::ms_data::Precursor { .. }
)
})
.map(|spectrum| {
(
spectrum.index.to_string(),
Precursor::from(spectrum.precursor),
)
.collect::<Result<Vec<_>, _>>()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;

let precursor_info = spectra
.into_iter()
.filter_map(|spectrum| match spectrum.precursor {
Some(precursor) => Some((spectrum.index.to_string(), Precursor::from(precursor))),
None => None,
})
.collect::<HashMap<String, Precursor>>())
.collect::<HashMap<_, _>>();

Ok(precursor_info)
}

/// Read MS2 spectra from spectrum files with timsrust
pub fn read_ms2_spectra(spectrum_path: &str) -> Result<Vec<MS2Spectrum>, std::io::Error> {
let reader = timsrust::FileReader::new(spectrum_path)
let reader = timsrust::readers::SpectrumReader::new(spectrum_path)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;

Ok(reader
.read_all_spectra()
let spectra = reader
.get_all()
.into_iter()
.map(MS2Spectrum::from)
.collect())
.collect::<Result<Vec<_>, _>>()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;

Ok(spectra.into_iter().map(MS2Spectrum::from).collect())
}

0 comments on commit e5ad3d6

Please sign in to comment.