1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use flate2::read::ZlibDecoder;
use std::io::prelude::*;
use crate::io::mzml_parser::types::{Binary, CompressionType, DataType};
use byteorder::{LittleEndian, ReadBytesExt};
use std::io::Cursor;
pub fn decode_binary_array(b: &Binary, ct: &CompressionType, dt: &DataType) -> Vec<f64> {
let decoded = base64::decode(&b.content).expect("Unable to decode binary.");
match (ct, dt) {
(CompressionType::NoCompression, DataType::Float32Bit) => {
binary_string_to_array_f32(decoded)
}
(CompressionType::NoCompression, DataType::Float64Bit) => {
binary_string_to_array_f64(decoded)
}
(CompressionType::ZlibCompression, DataType::Float64Bit) => {
let mut decoded_bytes = Vec::<u8>::new();
let rdr = Cursor::new(decoded);
let mut d = ZlibDecoder::new(rdr);
d.read_to_end(&mut decoded_bytes).unwrap();
binary_string_to_array_f64(decoded_bytes)
}
(CompressionType::ZlibCompression, DataType::Float32Bit) => {
let mut decoded_bytes = Vec::<u8>::new();
let rdr = Cursor::new(decoded);
let mut d = ZlibDecoder::new(rdr);
d.read_to_end(&mut decoded_bytes).unwrap();
binary_string_to_array_f32(decoded_bytes)
}
}
}
pub fn binary_string_to_array_f32(decoded: Vec<u8>) -> Vec<f64> {
let mut rdr = Cursor::new(decoded);
let mut peaks = Vec::<f64>::new();
while let Ok(fl) = rdr.read_f32::<LittleEndian>() {
peaks.push(f64::from(fl));
}
peaks
}
pub fn binary_string_to_array_f64(decoded: Vec<u8>) -> Vec<f64> {
let mut rdr = Cursor::new(decoded);
let mut peaks = Vec::<f64>::new();
while let Ok(fl) = rdr.read_f64::<LittleEndian>() {
peaks.push(fl);
}
peaks
}
#[cfg(test)]
mod tests {
use super::*;
use crate::io::mzml_parser::types::Binary;
use crate::io::mzml_parser::types::DataType;
#[test]
fn parse_mzml_test() {
struct TestData {
binary: Binary,
compression_type: CompressionType,
data_type: DataType,
expected_array: Vec<f64>,
}
impl TestData {
pub fn new(
binary: Binary,
compression_type: CompressionType,
data_type: DataType,
expected_array: Vec<f64>,
) -> Self {
Self {
binary,
compression_type,
data_type,
expected_array,
}
}
}
let tests = vec![
TestData::new(Binary::new(String::from("AAAAAAAALkAAAAAAAAAsQAAAAAAAACpAAAAAAAAAKEAAAAAAAAAmQAAAAAAAACRAAAAAAAAAIkAAAAAAAAAgQAAAAAAAABxAAAAAAAAAGEAAAAAAAAAUQAAAAAAAABBAAAAAAAAACEAAAAAAAAAAQAAAAAAAAPA/")), CompressionType::NoCompression, DataType::Float64Bit, vec![15.0, 14.0, 13.0, 12.0, 11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0]),
TestData::new(Binary::new(String::from("eJxjYEABDhBKAEpLQGkFKK0CpTWgtA6UNoDSRg4AZlQDYw==")), CompressionType::ZlibCompression, DataType::Float64Bit, vec![0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0])
];
for test in tests.iter() {
let array = decode_binary_array(&test.binary, &test.compression_type, &test.data_type);
assert_eq!(array, test.expected_array);
}
}
}