Expand granges object different amounts upstream vs. downstream I am attempting to get gene regions and their immediate neighborhoods using Bioconductor GenomicRanges packages.

It is very easy for me to extend the regions around genes if I want to extend it the same distance upstream and downstream as in this example:

library(GenomicRanges)
library(Homo.sapiens)

genes = genes(Homo.sapiens)
genes_5kb_flank = genes + 5000


And if I want to extend both upstream and downstream different amounts centered on the TSS I can use promoters(genes, upstream = 5000, downstream= 1000)

But I am having trouble finding a function that is strand-aware and also chromosome length aware that allows me to extend the gene region a different amount upstream vs. downstream... For instance if I wanted a 5kb upstream flanking region and a 1kb downstream flanking region as is used for genomic region annotations on GREAT.

Anyone know of a drop in function for this?  something like promoters() but for the full gene region instead of only around the TSS?
There doesn't appear to be a built in function for that, so here's a modified version of promoters():

expandRange = function(x, upstream=2000, downstream=1000) {
  strand_is_minus = strand(x) == "-"
  on_plus = which(!strand_is_minus)
  on_minus = which(strand_is_minus)
  start(x)[on_plus] = start(x)[on_plus] - upstream
  start(x)[on_minus] = start(x)[on_minus] - downstream
  end(x)[on_plus] = end(x)[on_plus] + downstream
  end(x)[on_minus] = end(x)[on_minus] + upstream
  x
}


You can then call expandedGenes = expandRange(genes, 5000, 1000).