Contents

The material in this course requires R version 3.2 and Bioconductor version 3.2

stopifnot(
    getRversion() >= '3.2' && getRversion() < '3.3',
    BiocInstaller::biocVersion() == "3.2"
)

1 Bioconductor ‘infrastructure’ for sequence analysis

1.1 Classes, methods, and packages

This section focuses on classes, methods, and packages, with the goal being to learn to navigate the help system and interactive discovery facilities.

1.2 Motivation

Sequence analysis is specialized

Additional considerations

Solution: use well-defined classes to represent complex data; methods operate on the classes to perform useful functions. Classes and methods are placed together and distributed as packages so that we can all benefit from the hard work and tested code of others.

2 Core packages

                   VariantAnnotation
                           |
                           v
                    GenomicFeatures
                           |
                           v
                       BSgenome
                           |
                           v
                      rtracklayer
                           |
                           v
                    GenomicAlignments
                      |           |
                      v           v
     SummarizedExperiment   Rsamtools  ShortRead
                  |         |      |      |
                  v         v      v      v
                GenomicRanges     Biostrings
                        |          |
                        v          v
               GenomeInfoDb   (XVector)
                        |     |
                        v     v
                        IRanges
                           |
                           v 
                      (S4Vectors)

3 Core classes

3.1 Case study: IRanges and GRanges

The IRanges package defines an important class for specifying integer ranges, e.g.,

library(IRanges)
ir <- IRanges(start=c(10, 20, 30), width=5)
ir
## IRanges of length 3
##     start end width
## [1]    10  14     5
## [2]    20  24     5
## [3]    30  34     5

There are many interesting operations to be performed on ranges, e.g, flank() identifies adjacent ranges

flank(ir, 3)
## IRanges of length 3
##     start end width
## [1]     7   9     3
## [2]    17  19     3
## [3]    27  29     3

The IRanges class is part of a class hierarchy. To see this, ask R for the class of ir, and for the class definition of the IRanges class

class(ir)
## [1] "IRanges"
## attr(,"package")
## [1] "IRanges"
getClass(class(ir))
## Class "IRanges" [package "IRanges"]
## 
## Slots:
##                                                                                       
## Name:            start           width           NAMES     elementType elementMetadata
## Class:         integer         integer characterORNULL       character DataTableORNULL
##                       
## Name:         metadata
## Class:            list
## 
## Extends: 
## Class "Ranges", directly
## Class "IntegerList", by class "Ranges", distance 2
## Class "RangesORmissing", by class "Ranges", distance 2
## Class "AtomicList", by class "Ranges", distance 3
## Class "List", by class "Ranges", distance 4
## Class "Vector", by class "Ranges", distance 5
## Class "Annotated", by class "Ranges", distance 6
## 
## Known Subclasses: "NormalIRanges"

Notice that IRanges extends the Ranges class. Show

Now try entering ?flank (if not using RStudio, enter ?"flank,<tab>" where <tab> means to press the tab key to ask for tab completion). You can see that there are help pages for flank operating on several different classes. Select the completion

?"flank,Ranges-method" 

and verify that you’re at the page that describes the method relevant to an IRanges instance. Explore other range-based operations.

The GenomicRanges package extends the notion of ranges to include features relevant to application of ranges in sequence analysis, particularly the ability to associate a range with a sequence name (e.g., chromosome) and a strand. Create a GRanges instance based on our IRanges instance, as follows

library(GenomicRanges)
gr <- GRanges(c("chr1", "chr1", "chr2"), ir, strand=c("+", "-", "+"))
gr
## GRanges object with 3 ranges and 0 metadata columns:
##       seqnames    ranges strand
##          <Rle> <IRanges>  <Rle>
##   [1]     chr1  [10, 14]      +
##   [2]     chr1  [20, 24]      -
##   [3]     chr2  [30, 34]      +
##   -------
##   seqinfo: 2 sequences from an unspecified genome; no seqlengths

The notion of flanking sequence has a more nuanced meaning in biology. In particular we might expect that flanking sequence on the + strand would precede the range, but on the minus strand would follow it. Verify that flank applied to a GRanges object has this behavior.

