|
@@ -1,33 +1,56 @@
|
|
|
use std::{
|
|
use std::{
|
|
|
fs::{self, File, OpenOptions},
|
|
fs::{self, File, OpenOptions},
|
|
|
- io::BufWriter,
|
|
|
|
|
|
|
+ io::{BufWriter, Write},
|
|
|
path::Path,
|
|
path::Path,
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
use anyhow::Context;
|
|
use anyhow::Context;
|
|
|
-use bgzip::{BGZFWriter, Compression};
|
|
|
|
|
|
|
+// use bgzip::{BGZFWriter, Compression};
|
|
|
use log::info;
|
|
use log::info;
|
|
|
|
|
|
|
|
use crate::io::readers::get_reader;
|
|
use crate::io::readers::get_reader;
|
|
|
|
|
|
|
|
-pub fn get_gz_writer(path: &str, force: bool) -> anyhow::Result<BGZFWriter<File>> {
|
|
|
|
|
|
|
+// pub fn get_gz_writer(path: &str, force: bool) -> anyhow::Result<BGZFWriter<File>> {
|
|
|
|
|
+// if !path.ends_with(".gz") {
|
|
|
|
|
+// anyhow::bail!("The file should end with gz");
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// if force && Path::new(path).exists() {
|
|
|
|
|
+// fs::remove_file(path).with_context(|| anyhow::anyhow!("Failed to remove file: {path}"))?;
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// let file = OpenOptions::new()
|
|
|
|
|
+// .write(true) // Open the file for writing
|
|
|
|
|
+// .create_new(true)
|
|
|
|
|
+// .truncate(true)
|
|
|
|
|
+// .open(path)
|
|
|
|
|
+// .with_context(|| anyhow::anyhow!("failed to open the file: {path}"))?;
|
|
|
|
|
+//
|
|
|
|
|
+// info!("Writing into {path}");
|
|
|
|
|
+// Ok(BGZFWriter::new(file, Compression::default()))
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+use noodles_bgzf as bgzf;
|
|
|
|
|
+
|
|
|
|
|
+pub fn get_gz_writer(
|
|
|
|
|
+ path: &str,
|
|
|
|
|
+ force: bool,
|
|
|
|
|
+) -> anyhow::Result<bgzf::io::Writer<BufWriter<std::fs::File>>> {
|
|
|
if !path.ends_with(".gz") {
|
|
if !path.ends_with(".gz") {
|
|
|
- anyhow::bail!("The file should end with gz");
|
|
|
|
|
|
|
+ anyhow::bail!("file should end with .gz: {path}");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if force && Path::new(path).exists() {
|
|
|
|
|
- fs::remove_file(path).with_context(|| anyhow::anyhow!("Failed to remove file: {path}"))?;
|
|
|
|
|
|
|
+ if force && std::path::Path::new(path).exists() {
|
|
|
|
|
+ fs::remove_file(path).with_context(|| format!("failed to remove file: {path}"))?;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
let file = OpenOptions::new()
|
|
let file = OpenOptions::new()
|
|
|
- .write(true) // Open the file for writing
|
|
|
|
|
|
|
+ .write(true)
|
|
|
.create_new(true)
|
|
.create_new(true)
|
|
|
- .truncate(true)
|
|
|
|
|
.open(path)
|
|
.open(path)
|
|
|
- .with_context(|| anyhow::anyhow!("failed to open the file: {path}"))?;
|
|
|
|
|
|
|
+ .with_context(|| format!("failed to create BGZF file: {path}"))?;
|
|
|
|
|
|
|
|
- info!("Writing into {path}");
|
|
|
|
|
- Ok(BGZFWriter::new(file, Compression::default()))
|
|
|
|
|
|
|
+ Ok(bgzf::io::Writer::new(BufWriter::new(file)))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
pub fn get_writer(path: &str) -> anyhow::Result<BufWriter<File>> {
|
|
pub fn get_writer(path: &str) -> anyhow::Result<BufWriter<File>> {
|
|
@@ -50,7 +73,44 @@ pub fn convert_bgz(input: impl AsRef<Path>, force: bool) -> anyhow::Result<()> {
|
|
|
|
|
|
|
|
std::io::copy(&mut reader, &mut writer)?;
|
|
std::io::copy(&mut reader, &mut writer)?;
|
|
|
|
|
|
|
|
- writer.close()?; // or writer.finish()? if gzip encoder
|
|
|
|
|
|
|
+ writer
|
|
|
|
|
+ .try_finish()
|
|
|
|
|
+ .with_context(|| format!("failed finishing BGZF writer: {}", input.display()))?;
|
|
|
|
|
+
|
|
|
|
|
+ let mut inner = writer
|
|
|
|
|
+ .finish()
|
|
|
|
|
+ .with_context(|| format!("failed returning inner BGZF writer: {}", input.display()))?;
|
|
|
|
|
+
|
|
|
|
|
+ inner
|
|
|
|
|
+ .flush()
|
|
|
|
|
+ .with_context(|| format!("failed flushing inner writer: {}", input.display()))?;
|
|
|
|
|
+
|
|
|
|
|
+ inner
|
|
|
|
|
+ .into_inner()
|
|
|
|
|
+ .with_context(|| format!("failed unwrapping BufWriter: {}", input.display()))?
|
|
|
|
|
+ .sync_all()
|
|
|
|
|
+ .with_context(|| format!("failed syncing file: {}", input.display()))?;
|
|
|
|
|
+
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+pub fn finalize_bgzf_file(
|
|
|
|
|
+ writer: bgzf::io::Writer<BufWriter<File>>,
|
|
|
|
|
+ path: &str,
|
|
|
|
|
+) -> anyhow::Result<()> {
|
|
|
|
|
+ let mut inner = writer
|
|
|
|
|
+ .finish()
|
|
|
|
|
+ .with_context(|| format!("failed finishing BGZF writer: {path}"))?;
|
|
|
|
|
+
|
|
|
|
|
+ inner
|
|
|
|
|
+ .flush()
|
|
|
|
|
+ .with_context(|| format!("failed flushing BufWriter: {path}"))?;
|
|
|
|
|
+
|
|
|
|
|
+ inner
|
|
|
|
|
+ .into_inner()
|
|
|
|
|
+ .with_context(|| format!("failed unwrapping BufWriter: {path}"))?
|
|
|
|
|
+ .sync_all()
|
|
|
|
|
+ .with_context(|| format!("failed syncing file: {path}"))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
Ok(())
|
|
|
}
|
|
}
|