diff --git a/d4tools/src/create/main.rs b/d4tools/src/create/main.rs index 0fdb1a5..fdad390 100644 --- a/d4tools/src/create/main.rs +++ b/d4tools/src/create/main.rs @@ -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) { @@ -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)?); @@ -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 { @@ -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 { diff --git a/d4tools/test/create/fractional-bedgraph/genome.size b/d4tools/test/create/fractional-bedgraph/genome.size index 7c19d95..d2cb24c 100644 --- a/d4tools/test/create/fractional-bedgraph/genome.size +++ b/d4tools/test/create/fractional-bedgraph/genome.size @@ -1,2 +1,3 @@ chr1 1000 chr2 1000 +chrM 1000 diff --git a/d4tools/test/create/fractional-bedgraph/input.bedgraph b/d4tools/test/create/fractional-bedgraph/input.bedgraph index 809b228..1a85ee6 100644 --- a/d4tools/test/create/fractional-bedgraph/input.bedgraph +++ b/d4tools/test/create/fractional-bedgraph/input.bedgraph @@ -1,2 +1,4 @@ chr1 100 200 0.2 chr2 300 400 10.8 +chrM 100 200 33262.09 +chrM 200 300 8284.95 \ No newline at end of file diff --git a/d4tools/test/create/fractional-bedgraph/output.txt b/d4tools/test/create/fractional-bedgraph/output.txt index 126fa46..6e0a357 100644 --- a/d4tools/test/create/fractional-bedgraph/output.txt +++ b/d4tools/test/create/fractional-bedgraph/output.txt @@ -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