Annotating Genomic Ranges

1 Background

Bioconductor can import diverse sequence-related file types, including fasta, fastq, BAM, VCF, gff, bed, and wig files, among others. Packages support common and advanced sequence manipulation operations such as trimming, transformation, and alignment. Domain-specific analyses include quality assessment, ChIP-seq, differential expression, RNA-seq, and other approaches. Bioconductor includes an interface to the Sequence Read Archive (via the SRAdb package).

This workflow walks through the annotation of a generic set of ranges with Bioconductor packages. The ranges can be any user-defined region of interest or can be from a public file.

2 Data Preparation

2.1 Human hg19

As a first step, data are put into a GRanges object so we can take advantage of overlap operations and store identifiers as metadata columns.

The first set of ranges are variants from a dbSNP Variant Call Format (VCF) file. This file can be downloaded from the ftp site at NCBI ftp://ftp.ncbi.nlm.nih.gov/snp/ and imported with readVcf() from the VariantAnnotation package. Alternatively, the file is available as a pre-parsed VCF object in the AnnotationHub.

library(VariantAnnotation)
library(AnnotationHub)

The Hub returns a VcfFile object with a reference to the file on disk.

setAnnotationHubOption("CACHE", "/liftrroot/")
hub <- AnnotationHub()
## updating metadata:
## retrieving 1 resource
## snapshotDate(): 2017-04-25

Query the Hub for clinvar VCF files build GRCh37:

mcols(query(hub, "clinvar.vcf", "GRCh37"))[,"sourceurl", drop=FALSE]
## DataFrame with 8 rows and 1 column
##                                                                                                             sourceurl
##                                                                                                           <character>
## AH50420                        ftp://ftp.ncbi.nlm.nih.gov/pub/clinvar/vcf_GRCh37/archive/2016/clinvar_20160203.vcf.gz
## AH50421                   ftp://ftp.ncbi.nlm.nih.gov/pub/clinvar/vcf_GRCh37/archive/2016/clinvar_20160203_papu.vcf.gz
## AH50422            ftp://ftp.ncbi.nlm.nih.gov/pub/clinvar/vcf_GRCh37/archive/2016/common_and_clinical_20160203.vcf.gz
## AH50423 ftp://ftp.ncbi.nlm.nih.gov/pub/clinvar/vcf_GRCh37/archive/2016/common_no_known_medical_impact_20160203.vcf.gz
## AH50424                        ftp://ftp.ncbi.nlm.nih.gov/pub/clinvar/vcf_GRCh38/archive/2016/clinvar_20160203.vcf.gz
## AH50425                   ftp://ftp.ncbi.nlm.nih.gov/pub/clinvar/vcf_GRCh38/archive/2016/clinvar_20160203_papu.vcf.gz
## AH50426            ftp://ftp.ncbi.nlm.nih.gov/pub/clinvar/vcf_GRCh38/archive/2016/common_and_clinical_20160203.vcf.gz
## AH50427 ftp://ftp.ncbi.nlm.nih.gov/pub/clinvar/vcf_GRCh38/archive/2016/common_no_known_medical_impact_20160203.vcf.gz

Retrieve one of the files:

fl <- query(hub, "clinvar.vcf", "GRCh37")[[1]]
## downloading from 'https://annotationhub.bioconductor.org/fetch/57150'
##     'https://annotationhub.bioconductor.org/fetch/57151'
## retrieving 2 resources

Read the data into a VCF object:

vcf <- readVcf(fl, "hg19")
dim(vcf)
## [1] 109721      0

Overlap operations require that seqlevels and the genome of the objects match. Here the VCF seqlevels are modified to match the TxDb.

library(TxDb.Hsapiens.UCSC.hg19.knownGene)
txdb_hg19 <- TxDb.Hsapiens.UCSC.hg19.knownGene
head(seqlevels(txdb_hg19))
## [1] "chr1" "chr2" "chr3" "chr4" "chr5" "chr6"
seqlevels(vcf)
##  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14"
## [15] "15" "16" "17" "18" "19" "20" "21" "22" "X"  "Y"  "MT"
seqlevels(vcf) <- paste0("chr", seqlevels(vcf))

For this example we’ll annotate chromosomes 3 and 18:

seqlevels(vcf, force=TRUE) <- c("chr3", "chr18")
## Warning: In BioC 3.5, the 'force' argument was replaced by the more
##   flexible 'pruning.mode' argument, and is deprecated. See ?seqinfo
##   for the supported pruning modes. Note that 'force=TRUE' is
##   equivalent to 'pruning.mode="coarse"'.
seqlevels(txdb_hg19) <- c("chr3", "chr18")

Sanity check to confirm we have matching seqlevels.

