Skip to content

Commit

Permalink
csi/reader: Fix column indicies being off-by-one when reading a tabix…
Browse files Browse the repository at this point in the history
… header

Fixes #215.
  • Loading branch information
zaeleus committed Nov 13, 2023
1 parent 75c53f7 commit a8dde6e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
3 changes: 3 additions & 0 deletions noodles-csi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

### Fixed

* csi/reader: Fix column indices being off-by-one when reading a tabix
header ([#215]).

* csi/writer: Fix column indices being off-by-one when writing a tabix
header ([#215]).

Expand Down
47 changes: 43 additions & 4 deletions noodles-csi/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,32 @@ where
})?;

let col_seq = reader.read_i32::<LittleEndian>().and_then(|i| {
usize::try_from(i).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
usize::try_from(i)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
.and_then(|n| {
n.checked_sub(1)
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "invalid col_seq"))
})
})?;

let col_beg = reader.read_i32::<LittleEndian>().and_then(|i| {
usize::try_from(i).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
usize::try_from(i)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
.and_then(|n| {
n.checked_sub(1)
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "invalid col_beg"))
})
})?;

let col_end = reader.read_i32::<LittleEndian>().and_then(|i| match i {
0 => Ok(None),
_ => usize::try_from(i)
.map(Some)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)),
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
.and_then(|n| {
n.checked_sub(1)
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "invalid col_end"))
})
.map(Some),
})?;

let meta = reader
Expand Down Expand Up @@ -385,6 +399,31 @@ mod tests {
));
}

#[test]
fn test_read_header() -> io::Result<()> {
let src = [
0x02, 0x00, 0x00, 0x00, // format = 2 (VCF)
0x01, 0x00, 0x00, 0x00, // col_seq = 1 (1-based)
0x02, 0x00, 0x00, 0x00, // col_beg = 2 (1-based)
0x00, 0x00, 0x00, 0x00, // col_end = None (1-based)
0x23, 0x00, 0x00, 0x00, // meta = '#'
0x00, 0x00, 0x00, 0x00, // skip = 0
0x04, 0x00, 0x00, 0x00, // l_nm = 4
b's', b'q', b'0', 0x00, // names = ["sq0"]
];
let mut reader = &src[..];

let actual = read_header(&mut reader)?;

let expected = crate::index::header::Builder::vcf()
.set_reference_sequence_names([String::from("sq0")].into_iter().collect())
.build();

assert_eq!(actual, expected);

Ok(())
}

#[test]
fn test_read_bins() -> io::Result<()> {
const DEPTH: u8 = 5;
Expand Down

0 comments on commit a8dde6e

Please sign in to comment.