Skip to content

Commit 3e51c2d

Browse files
authored
Merge pull request #5 from di-hardt/fix-handling-empty-spectra
handling empty spectra
2 parents 78dbe83 + 38efa20 commit 3e51c2d

1 file changed

Lines changed: 47 additions & 10 deletions

File tree

src/parse_timsrust.rs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::collections::HashMap;
22

3+
use timsrust::readers::SpectrumReaderError;
4+
35
use crate::ms2_spectrum::MS2Spectrum;
46
use crate::precursor::Precursor;
57

@@ -30,18 +32,46 @@ impl From<timsrust::Spectrum> for MS2Spectrum {
3032
}
3133
}
3234

35+
/// Handles errors from the spectrum reader by converting each error into std::io::Error except for
36+
/// the `Decompression`` fails error.
37+
///
38+
/// # Arguments
39+
/// * `err` - The error to handle
40+
///
41+
fn handle_spectrum_reader_error(err: SpectrumReaderError) -> Result<(), std::io::Error> {
42+
// Unfortunately, the timsrust library does not make all errors publicly available yet,
43+
// so we have to convert them to string for comparison. As soon as the development branch of
44+
// timsrust is merged into the main branch, we can use the "No binary data"
45+
// (TdfBlobReaderError::EmptyData) error to be more specific.
46+
if err.to_string() == "Decompression fails" {
47+
return Ok(());
48+
}
49+
50+
Err(std::io::Error::other(format!(
51+
"SpectrumReaderError: {}",
52+
err
53+
)))
54+
}
55+
3356
/// Parse precursor info from spectrum files with timsrust
3457
pub fn parse_precursor_info(
3558
spectrum_path: &str,
3659
) -> Result<HashMap<String, Precursor>, std::io::Error> {
3760
let reader = timsrust::readers::SpectrumReader::new(spectrum_path)
38-
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
61+
.map_err(|e| std::io::Error::other(e.to_string()))?;
3962

40-
let spectra = reader
41-
.get_all()
63+
let spectra = (0..reader.len())
64+
.map(|index| match reader.get(index) {
65+
Ok(spectrum) => Ok(Some(spectrum)),
66+
Err(err) => handle_spectrum_reader_error(err).map(|_| None),
67+
})
68+
// resolve errors
69+
.collect::<Result<Vec<Option<timsrust::Spectrum>>, std::io::Error>>()
70+
.map_err(|e| std::io::Error::other(e.to_string()))?
71+
// remove None values
4272
.into_iter()
43-
.collect::<Result<Vec<_>, _>>()
44-
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
73+
.flatten()
74+
.collect::<Vec<_>>();
4575

4676
let precursor_info = spectra
4777
.into_iter()
@@ -57,13 +87,20 @@ pub fn parse_precursor_info(
5787
/// Read MS2 spectra from spectrum files with timsrust
5888
pub fn read_ms2_spectra(spectrum_path: &str) -> Result<Vec<MS2Spectrum>, std::io::Error> {
5989
let reader = timsrust::readers::SpectrumReader::new(spectrum_path)
60-
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
90+
.map_err(|e| std::io::Error::other(e.to_string()))?;
6191

62-
let spectra = reader
63-
.get_all()
92+
let spectra = (0..reader.len())
93+
.map(|index| match reader.get(index) {
94+
Ok(spectrum) => Ok(Some(spectrum)),
95+
Err(err) => handle_spectrum_reader_error(err).map(|_| None),
96+
})
97+
// resolve errors
98+
.collect::<Result<Vec<Option<timsrust::Spectrum>>, std::io::Error>>()
99+
.map_err(|e| std::io::Error::other(e.to_string()))?
100+
// remove None values
64101
.into_iter()
65-
.collect::<Result<Vec<_>, _>>()
66-
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
102+
.flatten()
103+
.collect::<Vec<_>>();
67104

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

0 commit comments

Comments
 (0)