diff --git a/src/file_readers.rs b/src/file_readers.rs index 883fb06..9596557 100644 --- a/src/file_readers.rs +++ b/src/file_readers.rs @@ -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, @@ -65,40 +56,4 @@ impl FileReader { pub fn read_all_spectra(&self) -> Vec { self.format.read_all_spectra() } - - pub fn get_frame_converter(&self) -> Result { - 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 { - 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 { - match &self.format { - FileFormat::DFolder(path) => Ok(TDFReader::new( - &path.to_str().unwrap_or_default().to_string(), - ) - .mz_converter), - _ => Err(Error::FileFormatError( - FileFormatError::MetadataFilesAreMissing, - )), - } - } } diff --git a/src/file_readers/file_formats.rs b/src/file_readers/file_formats.rs index ec83680..0ab4472 100644 --- a/src/file_readers/file_formats.rs +++ b/src/file_readers/file_formats.rs @@ -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), @@ -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 { + self.get_frame_reader().parallel_filter(|_| true).collect() + } + + pub fn read_all_ms1_frames(&self) -> Vec { + self.get_frame_reader() + .parallel_filter(|x| x.msms_type == 0) + .collect() + } + + pub fn read_all_ms2_frames(&self) -> Vec { + self.get_frame_reader() + .parallel_filter(|x| x.msms_type != 0) + .collect() + } +} + #[derive(thiserror::Error, Debug)] pub enum FileFormatError { #[error("DirectoryDoesNotExist")] diff --git a/src/file_readers/frame_readers.rs b/src/file_readers/frame_readers.rs deleted file mode 100644 index 94f0db3..0000000 --- a/src/file_readers/frame_readers.rs +++ /dev/null @@ -1,50 +0,0 @@ -use crate::ms_data::Frame; - -use self::tdf_reader::TDFReader; - -use super::file_formats::FileFormat; - -pub mod tdf_reader; - -pub trait ReadableFrames { - fn read_single_frame(&self, index: usize) -> Frame; - - fn read_all_frames(&self) -> Vec; - - fn read_all_ms1_frames(&self) -> Vec; - - fn read_all_ms2_frames(&self) -> Vec; -} - -impl FileFormat { - fn unwrap_frame_reader(&self) -> Box { - let result = match &self { - Self::DFolder(path) => Box::new(TDFReader::new( - &path.to_str().unwrap_or_default().to_string(), - )) as Box, - Self::MS2Folder(path) => panic!( - "Folder {:} is not frame readable", - path.to_str().unwrap_or_default().to_string() - ), - }; - result - } -} - -impl ReadableFrames for FileFormat { - fn read_single_frame(&self, index: usize) -> Frame { - self.unwrap_frame_reader().read_single_frame(index) - } - - fn read_all_frames(&self) -> Vec { - self.unwrap_frame_reader().read_all_frames() - } - - fn read_all_ms1_frames(&self) -> Vec { - self.unwrap_frame_reader().read_all_ms1_frames() - } - - fn read_all_ms2_frames(&self) -> Vec { - self.unwrap_frame_reader().read_all_ms2_frames() - } -} diff --git a/src/file_readers/frame_readers/tdf_reader.rs b/src/file_readers/frame_readers/tdf_reader.rs deleted file mode 100644 index 5292995..0000000 --- a/src/file_readers/frame_readers/tdf_reader.rs +++ /dev/null @@ -1,57 +0,0 @@ -use crate::{ - domain_converters::{Frame2RtConverter, Scan2ImConverter, Tof2MzConverter}, - file_readers::{ - common::sql_reader::{ReadableFromSql, SqlReader}, - ReadableFrames, - }, - io::readers::frame_reader::FrameReader, - ms_data::Frame, -}; -use rayon::iter::ParallelIterator; - -#[derive(Debug)] -pub struct TDFReader { - frame_reader: FrameReader, - pub tdf_sql_reader: SqlReader, - pub rt_converter: Frame2RtConverter, - pub im_converter: Scan2ImConverter, - pub mz_converter: Tof2MzConverter, -} - -impl TDFReader { - pub fn new(path: &String) -> Self { - let tdf_sql_reader: SqlReader = SqlReader { - path: String::from(path), - }; - let frame_reader: FrameReader = FrameReader::new(&path); - Self { - rt_converter: Frame2RtConverter::from_sql(&tdf_sql_reader), - im_converter: Scan2ImConverter::from_sql(&tdf_sql_reader), - mz_converter: Tof2MzConverter::from_sql(&tdf_sql_reader), - tdf_sql_reader: tdf_sql_reader, - frame_reader: frame_reader, - } - } -} - -impl ReadableFrames for TDFReader { - fn read_single_frame(&self, index: usize) -> Frame { - self.frame_reader.get(index) - } - - fn read_all_frames(&self) -> Vec { - self.frame_reader.parallel_filter(|_| true).collect() - } - - fn read_all_ms1_frames(&self) -> Vec { - self.frame_reader - .parallel_filter(|x| x.msms_type == 0) - .collect() - } - - fn read_all_ms2_frames(&self) -> Vec { - self.frame_reader - .parallel_filter(|x| x.msms_type != 0) - .collect() - } -} diff --git a/src/file_readers/spectrum_readers/dda_reader.rs b/src/file_readers/spectrum_readers/dda_reader.rs index 81796d4..50eff5d 100644 --- a/src/file_readers/spectrum_readers/dda_reader.rs +++ b/src/file_readers/spectrum_readers/dda_reader.rs @@ -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, }; @@ -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 = 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_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, diff --git a/src/file_readers/spectrum_readers/dda_reader/precursors.rs b/src/file_readers/spectrum_readers/dda_reader/precursors.rs index c910b8c..dbccdbf 100644 --- a/src/file_readers/spectrum_readers/dda_reader/precursors.rs +++ b/src/file_readers/spectrum_readers/dda_reader/precursors.rs @@ -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, @@ -22,18 +21,23 @@ 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 = 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 = (0..precursor_table.mz.len()) .into_par_iter() .map(|index| { @@ -41,8 +45,8 @@ impl PrecursorReader { 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?