# How to convert BAM file to bigWig in Python?

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.

Thanks

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, ...)

• Thanks, I am wondering what this scale factor means. Is it possible to calculate a RPKM or CPM like bamCoverage? May 10 '20 at 14:01
• 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). May 10 '20 at 18:51
import pyranges as pr