5

I have a GRanges object and I would like to extend all ranges eg 1kb on both sides, so each range will become 2kb longer. It is strange but I couldn't manage to do this using the inter-range-methods of GenomicRanges or IRanges. One way that would produce the desired result is to use resize twice, to first extend the 5' and then the 3'. But this of course is extremely awkward. Isn't there a more direct way of doing this? Please advice

gr <- GRanges(c('chr1','chr1'), IRanges(start=c(20, 120), width=10), strand='+')
gr <- resize(gr, fix='start', width=width(gr)+10)
gr <- resize(gr, fix='end', width=width(gr)+10)
gr
zx8754
  • 52,746
  • 12
  • 114
  • 209
Ludo
  • 417
  • 5
  • 8

4 Answers4

9

GRanges supports operators such as - and +

gr + 10 

will do the trick.

crazyhottommy
  • 310
  • 1
  • 3
  • 8
5

It's very simple. You can use start and end functions in GenomicRanges.

gr <- GRanges(c('chr1','chr1'), IRanges(start=c(20, 120), width=10), strand='+')
gr
# GRanges object with 2 ranges and 0 metadata columns:
#       seqnames     ranges strand
#          <Rle>  <IRanges>  <Rle>
#   [1]     chr1 [ 20,  29]      +
#   [2]     chr1 [120, 129]      +
#   -------
#   seqinfo: 1 sequence from an unspecified genome; no seqlengths

start(gr) <- start(gr) - 10
end(gr) <- end(gr) + 10
gr
# GRanges object with 2 ranges and 0 metadata columns:
#      seqnames     ranges strand
#         <Rle>  <IRanges>  <Rle>
#  [1]     chr1 [ 10,  39]      +
#  [2]     chr1 [110, 139]      +
#  -------
#  seqinfo: 1 sequence from an unspecified genome; no seqlengths
Ven Yao
  • 3,680
  • 2
  • 27
  • 42
  • 2
    Sure. But since there are so many functions for changing ranges (resize, narrow, etc), or defining new ranges based on existing (flanks, promoters) I expected a dedicated function capable of doing this fairly simple but (I think) very useful operation. – Ludo Dec 17 '15 at 15:01
3

Three years later but...

A direct way of do that is using resize method of GRanges with fix parameter:

gr <- resize(gr, width = width(gr)+(desired_size*2), fix = "center")
-1

You can use flank:

gr <- flank(gr, width=10, both=TRUE)
user3091668
  • 2,230
  • 6
  • 25
  • 42