Skip to content

Commit

Permalink
FEAT: updated TdfReader to use frameReader instead
Browse files Browse the repository at this point in the history
  • Loading branch information
sander-willems-bruker committed Apr 26, 2024
1 parent c2aada8 commit 02bc4e5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 96 deletions.
104 changes: 17 additions & 87 deletions src/file_readers/frame_readers/tdf_reader.rs
Original file line number Diff line number Diff line change
@@ -1,124 +1,54 @@
use {
crate::{
domain_converters::{
ConvertableDomain, Frame2RtConverter, Scan2ImConverter,
Tof2MzConverter,
},
file_readers::{
common::{
ms_data_blobs::ReadableFromBinFile,
sql_reader::{FrameTable, ReadableFromSql, SqlReader},
},
ReadableFrames,
},
io::readers::common::tdf_blobs::TdfBlobReader,
ms_data::{AcquisitionType, Frame, MSLevel, QuadrupoleSettings},
use crate::{
domain_converters::{Frame2RtConverter, Scan2ImConverter, Tof2MzConverter},
file_readers::{
common::sql_reader::{ReadableFromSql, SqlReader},
ReadableFrames,
},
rayon::prelude::*,
std::{path::Path, sync::Arc},
io::readers::frame_reader::FrameReader,
ms_data::Frame,
};

#[derive(Debug)]
pub struct TDFReader {
pub path: String,
frame_reader: FrameReader,
pub tdf_sql_reader: SqlReader,
tdf_bin_reader: TdfBlobReader,
pub rt_converter: Frame2RtConverter,
pub im_converter: Scan2ImConverter,
pub mz_converter: Tof2MzConverter,
pub frame_table: FrameTable,
pub acquisition: AcquisitionType,
ms_levels: Vec<MSLevel>,
}

impl TDFReader {
pub fn new(path: &String) -> Self {
let tdf_sql_reader: SqlReader = SqlReader {
path: String::from(path),
};
let frame_table: FrameTable = FrameTable::from_sql(&tdf_sql_reader);
let file_name: String = Path::new(&path)
.join("analysis.tdf_bin")
.to_string_lossy()
.to_string();
let tdf_bin_reader: TdfBlobReader = TdfBlobReader::new(
String::from(&file_name),
frame_table.offsets.iter().map(|x| *x as usize).collect(),
)
.unwrap();
let ms_levels: Vec<MSLevel> = frame_table
.msms_type
.iter()
.map(|msms_type| match msms_type {
0 => MSLevel::MS1,
8 => MSLevel::MS2,
9 => MSLevel::MS2,
_ => MSLevel::Unknown,
})
.collect();
let mut acquisition = AcquisitionType::Unknown;
if frame_table.msms_type.contains(&8) {
acquisition = AcquisitionType::DDAPASEF;
} else if frame_table.msms_type.contains(&9) {
acquisition = AcquisitionType::DIAPASEF;
}
let frame_reader: FrameReader = FrameReader::new(&path);
Self {
path: path.to_string(),
tdf_bin_reader: tdf_bin_reader,
rt_converter: Self::get_rt_converter(&frame_table),
rt_converter: Frame2RtConverter::from_values(
frame_reader.sql_frames.iter().map(|x| x.rt).collect(),
),
im_converter: Scan2ImConverter::from_sql(&tdf_sql_reader),
mz_converter: Tof2MzConverter::from_sql(&tdf_sql_reader),
frame_table: frame_table,
tdf_sql_reader: tdf_sql_reader,
ms_levels: ms_levels,
acquisition: acquisition,
frame_reader: frame_reader,
}
}

fn get_rt_converter(frame_table: &FrameTable) -> Frame2RtConverter {
let retention_times: Vec<f64> = frame_table.rt.clone();
Frame2RtConverter::from_values(retention_times)
}
}

impl ReadableFrames for TDFReader {
fn read_single_frame(&self, index: usize) -> Frame {
let mut frame: Frame =
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];
frame.ms_level = self.ms_levels[index];
frame.acquisition_type = self.acquisition;
if frame.ms_level == MSLevel::MS2 {
frame.quadrupole_settings = Arc::new(QuadrupoleSettings::default());
}
frame
self.frame_reader.get(index)
}

fn read_all_frames(&self) -> Vec<Frame> {
(0..self.tdf_bin_reader.len())
.into_par_iter()
.map(|index| self.read_single_frame(index))
.collect()
self.frame_reader.collect(|_| true)
}

fn read_all_ms1_frames(&self) -> Vec<Frame> {
(0..self.tdf_bin_reader.len())
.into_par_iter()
.map(|index| match self.ms_levels[index] {
MSLevel::MS1 => self.read_single_frame(index),
_ => Frame::default(),
})
.collect()
self.frame_reader.collect(|x| x.msms_type == 0)
}

fn read_all_ms2_frames(&self) -> Vec<Frame> {
(0..self.tdf_bin_reader.len())
.into_par_iter()
.map(|index| match self.ms_levels[index] {
MSLevel::MS2 => self.read_single_frame(index),
_ => Frame::default(),
})
.collect()
self.frame_reader.collect(|x| x.msms_type != 0)
}
}
38 changes: 29 additions & 9 deletions src/file_readers/spectrum_readers/mini_tdf_reader.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use crate::{
file_readers::FileFormatError,
io::readers::common::tdf_blobs::TdfBlobReader,
io::readers::common::tdf_blobs::{
IndexedTdfBlobReader, TdfBlob, TdfBlobParsable,
},
};
use std::fs;
use {
crate::{
file_readers::{
common::{
ms_data_blobs::ReadableFromBinFile,
parquet_reader::read_parquet_precursors,
},
ReadableSpectra,
common::parquet_reader::read_parquet_precursors, ReadableSpectra,
},
ms_data::{Precursor, Spectrum},
},
Expand All @@ -24,7 +22,7 @@ pub struct MiniTDFReader {
parquet_file_name: String,
precursors: Vec<Precursor>,
offsets: Vec<u64>,
frame_reader: Option<TdfBlobReader>,
frame_reader: Option<IndexedTdfBlobReader>,
}

fn find_ms2spectrum_file(
Expand Down Expand Up @@ -100,7 +98,7 @@ impl MiniTDFReader {
path.push(ms2_bin_file);
let file_name: String = path.to_string_lossy().into_owned();
self.frame_reader = Some(
TdfBlobReader::new(
IndexedTdfBlobReader::new(
String::from(&file_name),
self.offsets.iter().map(|x| *x as usize).collect(),
)
Expand All @@ -111,7 +109,7 @@ impl MiniTDFReader {

impl ReadableSpectra for MiniTDFReader {
fn read_single_spectrum(&self, index: usize) -> Spectrum {
let mut spectrum: Spectrum = Spectrum::read_from_file(
let mut spectrum: Spectrum = Spectrum::create_from_tdf_blob_reader(
&self.frame_reader.as_ref().unwrap(),
index,
);
Expand All @@ -134,3 +132,25 @@ impl ReadableSpectra for MiniTDFReader {
spectra
}
}

impl TdfBlobParsable for Spectrum {
fn set_tdf_blob_index(&mut self, index: usize) {
self.index = index;
}

fn update_from_tdf_blob(&mut self, blob: TdfBlob) {
let size: usize = blob.len();
let spectrum_data: Vec<u32> = (0..size).map(|i| blob.get(i)).collect();
let scan_count: usize = blob.len() / 3;
let tof_indices_bytes: &[u32] =
&spectrum_data[..scan_count as usize * 2];
let intensities_bytes: &[u32] =
&spectrum_data[scan_count as usize * 2..];
let mz_values: &[f64] =
bytemuck::cast_slice::<u32, f64>(tof_indices_bytes);
let intensity_values: &[f32] =
bytemuck::cast_slice::<u32, f32>(intensities_bytes);
self.intensities = intensity_values.iter().map(|&x| x as f64).collect();
self.mz_values = mz_values.to_vec();
}
}

0 comments on commit 02bc4e5

Please sign in to comment.