File: base_coverage.py

package info (click to toggle)
python-bx 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,000 kB
  • sloc: python: 17,136; ansic: 2,326; makefile: 24; sh: 8
file content (24 lines) | stat: -rw-r--r-- 897 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""
Determine the number of bases covered by a set of intervals.
"""

from bx.intervals.io import BitsetSafeReaderWrapper
from bx.intervals.operations import MAX_END


def base_coverage(reader):
    # Handle any ValueError, IndexError and OverflowError exceptions that may be thrown when
    # the bitsets are being created by skipping the problem lines
    base_reader = BitsetSafeReaderWrapper(reader, lens={})
    bitsets = base_reader.binned_bitsets()
    coverage = 0
    for chrom in bitsets:
        try:
            coverage += bitsets[chrom].count_range(0, MAX_END)
        except IndexError as e:
            base_reader.skipped += 1
            # no reason to stuff an entire bad file into memmory
            if base_reader.skipped < 10:
                base_reader.skipped_lines.append((base_reader.linenum, base_reader.current_line, str(e)))
            continue
    return coverage