Given two GenomicRanges like:
library(GenomicRanges)
gr1 <-
makeGRangesFromDataFrame(
data.frame(
chr = c("1","1","2","2"),
start = c(10,50,10,50),
end = c(20,60,20,60)
)
)
gr2 <-
makeGRangesFromDataFrame(
data.frame(
chr = c("2","2","3","3"),
start = c(15,40,10,50),
end = c(25,55,20,60)
)
)
I need to find the overlapping size (width) of the overlapping segments. In my case this would be 5 (for gr1[3] and gr21) and 5 (for gr[4] and gr2[2]). The solution given here using ranges()
on the hit class does not fit with GenomicRanges class (it seems):
mm <- findOverlaps(gr1,gr2)
ranges(mm,gr1,gr2)
Error in .local(x, ...) : 'query' must be a Ranges of length equal to number of queries
One would like that there would be a parameter to GenomicRanges::subsetByOverlaps()
that literally slices and return the overlap.
UPDATE (see below): the solution is in the package itself, GenomicRanges::intersect()
, so:
width(intersect(gr1, gr2))