From fe44c6c6652932aa16f9f7373991071a258a2705 Mon Sep 17 00:00:00 2001 From: Kevin Velghe Date: Wed, 23 Oct 2024 19:07:10 +0200 Subject: [PATCH 1/3] fix parsing precursor charge in MGF files This fixes two issues: - the charge value in the PEPMASS header can have the same suffixed format as the CHARGE header - the order of the headers isn't determined, so the charge value should still be saved correctly if the CHARGE header is before the PEPMASS header NOTE: I'm not sure if there is a clear precedence between PEPMASS or CHARGE here, but it might be better to only do this if charge isn't set already, to keep the behaviour consistent regardless of order. There could be multiple PEPMASS headers, but it's not clear to me how the CHARGE header should behave then: should there be multiple of them, or should we actually set the charge of all precursor ions, like we do now if CHARGE is first. --- src/io/mgf.rs | 88 ++++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 46 deletions(-) diff --git a/src/io/mgf.rs b/src/io/mgf.rs index 6a1b98a..69183a0 100644 --- a/src/io/mgf.rs +++ b/src/io/mgf.rs @@ -90,6 +90,7 @@ struct SpectrumBuilder< pub intensity_array: Vec, pub charge_array: Vec, pub has_charge: u32, + pub precursor_charge: Option, pub detail_level: DetailLevel, empty_metadata: bool, centroided_type: PhantomData, @@ -114,6 +115,7 @@ impl Default for SpectrumB intensity_array: Default::default(), charge_array: Default::default(), has_charge: Default::default(), + precursor_charge: Default::default(), detail_level: Default::default(), centroided_type: Default::default(), deconvoluted_type: Default::default(), @@ -341,17 +343,8 @@ impl MGFReade .map_err(|e| warn!("Failed to parse PEPMASS intensity {value}: {e}")) .unwrap_or_default(); let charge: Option = match parts.next() { - Some(c) => match c.parse::() { - Ok(val) => Some(val), - Err(e) => { - self.state = MGFParserState::Error; - self.error = Some(MGFError::MalformedHeaderLine(format!( - "Malformed charge value in PEPMASS header {value}: {e}" - ))); - return false; - } - }, - None => None, + Some(c) => self.parse_charge(c), + None => builder.precursor_charge, }; builder.description.precursor = Some(Precursor { ions: vec![SelectedIon { @@ -364,41 +357,15 @@ impl MGFReade }); } "CHARGE" => { - let (sign, value, tail_sign) = if let Some(stripped) = value.strip_suffix('+') { - (1, stripped, true) - } else if let Some(stripped) = value.strip_suffix('-') { - (-1, stripped, true) - } else { - (1, value, false) - }; - - if tail_sign && (value.starts_with('-') || value.starts_with('+')) { - self.state = MGFParserState::Error; - self.error = Some(MGFError::MalformedHeaderLine(format!( - "Could not parse CHARGE header {value}" - ))); - return false; - } - - match value.parse::() { - Ok(z) => { - if let Some(ion) = builder - .description - .precursor - .get_or_insert_with(Precursor::default) - .iter_mut() - .last() - { - ion.charge = Some(sign * z); - } - } - Err(e) => { - self.state = MGFParserState::Error; - self.error = Some(MGFError::MalformedHeaderLine(format!( - "Could not parse CHARGE header {value} : {e}" - ))); - return false; - } + builder.precursor_charge = self.parse_charge(value); + if let Some(ion) = builder + .description + .precursor + .get_or_insert_with(Precursor::default) + .iter_mut() + .last() + { + ion.charge = builder.precursor_charge } } &_ => { @@ -418,6 +385,35 @@ impl MGFReade } } + fn parse_charge(&mut self, value: &str) -> Option { + let (sign, value, tail_sign) = if let Some(stripped) = value.strip_suffix('+') { + (1, stripped, true) + } else if let Some(stripped) = value.strip_suffix('-') { + (-1, stripped, true) + } else { + (1, value, false) + }; + + if tail_sign && (value.starts_with('-') || value.starts_with('+')) { + self.state = MGFParserState::Error; + self.error = Some(MGFError::MalformedHeaderLine(format!( + "Could not parse charge value {value}" + ))); + return None; + } + + match value.parse::() { + Ok(z) => Some(sign * z), + Err(e) => { + self.state = MGFParserState::Error; + self.error = Some(MGFError::MalformedHeaderLine(format!( + "Could not parse charge value {value} : {e}" + ))); + return None; + } + } + } + fn handle_peak(&mut self, line: &str, builder: &mut SpectrumBuilder) -> bool { let peak_line = self.parse_peak_from_line(line, builder).unwrap_or(false); if peak_line { From d54040f3135145affca7418b13a674c72d39a35e Mon Sep 17 00:00:00 2001 From: Kevin Velghe Date: Thu, 24 Oct 2024 14:25:46 +0200 Subject: [PATCH 2/3] don't overwrite charge if it was already set --- src/io/mgf.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/io/mgf.rs b/src/io/mgf.rs index 69183a0..a51636e 100644 --- a/src/io/mgf.rs +++ b/src/io/mgf.rs @@ -365,7 +365,9 @@ impl MGFReade .iter_mut() .last() { - ion.charge = builder.precursor_charge + if ion.charge.is_none() { + ion.charge = builder.precursor_charge + } } } &_ => { From edc09dc4d17984c09e3f81214bf2df7547be8711 Mon Sep 17 00:00:00 2001 From: Kevin Velghe Date: Thu, 24 Oct 2024 16:13:22 +0200 Subject: [PATCH 3/3] add mgf test case --- src/io/mgf.rs | 4 ++-- src/io/shorthand.rs | 2 +- test/data/small.mgf | 13 +++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/io/mgf.rs b/src/io/mgf.rs index a51636e..7bc83cc 100644 --- a/src/io/mgf.rs +++ b/src/io/mgf.rs @@ -1195,7 +1195,7 @@ mod test { } } assert_eq!(ms1_count, 0); - assert_eq!(msn_count, 34); + assert_eq!(msn_count, 35); } #[test] @@ -1222,7 +1222,7 @@ mod test { }) } assert_eq!(ms1_count, 0); - assert_eq!(msn_count, 34); + assert_eq!(msn_count, 35); } #[test] diff --git a/src/io/shorthand.rs b/src/io/shorthand.rs index 090a66c..9b34038 100644 --- a/src/io/shorthand.rs +++ b/src/io/shorthand.rs @@ -310,7 +310,7 @@ mod test { assert_eq!(val.len(), 48); let val: Vec<_> = mz_read!("./test/data/small.mgf".as_ref(), reader => { reader.collect() })?; - assert_eq!(val.len(), 34); + assert_eq!(val.len(), 35); let val = mz_read!("./test/data/small.mzML".as_ref(), reader => { reader.file_description().clone() })?; assert_eq!(val.source_files.len(), 1); Ok(()) diff --git a/test/data/small.mgf b/test/data/small.mgf index 018de26..7ee903d 100644 --- a/test/data/small.mgf +++ b/test/data/small.mgf @@ -25514,3 +25514,16 @@ PEPMASS=882.535034179688 29807.576171875 1743.361206 3.1923613548 END IONS +BEGIN IONS +TITLE=charge before pepmass +CHARGE=2+ +PEPMASS=475.137295 +ION_MOBILITY=42.42 +RTINSECONDS=51.2 +72.04439 100 +148.06043 600 +232.07504 300 +263.08737 400 +347.10198 500 +423.11802 200 +END IONS