text: user39: 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?
  
  user77: 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).
  
original_text: 39: <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>
   77: <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>
  