Skip to content

Commit d4f8acb

Browse files
authored
Fix runaway default denominator (#107)
* Ensure that default denominator doesn't grow too large * Update test for fractional bedgraph
1 parent 46ccfff commit d4f8acb

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

d4tools/src/create/main.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ impl CreateAppCtx {
199199
let mut purposed_denominator = 1.0f64;
200200
let mut num_of_intervals = 0;
201201
let mut genome_size = 0;
202+
let mut max_value = 0.0f64;
202203
for (chr_name, chr_size) in bw_file.chroms() {
203204
genome_size += chr_size;
204205
if let Some(result) = bw_file.query_range(&chr_name, 0, chr_size as u32) {
@@ -216,9 +217,16 @@ impl CreateAppCtx {
216217
}
217218

218219
purposed_denominator = purposed_denominator.max(denominator);
220+
max_value = max_value.max(value.abs());
219221
}
220222
}
221223
}
224+
225+
// Reduce the denominator if the max value is too large to fit in i32
226+
while max_value * purposed_denominator > i32::MAX as f64 {
227+
purposed_denominator /= 10.0;
228+
}
229+
222230
if auto_dict_detection && num_of_intervals * 10 < genome_size * 6 {
223231
self.builder
224232
.set_dictionary(Dictionary::new_simple_range_dict(0, 1)?);
@@ -234,6 +242,7 @@ impl CreateAppCtx {
234242
fn detect_default_denominator_for_bedgraph(&mut self) -> Result<(), DynErr> {
235243
let input = parse_bed_file(self.input_path.as_path())?;
236244
let mut purposed_denominator = 1.0f64;
245+
let mut max_value = 0.0f64;
237246

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

248257
purposed_denominator = purposed_denominator.max(denominator);
258+
max_value = max_value.max(value.abs());
259+
}
260+
261+
// Reduce the denominator if the max value is too large to fit in i32
262+
while max_value * purposed_denominator > i32::MAX as f64 {
263+
purposed_denominator /= 10.0;
249264
}
250265

251266
if purposed_denominator != 1.0 {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
chr1 1000
22
chr2 1000
3+
chrM 1000
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
chr1 100 200 0.2
22
chr2 300 400 10.8
3+
chrM 100 200 33262.09
4+
chrM 200 300 8284.95

d4tools/test/create/fractional-bedgraph/output.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ chr1 200 1000 0
44
chr2 0 300 0
55
chr2 300 400 10.8
66
chr2 400 1000 0
7+
chrM 0 100 0
8+
chrM 100 200 33262.09
9+
chrM 200 300 8284.95
10+
chrM 300 1000 0

0 commit comments

Comments
 (0)