Skip to content

Commit

Permalink
move empty_record to header
Browse files Browse the repository at this point in the history
this makes it so we don't need access to the VCF struct in order to make an empty record.
since the header is always available as record.header() then we can create an empty record
  • Loading branch information
brentp committed Nov 18, 2024
1 parent 7f3cfea commit 0dafbdc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/bcf/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use std::ffi;
use std::os::raw::c_char;
use std::rc::Rc;
use std::slice;
use std::str;

Expand Down Expand Up @@ -506,6 +507,13 @@ impl HeaderView {
}
result
}

/// Create an empty record using this header view.
///
/// The record can be reused multiple times.
pub fn empty_record(&self) -> crate::bcf::Record {
crate::bcf::Record::new(Rc::new(self.clone()))
}
}

impl Clone for HeaderView {
Expand Down Expand Up @@ -540,3 +548,25 @@ pub enum TagLength {
Genotypes,
Variable,
}

#[cfg(test)]
mod tests {
use super::*;

Check warning on line 554 in src/bcf/header.rs

View workflow job for this annotation

GitHub Actions / Testing-Features (no-default-features)

unused import: `super::*`

Check warning on line 554 in src/bcf/header.rs

View workflow job for this annotation

GitHub Actions / Testing-Features (all-features)

unused import: `super::*`
use crate::bcf::Reader;

#[test]
fn test_header_view_empty_record() {
// Open a VCF file to get a HeaderView
let vcf = Reader::from_path("test/test_string.vcf").expect("Error opening file");
let header_view = vcf.header.clone();

// Create an empty record from the HeaderView
let record = header_view.empty_record();
eprintln!("{:?}", record.rid());

// Verify the record is properly initialized with default/empty values
assert_eq!(record.rid(), Some(0)); // No chromosome/contig set
assert_eq!(record.pos(), 0); // No position set
assert_eq!(record.qual(), 0.0); // No quality score set
}
}
2 changes: 1 addition & 1 deletion src/bcf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl Read for Reader {

/// Return empty record. Can be reused multiple times.
fn empty_record(&self) -> Record {
Record::new(Rc::clone(&self.header))
self.header.empty_record()
}
}

Expand Down

0 comments on commit 0dafbdc

Please sign in to comment.