Skip to content

Commit

Permalink
fix(index): add optional index_file parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
cauliyang committed Dec 19, 2023
1 parent 2dacf2c commit b5261b2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,6 @@ where
}
}
}

Ok(())
}
15 changes: 9 additions & 6 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn is_coordinate_sorted(header: &sam::Header) -> bool {
false
}

pub fn index_bam<P: AsRef<Path>>(file: P) -> io::Result<()> {
pub fn index_bam<P: AsRef<Path>, W: io::Write>(file: P, index_file: Option<W>) -> io::Result<()> {
let mut reader = bam::reader::Builder.build_from_path(file.as_ref())?;
let header = reader.read_header()?;

Expand Down Expand Up @@ -54,12 +54,15 @@ pub fn index_bam<P: AsRef<Path>>(file: P) -> io::Result<()> {
}

let index = builder.build(header.reference_sequences().len());
// let index_file = file.as_ref().with_extension("bai");

// write to index file
let stdout = io::stdout().lock();
let mut writer = bai::Writer::new(stdout);
writer.write_index(&index)?;
if let Some(index_file) = index_file {
let mut writer = bai::Writer::new(index_file);
writer.write_index(&index)?;
} else {
let stdout = io::stdout().lock();
let mut writer = bai::Writer::new(stdout);
writer.write_index(&index)?;
}

Ok(())
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn main() {

Some(Commands::Index { input }) => {
info!("'index' {input:?} ");
index::index_bam(input).unwrap();
index::index_bam(input, None::<Box<dyn io::Write>>).unwrap();
}

Some(Commands::Fa2fq { input }) => {
Expand Down

0 comments on commit b5261b2

Please sign in to comment.