<p><a href="https://github.com/biocore-ntnu/pyranges" rel="nofollow noreferrer">pyranges</a>:</p>
<pre><code>import pyranges as pr
gr = pr.read_bam(&quot;your_file.bam&quot;)
gr.to_bigwig(&quot;out.bw&quot;, chromosome_sizes=pr.data.chromsizes()) # for hg19
</code></pre>

CC BY-SA 4.0
2021-02-28T20:03:42.263Z
2021-02-28T20:03:42.263Z
The Unfun Cat
<p>deepTools has a (somewhat poorly documented) API, since it's a python package too. The basic code framework is:</p>

<pre><code>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, ...)
</code></pre>

CC BY-SA 4.0
2020-05-10T14:01:29.410Z
Thanks, I am wondering what this scale factor means. Is it possible to calculate a RPKM or CPM like bamCoverage?
Phoenix Mu
CC BY-SA 4.0
2020-05-10T18:51:58.003Z
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).
Devon Ryan
CC BY-SA 4.0
2020-05-10T06:50:10.850Z
2020-05-10T06:50:10.850Z
Devon Ryan
<p>I am manipulating some BAM files using <code>pysam</code> 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 <code>bamCoverage</code> from <code>deeptools</code> to do this previously, but it is a standalone tool that cannot be used from within my Python scripts.</p>

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

<p>Thanks</p>

<python><sam><pysam><deeptools><bigwig>
How to convert BAM file to bigWig in Python?
# How to convert BAM file to bigWig in Python?

(Asked by: username_0 on 2020-05-10)

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

**Tags:** python, sam, pysam, deeptools, bigwig

---

## 2 answers

### Answer by username_2 on 2020-05-10. Score: 4 | Accepted answer

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

> **Comments:**
> **username_0**: Thanks, I am wondering what this scale factor means. Is it possible to calculate a RPKM or CPM like bamCoverage?  
> **username_2**: 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).  

---

### Answer by username_1 on 2021-02-28. Score: 3

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


---

