# 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?

• Can't you subset on this data? I mean do upstream +5000 and downstream +5000 and then subset for those that are above 1kb downstream. Probably you can use plyranges
– llrs
May 30 '18 at 7:17

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).

• Thanks Devon this is a great. The only problem I see with this function is that it doesn't take into account the length of the chromosomes when expanding the ranges -- when I tried to do something similar using start() and end() I kept getting errors that I expanded beyond the ranges of the chromosome. This shouldn't be hard to fix though now that I have your function as a base, so thanks for your help! May 30 '18 at 16:59