Hi all,
I am currently learning rust-htslib and working on a Rust script to process BAM files using the rust-htslib crate and expose the functionality to Python using pyo3. My goal is to read an input BAM file (which includes both a header and records) and write the entire content (header + records) to an output BAM file. However, the output BAM file only contains the header, and all the records are missing. My code:
use pyo3::prelude::*;
use rust_htslib::{bam, bam::Read};
#[pyfunction]
fn parse_bam(input_path: &str, output_path: &str) -> PyResult<()> {
// Open BAM file for reading
let mut bam = bam::Reader::from_path(input_path).unwrap();
// Create a header for the output BAM
let header = bam::Header::from_template(bam.header());
// Open BAM file for writing
let mut out = bam::Writer::from_path(output_path, &header, bam::Format::Bam)
.map_err(|e| PyErr::new::<pyo3::exceptions::PyIOError, _>(format!("{}", e)))?;
for r in bam.records() {
let record = r.unwrap();
out.write(&record).unwrap();
}
Ok(())
}
#[pymodule]
fn deidentify_utils(m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(parse_bam, m)?)
}
The input.bam file contains both a header and records (I verified this using samtools view input.bam & pysam). However, the output.bam file only contains the header, and all the records are missing.
Is there something I am missing in how rust-htslib handles BAM writing? Any help or suggestions would be greatly appreciated!
Cross-posted to: https://stackoverflow.com/questions/79737920/rust-htslib-script-only-outputs-bam-header-records-are-missing
If you get an answer there please come back and post it here.