| 1 |
use crate::*; |
| 2 |
use std::collections::HashMap; |
| 3 |
use std::convert::TryInto; |
| 4 |
use std::i32; |
| 5 |
use std::io::{self, Read}; |
| 6 |
|
| 7 |
#[derive(Debug, Clone, PartialEq)] |
| 8 |
pub struct TabixChunk { |
| 9 |
pub begin: u64, |
| 10 |
pub end: u64, |
| 11 |
} |
| 12 |
|
| 13 |
impl TabixChunk { |
| 14 |
fn from_reader<R: Read + BinaryReader>(reader: &mut R) -> io::Result<Self> { |
| 15 |
let begin = reader.read_le_u64()?; |
| 16 |
let end = reader.read_le_u64()?; |
| 17 |
Ok(TabixChunk { begin, end }) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
#[derive(Debug, Clone, PartialEq)] |
| 22 |
pub struct TabixBin { |
| 23 |
pub bin: u32, |
| 24 |
pub number_of_chunk: i32, |
| 25 |
pub chunks: Vec<TabixChunk>, |
| 26 |
} |
| 27 |
|
| 28 |
impl TabixBin { |
| 29 |
fn from_reader<R: Read + BinaryReader>(reader: &mut R) -> io::Result<Self> { |
| 30 |
let bin = reader.read_le_u32()?; |
| 31 |
let number_of_chunk = reader.read_le_i32()?; |
| 32 |
let mut chunks = Vec::new(); |
| 33 |
for _ in 0..number_of_chunk { |
| 34 |
chunks.push(TabixChunk::from_reader(reader)?); |
| 35 |
} |
| 36 |
|
| 37 |
Ok(TabixBin { |
| 38 |
bin, |
| 39 |
number_of_chunk, |
| 40 |
chunks, |
| 41 |
}) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
#[derive(Debug, Clone, PartialEq)] |
| 46 |
pub struct TabixSequence { |
| 47 |
pub number_of_distinct_bin: i32, |
| 48 |
pub bins: HashMap<u32, TabixBin>, |
| 49 |
pub number_of_intervals: i32, |
| 50 |
pub intervals: Vec<u64>, |
| 51 |
} |
| 52 |
|
| 53 |
impl TabixSequence { |
| 54 |
fn from_reader<R: Read + BinaryReader>(reader: &mut R) -> io::Result<Self> { |
| 55 |
let number_of_distinct_bin = reader.read_le_i32()?; |
| 56 |
let mut bins = HashMap::new(); |
| 57 |
for _ in 0..number_of_distinct_bin { |
| 58 |
let one_bin = TabixBin::from_reader(reader)?; |
| 59 |
bins.insert(one_bin.bin, one_bin); |
| 60 |
} |
| 61 |
|
| 62 |
let number_of_intervals = reader.read_le_i32()?; |
| 63 |
|
| 64 |
let mut intervals = Vec::new(); |
| 65 |
for _ in 0..number_of_intervals { |
| 66 |
intervals.push(reader.read_le_u64()?); |
| 67 |
} |
| 68 |
Ok(TabixSequence { |
| 69 |
number_of_distinct_bin, |
| 70 |
bins, |
| 71 |
number_of_intervals, |
| 72 |
intervals, |
| 73 |
}) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
#[derive(Debug, Clone, PartialEq)] |
| 78 |
pub struct Tabix { |
| 79 |
pub number_of_references: i32, |
| 80 |
pub format: i32, |
| 81 |
pub column_for_sequence: i32, |
| 82 |
pub column_for_begin: i32, |
| 83 |
pub column_for_end: i32, |
| 84 |
pub meta: [u8; 4], |
| 85 |
pub skip: i32, |
| 86 |
pub length_of_concatenated_sequence_names: i32, |
| 87 |
pub names: Vec<Vec<u8>>, |
| 88 |
pub sequences: Vec<TabixSequence>, |
| 89 |
} |
| 90 |
|
| 91 |
impl Tabix { |
| 92 |
pub fn from_reader<R: Read>(reader: R) -> Result<Self, crate::BGZFError> { |
| 93 |
let mut reader = io::BufReader::new(crate::read::BGZFReader::new(reader)?); |
| 94 |
|
| 95 |
let mut buf: [u8; 4] = [0, 0, 0, 0]; |
| 96 |
reader.read_exact(&mut buf)?; |
| 97 |
if buf != [b'T', b'B', b'I', 1] { |
| 98 |
return Err(BGZFError::Other("Not Tabix format")); |
| 99 |
} |
| 100 |
let number_of_references = reader.read_le_i32()?; |
| 101 |
let format = reader.read_le_i32()?; |
| 102 |
let column_for_sequence = reader.read_le_i32()?; |
| 103 |
let column_for_begin = reader.read_le_i32()?; |
| 104 |
let column_for_end = reader.read_le_i32()?; |
| 105 |
reader.read_exact(&mut buf)?; |
| 106 |
let meta = buf; |
| 107 |
let skip = reader.read_le_i32()?; |
| 108 |
let length_of_concatenated_sequence_names = reader.read_le_i32()?; |
| 109 |
let mut name_bytes: Vec<u8> = |
| 110 |
vec![0; length_of_concatenated_sequence_names.try_into().unwrap()]; |
| 111 |
reader.read_exact(&mut name_bytes)?; |
| 112 |
let names = split_names(&name_bytes); |
| 113 |
|
| 114 |
let mut sequences = Vec::new(); |
| 115 |
for _ in 0..number_of_references { |
| 116 |
sequences.push(TabixSequence::from_reader(&mut reader)?); |
| 117 |
} |
| 118 |
|
| 119 |
Ok(Tabix { |
| 120 |
number_of_references, |
| 121 |
format, |
| 122 |
column_for_sequence, |
| 123 |
column_for_begin, |
| 124 |
column_for_end, |
| 125 |
meta, |
| 126 |
skip, |
| 127 |
length_of_concatenated_sequence_names, |
| 128 |
names, |
| 129 |
sequences, |
| 130 |
}) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
fn split_names(data: &[u8]) -> Vec<Vec<u8>> { |
| 135 |
let mut reader = io::BufReader::new(data); |
| 136 |
let mut result = Vec::new(); |
| 137 |
|
| 138 |
loop { |
| 139 |
let mut buf = Vec::new(); |
| 140 |
let l = reader.read_until(0, &mut buf).unwrap(); |
| 141 |
if l == 0 { |
| 142 |
break; |
| 143 |
} |
| 144 |
result.push(buf); |
| 145 |
} |
| 146 |
|
| 147 |
result |
| 148 |
} |
| 149 |
|
| 150 |
const MIN_SHIFT: u32 = 14; |
| 151 |
const DEPTH: u32 = 5; |
| 152 |
|
| 153 |
|
| 154 |
pub fn reg2bin(beg: u32, end: u32) -> u32 { |
| 155 |
crate::csi::reg2bin(beg.into(), end.into(), MIN_SHIFT, DEPTH) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
pub fn reg2bins(beg: u32, end: u32) -> Vec<u32> { |
| 160 |
crate::csi::reg2bins(beg.into(), end.into(), MIN_SHIFT, DEPTH) |
| 161 |
} |
| 162 |
|
| 163 |
#[cfg(test)] |
| 164 |
mod test { |
| 165 |
use anyhow::anyhow; |
| 166 |
|
| 167 |
use super::*; |
| 168 |
use std::fs::File; |
| 169 |
use std::str; |
| 170 |
|
| 171 |
#[test] |
| 172 |
fn test_tabix_read() -> anyhow::Result<()> { |
| 173 |
let mut reader = File::open("testfiles/common_all_20180418_half.vcf.gz.tbi")?; |
| 174 |
let tabix = Tabix::from_reader(&mut reader)?; |
| 175 |
|
| 176 |
|
| 177 |
let mut chunks_writer = csv::Writer::from_path("tmp/sequence.csv")?; |
| 178 |
chunks_writer.write_record(&[ |
| 179 |
"sequence name", |
| 180 |
"bin index", |
| 181 |
"bin number", |
| 182 |
"chunk index", |
| 183 |
"chunk begin", |
| 184 |
"chunk end", |
| 185 |
])?; |
| 186 |
|
| 187 |
for (i, one_seq) in tabix.sequences.iter().enumerate() { |
| 188 |
for (j, (_, one_bin)) in one_seq.bins.iter().enumerate() { |
| 189 |
for (k, one_chunk) in one_bin.chunks.iter().enumerate() { |
| 190 |
chunks_writer.write_record(&[ |
| 191 |
str::from_utf8(&tabix.names[i]).unwrap().to_string(), |
| 192 |
format!("{}", j), |
| 193 |
format!("{}", one_bin.bin), |
| 194 |
format!("{}", k), |
| 195 |
format!("{}", one_chunk.begin), |
| 196 |
format!("{}", one_chunk.end), |
| 197 |
])?; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
Ok(()) |
| 203 |
} |
| 204 |
|
| 205 |
#[test] |
| 206 |
fn test_bins() -> anyhow::Result<()> { |
| 207 |
let mut reader = csv::ReaderBuilder::new() |
| 208 |
.delimiter(b'\t') |
| 209 |
.quoting(false) |
| 210 |
.from_reader(flate2::read::MultiGzDecoder::new(File::open( |
| 211 |
"testfiles/bins.tsv.gz", |
| 212 |
)?)); |
| 213 |
for row in reader.records() { |
| 214 |
let row = row?; |
| 215 |
let start: u64 = row.get(1).ok_or_else(|| anyhow!("No Start"))?.parse()?; |
| 216 |
let end: u64 = row.get(2).ok_or_else(|| anyhow!("No End"))?.parse()?; |
| 217 |
let bin: u32 = row.get(3).ok_or_else(|| anyhow!("No Bin"))?.parse()?; |
| 218 |
let mut bins: Vec<u32> = row |
| 219 |
.get(4) |
| 220 |
.ok_or_else(|| anyhow!("No Bins"))? |
| 221 |
.split(',') |
| 222 |
.map(|x| x.parse().expect("Invalid bin")) |
| 223 |
.collect(); |
| 224 |
|
| 225 |
let calculated_bin = reg2bin( |
| 226 |
start.try_into().expect("Too large start"), |
| 227 |
end.try_into().expect("Too large end"), |
| 228 |
); |
| 229 |
let mut calculated_bins = reg2bins( |
| 230 |
start.try_into().expect("Too large start"), |
| 231 |
end.try_into().expect("Too large end"), |
| 232 |
); |
| 233 |
bins.sort(); |
| 234 |
calculated_bins.sort(); |
| 235 |
|
| 236 |
assert_eq!( |
| 237 |
bin, calculated_bin, |
| 238 |
"Start: {} / End: {} / Calculated bin: {} / Expected bin: {}", |
| 239 |
start, end, calculated_bin, bin, |
| 240 |
); |
| 241 |
assert_eq!( |
| 242 |
bins, calculated_bins, |
| 243 |
"Start: {} / End: {} / Calculated bins: {:?} / Expected bins: {:?}", |
| 244 |
start, end, calculated_bins, bins, |
| 245 |
); |
| 246 |
} |
| 247 |
Ok(()) |
| 248 |
} |
| 249 |
} |