Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions d4tools/src/create/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ impl CreateAppCtx {
let mut purposed_denominator = 1.0f64;
let mut num_of_intervals = 0;
let mut genome_size = 0;
let mut max_value = 0.0f64;
for (chr_name, chr_size) in bw_file.chroms() {
genome_size += chr_size;
if let Some(result) = bw_file.query_range(&chr_name, 0, chr_size as u32) {
Expand All @@ -216,9 +217,16 @@ impl CreateAppCtx {
}

purposed_denominator = purposed_denominator.max(denominator);
max_value = max_value.max(value.abs());
}
}
}

// Reduce the denominator if the max value is too large to fit in i32
while max_value * purposed_denominator > i32::MAX as f64 {
purposed_denominator /= 10.0;
}

if auto_dict_detection && num_of_intervals * 10 < genome_size * 6 {
self.builder
.set_dictionary(Dictionary::new_simple_range_dict(0, 1)?);
Expand All @@ -234,6 +242,7 @@ impl CreateAppCtx {
fn detect_default_denominator_for_bedgraph(&mut self) -> Result<(), DynErr> {
let input = parse_bed_file(self.input_path.as_path())?;
let mut purposed_denominator = 1.0f64;
let mut max_value = 0.0f64;

for (_, _, _, value) in input {
if value.abs() < 1e-10 {
Expand All @@ -246,6 +255,12 @@ impl CreateAppCtx {
}

purposed_denominator = purposed_denominator.max(denominator);
max_value = max_value.max(value.abs());
}

// Reduce the denominator if the max value is too large to fit in i32
while max_value * purposed_denominator > i32::MAX as f64 {
purposed_denominator /= 10.0;
}

if purposed_denominator != 1.0 {
Expand Down
1 change: 1 addition & 0 deletions d4tools/test/create/fractional-bedgraph/genome.size
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
chr1 1000
chr2 1000
chrM 1000
2 changes: 2 additions & 0 deletions d4tools/test/create/fractional-bedgraph/input.bedgraph
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
chr1 100 200 0.2
chr2 300 400 10.8
chrM 100 200 33262.09
chrM 200 300 8284.95
4 changes: 4 additions & 0 deletions d4tools/test/create/fractional-bedgraph/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ chr1 200 1000 0
chr2 0 300 0
chr2 300 400 10.8
chr2 400 1000 0
chrM 0 100 0
chrM 100 200 33262.09
chrM 200 300 8284.95
chrM 300 1000 0