writers.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. use std::{
  2. fs::{self, File, OpenOptions},
  3. io::{BufWriter, Write},
  4. path::Path,
  5. };
  6. use anyhow::Context;
  7. // use bgzip::{BGZFWriter, Compression};
  8. use log::info;
  9. use crate::io::readers::get_reader;
  10. // pub fn get_gz_writer(path: &str, force: bool) -> anyhow::Result<BGZFWriter<File>> {
  11. // if !path.ends_with(".gz") {
  12. // anyhow::bail!("The file should end with gz");
  13. // }
  14. //
  15. // if force && Path::new(path).exists() {
  16. // fs::remove_file(path).with_context(|| anyhow::anyhow!("Failed to remove file: {path}"))?;
  17. // }
  18. //
  19. // let file = OpenOptions::new()
  20. // .write(true) // Open the file for writing
  21. // .create_new(true)
  22. // .truncate(true)
  23. // .open(path)
  24. // .with_context(|| anyhow::anyhow!("failed to open the file: {path}"))?;
  25. //
  26. // info!("Writing into {path}");
  27. // Ok(BGZFWriter::new(file, Compression::default()))
  28. // }
  29. use noodles_bgzf as bgzf;
  30. pub fn get_gz_writer(
  31. path: &str,
  32. force: bool,
  33. ) -> anyhow::Result<bgzf::io::Writer<BufWriter<std::fs::File>>> {
  34. if !path.ends_with(".gz") {
  35. anyhow::bail!("file should end with .gz: {path}");
  36. }
  37. if force && std::path::Path::new(path).exists() {
  38. fs::remove_file(path).with_context(|| format!("failed to remove file: {path}"))?;
  39. }
  40. let file = OpenOptions::new()
  41. .write(true)
  42. .create_new(true)
  43. .open(path)
  44. .with_context(|| format!("failed to create BGZF file: {path}"))?;
  45. Ok(bgzf::io::Writer::new(BufWriter::new(file)))
  46. }
  47. pub fn get_writer(path: &str) -> anyhow::Result<BufWriter<File>> {
  48. let file = OpenOptions::new()
  49. .write(true) // Open the file for writing
  50. .create_new(true)
  51. .truncate(true)
  52. .open(path)
  53. .with_context(|| anyhow::anyhow!("failed to open the file: {path}"))?;
  54. info!("Writing into {path}");
  55. Ok(BufWriter::new(file))
  56. }
  57. pub fn convert_bgz(input: impl AsRef<Path>, force: bool) -> anyhow::Result<()> {
  58. let input = input.as_ref();
  59. let mut reader = get_reader(&input.to_string_lossy())?;
  60. let mut writer = get_gz_writer(&format!("{}.gz", input.display()), force)?;
  61. std::io::copy(&mut reader, &mut writer)?;
  62. writer
  63. .try_finish()
  64. .with_context(|| format!("failed finishing BGZF writer: {}", input.display()))?;
  65. let mut inner = writer
  66. .finish()
  67. .with_context(|| format!("failed returning inner BGZF writer: {}", input.display()))?;
  68. inner
  69. .flush()
  70. .with_context(|| format!("failed flushing inner writer: {}", input.display()))?;
  71. inner
  72. .into_inner()
  73. .with_context(|| format!("failed unwrapping BufWriter: {}", input.display()))?
  74. .sync_all()
  75. .with_context(|| format!("failed syncing file: {}", input.display()))?;
  76. Ok(())
  77. }
  78. pub fn finalize_bgzf_file(
  79. writer: bgzf::io::Writer<BufWriter<File>>,
  80. path: &str,
  81. ) -> anyhow::Result<()> {
  82. let mut inner = writer
  83. .finish()
  84. .with_context(|| format!("failed finishing BGZF writer: {path}"))?;
  85. inner
  86. .flush()
  87. .with_context(|| format!("failed flushing BufWriter: {path}"))?;
  88. inner
  89. .into_inner()
  90. .with_context(|| format!("failed unwrapping BufWriter: {path}"))?
  91. .sync_all()
  92. .with_context(|| format!("failed syncing file: {path}"))?;
  93. Ok(())
  94. }
  95. #[cfg(test)]
  96. mod tests {
  97. use super::*;
  98. use crate::helpers::test_init;
  99. #[test]
  100. fn bgz_convert() -> anyhow::Result<()> {
  101. test_init();
  102. let a = "/mnt/beegfs02/scratch/t_steimle/data/wgs/DUMCO/diag/DUMCO_diag_modkit_pileup.bed";
  103. convert_bgz(a, true)?;
  104. let a = "/mnt/beegfs02/scratch/t_steimle/data/wgs/DUMCO/norm/DUMCO_norm_modkit_pileup.bed";
  105. convert_bgz(a, true)?;
  106. Ok(())
  107. }
  108. }