This is a test version of Biostars. For the public version, visit https://www.biostars.org.
calculating genomic coverage/ base overlap in R

I have a list of regions for different genes. I want to know how much of overlapping is there in terms of basepairs with corresponding genes (Exons). I want to do it in R. How to do it?

genomics

1 answer

with findOverlaps function in GenomicRanges package or directly with this package GeneOverlap. Check out the documentation of these packages where you find the details.

# Install and load necessary packages
if (!requireNamespace("BiocManager", quietly = TRUE))
  install.packages("BiocManager")
BiocManager::install("TxDb.Hsapiens.UCSC.hg19.knownGene")
library(TxDb.Hsapiens.UCSC.hg19.knownGene)
library(GenomicRanges)
library(readxl)

# Load the gene annotation database
txdb <- TxDb.Hsapiens.UCSC.hg19.knownGene

# Read gene information and regions of interest from Excel file
gene_data <- read_excel("coverage.xlsx")

# Convert gene information and regions of interest data to GRanges object
regions_gr <- GRanges(
  seqnames = gene_data$chromosome,
  ranges = IRanges(start = gene_data$region_start, end = gene_data$region_end),
  strand = "*",
  region_id = gene_data$region_id,  # Replace with actual column name for region ID
  gene_id = gene_data$gene_id  # Replace with actual column name for gene ID
)

# Extract exon information
exon_info <- exons(txdb)

# Find overlaps between regions and exons
overlap <- findOverlaps(regions_gr, exon_info)

# Extract the overlapping exons
overlapping_exons <- subjectHits(overlap)

# Calculate the width of overlapping exons
exon_widths <- width(exon_info[overlapping_exons])

# Calculate the coverage for each region
coverage <- width(coverage(overlap, weight = TRUE)) / exon_widths

----this is my code but its showing no overlaps found

The code seems correct at first glance... How did you generate the data in the Excel file? Have you checked the exon annotations? You could check if the exons in the database are of significant length to allow meaningful overlaps with the regions of interest.

enter image description hereHi, this is the list/ excel I am uploading ( coverage.xlsx )

enter image description here

you can dput(head(df) for the both the data frame so that other can test for possible solutions

Log in to answer this question.