I am manipulating some BAM files using pysam package which seems to be very fast and handy. However, I ran into problem when I am trying to generate bigWig files from BAM files in Python. I used bamCoverage from deeptools to do this previously, but it is a standalone tool that cannot be used from within my Python scripts.

Therefore, I am wondering what Python package would allow me to read in BAM files and convert them to bigWig files.



2 Answers 2


deepTools has a (somewhat poorly documented) API, since it's a python package too. The basic code framework is:

from deeptools import writeBedGraph
from deeptools.getScaleFactor import get_scale_factor

wr = writeBedGraph.WriteBedGraph(options and input files)
func_args = {'scaleFactor': get_scale_factor(...options...)}
wr.run(writeBedGraph.scaleCoverage, func_args, output_file_name, ...)
  • $\begingroup$ Thanks, I am wondering what this scale factor means. Is it possible to calculate a RPKM or CPM like bamCoverage? $\endgroup$
    – Phoenix Mu
    May 10, 2020 at 14:01
  • $\begingroup$ The scale factor dictates how the coverage track is transformed before writing the results to a bigWig file. Among other things, this then handles RPKM and other normalizations. Please notes that you'll have to pass an argparse object into it, which I understand is a bit annoying (I had't considered external use of that function). $\endgroup$
    – Devon Ryan
    May 10, 2020 at 18:51


import pyranges as pr
gr = pr.read_bam("your_file.bam")
gr.to_bigwig("out.bw", chromosome_sizes=pr.data.chromsizes()) # for hg19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.