Skip to content

Commit cf1bcd6

Browse files
committed
Made the MzSpecLibEncode trait more flexible
1 parent 4093d9b commit cf1bcd6

3 files changed

Lines changed: 37 additions & 14 deletions

File tree

mzannotate/src/mzspeclib/write.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use std::{
44
num::NonZeroU32,
55
};
66

7-
use itertools::Itertools;
87
use mzcv::term;
9-
use mzdata::{mzpeaks::peak_set::PeakSetIter, params::Value, prelude::*};
8+
use mzdata::{params::Value, prelude::*};
109

1110
use crate::{
1211
mzspeclib::{Attribute, AttributeValue, Attributes, EntryType, Id, LibraryHeader},
@@ -203,7 +202,14 @@ impl<Writer: Write> MzSpecLibTextWriter<Writer, HeaderWritten> {
203202
write!(&mut self.writer, "\t")?;
204203
}
205204
write!(&mut self.writer, "\t")?;
206-
write!(&mut self.writer, "{}", p.aggregations().join(","))?;
205+
let mut trailing = false;
206+
for agg in p.aggregations() {
207+
if trailing {
208+
write!(&mut self.writer, ",")?;
209+
}
210+
write!(&mut self.writer, "{agg}")?;
211+
trailing = true;
212+
}
207213
}
208214
writeln!(&mut self.writer)?;
209215
}
@@ -235,7 +241,9 @@ pub struct HeaderWritten;
235241
/// A single spectrum that can be encoded as an mzSpecLib file
236242
pub trait MzSpecLibEncode {
237243
/// The peak type
238-
type Peak: MzSpecLibPeakEncode;
244+
type Peak<'a>: MzSpecLibPeakEncode + 'a
245+
where
246+
Self: 'a;
239247
/// The key for this spectrum
240248
fn key(&self) -> Id;
241249
/// The attributes for this spectrum
@@ -249,7 +257,7 @@ pub trait MzSpecLibEncode {
249257
&self,
250258
) -> impl Iterator<Item = (Id, Attributes, Self::InterpretationMemberIter)>;
251259
/// The peaks
252-
fn peaks(&self) -> PeakSetIter<'_, Self::Peak>;
260+
fn peaks(&self) -> impl Iterator<Item = Self::Peak<'_>>;
253261
}
254262

255263
/// A peak that can be encoded for use in an mzSpecLib file
@@ -259,5 +267,5 @@ pub trait MzSpecLibPeakEncode: CentroidLike {
259267
/// The annotations
260268
fn annotations(&self) -> impl Iterator<Item = &Self::A>;
261269
/// The aggregations
262-
fn aggregations(&self) -> impl Iterator<Item = &str>;
270+
fn aggregations(&self) -> impl Iterator<Item = impl std::fmt::Display>;
263271
}

mzannotate/src/spectrum/peak.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt::Display;
2+
13
use mzcore::system::MassOverCharge;
24
use mzdata::{
35
mzpeaks::prelude::*,
@@ -212,7 +214,21 @@ impl<A: ToMzPAF> crate::mzspeclib::MzSpecLibPeakEncode for AnnotatedPeak<A> {
212214
}
213215

214216
/// The aggregations
215-
fn aggregations(&self) -> impl Iterator<Item = &str> {
217+
fn aggregations(&self) -> impl Iterator<Item = impl Display> {
218+
self.aggregations.iter().map(String::as_str)
219+
}
220+
}
221+
222+
impl<A: ToMzPAF> crate::mzspeclib::MzSpecLibPeakEncode for &AnnotatedPeak<A> {
223+
type A = A;
224+
225+
/// The annotations
226+
fn annotations(&self) -> impl Iterator<Item = &Self::A> {
227+
self.annotations.iter()
228+
}
229+
230+
/// The aggregations
231+
fn aggregations(&self) -> impl Iterator<Item = impl Display> {
216232
self.aggregations.iter().map(String::as_str)
217233
}
218234
}

mzannotate/src/spectrum/spectrum.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ use std::num::NonZeroU32;
33
use mzcore::{chemistry::MassOutputMode, system::MassOverCharge};
44
use mzcv::term;
55
use mzdata::{
6-
mzpeaks::{
7-
MZPeakSetType,
8-
peak_set::{PeakSetIter, PeakSetVec},
9-
prelude::*,
10-
},
6+
mzpeaks::{MZPeakSetType, peak_set::PeakSetVec, prelude::*},
117
params::{ParamDescribed, ParamLike, Unit, Value, ValueRef},
128
prelude::{IonProperties, SpectrumLike},
139
spectrum::{MultiLayerSpectrum, SignalContinuity, SpectrumDescription},
@@ -264,7 +260,10 @@ impl<Mode: MassOutputMode> From<AnnotatedSpectrum<Mode>> for MultiLayerSpectrum
264260
impl<Mode: MassOutputMode> crate::mzspeclib::MzSpecLibEncode for AnnotatedSpectrum<Mode> {
265261
type InterpretationMemberIter = Vec<(Id, Attributes)>;
266262
/// The peak type
267-
type Peak = AnnotatedPeak<Fragment<Mode>>;
263+
type Peak<'a>
264+
= &'a AnnotatedPeak<Fragment<Mode>>
265+
where
266+
Mode: 'a;
268267

269268
/// The key for this spectrum
270269
fn key(&self) -> Id {
@@ -327,7 +326,7 @@ impl<Mode: MassOutputMode> crate::mzspeclib::MzSpecLibEncode for AnnotatedSpectr
327326
}
328327

329328
/// The peaks
330-
fn peaks(&self) -> PeakSetIter<'_, Self::Peak> {
329+
fn peaks(&self) -> impl Iterator<Item = Self::Peak<'_>> {
331330
self.peaks.iter()
332331
}
333332
}

0 commit comments

Comments
 (0)