Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! bgzip-rs
2 //! ========
3 //! [![Build](https://github.com/informationsea/bgzip-rs/actions/workflows/build.yml/badge.svg)](https://github.com/informationsea/bgzip-rs/actions/workflows/build.yml)
4 //! [![Crates.io](https://img.shields.io/crates/v/bgzip)](https://crates.io/crates/bgzip)
5 //! [![Crates.io](https://img.shields.io/crates/d/bgzip)](https://crates.io/crates/bgzip)
6 //! [![Crates.io](https://img.shields.io/crates/l/bgzip)](https://crates.io/crates/bgzip)
7 //! [![doc-rs](https://docs.rs/bgzip/badge.svg)](https://docs.rs/bgzip)
8 //!
9 //!
10 //! Rust implementation of [BGZF format](https://samtools.github.io/hts-specs/SAMv1.pdf)
11 //!
12 //! Feature flags
13 //! -------------
14 //!
15 //! * `rayon`: Enable [rayon](https://github.com/rayon-rs/rayon) based multi-threaded writer. This is default feature.
16 //! * `log`: Enable [log](https://github.com/rust-lang/log) crate to log warnings. This is default feature.
17 //! * `rust_backend`: use use [miniz_oxide](https://crates.io/crates/miniz_oxide) crate for [flate2](https://github.com/rust-lang/flate2-rs) backend. This is default feature.
18 //! * `zlib`: use `zlib` for flate2 backend. Please read [flate2](https://github.com/rust-lang/flate2-rs) description for the detail.
19 //! * `zlib-ng`: use `zlib-ng` for flate2 backend. Please read [flate2](https://github.com/rust-lang/flate2-rs) description for the detail.
20 //! * `zlib-ng-compat`: Please read [flate2](https://github.com/rust-lang/flate2-rs) description for the detail.
21 //! * `cloudflare_zlib`: Please read [flate2](https://github.com/rust-lang/flate2-rs) description for the detail.
22 //! * `libdeflater`: use [libdeflater](https://github.com/adamkewley/libdeflater) instead of [flate2](https://github.com/rust-lang/flate2-rs) crate.
23 //!
24 //! Write Examples
25 //! --------
26 //! ```rust
27 //! use bgzip::{BGZFWriter, BGZFError, Compression};
28 //! use std::io::{self, Write};
29 //! fn main() -> Result<(), BGZFError> {
30 //! let mut write_buffer = Vec::new();
31 //! let mut writer = BGZFWriter::new(&mut write_buffer, Compression::default());
32 //! writer.write_all(b"##fileformat=VCFv4.2\n")?;
33 //! writer.write_all(b"#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n")?;
34 //! writer.close()?;
35 //! Ok(())
36 //! }
37 //! ```
38 //!
39 //! Multi-thread support is available via [`write::BGZFMultiThreadWriter`]. `rayon` flag is required to use this feature.
40 //!
41 //! Read Examples
42 //! --------
43 //! ```rust
44 //! use bgzip::{BGZFReader, BGZFError};
45 //! use std::io::{self, BufRead};
46 //! use std::fs;
47 //! fn main() -> Result<(), BGZFError> {
48 //! let mut reader =
49 //! BGZFReader::new(fs::File::open("testfiles/common_all_20180418_half.vcf.gz")?)?;
50 //! let mut line = String::new();
51 //! reader.read_line(&mut line)?;
52 //! assert_eq!("##fileformat=VCFv4.0\n", line);
53 //! reader.bgzf_seek(4210818610)?;
54 //! line.clear();
55 //! reader.read_line(&mut line)?;
56 //! assert_eq!("1\t72700625\trs12116859\tT\tA,C\t.\t.\tRS=12116859;RSPOS=72700625;dbSNPBuildID=120;SSR=0;SAO=0;VP=0x05010008000517053e000100;GENEINFO=LOC105378798:105378798;WGT=1;VC=SNV;SLO;INT;ASP;VLD;G5A;G5;HD;GNO;KGPhase1;KGPhase3;CAF=0.508,.,0.492;COMMON=1;TOPMED=0.37743692660550458,0.00608435270132517,0.61647872069317023\n", line);
57 //!
58 //! Ok(())
59 //! }
60 //! ```
61
62 mod error;
63
64 pub(crate) mod csi;
65 pub mod deflate;
66 /// BGZ header parser
67 pub mod header;
68 pub mod index;
69 pub mod read;
70 pub use deflate::Compression;
71 /// Tabix file parser. (This module is alpha state.)
72 pub mod tabix;
73 pub mod write;
74 pub use error::BGZFError;
75 pub use read::BGZFReader;
76 pub use write::BGZFWriter;
77
78 use std::io;
79
80 /// End-of-file maker.
81 ///
82 /// This marker should be written at end of the BGZF files.
83 pub const EOF_MARKER: [u8; 28] = [
84 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x06, 0x00, 0x42, 0x43, 0x02, 0x00,
85 0x1b, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86 ];
87
88 pub(crate) trait BinaryReader: io::Read {
89 fn read_le_u8(&mut self) -> io::Result<u8> {
90 let mut buf: [u8; 1] = [0];
91 self.read_exact(&mut buf)?;
92 Ok(u8::from_le_bytes(buf))
93 }
94 fn read_le_u16(&mut self) -> io::Result<u16> {
95 let mut buf: [u8; 2] = [0, 0];
96 self.read_exact(&mut buf)?;
97 Ok(u16::from_le_bytes(buf))
98 }
99 fn read_le_u32(&mut self) -> io::Result<u32> {
100 let mut buf: [u8; 4] = [0, 0, 0, 0];
101 self.read_exact(&mut buf)?;
102 Ok(u32::from_le_bytes(buf))
103 }
104 fn read_le_i32(&mut self) -> io::Result<i32> {
105 let mut buf: [u8; 4] = [0, 0, 0, 0];
106 self.read_exact(&mut buf)?;
107 Ok(i32::from_le_bytes(buf))
108 }
109 fn read_le_u64(&mut self) -> io::Result<u64> {
110 let mut buf: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
111 self.read_exact(&mut buf)?;
112 Ok(u64::from_le_bytes(buf))
113 }
114 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
115 let mut tmp = [0u8];
116 let mut total_bytes: usize = 0;
117 loop {
118 let l = self.read(&mut tmp)?;
119 if l == 0 {
120 break;
121 }
122 buf.extend_from_slice(&tmp);
123 total_bytes += 1;
124 if tmp[0] == byte {
125 break;
126 }
127 }
128
129 Ok(total_bytes)
130 }
131 }
132
133 impl<R: io::Read> BinaryReader for R {}
134
135 #[cfg(test)]
136 mod test {
137 use crate::index::BGZFIndex;
138
139 use super::*;
140 use std::fs;
141 use std::io::{BufRead, Write};
142 #[test]
143 fn test_run() -> Result<(), BGZFError> {
144 let mut write_buffer = Vec::new();
145 let mut writer = BGZFWriter::new(&mut write_buffer, Compression::default());
146 writer.write_all(b"##fileformat=VCFv4.2\n")?;
147 writer.write_all(b"#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n")?;
148 writer.close()?;
149 Ok(())
150 }
151
152 #[test]
153 fn test_read() -> Result<(), BGZFError> {
154 let mut reader =
155 BGZFReader::new(fs::File::open("testfiles/common_all_20180418_half.vcf.gz")?)?;
156 let mut line = String::new();
157 reader.read_line(&mut line)?;
158 assert_eq!("##fileformat=VCFv4.0\n", line);
159 reader.bgzf_seek(4210818610)?;
160 line.clear();
161 reader.read_line(&mut line)?;
162 assert_eq!("1\t72700625\trs12116859\tT\tA,C\t.\t.\tRS=12116859;RSPOS=72700625;dbSNPBuildID=120;SSR=0;SAO=0;VP=0x05010008000517053e000100;GENEINFO=LOC105378798:105378798;WGT=1;VC=SNV;SLO;INT;ASP;VLD;G5A;G5;HD;GNO;KGPhase1;KGPhase3;CAF=0.508,.,0.492;COMMON=1;TOPMED=0.37743692660550458,0.00608435270132517,0.61647872069317023\n", line);
163
164 Ok(())
165 }
166
167 #[test]
168 fn test_read_all() -> Result<(), BGZFError> {
169 let reader = BGZFReader::new(fs::File::open("testfiles/common_all_20180418_half.vcf.gz")?)?;
170 let expected_reader = std::io::BufReader::new(flate2::read::MultiGzDecoder::new(
171 fs::File::open("testfiles/common_all_20180418_half.vcf.gz")?,
172 ));
173 for (line1, line2) in reader.lines().zip(expected_reader.lines()) {
174 assert_eq!(line1?, line2?);
175 }
176 Ok(())
177 }
178
179 #[test]
180 fn test_index_read_write() -> anyhow::Result<()> {
181 let data = fs::read("testfiles/generated.bed.gz.gzi")?;
182 let index = BGZFIndex::from_reader(&data[..])?;
183 assert_eq!(index.entries.len(), 295);
184 let mut generated_data = Vec::new();
185 index.write(&mut generated_data)?;
186 assert_eq!(data, generated_data);
187
188 Ok(())
189 }
190 }