# IRanges

``This analysis was performed using R (ver. 3.1.0).``

## IRanges

IRanges is a library for representing ranges of integers, which is useful in genomics, because we have base pair ranges that we'd like to manipulate.There's a very detailed vignette on IRanges. IRanges have a start, an end and a width.

``````library(IRanges)
#IR with 6 base-pairs long : start and end are indicated
ir <- IRanges(5,10)
ir``````
``````## IRanges of length 1
##     start end width
##      5  10     6``````
``start(ir) #Return the start value``
``##  5``
``end(ir) #Return the end value``
``##  10``
``width(ir) #return the width``
``##  6``
``# ?IRanges``

If you specify that the start is 5, and the width should be 6, you'll get the identical IRange.

``````#IR with 6 base-pairs long : start and width are indicated
ir <- IRanges(5,width=6)
ir``````
``````## IRanges of length 1
##     start end width
##      5  10     6``````

You can also specify more than one range at a time. If you give three starts and three ends, you get a IRanges object of length 3. If you, again, ask for the starts, you'll get the three different starts, 3, 5, and 17.

``````# Miultiple ranges
ir <- IRanges(start=c(3,5,17), end=c(10,8,20))
ir``````
``````## IRanges of length 3
##     start end width
##      3  10     8
##      5   8     4
##     17  20     4``````
``length(ir)``
``##  3``
``start(ir)``
``##   3  5 17``

There are a number of intra range methods for IRanges. And intra-range means that the operation will occur for each range that you have. An example of this is to shift the IRange to the left by two. Before we had an IRange that started at 5 and ended at 10. Applying the shift operation produces an IRange which starts at 3 and ends at 8.

``````# ?"intra-range-methods"
ir <- IRanges(5,10)
shift(ir, -2)``````
``````## IRanges of length 1
##     start end width
##      3   8     6``````
``Remember, all of these commands can work on more than one range at once.``

Here we show the effects of the different methods using a single range:

``````#Shift the IRange to the left by 2 base-pairs
shift(ir,-2)``````
``````## IRanges of length 1
##     start end width
##      3   8     6``````
``````#narrow : Relative to the start, you should start this range at the second base pair.
narrow(ir, start=2)``````
``````## IRanges of length 1
##     start end width
##      6  10     5``````
``````#narrow: Relative to 5, you should end on the fifth base pair, which means it should end at 9.
narrow(ir, end=5)``````
``````## IRanges of length 1
##     start end width
##      5   9     5``````
``````#flank allows you to get flanking sequence
#here three base pairs from the start.
flank(ir, width=3, start=TRUE, both=FALSE)``````
``````## IRanges of length 1
##     start end width
##      2   4     3``````
``````#flank: three base pairs from the end by specifying start equals false
flank(ir, width=3, start=FALSE, both=FALSE)``````
``````## IRanges of length 1
##     start end width
##     11  13     3``````
``````#bidirectional flanking sequence from the start by specifying both equals true.
flank(ir, width=3, start=TRUE, both=TRUE)``````
``````## IRanges of length 1
##     start end width
##      2   7     6``````
``ir * 2``
``````## IRanges of length 1
##     start end width
##      6   8     3``````
``ir + 2``
``````## IRanges of length 1
##     start end width
##      3  12    10``````
``ir - 2``
``````## IRanges of length 1
##     start end width
##      7   8     2``````

The exact same functions are shown graphically with the code below:

``````# set up a plotting window so we can look at range operations
plotir <- function(ir,i) { arrows(start(ir)-.5,i,end(ir)+.5,i,code=3,angle=90,lwd=3) }
plot(0,0,xlim=c(0,15),ylim=c(0,11),type="n",xlab="",ylab="",xaxt="n")
axis(1,0:15)
abline(v=0:30 + .5,col=rgb(0,0,0,.5))
# plot the original IRange
plotir(ir,1)
# draw a red shadow for the original IRange
polygon(c(start(ir)-.5,start(ir)-.5,end(ir)+.5,end(ir)+.5),c(-1,12,12,-1),col=rgb(1,0,0,.2),border=NA)
plotir(shift(ir,-2), 2)
plotir(narrow(ir, start=2), 3)
plotir(narrow(ir, end=5), 4)
plotir(flank(ir, width=3, start=TRUE, both=FALSE), 5)
plotir(flank(ir, width=3, start=FALSE, both=FALSE), 6)
plotir(flank(ir, width=3, start=TRUE, both=TRUE), 7)
plotir(ir * 2, 8)
plotir(ir + 2, 9)
plotir(ir - 2, 10)`````` The inter-range methods are those functions which depend on the other ranges in the object. Let's create an IRanges object with three ranges, which starts at 3, 5, 17, ends at 10, 8, and 20.

``````# ?"inter-range-methods"
ir <- IRanges(start=c(3,5,17), end=c(10,8,20))
#range: returns the beginning of the IRanges to the end, including gaps in between.
range(ir)``````
``````## IRanges of length 1
##     start end width
##      3  20    18``````
``````#reduce, returns those base pairs which are covered by the original ranges.
#So we do not get the gap, so the end at 10 and the beginning at 17.
reduce(ir)``````
``````## IRanges of length 2
##     start end width
##      3  10     8
##     17  20     4``````
``````#gaps: returns a gap from 10 to 17.
gaps(ir)``````
``````## IRanges of length 1
##     start end width
##     11  16     6``````
``````#disjoint: gives a set of ranges which has the same coverage as the original IRanges object,
#but they're not overlapping in any way, and they also contain the union of all the endpoints of the original range.
disjoin(ir)``````
``````## IRanges of length 4
##     start end width
##      3   4     2
##      5   8     4
##      9  10     2
##     17  20     4``````

Licence