Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bam::Record:new should return a valid record #361

Merged
merged 13 commits into from
May 22, 2024
11 changes: 9 additions & 2 deletions src/bam/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,19 @@ fn extranul_from_qname(qname: &[u8]) -> usize {
impl Record {
/// Create an empty BAM record.
pub fn new() -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal in this PR is to that one does not need to call any other methods on Record to have a valid record. Currently, this requires setting the read name and alignment positions (and unmapped flag if the positions are -1). So this PR has an opinion, that a new record is unmapped with empty name/seq/qual/cigar.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for indirectly making me think about Default trait vs new(): https://users.rust-lang.org/t/when-to-use-default-trait/11190

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Record {
let record = Record {
nh13 marked this conversation as resolved.
Show resolved Hide resolved
inner: unsafe { MaybeUninit::zeroed().assume_init() },
own: true,
cigar: None,
header: None,
}
};
// Developer note: these are needed so the returned record is properly
// initialized as unmapped.
record.set_tid(-1);
record.set_pos(-1);
record.set_mpos(-1);
record.set_mtid(-1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would argue that we should also set the record to be unmapped by default. I mean, how can it be mapped if the tid/pos are -1?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable but I'd quickly compare third party implementations (htslib/htsjdk/noodles) and/or spec footnotes (lots of gotchas there).

record
}

pub fn from_inner(from: *mut htslib::bam1_t) -> Self {
Expand Down