flank(gr, 3)
## GRanges object with 3 ranges and 0 metadata columns:
##       seqnames    ranges strand
##          <Rle> <IRanges>  <Rle>
##   [1]     chr1  [ 7,  9]      +
##   [2]     chr1  [25, 27]      -
##   [3]     chr2  [27, 29]      +
##   -------
##   seqinfo: 2 sequences from an unspecified genome; no seqlengths

Discover what classes GRanges extends, find the help page documenting the behavior of flank when applied to a GRanges object,

It seems like there might be a number of helpful methods available for working with genomic ranges; we can discover some of these from the command line, indicating that the methods should be on the current search() path

methods(class="GRanges")
##   [1] aggregate           anyNA               <=                  <                  
##   [5] ==                  >=                  >                   !=                 
##   [9] append              as.character        as.complex          as.data.frame      
##  [13] as.env              as.factor           as.integer          as.list            
##  [17] as.logical          as.numeric          as.raw              BamViews           
##  [21] bamWhich<-          blocks              browseGenome        c                  
##  [25] chrom<-             chrom               coerce              coerce<-           
##  [29] compare             countOverlaps       coverage            disjoin            
##  [33] disjointBins        distance            distanceToNearest   duplicated         
##  [37] elementMetadata<-   elementMetadata     end<-               end                
##  [41] eval                expand              export              extractROWS        
##  [45] extractUpstreamSeqs findOverlaps        flank               follow             
##  [49] gaps                [<-                 [                   $<-                
##  [53] $                   getPromoterSeq      granges             head               
##  [57] high2low            %in%                intersect           isDisjoint         
##  [61] length              lengths             liftOver            mapCoords          
##  [65] mapFromAlignments   mapFromTranscripts  mapToAlignments     mapToTranscripts   
##  [69] match               mcols<-             mcols               metadata<-         
##  [73] metadata            mstack              names<-             names              
##  [77] narrow              nearest             NROW                Ops                
##  [81] order               overlapsAny         parallelSlotNames   parallelVectorNames
##  [85] pgap                pintersect          pmapCoords          pmapFromAlignments 
##  [89] pmapFromTranscripts pmapToAlignments    pmapToTranscripts   precede            
##  [93] promoters           psetdiff            punion              range              
##  [97] ranges<-            ranges              rank                reduce             
## [101] relistToClass       relist              rename              rep.int            
## [105] replaceROWS         rep                 resize              restrict           
## [109] rev                 ROWNAMES            rowRanges<-         ScanBamParam       
## [113] ScanBcfParam        scanFa              scanTabix           score<-            
## [117] score               seqinfo<-           seqinfo             seqlevelsInUse     
## [121] seqnames<-          seqnames            setdiff             shiftApply         
## [125] shift               showAsCell          show                sort               
## [129] split               split<-             start<-             start              
## [133] strand<-            strand              subsetByOverlaps    subset             
## [137] summarizeOverlaps   table               tail                tapply             
## [141] tile                trim                union               unique             
## [145] update              updateObject        values<-            values             
## [149] width<-             width               window<-            window             
## [153] with                xtfrm              
## see '?methods' for accessing help and source code

Notice that the available flank() methods have been augmented by the methods defined in the GenomicRanges package, including those that are relevant (via inheritance) to the GRanges class.

grep("flank", methods(class="GRanges"), value=TRUE)
## [1] "flank,GenomicRanges-method"

Verify that the help page documents the behavior we just observed.

?"flank,GenomicRanges-method"

Use help() to list the help pages in the GenomicRanges package, and vignettes() to view and access available vignettes; these are also available in the Rstudio ‘Help’ tab.

help(package="GenomicRanges")
vignette(package="GenomicRanges")
vignette(package="GenomicRanges", "GenomicRangesHOWTOs")

3.2 GenomicRanges

3.2.1 The GRanges and GRangesList classes

Aside: ‘TxDb’ packages provide an R representation of gene models

library(TxDb.Hsapiens.UCSC.hg19.knownGene)
txdb <- TxDb.Hsapiens.UCSC.hg19.knownGene

