This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Exon density in uniform bins across bam files

I'd like to calculate the percent of bases that overlap exons in uniformly sized (n mB) windows across several bam files. Could anyone help me determine this preferably using bedtools or R?

alignment sequencing

1 answer

using R GenomicRanges and given two granges objects exonic_bed and bins

hits <- GenomicRanges::findOverlaps(query = exonic_bed, 
subject = bins, ignore.strand = TRUE)
overlaps <- pintersect(bins[subjectHits(hits)], 
exonic_bed[queryHits(hits)],  ignore.strand = TRUE)

subjecthits <- bins[subjectHits(hits)]
subjecthits$n_overlap <- width(overlaps)

subjecthits <- 
  subjecthits %>% 
  as_tibble() %>% 
  dplyr::group_by(seqnames, start, end, width) %>% 
  dplyr::summarize(overlap = sum(n_overlap)) %>% 
  dplyr::mutate(percent_overlap = overlap/width)

Log in to answer this question.