Skip to content

fix: ZeroDivisionError in _ccbr_counts2density.py when flanking TSS bins have zero reads #122

Closed

Description

@kopardev

Summary

atac_tss rule crashes with ZeroDivisionError when running on genomes (e.g. hs1_chrR) where some genes have zero read coverage in the outermost TSS flanking bins.

Discovered during the Biowulf test run for PR #120.


Error

Traceback (most recent call last):
  File "_ccbr_counts2density.py", line 22, in <module>
    density[n2bin[i]] = density[n2bin[i]] * 1.0 / flankavg
ZeroDivisionError: float division by zero

Rule: atac_tss
Script: workflow/scripts/_ccbr_counts2density.py (line 22)


Root cause

flanksum = 0
for i in range(1, 11):       # first 10 bins: positions -1995 to -1905
    flanksum += density[n2bin[i]]
for i in range(391, 401):    # last 10 bins: positions +1905 to +1995
    flanksum += density[n2bin[i]]
flankavg = flanksum * 1.0 / 20   # <-- crashes if flanksum == 0

flankavg is the mean read depth across the 10 outermost bins on each side of the ±2 kb TSS window. If a gene (e.g. on chrR) has zero reads in all 20 flanking bins, flankavg = 0 and the normalization on line 22 raises ZeroDivisionError.


Proposed fix

Guard against zero before the normalization loop. If flankavg == 0, emit 0.0 for all density values and report TSS enrichment as 0.0 rather than crashing:

flankavg = flanksum * 1.0 / 20
if flankavg == 0:
    for i in range(1, 401):
        print("%d\t%.4f" % (n2bin[i], 0.0))
    print("# TSS enrichment: 0.0000")
    sys.exit(0)

Related

⚡ Generated using AI ⚡

Activity

  1. self-assigned this
    on Aug 4, 2026
  2. changed the title [-]fix: hs1_chrR support broken in annotation scripts and TSS enrichment calculation[/-] [+]fix: ZeroDivisionError in _ccbr_counts2density.py when flanking TSS bins have zero reads[/+] on Aug 4, 2026
  3. added a commit that references this issue on Aug 4, 2026
    19dcbf5
  4. kopardev commented on Aug 4, 2026

    @kopardev
    ContributorAuthor

    Fixed in commit 19dcbf5 on branch issue_117 (folded into PR #120).

    Change in workflow/scripts/_ccbr_counts2density.py: added a zero-guard immediately after computing flankavg. If all 20 flanking bins carry zero reads, the script now emits 0.0 for every density value and prints # TSS enrichment: 0.0000, then exits cleanly. This is the correct biological interpretation — a gene with no detectable background flanking the TSS has effectively zero TSS enrichment score.

    ⚡ Generated using AI ⚡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ASPENRepoNamebugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions