is there an R function similar to bedtools intersect?

1.6k views Asked by At

when working with bedtools intersect , there are many options that you could use like setting minimum overlap as percentage of file A or B , or whether to write original A or B entries. I was wondering if there is a package in R that does the same

I would appreciate your answer

2

There are 2 answers

0
Dominik Szabo On

There is the 'valr' package, that allows you to execute some of the basic bedtools commands in R.

https://rnabioco.github.io/valr/

But I don't think it yet supports the percentage overlap -f option of bedtools unfortunately

You can code it yourself though:

library(tidyverse)
library(valr)
A <- tibble(chrom='chr1', start=100, end=900)
B <- tibble(chrom='chr1', start=700, end=1000) %>% 
       dplyr::mutate(width=end-start)
bed_intersect(A,B) %>% 
       dplyr::mutate(percent_overlap = .overlap / width.y) %>%
       dplyr::filter(percent_overlap >= 0.5
0
Jabber1 On

The bioconductor package GenomicRanges has a function to find overlaps: findOverlaps() and intersect() and you can set an option minoverlap More info here.

You can also read bedFiles into GRanges using rtracklayer