Skip to content

Commit

Permalink
Merge pull request #4 from MannLabs/separate_frame_channels
Browse files Browse the repository at this point in the history
Separate frame channels
  • Loading branch information
sander-willems-bruker authored Oct 19, 2023
2 parents 81e4999 + 1ed2d54 commit d9d009a
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 22 deletions.
5 changes: 5 additions & 0 deletions src/acquisition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum AcquisitionType {
DDAPASEF,
DIAPASEF,
}
12 changes: 12 additions & 0 deletions src/file_readers/frame_readers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub trait ReadableFrames {
fn read_single_frame(&self, index: usize) -> Frame;

fn read_all_frames(&self) -> Vec<Frame>;

fn read_ms1_frames(&self) -> Vec<Frame>;

fn read_ms2_frames(&self) -> Vec<Frame>;
}

impl FileFormat {
Expand All @@ -35,4 +39,12 @@ impl ReadableFrames for FileFormat {
fn read_all_frames(&self) -> Vec<Frame> {
self.unwrap_frame_reader().read_all_frames()
}

fn read_ms1_frames(&self) -> Vec<Frame> {
self.unwrap_frame_reader().read_ms1_frames()
}

fn read_ms2_frames(&self) -> Vec<Frame> {
self.unwrap_frame_reader().read_ms2_frames()
}
}
41 changes: 34 additions & 7 deletions src/file_readers/frame_readers/tdf_reader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
crate::{
acquisition::AcquisitionType,
converters::{
ConvertableIndex, Frame2RtConverter, Scan2ImConverter,
Tof2MzConverter,
Expand All @@ -26,6 +27,7 @@ pub struct TDFReader {
pub im_converter: Scan2ImConverter,
pub mz_converter: Tof2MzConverter,
pub frame_table: FrameTable,
frame_types: Vec<FrameType>,
}

impl TDFReader {
Expand All @@ -42,6 +44,16 @@ impl TDFReader {
String::from(&file_name),
frame_table.offsets.clone(),
);
let frame_types: Vec<FrameType> = frame_table
.msms_type
.iter()
.map(|msms_type| match msms_type {
0 => FrameType::MS1,
8 => FrameType::MS2(AcquisitionType::DDAPASEF),
9 => FrameType::MS2(AcquisitionType::DIAPASEF),
_ => FrameType::Unknown,
})
.collect();
Self {
path: path.to_string(),
tdf_bin_reader: tdf_bin_reader,
Expand All @@ -50,6 +62,7 @@ impl TDFReader {
mz_converter: Tof2MzConverter::from_sql(&tdf_sql_reader),
frame_table: frame_table,
tdf_sql_reader: tdf_sql_reader,
frame_types: frame_types,
}
}

Expand All @@ -65,13 +78,7 @@ impl ReadableFrames for TDFReader {
Frame::read_from_file(&self.tdf_bin_reader, index);
frame.rt = self.rt_converter.convert(index as u32);
frame.index = self.frame_table.id[index];
let msms_type = self.frame_table.msms_type[index];
frame.frame_type = match msms_type {
0 => FrameType::MS1,
8 => FrameType::MS2DDA,
9 => FrameType::MS2DIA,
_ => FrameType::Unknown,
};
frame.frame_type = self.frame_types[index];
frame
}

Expand All @@ -81,4 +88,24 @@ impl ReadableFrames for TDFReader {
.map(|index| self.read_single_frame(index))
.collect()
}

fn read_ms1_frames(&self) -> Vec<Frame> {
(0..self.tdf_bin_reader.size())
.into_par_iter()
.map(|index| match self.frame_types[index] {
FrameType::MS1 => self.read_single_frame(index),
_ => Frame::default(),
})
.collect()
}

fn read_ms2_frames(&self) -> Vec<Frame> {
(0..self.tdf_bin_reader.size())
.into_par_iter()
.map(|index| match self.frame_types[index] {
FrameType::MS2(_) => self.read_single_frame(index),
_ => Frame::default(),
})
.collect()
}
}
16 changes: 8 additions & 8 deletions src/file_readers/spectrum_readers/dda_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ pub struct DDASpectrumReader {
pub path_name: String,
precursor_reader: PrecursorReader,
mz_reader: Tof2MzConverter,
frames: Vec<Frame>,
ms2_frames: Vec<Frame>,
}

impl DDASpectrumReader {
pub fn new(path_name: String) -> Self {
let tdf_reader: TDFReader = TDFReader::new(&path_name.to_string());
let mz_reader: Tof2MzConverter = tdf_reader.mz_converter;
let frames: Vec<Frame> = tdf_reader.read_all_frames();
let ms2_frames: Vec<Frame> = tdf_reader.read_ms2_frames();
let precursor_reader: PrecursorReader =
PrecursorReader::new(&tdf_reader);
Self {
path_name,
precursor_reader,
mz_reader,
frames,
ms2_frames,
}
}

Expand All @@ -53,21 +53,21 @@ impl DDASpectrumReader {
for &index in selection.iter() {
let frame: usize =
self.precursor_reader.pasef_frames.frame[index] - 1;
if self.frames[frame].intensities.len() == 0 {
if self.ms2_frames[frame].intensities.len() == 0 {
continue;
}
let scan_start: usize =
self.precursor_reader.pasef_frames.scan_start[index];
let scan_end: usize =
self.precursor_reader.pasef_frames.scan_end[index];
let offset_start: usize =
self.frames[frame].scan_offsets[scan_start] as usize;
self.ms2_frames[frame].scan_offsets[scan_start] as usize;
let offset_end: usize =
self.frames[frame].scan_offsets[scan_end] as usize;
self.ms2_frames[frame].scan_offsets[scan_end] as usize;
let tof_selection: &[u32] =
&self.frames[frame].tof_indices[offset_start..offset_end];
&self.ms2_frames[frame].tof_indices[offset_start..offset_end];
let intensity_selection: &[u32] =
&self.frames[frame].intensities[offset_start..offset_end];
&self.ms2_frames[frame].intensities[offset_start..offset_end];
tof_indices.extend(tof_selection);
intensities.extend(intensity_selection);
}
Expand Down
7 changes: 4 additions & 3 deletions src/frames.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::acquisition::AcquisitionType;

#[derive(Debug, PartialEq, Default)]
pub struct Frame {
pub scan_offsets: Vec<u64>,
Expand All @@ -8,11 +10,10 @@ pub struct Frame {
pub frame_type: FrameType,
}

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum FrameType {
MS1,
MS2DDA,
MS2DIA,
MS2(AcquisitionType),
Unknown,
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//! * analysis.tdf
//! * analysis.tdf_bin
mod acquisition;
mod calibration;
mod converters;
mod errors;
Expand All @@ -28,6 +29,7 @@ mod spectra;
mod vec_utils;

pub use crate::{
acquisition::AcquisitionType,
errors::*,
file_readers::FileReader,
frames::{Frame, FrameType},
Expand Down
9 changes: 5 additions & 4 deletions tests/frame_readers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::path::Path;
use timsrust::{FileReader, Frame, FrameType};
use timsrust::{AcquisitionType, FileReader, Frame, FrameType};

fn get_local_directory() -> &'static Path {
Path::new(std::file!())
Expand All @@ -15,7 +15,8 @@ fn tdf_reader_frames() {
.to_str()
.unwrap()
.to_string();
let frames: Vec<Frame> = FileReader::new(file_path).unwrap().read_all_frames();
let frames: Vec<Frame> =
FileReader::new(file_path).unwrap().read_all_frames();
let expected: Vec<Frame> = vec![
Frame {
scan_offsets: vec![0, 1, 3, 6, 10],
Expand All @@ -31,7 +32,7 @@ fn tdf_reader_frames() {
intensities: (10..36).map(|x| (x + 1) * 2).collect(),
index: 2,
rt: 0.2,
frame_type: FrameType::MS2DDA,
frame_type: FrameType::MS2(AcquisitionType::DDAPASEF),
},
Frame {
scan_offsets: vec![0, 9, 19, 30, 42],
Expand All @@ -47,7 +48,7 @@ fn tdf_reader_frames() {
intensities: (78..136).map(|x| (x + 1) * 2).collect(),
index: 4,
rt: 0.4,
frame_type: FrameType::MS2DDA,
frame_type: FrameType::MS2(AcquisitionType::DDAPASEF),
},
];
for i in 0..frames.len() {
Expand Down

0 comments on commit d9d009a

Please sign in to comment.