Extract/subset bigwig file for a given genomic region

This is a solution in python version (3.0+) using a package called pyBigWig to extract a given genomic region from a whole genome bigwig file.

Prepare your input bigwig file:

import pyBigWig

# First open bigwig file
bwIn = pyBigWig.open('input.bw')

# check bigwig file header
print(bwIn.header())

Prepare output, since your output doesn’t have a header, you need to add the header using the chosen chromosome size, here I’m using a region from chr18 as an example.

bwOutput = pyBigWig.open('output.bw','w')
bwOutput.addHeader([('chr18',80373285)]) # chromosome size

for x in bwIn.intervals('chr18',62926563,63516911):
    bwOutput.addEntries(['chr18'],[x[0]],ends=[x[1]],values=[x[2]])

bwOutput.close()

Python pickle save/load dictionary into a file

Sometimes you want to save a dictionary into a reusable file, and reuse it later. This is specifically useful when you need to save a color scheme for a large category and want to keep it consistent across your whole project.

Output it as a file:

import pickle

colorDicOut = open('colorOut_dic.pkl', 'wb')
pickle.dump(colorDic, colorDicOut)
colorDicOut.close()

with open('colorOut_dic.pkl', 'wb') as colorDicOut:
    pickle.dump(colorDic, colorDicOut)

Load it into a variable:

colorDicIn = open("colorOut_dic.pkl","rb")
colorDic = pickle.load(colorDicIn)

If UnicodeDecodeError comes up, then it is likely an issue of incompatibility between python2 and python3, please refer to this post for solutions.

Source:
https://stackoverflow.com/questions/11218477/how-can-i-use-pickle-to-save-a-dict