intersect(seqlevels(txdb_hg19), seqlevels(vcf))
## [1] "chr3"  "chr18"

The genomes already match so no change is needed.

unique(genome(txdb_hg19))
## [1] "hg19"
unique(genome(vcf))
## [1] "hg19"

The GRanges in a VCF object is extracted with ‘rowRanges()’.

gr_hg19 <- rowRanges(vcf)

2.2 Mouse mm10

The second set of ranges is a user-defined region of chromosome 4 in mouse. The idea here is that any region, known or unknown, can be annotated with the following steps.

Load the TxDb package and keep only the standard chromosomes.

library(TxDb.Mmusculus.UCSC.mm10.ensGene)
txdb_mm10 <- keepStandardChromosomes(TxDb.Mmusculus.UCSC.mm10.ensGene)

We are creating the GRanges from scratch and can specify the seqlevels (chromosome names) to match the TxDb.

head(seqlevels(txdb_mm10))
## [1] "chr1" "chr2" "chr3" "chr4" "chr5" "chr6"
gr_mm10 <- GRanges("chr4", IRanges(c(4000000, 107889000), width=1000))

Now assign the genome.

unique(genome(txdb_mm10))
## [1] "mm10"
genome(gr_mm10) <- "mm10"

3 Location in and Around Genes

locateVariants() in the VariantAnnotation package annotates ranges with transcript, exon, cds and gene ID’s from a TxDb. Various extractions are performed on the TxDb (exonsBy(), transcripts(), cdsBy(), etc.) and the result is overlapped with the ranges. An appropriate GRangesList can also be supplied as the annotation. Different variants such as ‘coding’, ‘fiveUTR’, ‘threeUTR’, ‘spliceSite’, ‘intron’, ‘promoter’, and ‘intergenic’ can be searched for by passing the appropriate constructor as the ‘region’ argument. See ?locateVariants for details.

loc_hg19 <- locateVariants(gr_hg19, txdb_hg19, AllVariants())
## 'select()' returned many:1 mapping between keys and columns
## 'select()' returned many:1 mapping between keys and columns
## 'select()' returned many:1 mapping between keys and columns
## 'select()' returned many:1 mapping between keys and columns
## 'select()' returned many:1 mapping between keys and columns
## 'select()' returned many:1 mapping between keys and columns
table(loc_hg19$LOCATION)
## 
## spliceSite     intron    fiveUTR   threeUTR     coding intergenic 
##       1520       7522       2049       1596      28014          9 
##   promoter 
##       2374
loc_mm10 <- locateVariants(gr_mm10, txdb_mm10, AllVariants()) 
## 'select()' returned 1:1 mapping between keys and columns
## 'select()' returned 1:1 mapping between keys and columns
## 'select()' returned 1:1 mapping between keys and columns
table(loc_mm10$LOCATION)
## 
## spliceSite     intron    fiveUTR   threeUTR     coding intergenic 
##          6          1          0          0          0          0 
##   promoter 
##         12

4 Annotate by ID

The ID’s returned from locateVariants() can be used in select() to map to ID’s in other annotation packages.

library(org.Hs.eg.db)
cols <- c("UNIPROT", "PFAM")
keys <- na.omit(unique(loc_hg19$GENEID))
head(select(org.Hs.eg.db, keys, cols, keytype="ENTREZID"))
## 'select()' returned 1:many mapping between keys and columns
##   ENTREZID    UNIPROT    PFAM
## 1    27255 A0A024R2C7 PF07679
## 2    27255 A0A024R2C7 PF00041
## 3    27255     B4DGV0 PF07679
## 4    27255     B4DGV0 PF00041
## 5    27255     Q9UQ52 PF07679
## 6    27255     Q9UQ52 PF00041

The ‘keytype’ argument specifies that the mouse TxDb contains Ensembl instead of Entrez gene id’s.

library(org.Mm.eg.db)
keys <- unique(loc_mm10$GENEID)
head(select(org.Mm.eg.db, keys, cols, keytype="ENSEMBL"))
## 'select()' returned 1:1 mapping between keys and columns
##              ENSEMBL UNIPROT    PFAM
## 1 ENSMUSG00000028236  Q7TQA3 PF00106
## 2 ENSMUSG00000028608  Q8BHG2 PF05907

5 Annotate by Position

Files stored in the AnnotationHub have been pre-processed into ranged-based R objects such as a GRanges, GAlignments and VCF. The positions in our GRanges can be overlapped with the ranges in the AnnotationHub files. This allows for easy subsetting of multiple files, resulting in only the ranges of interest.

Create a ‘hub’ from AnnotationHub and filter the files based on organism and genome build.

hub <- AnnotationHub()
## snapshotDate(): 2017-04-25
hub_hg19 <- subset(hub, 
                  (hub$species == "Homo sapiens") & (hub$genome == "hg19"))
