| 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 |
mod error; |
| 63 |
|
| 64 |
pub(crate) mod csi; |
| 65 |
pub mod deflate; |
| 66 |
|
| 67 |
pub mod header; |
| 68 |
pub mod index; |
| 69 |
pub mod read; |
| 70 |
pub use deflate::Compression; |
| 71 |
|
| 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 |
|
| 81 |
|
| 82 |
|
| 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 |
} |