R,GenomicRanges:找到重叠基因组范围的宽度

R, GenomicRanges: find the width of overlapping genomic ranges

给定两个 GenomicRanges,例如:

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)
    )
  )

我需要找到重叠段的重叠大小(宽度)。在我的例子中,这将是 5(对于 gr1[3] 和 gr21) and 5 (for gr[4] and gr2[2]). The solution given here 在命中 class 上使用 ranges() 不适合 GenomicRanges class(看起来):

mm <- findOverlaps(gr1,gr2)
ranges(mm,gr1,gr2)

Error in .local(x, ...) : 'query' must be a Ranges of length equal to number of queries

人们希望 GenomicRanges::subsetByOverlaps() 有一个参数,字面上的切片和 return 重叠。

更新(见下文):解决方案在包本身,GenomicRanges::intersect(),所以:

width(intersect(gr1, gr2))

GenomicRanges 包似乎有一个特定的功能,intersect()。所以解决方法很简单:

width(intersect(gr1, gr2))

[1] 6 6

(正确)