length(hub_hg19)
## [1] 23859

Iterate over the first 3 files and extract ranges that overlap ‘gr_hg19’.

## downloading from 'https://annotationhub.bioconductor.org/fetch/5012'
## retrieving 1 resource
## downloading from 'https://annotationhub.bioconductor.org/fetch/5013'
## retrieving 1 resource
## downloading from 'https://annotationhub.bioconductor.org/fetch/5014'
## retrieving 1 resource
ov_hg19 <- lapply(1:3, function(i) subsetByOverlaps(hub_hg19[[i]], gr_hg19))
## loading from cache '/liftrroot//5012'
## loading from cache '/liftrroot//5013'
## loading from cache '/liftrroot//5014'

Inspect the results.

names(ov_hg19) <- names(hub_hg19)[1:3]
lapply(ov_hg19, head, n=3)
## $AH5012
## UCSC track 'cytoBand'
## UCSCData object with 3 ranges and 1 metadata column:
##       seqnames             ranges strand |        name
##          <Rle>          <IRanges>  <Rle> | <character>
##   [1]     chr3 [      1, 2800000]      * |       p26.3
##   [2]     chr3 [2800001, 4000000]      * |       p26.2
##   [3]     chr3 [4000001, 8700000]      * |       p26.1
##   -------
##   seqinfo: 93 sequences (1 circular) from hg19 genome
## 
## $AH5013
## UCSC track 'stsMap'
## UCSCData object with 3 ranges and 2 metadata columns:
##       seqnames               ranges strand |        name     score
##          <Rle>            <IRanges>  <Rle> | <character> <numeric>
##   [1]     chr3 [ 8787843,  8788458]      * |    BV209804      1000
##   [2]     chr3 [ 8788295,  8788424]      * |     RH92828      1000
##   [3]     chr3 [10183290, 10183558]      * |  GDB:361137      1000
##   -------
##   seqinfo: 93 sequences (1 circular) from hg19 genome
## 
## $AH5014
## UCSC track 'fishClones'
## UCSCData object with 3 ranges and 2 metadata columns:
##       seqnames             ranges strand |        name     score
##          <Rle>          <IRanges>  <Rle> | <character> <numeric>
##   [1]     chr3 [4354970, 4542349]      * |  RP11-91K16      1000
##   [2]     chr3 [4765290, 4929842]      * | RP11-106B10      1000
##   [3]     chr3 [8711065, 8882271]      * |  RP11-128A5      1000
##   -------
##   seqinfo: 93 sequences (1 circular) from hg19 genome

Annotating the mouse ranges in the same fashion is left as an exercise.

6 Annotating Variants

Amino acid coding changes

For the set of dbSNP variants that fall in coding regions, amino acid changes can be computed. The output contains one line for each variant-transcript match which can result in multiple lines for each variant.

