Skip to content

Commit

Permalink
FEAT: option to filter spectra by top n peaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sander-willems-bruker committed Jul 5, 2024
1 parent 3b0636e commit 2a6ff12
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/ms_data/spectra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,34 @@ pub struct Spectrum {
pub isolation_mz: f64,
pub isolation_width: f64,
}

impl Spectrum {
pub fn get_top_n(&self, top_n: usize) -> Self {
let mut indexed: Vec<(f64, usize)> =
self.intensities.iter().cloned().zip(0..).collect();
indexed.sort_by(|a, b| {
b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal)
});
let mut top_indices: Vec<usize> = indexed
.iter()
.take(top_n)
.map(|&(_, index)| index)
.collect();
top_indices.sort();
Spectrum {
mz_values: top_indices
.iter()
.map(|&index| self.mz_values[index])
.collect(),
intensities: top_indices
.iter()
.map(|&index| self.intensities[index])
.collect(),
precursor: self.precursor,
index: self.index,
collision_energy: self.collision_energy,
isolation_mz: self.isolation_mz,
isolation_width: self.isolation_width,
}
}
}

0 comments on commit 2a6ff12

Please sign in to comment.