This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Count total base pairs in bedfile B that overlap bedfile A

I want to count the base pairs of CNVs in bedfile B that overlap bedfile A preferably in bedtools in one line.

bedfile A:

chr1    100    500

bedfile B:

chr1    50    99
chr1   250    299

Result:

chr1    100    500    100

Thanks

cnv bedtools

1 answer

Via BEDOPS bedmap --bases:

$ bedmap --echo --bases --delim '\t' A.bed B.bed > answer.bed

Make sure your inputs are sorted per BEDOPS sort-bed. Not sure what happens with other tools and sort-bed is the faster of the options, anyway.

Note that the base count is not unique, if elements in B overlap amongst themselves. You could pre-process the B set with bedops --merge to get a unique base count. Here's a one-liner to demo that use case:

``` $ bedops --merge B.bed \ | bedmap --echo --bases --delim '\t' A.bed - \

> unique_answer.bed

Or you could do process substitution, if you use bash:

$ bedmap --echo --bases --delim '\t' A.bed <(bedops --merge B.bed) > unique_answer.bed

Cool this is exactly what I want. I do already sort and merged file B to make things easier.

Is there a BEDOPS python api?

Log in to answer this question.