Skip to content

Instantly share code, notes, and snippets.

@ATpoint
Created July 2, 2020 19:25
Show Gist options
  • Save ATpoint/0c05bf59b8c80803a525494e63e05e8e to your computer and use it in GitHub Desktop.
Save ATpoint/0c05bf59b8c80803a525494e63e05e8e to your computer and use it in GitHub Desktop.
library(GenomicRanges)
library(matrixStats)
ranges1 <- GRanges(seqnames = c("chr1", "chr2", "chr3"),
ranges = IRanges(start=c(1,100,1000),
end = c(10,150,5000)))
ranges2 <- GRanges(seqnames = c("chr1", "chr2", "chr3"),
ranges = IRanges(start=c(11,110,2000),
end = c(20,130,3000)))
GetPercentOverlap <- function(query,
subject){
#/ find overlap positions
ovlap <- findOverlaps(query, subject)
#/ subset the input ranges to those ranges that have overlaps with the second ranges object
r1 <- ranges(query[ovlap@from] )
r2 <- ranges(subject[ovlap@to])
#/ new GRanges object only with the overlapping intervals
gr<-
GRanges(seqnames = as.character(seqnames(query[ovlap@from])),
ranges = IRanges(start = matrixStats::rowMaxs(as.matrix(data.frame(r1=start(r1), r2=start(r2)))),
end = matrixStats::rowMins(as.matrix(data.frame(r1=end(r1), r2=end(r2))))))
mcols(gr)$percentOverlap <- 100 * width(gr) / width(r1)
#/ add original query ranges
ranges(gr) <- ranges(r1)
#/ add subject coordinates to mcols(gr)
mcols(gr)$subject <- r2
gr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment