Search code examples
roverlappingbioconductorsegments

Width of the overlapped segment in GenomicRanges package


I'm using GenomicRanges to find which transcripts from one experiment overlap with those coming from other one.

head(to_ranges1)
   knowngene  chr strand Start    Gene
1 uc001aaa.3  chr1    +  9873 16409   DDX11L1
2 uc001aac.4  chr1    - 12361 31370  WASH7P
3 uc001aae.4  chr1    - 12361 21759  WASH7P
library(GenomicRanges)
object_one<-with(to_ranges, GRanges(chr, IRanges(Start,End), 
                                     strand,names=knowngene,Gene=Gene)
object_two<-with(to_ranges, GRanges(chr, IRanges(Start,End), 
                                     strand,names=knowngene, Gene=Gene))
mm<-findOverlaps(object_one,object_two)
solution <- data.frame(as.data.frame(object_one[as.matrix(mm)[,1],]),
                       as.data.frame(object_two[as.matrix(mm)[,2],]))

What I am trying to find is the WIDTH of the overlapped segment between the hits in the solution data frame, however the only width I could get is the related to the original transcripts before the overlapping procedure.

Could you help me pleas?


Solution

  • You can apply the ranges function over hits class( results of findOverlaps) . ranges returns a Ranges holding the intersection of the ranges in the Ranges objects query and subject.

    You don't supply a reproducible example , so here an example :

    query <- IRanges(c(1, 4, 9), c(5, 7, 10))
    subject <- IRanges(c(2, 2, 10), c(2, 3, 12))
    mm <- findOverlaps(query,subject)
    ranges(mm,query,subject)
    Ranges of length 3
        start end width
    [1]     2   2     1
    [2]     2   3     2
    [3]    10  10     1