From 929b196e3e1452000585846b25fb779159980ead Mon Sep 17 00:00:00 2001 From: Brent Pedersen Date: Mon, 6 Jan 2025 10:30:52 -0800 Subject: [PATCH] debugging empty output file --- src/sniff.rs | 40 ++++++++++++++++++++++++++++++---------- src/writer.rs | 5 +++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/sniff.rs b/src/sniff.rs index cf4a702..10722ae 100644 --- a/src/sniff.rs +++ b/src/sniff.rs @@ -2,7 +2,7 @@ use bio::io::bed; use rust_htslib::bcf; pub(crate) use rust_htslib::htslib as hts; use std::fmt; -use std::io; +use std::io::{self, Write}; use std::mem; use std::path::Path; use std::rc::Rc; @@ -79,25 +79,45 @@ impl io::Read for HtsFile { impl io::Write for HtsFile { fn write(&mut self, buf: &[u8]) -> io::Result { - eprintln!("writing!, bgzf: {}", self.fh.is_bgzf()); - assert!(self.fh.is_write() != 0); + eprintln!("Writing {} bytes", buf.len()); + assert!(self.fh.is_write() != 0, "File not opened for writing!"); + assert!(self.is_bgzf(), "File is not BGZF!"); + + // Ensure we have data to write + if buf.is_empty() { + return Ok(0); + } + + // Get a direct pointer to the BGZF structure + let bgzf = unsafe { self.fh.fp.bgzf }; + + // Write the buffer using a direct pointer to the data let n = unsafe { hts::bgzf_write( - self.fh.fp.bgzf as *mut _, - buf.as_ptr() as *const std::os::raw::c_void, + bgzf, + buf.as_ptr() as *const _, // Simplified pointer cast buf.len(), ) }; + eprintln!("bgzf_write returned {}", n); + if n < 0 { return Err(io::Error::last_os_error()); } - unsafe { hts::hts_flush(&mut self.fh) }; - eprintln!("wrote {} bytes", n); + Ok(n as usize) } fn flush(&mut self) -> io::Result<()> { - match unsafe { hts::hts_flush(&mut self.fh) } { + if self.is_bgzf() { + let c = unsafe { hts::bgzf_flush(self.fh.fp.bgzf as *mut _) }; + if c < 0 { + return Err(io::Error::last_os_error()); + } + } + + let result = unsafe { hts::hts_flush(&mut self.fh) }; + match result { 0 => Ok(()), _ => Err(io::Error::last_os_error()), } @@ -106,8 +126,7 @@ impl io::Write for HtsFile { impl Drop for HtsFile { fn drop(&mut self) { - eprintln!("dropping htsfile :{:?}", self); - unsafe { hts::hts_flush(&mut self.fh) }; + self.flush().expect("Failed to flush htsfile"); } } @@ -138,6 +157,7 @@ impl HtsFile { } pub fn new(path: &Path, mode: &str) -> io::Result { + eprintln!("opening file: {:?} with mode: {}", path, mode); open(path, mode) } diff --git a/src/writer.rs b/src/writer.rs index 7ce71ee..30071c8 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -147,6 +147,7 @@ impl Writer { hts::htsCompression_bgzf => "wz", _ => "w", }; + eprintln!("open file: {:?} with mode: {}", path, write_mode); let hf = HtsFile::new(path.as_ref(), write_mode) .map_err(|e| FormatConversionError::HtslibError(e.to_string()))?; let bed_writer = bio::io::bed::Writer::new(hf); @@ -210,6 +211,9 @@ impl Writer { report: &Report, crs: &[Box], ) -> Result<(), std::io::Error> { + if report.len() == 0 { + return Ok(()); + } match self.format { hts::htsExactFormat_vcf => { let vcf_writer = match &mut self.writer { @@ -264,6 +268,7 @@ impl Writer { } } hts::htsExactFormat_bed => { + eprintln!("report: {:?}", report); for fragment in report { // return an error if fragment.a is None let frag_a = fragment.a.as_ref().ok_or_else(|| {