Skip to main content

mzdata_bindata/
lib.rs

1//! The `DataArray` implementation for data buffer manipulation used for, amongst other things,
2//! mapping numeric data to and from optionally compressed little endian base-64 byte arrays with
3//! as few data copies as possible, algebraically.
4//!
5//! Also covers the `ArrayType` enum that covers the standardized array flavors telling us which
6//! dimension an array describes, `BinaryArrayMap` collections of (`ArrayType`, `DataArray`), and
7//! traits for converting to and from `BinaryArrayMap` based upon the set of available `ArrayType`.
8//! This includes a generalization for `BinaryArrayMap3D` when ion mobility is available.
9//!
10//! ## Compression
11//!
12//! `mzdata` supports several kinds of data compression methods with tradeoffs
13//!
14//! ### Lossless methods
15//! - `zlib` compression [`BinaryCompressionType::Zlib`]: A well known, widely available general-purpose compressor codec with many implementations,
16//!     with varying speed and size tradeoffs taken from [`flate2`].
17//! - `zstd` compression [`BinaryCompressionType::Zstd`]: A newer general-purpose codec that is not widely used in mzML but very popular in other applications.
18//!     Much faster and often better compression compared to `zlib`.
19//!     Requires feature `zstd`
20//! - `byte-shuffled zstd` compression [`BinaryCompressionType::ShuffleZstd`]: This first applies a byte shuffle transform to the data to compress
21//!     the `n`th byte of each value contiguously, and then apply `zstd`. This makes sorted data compress more effectively.
22//!     Requires feature `zstd`
23//! - `dictionary encoded zstd` [`BinaryCompressionType::ZstdDict`]: Another elaboration of `byte-shuffled zstd` where the values are first dictionary-encoded
24//!     so that repeated values are denoted by indices rather than the full size value itself. The dictionary is sorted and then both the dictionary and the
25//!     indices are byte shuffle encoded and then `zstd` compressed. This is even more effective than previous codecs for data with repeated, semi-sorted values
26//!     like m/z and ion mobility.
27//!     Requires feature `zstd`
28//!
29//! ### Lossy methods
30//! - `numpress-linear` compression [`BinaryCompressionType::NumpressLinear`]: A combination simple linear model fit and byte packing codec that stores the model
31//!     coefficients as float64 and the residuals as unsigned 16 or 32-bit integers. With an optimal fixed point, this will produce a median loss of accuracy below
32//!     of about 2e-8 absolute units, but may vary widely from dataset to dataset. This codec **MUST** be used with sorted data only, and is only suitable for
33//!     float64 data. It may be combined with `zlib` [`BinaryCompressionType::NumpressLinearZlib`] or `zstd` [`BinaryCompressionType::NumpressLinearZstd`] for even
34//!     greater space savings. See details at <https://doi.org/10.1074/mcp.O114.037879>. /// Requires feature `numpress`
35//! - `numpress-slof` compression [`BinaryCompressionType::NumpressSLOF`]: A lossy numerical approximation that log transforms and scales values to make them very
36//!     close to 16-bit integers. This codec is lossier the larger the value is, and is only appropriate for count data like intensities. It can also combine with
37//!     `zlib` [`BinaryCompressionType::NumpressSLOFZlib`] and `zstd` [`BinaryCompressionType::NumpressSLOFZstd`]. See details at <https://doi.org/10.1074/mcp.O114.037879>
38//!     Requires feature `numpress`
39//! - `numpress-pic` compression [`BinaryCompressionType::NumpressPIC`]: This simply rounds a floating point value and then packs it
40//!     into a truncated integer. May be suitable for intensity values, but not consistently better than [`BinaryCompressionType::NumpressSLOF`].
41//!     See details at <https://doi.org/10.1074/mcp.O114.037879>
42//!     Requires feature `numpress`
43//! - Linear prediction compression [`BinaryCompressionType::LinearPrediction`]: The same concept as [`BinaryCompressionType::NumpressLinear`] without the truncation
44//!     All of the same concerns apply though it loses less precision. This does not perform any form of compression. It was uses as part of mzMLb which applies
45//!     container-level compression. Only suitable for float64 data.
46//! - Delta encoding [`BinaryCompressionType::DeltaPrediction`]: This computes first order difference of a sorted array of values and stores the difference
47//!     and the starting point. It is sometimes more accurate than [`BinaryCompressionType::LinearPrediction`]. Like [`BinaryCompressionType::LinearPrediction`],
48//!     it does not do any actual compression. It was meant for use with mzMLb, which compresses the entire container. Only suitable for float64 data.
49//! - Delta-shuffled `zstd` [`BinaryCompressionType::DeltaShuffleZstd`]: Combines [`BinaryCompressionType::DeltaPrediction`] with [`BinaryCompressionType::ShuffleZstd`].
50//!     Requires feature `zstd`
51//!
52//!
53//! ## Supported Data types
54//!
55//! `mzdata` supports five basic binary data types via the [`BinaryDataArrayType`] enum. Data that doesn't follow any of these types
56//! may still be transported with the [`BinaryDataArrayType::Unknown`] variant, but it will not be directly usable.
57//!
58//! These values may be decoded on the fly or stored for faster repeated access. Coercion with [`DataArray::coerce`] relies on
59//! [`bytemuck`] to do the heavy lifting. Conversion through [`DataArray::convert`] maps between different types but makes copies.
60//!
61//!
62//!
63
64mod encodings;
65mod array;
66mod conversion;
67mod map;
68mod traits;
69
70pub mod utils;
71
72
73pub use encodings::{
74    ArrayRetrievalError, ArrayType, BinaryCompressionType, BinaryDataArrayType, Bytes, as_bytes,
75    delta_decoding, delta_encoding, linear_prediction_decoding, linear_prediction_encoding,
76    to_bytes, vec_as_bytes,
77};
78
79pub use array::{DataArray, DataArraySlice};
80pub use map::{BinaryArrayMap, BinaryArrayMap3D};
81pub use traits::{ByteArrayView, ByteArrayViewMut};
82
83pub use conversion::{
84    ArraysAvailable, BuildArrayMap3DFrom, BuildArrayMapFrom, BuildFromArrayMap, BuildFromArrayMap3D,
85};