exons(): GRanges

exons(txdb)
## GRanges object with 289969 ranges and 1 metadata column:
##                  seqnames         ranges strand   |   exon_id
##                     <Rle>      <IRanges>  <Rle>   | <integer>
##        [1]           chr1 [11874, 12227]      +   |         1
##        [2]           chr1 [12595, 12721]      +   |         2
##        [3]           chr1 [12613, 12721]      +   |         3
##        [4]           chr1 [12646, 12697]      +   |         4
##        [5]           chr1 [13221, 14409]      +   |         5
##        ...            ...            ...    ... ...       ...
##   [289965] chrUn_gl000241 [35706, 35859]      -   |    289965
##   [289966] chrUn_gl000241 [36711, 36875]      -   |    289966
##   [289967] chrUn_gl000243 [11501, 11530]      +   |    289967
##   [289968] chrUn_gl000243 [13608, 13637]      +   |    289968
##   [289969] chrUn_gl000247 [ 5787,  5816]      -   |    289969
##   -------
##   seqinfo: 93 sequences (1 circular) from hg19 genome

Alt Genomic Ranges

exonsBy(): GRangesList

exonsBy(txdb, "tx")
## GRangesList object of length 82960:
## $1 
## GRanges object with 3 ranges and 3 metadata columns:
##       seqnames         ranges strand |   exon_id   exon_name exon_rank
##          <Rle>      <IRanges>  <Rle> | <integer> <character> <integer>
##   [1]     chr1 [11874, 12227]      + |         1        <NA>         1
##   [2]     chr1 [12613, 12721]      + |         3        <NA>         2
##   [3]     chr1 [13221, 14409]      + |         5        <NA>         3
## 
## $2 
## GRanges object with 3 ranges and 3 metadata columns:
##       seqnames         ranges strand | exon_id exon_name exon_rank
##   [1]     chr1 [11874, 12227]      + |       1      <NA>         1
##   [2]     chr1 [12595, 12721]      + |       2      <NA>         2
##   [3]     chr1 [13403, 14409]      + |       6      <NA>         3
## 
## $3 
## GRanges object with 3 ranges and 3 metadata columns:
##       seqnames         ranges strand | exon_id exon_name exon_rank
##   [1]     chr1 [11874, 12227]      + |       1      <NA>         1
##   [2]     chr1 [12646, 12697]      + |       4      <NA>         2
##   [3]     chr1 [13221, 14409]      + |       5      <NA>         3
## 
## ...
## <82957 more elements>
## -------
## seqinfo: 93 sequences (1 circular) from hg19 genome

Alt Genomic Ranges List

GRanges / GRangesList are incredibly useful

  • Represent annotations – genes, variants, regulatory elements, copy number regions, …
  • Represent data – aligned reads, ChIP peaks, called variants, …

3.2.2 Algebra of genomic ranges

Many biologically interesting questions represent operations on ranges

  • Count overlaps between aligned reads and known genes – GenomicRanges::summarizeOverlaps()
  • Genes nearest to regulatory regions – GenomicRanges::nearest(), [ChIPseeker][]
  • Called variants relevant to clinical phenotypes – VariantFiltering

GRanges Algebra

  • Intra-range methods
    • Independent of other ranges in the same object
    • GRanges variants strand-aware
    • shift(), narrow(), flank(), promoters(), resize(), restrict(), trim()
    • See ?"intra-range-methods"
  • Inter-range methods
    • Depends on other ranges in the same object
    • range(), reduce(), gaps(), disjoin()
    • coverage() (!)
    • see ?"inter-range-methods"
  • Between-range methods
    • Functions of two (or more) range objects
    • findOverlaps(), countOverlaps(), …, %over%, %within%, %outside%; union(), intersect(), setdiff(), punion(), pintersect(), psetdiff()

Alt Ranges Algebra

3.3 Biostrings (DNA or amino acid sequences)

Classes

Methods –

Related packages

Example

3.4 GenomicAlignments (Aligned reads)

Classes – GenomicRanges-like behaivor

Methods

