Today I was looking for a handy way of converting BAM to bigWig. Biostars turned out to be handy place for a start. Guess what, there is ready module implemented in pybedtools that does exactly that:
from pybedtools.contrib.bigwig import bam_to_bigwig bam_to_bigwig(bam='path/to/bam', genome='hg19', output='path/to/bigwig')
Unfortunately, my genome of interested in not hosted at UCSC, so I needed to alter bam_to_bigwig slightly. In addition, I’ve replaced the read counting function mapped_read_count
with my own implementation that rely on BAM index and therefore return number of alignments nearly instantly. This reduces the time by some minutes for large BAMs.
My own bam2bigwig.py implementation can be found github repo:
bam2bigwig.py -i SOME.BAM -g GENOME.fa -o SOME.BAM.bw