| 1 |
|
| 2 |
|
| 3 |
use std::convert::TryInto; |
| 4 |
|
| 5 |
use crate::{BGZFError, BinaryReader}; |
| 6 |
|
| 7 |
|
| 8 |
#[derive(Debug, Clone, PartialEq, Default)] |
| 9 |
pub struct BGZFIndex { |
| 10 |
pub(crate) entries: Vec<BGZFIndexEntry>, |
| 11 |
} |
| 12 |
|
| 13 |
impl BGZFIndex { |
| 14 |
pub(crate) fn new() -> Self { |
| 15 |
BGZFIndex::default() |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
pub fn entries(&self) -> &[BGZFIndexEntry] { |
| 20 |
&self.entries |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
pub fn from_reader<R: std::io::Read>(mut reader: R) -> std::io::Result<Self> { |
| 25 |
let num_entries = reader.read_le_u64()?; |
| 26 |
let mut result = BGZFIndex::default(); |
| 27 |
for _ in 0..num_entries { |
| 28 |
let compressed_offset = reader.read_le_u64()?; |
| 29 |
let uncompressed_offset = reader.read_le_u64()?; |
| 30 |
result.entries.push(BGZFIndexEntry { |
| 31 |
compressed_offset, |
| 32 |
uncompressed_offset, |
| 33 |
}) |
| 34 |
} |
| 35 |
Ok(result) |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
pub fn write<W: std::io::Write>(&self, mut writer: W) -> std::io::Result<()> { |
| 40 |
let entries: u64 = self.entries.len().try_into().unwrap(); |
| 41 |
writer.write_all(&entries.to_le_bytes())?; |
| 42 |
for one in &self.entries { |
| 43 |
writer.write_all(&one.compressed_offset.to_le_bytes())?; |
| 44 |
writer.write_all(&one.uncompressed_offset.to_le_bytes())?; |
| 45 |
} |
| 46 |
Ok(()) |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
pub fn uncompressed_pos_to_bgzf_pos(&self, pos: u64) -> Result<u64, BGZFError> { |
| 51 |
let i = self |
| 52 |
.entries |
| 53 |
.partition_point(|x| x.uncompressed_offset <= pos); |
| 54 |
let entry = match i { |
| 55 |
0 => BGZFIndexEntry { |
| 56 |
compressed_offset: 0, |
| 57 |
uncompressed_offset: 0, |
| 58 |
}, |
| 59 |
i => self.entries[i - 1].clone(), |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
Ok((entry.compressed_offset << 16) + ((pos - entry.uncompressed_offset) & ((1 << 16) - 1))) |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
pub fn bgzf_pos_to_uncompressed_pos(&self, bgzf_pos: u64) -> Result<u64, BGZFError> { |
| 73 |
let compressed_pos = bgzf_pos >> 16; |
| 74 |
if compressed_pos == 0 { |
| 75 |
return Ok(bgzf_pos); |
| 76 |
} |
| 77 |
let i = self |
| 78 |
.entries |
| 79 |
.binary_search_by(|x| x.compressed_offset.cmp(&compressed_pos)) |
| 80 |
.map_err(|_| BGZFError::Other("Invalid BGZF position"))?; |
| 81 |
Ok(self.entries[i].uncompressed_offset + (bgzf_pos & ((1 << 16) - 1))) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 87 |
pub struct BGZFIndexEntry { |
| 88 |
pub compressed_offset: u64, |
| 89 |
pub uncompressed_offset: u64, |
| 90 |
} |
| 91 |
|
| 92 |
#[cfg(test)] |
| 93 |
mod test { |
| 94 |
use super::*; |
| 95 |
use crate::{BGZFWriter, Compression}; |
| 96 |
use std::fs; |
| 97 |
use std::io::prelude::*; |
| 98 |
|
| 99 |
#[test] |
| 100 |
fn test_index_read_write() -> anyhow::Result<()> { |
| 101 |
let data = fs::read("testfiles/generated.bed.gz.gzi")?; |
| 102 |
let index = BGZFIndex::from_reader(&data[..])?; |
| 103 |
assert_eq!(index.entries.len(), 295); |
| 104 |
let mut generated_data = Vec::new(); |
| 105 |
index.write(&mut generated_data)?; |
| 106 |
assert_eq!(data, generated_data); |
| 107 |
|
| 108 |
Ok(()) |
| 109 |
} |
| 110 |
|
| 111 |
#[test] |
| 112 |
fn test_index_position_convert() -> anyhow::Result<()> { |
| 113 |
let mut data_reader = std::io::BufReader::new(flate2::read::MultiGzDecoder::new( |
| 114 |
fs::File::open("testfiles/generated.bed.gz")?, |
| 115 |
)); |
| 116 |
let mut line = String::new(); |
| 117 |
let mut line_list = Vec::new(); |
| 118 |
let mut writer = BGZFWriter::new( |
| 119 |
fs::File::create("tmp/test_index_position_convert.bed.gz")?, |
| 120 |
Compression::default(), |
| 121 |
); |
| 122 |
|
| 123 |
loop { |
| 124 |
let bgzf_pos = writer.bgzf_pos(); |
| 125 |
let uncompressed_pos = writer.pos(); |
| 126 |
line.clear(); |
| 127 |
let size = data_reader.read_line(&mut line)?; |
| 128 |
if size == 0 { |
| 129 |
break; |
| 130 |
} |
| 131 |
writer.write_all(&line.as_bytes())?; |
| 132 |
line_list.push((bgzf_pos, uncompressed_pos, line.clone())); |
| 133 |
} |
| 134 |
let index = writer.close()?.unwrap(); |
| 135 |
|
| 136 |
for (bgzf_pos, uncompressed_pos, _) in &line_list { |
| 137 |
assert_eq!( |
| 138 |
index.bgzf_pos_to_uncompressed_pos(*bgzf_pos)?, |
| 139 |
*uncompressed_pos |
| 140 |
); |
| 141 |
assert_eq!( |
| 142 |
index.uncompressed_pos_to_bgzf_pos(*uncompressed_pos)?, |
| 143 |
*bgzf_pos |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
Ok(()) |
| 148 |
} |
| 149 |
} |