This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to determine the density and mean of features in specified intervals?

Hi

I have 2 bed files.

File1.bed

chr1 1 100 0.5
chr1 900 1000 1.5
chr1 1100 1105 0.5

File2.bed

chr1  1  400
chr1 401 800
chr1 801 1200

I want to determine the frequency of events in File1.bed in each of the intervals in File2.bed

So for the above input files, the output would be

chr1  1  400 1
chr1 401 800 0
chr1 801 1200 2

Any suggestions?

Also, what if in addition to counting the number of features in the window, I also want to compute their average?

interval bed counting bedtools

1 answer

#!bin/bash

FILE1="file1.bed" ## make sure these files are consistently delimited (space or tab)
FILE2="file2.bed"

while read CHR START_POS END_POS; do
    echo "for the interval" $CHR $START_POS $END_POS
    awk -v CHR=$CHR -v SP=$START_POS -v EP=$END_POS '($1==CHR && $2>=SP && $3<=EP){print $0}' $FILE1 | wc -l
done < "$FILE2"

Hi Les - you are welcome! hope it helped. do you understand how it works?

VL

Log in to answer this question.