<!--
%\VignetteIndexEntry{01.2 Genomic Ranges}
%\VignettePackage{UseBioconductor}
%\VignetteEngine{knitr::knitr}
-->

```{r setup, echo=FALSE}
suppressPackageStartupMessages({
    library(UseBioconductor)
    library(GenomicRanges)
})
stopifnot(BiocInstaller::biocVersion() == "3.1")
```

```{r style, echo = FALSE, results = 'asis'}
BiocStyle::markdown()
knitr::opts_chunk$set(tidy=FALSE)
```

# Genomic Ranges

Herv&eacute; Pag&egrave;s, Martin Morgan<br/>
February 4, 2015

## A quick introduction to GRanges and GRangesList objects

See [slides](GRanges_and_GRangesList_slides.pdf).

## Recall: Genomic Ranges

- `GRanges`

  ![GRanges](our_figures/GRanges.png)

- `GRangesList`

  ![GRanges](our_figures/GRangesList.png)

## CompressedList

- `GRangesList` inherits from `CompressedList`

  ```{r}
  library(GenomicRanges)
  showClass("GRangesList")
  ```
  
- Consequences: `unlist()` and `relist()` are very cheap.
  The `unlist()` / `relist()` pattern can be used in many situations 
  where looping over the GRangesList would be inefficient.

- Example:

  ```{r}
  library(TxDb.Dmelanogaster.UCSC.dm3.ensGene)
  txdb <- TxDb.Dmelanogaster.UCSC.dm3.ensGene
  tx_by_gn <- transcriptsBy(txdb, by="gene")
  unlisted <- unlist(tx_by_gn)
  TSS <- ifelse(strand(unlisted) == "+", start(unlisted), end(unlisted))
  TSS <- GRanges(seqnames(unlisted), IRanges(TSS, width=1), strand(unlisted))
  TSS_by_gn <- relist(TSS, tx_by_gn)
  mcols(TSS) <- mcols(unlisted)
  TSS_by_gn <- relist(TSS, tx_by_gn) 
  ```
  
## Exercises

See HOWTOs vignette in the `r Biocpkg("GenomicRanges")` package

- e.g., Exercise 2.12 (using `r Biocannopkg("RNAseqData.HNRNPC.bam.chr14")`), 2.13, 2.16

