Skip to content

Commit

Permalink
feat: move empty_record to header (#453)
Browse files Browse the repository at this point in the history
* move empty_record to header

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

* wider thiserror
  • Loading branch information
brentp authored Dec 2, 2024
1 parent d9fe03a commit 797965c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ newtype_derive = "0.1"
regex = "1.3"
serde = {version = "^1", optional = true, features = ["derive"]}
serde_bytes = {version = "0.11", optional = true}
thiserror = "2"
thiserror = {version = "^2" }
url = "2.1"

[features]
Expand Down
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 797965c

Please sign in to comment.