<p>There doesn't appear to be a built in function for that, so here's a modified version of <code>promoters()</code>:</p>

<pre class="lang-r prettyprint-override"><code>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
}
</code></pre>

<p>You can then call <code>expandedGenes = expandRange(genes, 5000, 1000)</code>.</p>

CC BY-SA 4.0
2018-05-30T16:59:30.760Z
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!
Reilstein
CC BY-SA 4.0
2018-05-30T08:59:02.270Z
2018-05-30T08:59:02.270Z
Devon Ryan
<p>I am attempting to get gene regions and their immediate neighborhoods using Bioconductor GenomicRanges packages.</p>

<p>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:</p>

<pre><code>library(GenomicRanges)
library(Homo.sapiens)

genes = genes(Homo.sapiens)
genes_5kb_flank = genes + 5000
</code></pre>

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

<p>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.</p>

<p>Anyone know of a drop in function for this?  something like <code>promoters()</code> but for the full gene region instead of only around the TSS?</p>

CC BY-SA 4.0
2018-05-30T07:17:00.837Z
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
<bioconductor><sequence-annotation><genomicranges>
Expand granges object different amounts upstream vs. downstream
# Expand granges object different amounts upstream vs. downstream

(Asked by: username_0 on 2018-05-30)

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?

**Tags:** bioconductor, sequence-annotation, genomicranges
> **Comments:**
> **username_1**: 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`  

---

## 1 answers

### Answer by username_2 on 2018-05-30. Score: 2 | Accepted answer

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

> **Comments:**
> **username_0**: 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!  

---

