From a7668cd8952d313ddc4585fd6dc2d6442137e2c8 Mon Sep 17 00:00:00 2001 From: Fenner Macrae Date: Thu, 1 Feb 2024 11:17:58 -0800 Subject: [PATCH 1/4] Handle trailing omitted FORMAT records --- src/bcf/mod.rs | 24 ++++++++++++++++++++++++ src/bcf/record.rs | 3 +++ test/test_trailing_omitted_format.vcf | 9 +++++++++ 3 files changed, 36 insertions(+) create mode 100644 test/test_trailing_omitted_format.vcf diff --git a/src/bcf/mod.rs b/src/bcf/mod.rs index 7669911be..f781cddce 100644 --- a/src/bcf/mod.rs +++ b/src/bcf/mod.rs @@ -1548,6 +1548,30 @@ mod tests { ); } + #[test] + fn test_trailing_omitted_format_fields() { + let mut reader = Reader::from_path("test/test_trailing_omitted_format.vcf").unwrap(); + let first_record = reader + .records() + .next() + .unwrap() + .expect("Fail to read record"); + + let expected: Vec<&[u8]> = Vec::new(); + assert_eq!( + *first_record.format(b"STR").string().unwrap(), + expected, + ); + assert_eq!( + *first_record.format(b"INT").integer().unwrap(), + vec![&[i32::missing()]], + ); + assert!( + first_record.format(b"FLT").float().unwrap()[0][0].is_nan(), + ); + } + + // #[test] // fn test_buffer_lifetime() { // let mut reader = Reader::from_path("test/obs-cornercase.vcf").unwrap(); diff --git a/src/bcf/record.rs b/src/bcf/record.rs index b8f18871a..4ac478a92 100644 --- a/src/bcf/record.rs +++ b/src/bcf/record.rs @@ -1329,6 +1329,9 @@ impl<'a, 'b, B: BorrowMut + Borrow + 'b> Info<'a, B> { pub fn string(mut self) -> Result, B>>> { self.data(htslib::BCF_HT_STR).map(|data| { data.map(|ret| { + if ret == 0 { + return BufferBacked::new(Vec::new(), self.buffer); + } BufferBacked::new( unsafe { slice::from_raw_parts(self.buffer.borrow().inner as *const u8, ret as usize) diff --git a/test/test_trailing_omitted_format.vcf b/test/test_trailing_omitted_format.vcf new file mode 100644 index 000000000..68892198b --- /dev/null +++ b/test/test_trailing_omitted_format.vcf @@ -0,0 +1,9 @@ +##fileformat=VCFv4.3 +##contig= +##INFO= +##FORMAT= +##FORMAT= +##FORMAT= +##FORMAT= +#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT SAMPLE1 +chr1 1234 . t a . . FOO=1 GT:STR:FLT:INT . From 4711d1ec90033a41797f4aa5b2e06ae4038eb8ef Mon Sep 17 00:00:00 2001 From: Fenner Macrae Date: Thu, 1 Feb 2024 11:20:05 -0800 Subject: [PATCH 2/4] Remove newline --- src/bcf/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/bcf/mod.rs b/src/bcf/mod.rs index f781cddce..92cc3efb3 100644 --- a/src/bcf/mod.rs +++ b/src/bcf/mod.rs @@ -1571,7 +1571,6 @@ mod tests { ); } - // #[test] // fn test_buffer_lifetime() { // let mut reader = Reader::from_path("test/obs-cornercase.vcf").unwrap(); From 247974871f56c3d150e64bc0212a46362a1b26e2 Mon Sep 17 00:00:00 2001 From: Fenner Macrae Date: Thu, 1 Feb 2024 12:23:41 -0800 Subject: [PATCH 3/4] fix: Wrong if statement --- src/bcf/record.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bcf/record.rs b/src/bcf/record.rs index 4ac478a92..a5778d1ac 100644 --- a/src/bcf/record.rs +++ b/src/bcf/record.rs @@ -1329,9 +1329,6 @@ impl<'a, 'b, B: BorrowMut + Borrow + 'b> Info<'a, B> { pub fn string(mut self) -> Result, B>>> { self.data(htslib::BCF_HT_STR).map(|data| { data.map(|ret| { - if ret == 0 { - return BufferBacked::new(Vec::new(), self.buffer); - } BufferBacked::new( unsafe { slice::from_raw_parts(self.buffer.borrow().inner as *const u8, ret as usize) @@ -1486,6 +1483,9 @@ impl<'a, 'b, B: BorrowMut + Borrow + 'b> Format<'a, B> { /// memory. pub fn string(mut self) -> Result, B>> { self.data(htslib::BCF_HT_STR).map(|ret| { + if ret == 0 { + return BufferBacked::new(Vec::new(), self.buffer); + } BufferBacked::new( unsafe { slice::from_raw_parts(self.buffer.borrow_mut().inner as *const u8, ret as usize) From d8f21f299d3bfda73d6668508bf5b8f64ee642f7 Mon Sep 17 00:00:00 2001 From: Johannes Koester Date: Thu, 22 Feb 2024 11:30:45 +0100 Subject: [PATCH 4/4] fmt --- src/bcf/mod.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/bcf/mod.rs b/src/bcf/mod.rs index 92cc3efb3..e09521599 100644 --- a/src/bcf/mod.rs +++ b/src/bcf/mod.rs @@ -1558,17 +1558,12 @@ mod tests { .expect("Fail to read record"); let expected: Vec<&[u8]> = Vec::new(); - assert_eq!( - *first_record.format(b"STR").string().unwrap(), - expected, - ); + assert_eq!(*first_record.format(b"STR").string().unwrap(), expected,); assert_eq!( *first_record.format(b"INT").integer().unwrap(), vec![&[i32::missing()]], ); - assert!( - first_record.format(b"FLT").float().unwrap()[0][0].is_nan(), - ); + assert!(first_record.format(b"FLT").float().unwrap()[0][0].is_nan(),); } // #[test]