library(BSgenome.Hsapiens.UCSC.hg19)
head(predictCoding(vcf, txdb_hg19, Hsapiens), 3)
## GRanges object with 3 ranges and 17 metadata columns:
##               seqnames             ranges strand | paramRangeID
##                  <Rle>          <IRanges>  <Rle> |     <factor>
##   rs140337334     chr3 [1427481, 1427481]      + |         <NA>
##   rs140337334     chr3 [1427481, 1427481]      + |         <NA>
##   rs140337334     chr3 [1427481, 1427481]      + |         <NA>
##                          REF                ALT      QUAL      FILTER
##               <DNAStringSet> <DNAStringSetList> <numeric> <character>
##   rs140337334              C                G,T      <NA>           .
##   rs140337334              C                G,T      <NA>           .
##   rs140337334              C                G,T      <NA>           .
##                    varAllele       CDSLOC    PROTEINLOC   QUERYID
##               <DNAStringSet>    <IRanges> <IntegerList> <integer>
##   rs140337334              G [2704, 2704]           902         1
##   rs140337334              G [2488, 2488]           830         1
##   rs140337334              G [2704, 2704]           902         1
##                      TXID         CDSID      GENEID   CONSEQUENCE
##               <character> <IntegerList> <character>      <factor>
##   rs140337334       13067         40577       27255 nonsynonymous
##   rs140337334       13068         40577       27255 nonsynonymous
##   rs140337334       13069         40577       27255 nonsynonymous
##                     REFCODON       VARCODON         REFAA         VARAA
##               <DNAStringSet> <DNAStringSet> <AAStringSet> <AAStringSet>
##   rs140337334            CCT            GCT             P             A
##   rs140337334            CCT            GCT             P             A
##   rs140337334            CCT            GCT             P             A
##   -------
##   seqinfo: 2 sequences from hg19 genome; no seqlengths
sessionInfo()
## R version 3.4.1 (2017-06-30)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Debian GNU/Linux 9 (stretch)
## 
## Matrix products: default
## BLAS/LAPACK: /usr/lib/libopenblasp-r0.2.19.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C             
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats4    parallel  methods   stats     graphics  grDevices utils    
## [8] datasets  base     
## 
## other attached packages:
##  [1] BSgenome.Hsapiens.UCSC.hg19_1.4.0      
##  [2] BSgenome_1.44.0                        
##  [3] rtracklayer_1.36.4                     
##  [4] org.Mm.eg.db_3.4.1                     
##  [5] org.Hs.eg.db_3.4.1                     
##  [6] TxDb.Mmusculus.UCSC.mm10.ensGene_3.4.0 
##  [7] TxDb.Hsapiens.UCSC.hg19.knownGene_3.2.2
##  [8] GenomicFeatures_1.28.4                 
##  [9] AnnotationDbi_1.38.1                   
## [10] AnnotationHub_2.8.2                    
## [11] VariantAnnotation_1.22.3               
## [12] Rsamtools_1.28.0                       
## [13] Biostrings_2.44.2                      
## [14] XVector_0.16.0                         
## [15] SummarizedExperiment_1.6.3             
## [16] DelayedArray_0.2.7                     
## [17] matrixStats_0.52.2                     
## [18] Biobase_2.36.2                         
## [19] GenomicRanges_1.28.4                   
## [20] GenomeInfoDb_1.12.2                    
## [21] IRanges_2.10.2                         
## [22] S4Vectors_0.14.3                       
## [23] BiocGenerics_0.22.0                    
## [24] shiny_1.0.3                            
## [25] rmarkdown_1.6                          
## [26] knitr_1.16                             
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.11                  lattice_0.20-35              
##  [3] rprojroot_1.2                 digest_0.6.12                
##  [5] mime_0.5                      R6_2.2.2                     
##  [7] backports_1.1.0               RSQLite_2.0                  
##  [9] evaluate_0.10.1               httr_1.2.1                   
## [11] highr_0.6                     BiocInstaller_1.26.0         
## [13] zlibbioc_1.22.0               rlang_0.1.1                  
## [15] curl_2.7                      rstudioapi_0.6               
## [17] miniUI_0.1.1                  blob_1.1.0                   
## [19] Matrix_1.2-10                 BiocParallel_1.10.1          
## [21] stringr_1.2.0                 questionr_0.6.1              
## [23] RCurl_1.95-4.8                bit_1.1-12                   
## [25] biomaRt_2.32.1                compiler_3.4.1               
## [27] httpuv_1.3.5                  pkgconfig_2.0.1              
## [29] htmltools_0.3.6               tibble_1.3.3                 
## [31] GenomeInfoDbData_0.99.0       interactiveDisplayBase_1.14.0
## [33] bookdown_0.4                  XML_3.98-1.9                 
## [35] GenomicAlignments_1.12.1      bitops_1.0-6                 
## [37] grid_3.4.1                    xtable_1.8-2                 
## [39] DBI_0.7                       magrittr_1.5                 
## [41] rmdformats_0.3.3              stringi_1.1.5                
## [43] tools_3.4.1                   bit64_0.9-7                  
## [45] yaml_2.1.14                   memoise_1.1.0

7 Exercises

Exercise 1: VCF header and reading data subsets.

VCF files can be large and it’s often the case that only a subset of variables or genomic positions are of interest. The scanVcfHeader() function in the VariantAnnotation package retrieves header information from a VCF file. Based on the information returned from scanVcfHeader() a ScanVcfParam() object can be created to read in a subset of data from a VCF file. * Use scanVcfHeader() to inspect the header information in the ‘chr22.vcf.gz’ file in VariantAnnotation package. * Select a few ‘info’ or ‘geno’ variables and create a ScanVcfParam object. * Use the ScanVcfParam object as the ‘param’ argument to readVcf() to read in a subset of data. Note that the header() accessor operates on VCF objects in the R workspace. Try header(vcf) on the dbSNP file from AnnotationHub.

Exercise 2: Annotate the mouse ranges in ‘gr_mm10’ with AnnotationHub files. * Create a new ‘hub’ and filter on organism. * Isolate the files for the appropriate genome build and perform overlaps.

Exercise 3: Annotate a gene range from Saccharomyces Scerevisiae. * Load TxDb.Scerevisiae.UCSC.sacCer3.sgdGene and extract the gene ranges. (Hint: use transcriptsBy() and range()). * Isolate the range for gene “YBL086C”. * Create a new ‘hub’ from AnnotationHub and filter by organism. (You should see >= 39 files.) * Select the files for ‘sacCer3’ and perform overlaps.

[ Back to top ]

Valerie Obenchain