This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Finding minima from bigwig in specific regions from bed file

Hi all- I have a bigwig file that contains a score for each base pair of the genome. I also have a bed file containing ~70000 regions of interest with average size ~1500 bp. I would like to use this bed file to find the minimum score (from the bigwig) within each of these ~70000 regions of interest, as well as the position of that minimum. bwtool find provides a tool to find local minima, etc., but there is no option to limit this to specific regions, unless I were to do the analysis ~70000 times and limit it to each individual region. Any ideas?

bigwig bed

2 answers

#!/usr/bin/env python
import pyBigWig

bw = pyBigWig.open("some bigwig file.bw")
f = open("some bed file.bed")
for line in f:
    cols = line.strip().split()
    val = bw.stats(cols[0], int(cols[1]), int(cols[2]), type="min")
    print("The minimum in the range {}:{}-{} is {}".format(cols[0], int(cols[1])+1, cols[2], val))
f.close()
bw.close()

Or something along those lines.

Sure, you can use Kent tools and BEDOPS to solve this.

Convert from bigwig to wiggle:

$ bigWigToWig signal.bw signal.wig

Convert from wiggle to BED with wig2bed:

$ wig2bed < signal.wig > signal.bed

Sort your regions-of-interest file with sort-bed:

$ sort-bed roi.unsorted.bed > roi.bed

Map your regions-of-interest to your signal file with bedmap:

$ bedmap --echo --min-element roi.bed signal.bed > answer.bed

On each line in answer.bed, you get a region of interest from roi.bed and its associated minimum-scoring element from signal.bed.

If you're doing statistical tests, you might be interested in the --min-element-rand option in bedmap, which grabs a random map-set element in the case of ties. For instance, your signal might have a multimodal distribution and always grabbing the first of a set of tied minima (which --min-element and other approaches will do) may impart positional bias.

Log in to answer this question.