Skip to content

Commit

Permalink
feat: allow BGZ files for guess_sv_caller (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
holtgrewe authored Oct 6, 2023
1 parent 7fafcbf commit 31dc430
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/annotate/strucvars/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::path::Path;
use std::str::FromStr;
use std::{fs::File, io::BufWriter};

use crate::common::GenomeRelease;
use crate::common::{open_read_maybe_gz, GenomeRelease};
use crate::ped::PedigreeByName;
use annonars::common::cli::CANONICAL;
use annonars::freqs::cli::import::reading::guess_assembly;
Expand Down Expand Up @@ -1631,7 +1631,8 @@ pub fn guess_sv_caller<P>(p: P) -> Result<SvCaller, anyhow::Error>
where
P: AsRef<Path>,
{
let mut reader = noodles_vcf::reader::Builder.build_from_path(p)?;
let reader = open_read_maybe_gz(p)?;
let mut reader = noodles_vcf::reader::Builder.build_from_reader(reader)?;
let header = reader.read_header()?;
let mut records = reader.records(&header);
let record = records
Expand Down
60 changes: 59 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
//! Commonly used code.
use std::ops::Range;
use std::{
fs::File,
io::{BufRead, BufReader},
ops::Range,
path::Path,
};

use byte_unit::Byte;
use clap::Parser;
use clap_verbosity_flag::{InfoLevel, Verbosity};
use flate2::bufread::MultiGzDecoder;
use hgvs::static_data::Assembly;

/// Commonly used command line arguments.
Expand Down Expand Up @@ -92,6 +98,24 @@ pub fn version() -> &'static str {
return VERSION;
}

/// Transparently open a file with gzip decoder.
pub fn open_read_maybe_gz<P>(path: P) -> Result<Box<dyn BufRead>, anyhow::Error>
where
P: AsRef<Path>,
{
if path.as_ref().extension().map(|s| s.to_str()) == Some(Some("gz")) {
tracing::trace!("Opening {:?} as gzip for reading", path.as_ref());
let file = File::open(path)?;
let bufreader = BufReader::new(file);
let decoder = MultiGzDecoder::new(bufreader);
Ok(Box::new(BufReader::new(decoder)))
} else {
tracing::trace!("Opening {:?} as plain text for reading", path.as_ref());
let file = File::open(path).map(BufReader::new)?;
Ok(Box::new(BufReader::new(file)))
}
}

/// Version information that is returned by the HTTP server.
#[derive(serde::Serialize, serde::Deserialize, Default, Debug, Clone)]
#[serde_with::skip_serializing_none]
Expand All @@ -114,3 +138,37 @@ impl Version {
}
}
}

#[macro_export]
macro_rules! set_snapshot_suffix {
($($expr:expr),*) => {
let mut settings = insta::Settings::clone_current();
settings.set_snapshot_suffix(format!($($expr,)*));
let _guard = settings.bind_to_scope();
}
}

pub use set_snapshot_suffix;

#[cfg(test)]
mod test {
#[rstest::rstest]
#[case(true)]
#[case(false)]
fn open_read_maybe_gz(#[case] is_gzip: bool) -> Result<(), anyhow::Error> {
crate::common::set_snapshot_suffix!("{:?}", is_gzip);

let mut f = super::open_read_maybe_gz(if is_gzip {
"tests/common/test.txt.gz"
} else {
"tests/common/test.txt"
})?;

let mut buf = String::new();
f.read_to_string(&mut buf)?;

insta::assert_snapshot!(&buf);

Ok(())
}
}
6 changes: 6 additions & 0 deletions src/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: src/common.rs
expression: "&buf"
---
payload

6 changes: 6 additions & 0 deletions src/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: src/common.rs
expression: "&buf"
---
payload

1 change: 1 addition & 0 deletions tests/common/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
payload
Binary file added tests/common/test.txt.gz
Binary file not shown.

0 comments on commit 31dc430

Please sign in to comment.