def count_bigwig_total(bw_file): bw = open_bigwig(bw_file) result = sum(l * bw.stats(ch, 0, l, "mean")[0] for ch, l in bw.chroms().items()) assert ( abs(result) > 0 ) ## BigWig could have negative values, e.g. the negative-strand GroCAP bigwigs return result
def count_bigwig_total(bw_file, tmpdir): download_file(bw_file, tmpdir, overwrite_ok=True) bw_file = join(tmpdir, basename(bw_file)) from pyBigWig import open as open_bigwig bw = open_bigwig(bw_file) result = sum(l * bw.stats(ch, 0, l, "mean")[0] for ch, l in bw.chroms().items()) assert ( abs(result) > 0 ) ## BigWig could have negative values, e.g. the negative-strand GroCAP bigwigs return result
def count_bigwig(target, bed_file, output): from pyBigWig import open as open_bigwig bw = open_bigwig(target) bed = read_bed(bed_file) with open(output, "wb") as outfp: for chr, start, end, *rest in bed.itertuples(index=False, name=None): # if isinstance(name, np.float): # name = "" try: val = bw.stats(chr, int(start), int(max(end, start + 1)), "mean")[0] or 0 except RuntimeError: print("Failed on", chr, start, end) raise val *= abs(end - start) # convert to total coverage output = ("\t".join([chr, str(start), str(end), str(val)]) + "\n").encode('ascii') outfp.write(output)