Example

3.5 VariantAnnotation (Called variants)

Classes – GenomicRanges-like behavior

Functions and methods

Example

Related packages

Reference

3.6 rtracklayer (Genome annotations)

3.7 SummarizedExperiment

Alt SummarizedExperiment

Functions and methods

4 Input & representation of standard file formats

4.1 BAM files of aligned reads – GenomicAlignments

Recall: overall workflow

  1. Experimental design
  2. Wet-lab preparation
  3. High-throughput sequencing
  4. Alignment
    • Whole genome, vs. transcriptome
  5. Summary
  6. Statistical analysis
  7. Comprehension

BAM files of aligned reads

GenomicAlignments

4.2 Other formats and packages

Alt Files and the Bioconductor packages that input them

5 Resources

Acknowledgements

5.1 sessionInfo()

sessionInfo()
## R version 3.2.2 (2015-08-14)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Debian GNU/Linux stretch/sid
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
##  [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
## [10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats4    parallel  stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] VariantAnnotation_1.15.34               RNAseqData.HNRNPC.bam.chr14_0.7.0      
##  [3] GenomicAlignments_1.5.18                Rsamtools_1.21.21                      
##  [5] ALL_1.11.0                              org.Hs.eg.db_3.2.3                     
##  [7] RSQLite_1.0.0                           DBI_0.3.1                              
##  [9] ggplot2_1.0.1                           airway_0.103.1                         
## [11] limma_3.25.18                           DESeq2_1.9.51                          
## [13] RcppArmadillo_0.6.100.0.0               Rcpp_0.12.1                            
## [15] BSgenome.Hsapiens.UCSC.hg19_1.4.0       BSgenome_1.37.6                        
## [17] rtracklayer_1.29.28                     TxDb.Hsapiens.UCSC.hg19.knownGene_3.2.2
## [19] GenomicFeatures_1.21.33                 AnnotationDbi_1.31.19                  
## [21] SummarizedExperiment_0.3.11             Biobase_2.29.1                         
## [23] GenomicRanges_1.21.32                   GenomeInfoDb_1.5.16                    
## [25] microbenchmark_1.4-2                    Biostrings_2.37.8                      
## [27] XVector_0.9.4                           IRanges_2.3.26                         
## [29] S4Vectors_0.7.23                        BiocGenerics_0.15.11                   
## [31] BiocStyle_1.7.9                        
## 
## loaded via a namespace (and not attached):
##  [1] splines_3.2.2         Formula_1.2-1         latticeExtra_0.6-26   yaml_2.1.13          
##  [5] lattice_0.20-33       digest_0.6.8          RColorBrewer_1.1-2    colorspace_1.2-6     
##  [9] sandwich_2.3-4        htmltools_0.2.6       plyr_1.8.3            XML_3.98-1.3         
## [13] biomaRt_2.25.3        genefilter_1.51.1     zlibbioc_1.15.0       xtable_1.7-4         
## [17] mvtnorm_1.0-3         scales_0.3.0          BiocParallel_1.3.54   annotate_1.47.4      
## [21] TH.data_1.0-6         nnet_7.3-11           proto_0.3-10          survival_2.38-3      
## [25] magrittr_1.5          evaluate_0.8          MASS_7.3-44           foreign_0.8-66       
## [29] BiocInstaller_1.19.14 tools_3.2.2           formatR_1.2.1         multcomp_1.4-1       
## [33] stringr_1.0.0         munsell_0.4.2         locfit_1.5-9.1        cluster_2.0.3        
## [37] lambda.r_1.1.7        futile.logger_1.4.1   grid_3.2.2            RCurl_1.95-4.7       
## [41] labeling_0.3          bitops_1.0-6          rmarkdown_0.8.1       gtable_0.1.2         
## [45] codetools_0.2-14      reshape2_1.4.1        gridExtra_2.0.0       zoo_1.7-12           
## [49] knitr_1.11            Hmisc_3.17-0          futile.options_1.0.0  stringi_0.5-5        
## [53] geneplotter_1.47.0    rpart_4.1-10          acepack_1.3-3.3