Skip to content

Commit

Permalink
API
Browse files Browse the repository at this point in the history
  • Loading branch information
mobiusklein committed Oct 25, 2023
1 parent 65af80b commit 11099c1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ pub mod infer_format;
pub(crate) mod compression;

pub use crate::io::mgf::{MGFReader, MGFError, MGFWriter};
pub use crate::io::mzml::{MzMLReader, MzMLParserError};
pub use crate::io::mzml::{MzMLReader, MzMLParserError, MzMLWriter};
#[cfg(feature = "async")]
pub use crate::io::mzml::AsyncMzMLReaderType;
#[cfg(feature = "mzmlb")]
pub use crate::io::mzmlb::{MzMLbReader, MzMLbError};
pub use crate::io::offset_index::OffsetIndex;
Expand Down
12 changes: 12 additions & 0 deletions src/io/infer_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ use crate::io::compression::{is_gzipped, is_gzipped_extension};
#[cfg(feature = "mzmlb")]
use super::traits::MZFileReader;


/// Mass spectrometry file formats that [`mzdata`]
/// supports
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MassSpectrometryFormat {
MGF,
Expand All @@ -31,6 +34,8 @@ pub enum MassSpectrometryFormat {
}


/// Given a path, infer the file format and whether or not the file at that path is
/// GZIP compressed
pub(crate) fn infer_from_path<P: Into<path::PathBuf>,>(path: P) -> (MassSpectrometryFormat, bool) {
let path: path::PathBuf = path.into();
let (is_gzipped, path) = is_gzipped_extension(path);
Expand All @@ -53,6 +58,8 @@ pub(crate) fn infer_from_path<P: Into<path::PathBuf>,>(path: P) -> (MassSpectrom
}


/// Given a stream of bytes, infer the file format and whether or not the
/// stream is GZIP compressed. This assumes the stream is seekable.
pub(crate) fn infer_from_stream<R: Read + Seek>(stream: &mut R) -> io::Result<(MassSpectrometryFormat, bool)> {
let mut buf = Vec::with_capacity(100);
let current_pos = stream.seek(io::SeekFrom::Current(0))?;
Expand All @@ -76,6 +83,9 @@ pub(crate) fn infer_from_stream<R: Read + Seek>(stream: &mut R) -> io::Result<(M
}


/// Given a path, infer the file format and whether or not the file at that path is
/// GZIP compressed, using both the file name and by trying to open and read the file
/// header
pub(crate) fn infer_format<P: Into<path::PathBuf>>(path: P) -> io::Result<(MassSpectrometryFormat, bool)> {
let path: path::PathBuf = path.into();

Expand All @@ -94,6 +104,8 @@ pub(crate) fn infer_format<P: Into<path::PathBuf>>(path: P) -> io::Result<(MassS
}


/// Given a local file system path, infer the file format, and attempt to open it
/// for reading.
pub fn open_file<P: Into<path::PathBuf>>(path: P) -> io::Result<Box<dyn ScanSource>>{
let path = path.into();
let (format, is_gzipped) = infer_format(path.clone())?;
Expand Down
3 changes: 1 addition & 2 deletions src/io/mzml/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,7 @@ where
if self.state < MzMLWriterState::DocumentOpen {
self.start_document()?;
} else {
panic!(
"Cannot start writing the header of mzML, currently in state {:?} which happens after.", self.state)
return self.transition_err(MzMLWriterState::Header)
}
self.write_cv_list()?;
self.write_file_description()?;
Expand Down
6 changes: 3 additions & 3 deletions src/io/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub use super::traits::{
MZFileReader, RandomAccessSpectrumIterator, ScanAccessError, SpectrumIterator, ScanSource, ScanWriter,
SeekRead, SpectrumGroup, SpectrumGrouping, SpectrumGroupingIterator,
MZFileReader, RandomAccessSpectrumIterator, ScanAccessError, ScanSource, ScanWriter, SeekRead,
SpectrumGroup, SpectrumGrouping, SpectrumGroupingIterator, SpectrumIterator,
};
pub use crate::meta::MSDataFileMetadata;
pub use crate::params::{ParamDescribed, ParamLike};
pub use crate::spectrum::{PrecursorSelection, SpectrumBehavior, IonProperties};
pub use crate::spectrum::{IonProperties, PrecursorSelection, SpectrumBehavior};
pub use std::convert::TryInto;
pub use std::io::prelude::*;
4 changes: 2 additions & 2 deletions src/io/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub trait SeekRead: io::Read + io::Seek {}
impl<T: io::Read + io::Seek> SeekRead for T {}

/// A base trait defining the behaviors of a source of spectra.
///
/// A [`ScanSource`]
pub trait ScanSource<
C: CentroidLike + Default = CentroidPeak,
D: DeconvolutedCentroidLike + Default = DeconvolutedPeak,
Expand Down Expand Up @@ -293,12 +295,10 @@ pub trait MZFileReader<
Ok(_) => {}
Err(_err) => {
reader.construct_index_from_stream();
// _save_index(&index_path, &reader)?;
}
}
} else {
reader.construct_index_from_stream();
// _save_index(&index_path, &reader)?;
}
}
Ok(reader)
Expand Down
3 changes: 2 additions & 1 deletion src/io/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ where
}
}

/// A writable stream that keeps a running MD5 checksum of all bytes
#[derive(Clone)]
pub struct MD5HashingStream<T: io::Write> {
pub(crate) struct MD5HashingStream<T: io::Write> {
pub stream: T,
pub context: MD5Context,
}
Expand Down

0 comments on commit 11099c1

Please sign in to comment.