For a model organism, you could load the TxDb annotation package (see, e.g., TxDb.Hsapiens.UCSC.hg19.knownGene), extract the introns and transcripts from the data, and then calculate the relative offset (of the intron start from the transcript start on the plus strand, for instance)
library(TxDb.Hsapiens.UCSC.hg19.knownGene)
introns <- unlist(intronsByTranscript(
TxDb.Hsapiens.UCSC.hg19.knownGene, use.names=TRUE))
tx <- transcripts(TxDb.Hsapiens.UCSC.hg19.knownGene,
columns=c("tx_name", "TXCHROM"))
idx <- match(names(introns), tx$tx_name)
offset <- ifelse(strand(introns) == "+",
start(introns) - start(tx)[idx],
end(tx)[idx] - end(introns)) / width(tx)[idx]
The commands are from the GenomicFeatures and GenomicRanges packages. Plot the result
hist(offset)
For less model organisms, a 'TxDb' database can be created with the vignette on the GenomicFeatures landing page.
You could coordinate the offset information with chromosome (with a little bit of trickery to deal with the appropriate factor 'levels' and to focus only on the autosome and sex chromosomes) with
df <- data.frame(offset=offset, chrom=tx$TXCHROM[idx])
lvls <- paste0("chr", c(1:22, "X", "Y"))
df <- df[df$chrom %in% lvls,]
After subsetting factors, it is convenient to drop the unused levels
df$chrom <- factor(df$chrom, lvls)
Visualize the information with, e.g.,
library(lattice)
densityplot(~ offset | chrom, df, plot.points=FALSE)
densityplot(~ offset, group=chrom, df,
plot.points=FALSE)
To aggregate several levels, one possibility is along the lines of
levels(df$chrom) <- c(rep("auto", 22), rep("sex", 2))
densityplot(~offset, group=chrom, df, plot.points=FALSE)