Skip to content

Commit

Permalink
FEAT: removed old tdf and frame reader
Browse files Browse the repository at this point in the history
  • Loading branch information
sander-willems-bruker committed Jun 14, 2024
1 parent 80c47fb commit 0ae839d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 176 deletions.
49 changes: 2 additions & 47 deletions src/file_readers.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
use crate::{
domain_converters::{Frame2RtConverter, Scan2ImConverter, Tof2MzConverter},
Error,
};
use crate::Error;

mod common;
mod file_formats;
mod frame_readers;
mod spectrum_readers;

use {
self::{
file_formats::FileFormat, frame_readers::ReadableFrames,
spectrum_readers::ReadableSpectra,
},
self::{file_formats::FileFormat, spectrum_readers::ReadableSpectra},
crate::ms_data::{Frame, Spectrum},
};

pub use file_formats::FileFormatError;

use self::frame_readers::tdf_reader::TDFReader;

/// A reader to read [frames](crate::ms_data::Frame) and [spectra](crate::ms_data::Spectrum).
pub struct FileReader {
format: FileFormat,
Expand Down Expand Up @@ -65,40 +56,4 @@ impl FileReader {
pub fn read_all_spectra(&self) -> Vec<Spectrum> {
self.format.read_all_spectra()
}

pub fn get_frame_converter(&self) -> Result<Frame2RtConverter, Error> {
match &self.format {
FileFormat::DFolder(path) => Ok(TDFReader::new(
&path.to_str().unwrap_or_default().to_string(),
)
.rt_converter),
_ => Err(Error::FileFormatError(
FileFormatError::MetadataFilesAreMissing,
)),
}
}

pub fn get_scan_converter(&self) -> Result<Scan2ImConverter, Error> {
match &self.format {
FileFormat::DFolder(path) => Ok(TDFReader::new(
&path.to_str().unwrap_or_default().to_string(),
)
.im_converter),
_ => Err(Error::FileFormatError(
FileFormatError::MetadataFilesAreMissing,
)),
}
}

pub fn get_tof_converter(&self) -> Result<Tof2MzConverter, Error> {
match &self.format {
FileFormat::DFolder(path) => Ok(TDFReader::new(
&path.to_str().unwrap_or_default().to_string(),
)
.mz_converter),
_ => Err(Error::FileFormatError(
FileFormatError::MetadataFilesAreMissing,
)),
}
}
}
38 changes: 38 additions & 0 deletions src/file_readers/file_formats.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::{fs, path::PathBuf};

use crate::{io::readers::frame_reader::FrameReader, ms_data::Frame};
use rayon::iter::ParallelIterator;

use super::common::sql_reader::SqlReader;
pub enum FileFormat {
DFolder(PathBuf),
MS2Folder(PathBuf),
Expand Down Expand Up @@ -72,6 +76,40 @@ fn folder_contains_extension(
false
}

impl FileFormat {
fn get_frame_reader(&self) -> FrameReader {
let path = match &self {
Self::DFolder(path) => path,
Self::MS2Folder(path) => panic!(
"Folder {:} is not frame readable",
path.to_str().unwrap_or_default().to_string()
),
};
let frame_reader: FrameReader = FrameReader::new(&path);
frame_reader
}

pub fn read_single_frame(&self, index: usize) -> Frame {
self.get_frame_reader().get(index)
}

pub fn read_all_frames(&self) -> Vec<Frame> {
self.get_frame_reader().parallel_filter(|_| true).collect()
}

pub fn read_all_ms1_frames(&self) -> Vec<Frame> {
self.get_frame_reader()
.parallel_filter(|x| x.msms_type == 0)
.collect()
}

pub fn read_all_ms2_frames(&self) -> Vec<Frame> {
self.get_frame_reader()
.parallel_filter(|x| x.msms_type != 0)
.collect()
}
}

#[derive(thiserror::Error, Debug)]
pub enum FileFormatError {
#[error("DirectoryDoesNotExist")]
Expand Down
50 changes: 0 additions & 50 deletions src/file_readers/frame_readers.rs

This file was deleted.

57 changes: 0 additions & 57 deletions src/file_readers/frame_readers/tdf_reader.rs

This file was deleted.

24 changes: 17 additions & 7 deletions src/file_readers/spectrum_readers/dda_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ use crate::{
calibration::Tof2MzCalibrator,
domain_converters::Tof2MzConverter,
file_readers::{
frame_readers::{tdf_reader::TDFReader, ReadableFrames},
common::sql_reader::{ReadableFromSql, SqlReader},
ReadableSpectra,
},
ms_data::{Frame, Spectrum},
ms_data::{RawProcessedSpectrumState, RawSpectrum, RawSpectrumProcessor},
io::readers::frame_reader::FrameReader,
ms_data::{
Frame, RawProcessedSpectrumState, RawSpectrum, RawSpectrumProcessor,
Spectrum,
},
utils::vec_utils::group_and_sum,
};

Expand All @@ -29,11 +32,18 @@ pub struct DDASpectrumReader {

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.clone();
let ms2_frames: Vec<Frame> = tdf_reader.read_all_ms2_frames();
// let tdf_reader: TDFReader = TDFReader::new(&path_name.to_string());
let tdf_sql_reader: SqlReader = SqlReader {
path: String::from(&path_name),
};
let frame_reader: FrameReader = FrameReader::new(&path_name);
let mz_reader: Tof2MzConverter =
Tof2MzConverter::from_sql(&tdf_sql_reader);

let ms2_frames: Vec<Frame> =
frame_reader.parallel_filter(|x| x.msms_type != 0).collect();
let precursor_reader: PrecursorReader =
PrecursorReader::new(&tdf_reader);
PrecursorReader::new(&path_name);
Self {
path_name,
precursor_reader,
Expand Down
34 changes: 19 additions & 15 deletions src/file_readers/spectrum_readers/dda_reader/precursors.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use rayon::prelude::*;

use crate::{
domain_converters::ConvertableDomain,
file_readers::{
common::sql_reader::{
PasefFrameMsMsTable, PrecursorTable, ReadableFromSql,
},
frame_readers::tdf_reader::TDFReader,
domain_converters::{
ConvertableDomain, Frame2RtConverter, Scan2ImConverter, Tof2MzConverter,
},
file_readers::common::sql_reader::{
PasefFrameMsMsTable, PrecursorTable, ReadableFromSql, SqlReader,
},
ms_data::Precursor,
utils::vec_utils::argsort,
Expand All @@ -22,27 +21,32 @@ pub struct PrecursorReader {
}

impl PrecursorReader {
pub fn new(tdf_reader: &TDFReader) -> Self {
pub fn new(path: &String) -> Self {
let tdf_sql_reader: SqlReader = SqlReader {
path: String::from(path),
};
let rt_converter: Frame2RtConverter =
Frame2RtConverter::from_sql(&tdf_sql_reader);
let im_converter: Scan2ImConverter =
Scan2ImConverter::from_sql(&tdf_sql_reader);
let select_collision_energy_sql = String::from(
"SELECT CollisionEnergy FROM PasefFrameMsMsInfo GROUP BY Precursor",
);
let pasef_frames: PasefFrameMsMsTable =
PasefFrameMsMsTable::from_sql(&tdf_reader.tdf_sql_reader);
PasefFrameMsMsTable::from_sql(&tdf_sql_reader);
let precursor_table: PrecursorTable =
PrecursorTable::from_sql(&tdf_reader.tdf_sql_reader);
// let retention_times: Vec<f64> = tdf_reader.frame_table.rt.clone();
let collision_energies = tdf_reader
.tdf_sql_reader
.get_data_from_sql(&select_collision_energy_sql);
PrecursorTable::from_sql(&tdf_sql_reader);
let collision_energies =
tdf_sql_reader.get_data_from_sql(&select_collision_energy_sql);
let precursors: Vec<Precursor> = (0..precursor_table.mz.len())
.into_par_iter()
.map(|index| {
let frame_id: usize = precursor_table.precursor_frame[index];
let scan_id: f64 = precursor_table.scan_average[index];
Precursor {
mz: precursor_table.mz[index],
rt: tdf_reader.rt_converter.convert(frame_id as u32),
im: tdf_reader.im_converter.convert(scan_id),
rt: rt_converter.convert(frame_id as u32),
im: im_converter.convert(scan_id),
charge: precursor_table.charge[index],
intensity: precursor_table.intensity[index],
index: index + 1, //TODO?
Expand Down

0 comments on commit 0ae839d

Please sign in to comment.