Skip to content

Commit

Permalink
debugging empty output file
Browse files Browse the repository at this point in the history
  • Loading branch information
brentp committed Jan 6, 2025
1 parent 0eca864 commit 929b196
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/sniff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,25 +79,45 @@ impl io::Read for HtsFile {

impl io::Write for HtsFile {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
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()),
}
Expand All @@ -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");
}
}

Expand Down Expand Up @@ -138,6 +157,7 @@ impl HtsFile {
}

pub fn new(path: &Path, mode: &str) -> io::Result<Self> {
eprintln!("opening file: {:?} with mode: {}", path, mode);
open(path, mode)
}

Expand Down
5 changes: 5 additions & 0 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -210,6 +211,9 @@ impl Writer {
report: &Report,
crs: &[Box<dyn ColumnReporter>],
) -> Result<(), std::io::Error> {
if report.len() == 0 {
return Ok(());
}
match self.format {
hts::htsExactFormat_vcf => {
let vcf_writer = match &mut self.writer {
Expand Down Expand Up @@ -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(|| {
Expand Down

0 comments on commit 929b196

Please sign in to comment.