Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to read bw files and return maximum value across an interval #67

Closed
wants to merge 8 commits into from
Closed
2 changes: 1 addition & 1 deletion gtars/src/uniwig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use tokio::runtime;
pub mod cli;
pub mod counting;
pub mod reading;
mod utils;
pub mod utils;
pub mod writing;

pub mod consts {
Expand Down
25 changes: 25 additions & 0 deletions gtars/src/uniwig/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
use bigtools::{BigWigRead, BigWigReadOpenError};
use bigtools::utils::reopen::ReopenableFile;
use crate::uniwig::reading::{read_bed_vec, read_narrow_peak_vec};
use crate::uniwig::{Chromosome, FileType};



/// Read in bigwig file and return as object.
pub fn read_bw_file(path_to_bw: &str) -> Result<BigWigRead<ReopenableFile>, BigWigReadOpenError> {
let bigwig = BigWigRead::open_file(path_to_bw);

bigwig

}

/// Get maximum value across an interval over a specific chromosome within a bigwig file
pub fn get_max_val_chr_bw(mut bigwig: BigWigRead<ReopenableFile>, chr_name: &str, start: u32, end: u32) -> Option<f32> {

let bw_values = bigwig.values(chr_name,start,end).unwrap();

let max = bw_values.into_iter()
.reduce(f32::max)
;

max
}


/// Attempt to compress counts before writing to bedGraph
pub fn compress_counts(
count_results: &mut (Vec<u32>, Vec<i32>),
Expand Down
Binary file added gtars/tests/data/test_all_core.bw
Binary file not shown.
20 changes: 20 additions & 0 deletions gtars/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ fn path_to_bed_file() -> &'static str {
"tests/data/peaks.bed"
}

#[fixture]
fn path_to_bw_file() -> &'static str {
"tests/data/test_all_core.bw"
}

#[fixture]
fn path_to_sorted_small_bed_file() -> &'static str {
"tests/data/test_sorted_small.bed"
Expand Down Expand Up @@ -87,6 +92,7 @@ mod tests {
};

use gtars::uniwig::writing::write_bw_files;
use gtars::uniwig::utils::{get_max_val_chr_bw, read_bw_file};

use anyhow::Context;
use byteorder::{LittleEndian, ReadBytesExt};
Expand Down Expand Up @@ -1115,4 +1121,18 @@ mod tests {

Ok(())
}


#[rstest]
fn test_read_bw_and_get_max(
path_to_bw_file: &str,
) {
let bw_path = path_to_bw_file;

let bw_info = read_bw_file(bw_path).unwrap();

let max = get_max_val_chr_bw(bw_info, "chr1",0,29).unwrap();

assert_eq!(max, 4.0);
}
}