About This Document »

Package name annotation
Built with Bioconductor (R) 3.4 (3.3.1)
Last Built Fri, 21 Oct 2016 10:18:32 -0700
Last Modified Mon, 12 Sep 2016 04:07:21 -0700 (r120852)
Source Package ../annotation_0.99.120852.tar.gz
Windows Binary ../annotation_0.99.120852.zip
Mac OS X 10.10 (Yosemite) ../annotation_0.99.120852.tgz
R Script Annotating_Genomic_Ranges.R

To install this workflow under Bioconductor 3.4, start R and enter:
source("http://bioconductor.org/workflows.R")
workflowInstall("annotation")

Annotating Genomic Ranges

Valerie Obenchain

Contents

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.

Data Preparation

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.

hub <- AnnotationHub()

## snapshotDate(): 2016-10-11

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]]

## loading from cache '/var/lib/jenkins//.AnnotationHub/57150'
##     '/var/lib/jenkins//.AnnotationHub/57151'

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")
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)

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"

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

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 PF00041
## 2    27255 A0A024R2C7 PF07679
## 3    27255     B4DGV0 PF00041
## 4    27255     B4DGV0 PF07679
## 5    27255     Q9UQ52 PF00041
## 6    27255     Q9UQ52 PF07679

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

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(): 2016-10-11

hub_hg19 <- subset(hub, 
                  (hub$species == "Homo sapiens") & (hub$genome == "hg19"))
length(hub_hg19)

## [1] 29802

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

## loading from cache '/var/lib/jenkins//.AnnotationHub/522'

## loading from cache '/var/lib/jenkins//.AnnotationHub/523'

## loading from cache '/var/lib/jenkins//.AnnotationHub/524'

ov_hg19 <- lapply(1:3, function(i) subsetByOverlaps(hub_hg19[[i]], gr_hg19))

## loading from cache '/var/lib/jenkins//.AnnotationHub/522'

## loading from cache '/var/lib/jenkins//.AnnotationHub/523'

## loading from cache '/var/lib/jenkins//.AnnotationHub/524'

Inspect the results.

names(ov_hg19) <- names(hub_hg19)[1:3]
lapply(ov_hg19, head, n=3)

## $AH522
## GRanges object with 3 ranges and 6 metadata columns:
##       seqnames                 ranges strand |        name     score
##          <Rle>              <IRanges>  <Rle> | <character> <integer>
##   [1]     chr3 [129158536, 129159587]      * |           .         0
##   [2]     chr3 [ 52188184,  52189104]      * |           .         0
##   [3]     chr3 [ 81810026,  81811025]      * |           .         0
##       signalValue    pValue       qValue      peak
##         <numeric> <numeric>    <numeric> <integer>
##   [1]      22.892    91.780 4.430926e-90       265
##   [2]      17.158    84.056 1.807810e-82       627
##   [3]       8.621    41.489 1.671741e-40       235
##   -------
##   seqinfo: 23 sequences from hg19 genome
## 
## $AH523
## GRanges object with 3 ranges and 5 metadata columns:
##       seqnames             ranges strand |        name     score
##          <Rle>          <IRanges>  <Rle> | <character> <integer>
##   [1]    chr18 [2697996, 2698047]      * |           .         0
##   [2]    chr18 [2700716, 2700890]      * |           .         0
##   [3]    chr18 [2707558, 2707672]      * |           .         0
##       signalValue    pValue    qValue
##         <numeric> <numeric> <numeric>
##   [1]     157.267        -1        -1
##   [2]     288.597        -1        -1
##   [3]     277.875        -1        -1
##   -------
##   seqinfo: 25 sequences (1 circular) from hg19 genome
## 
## $AH524
## GRanges object with 3 ranges and 5 metadata columns:
##       seqnames             ranges strand |        name     score
##          <Rle>          <IRanges>  <Rle> | <character> <integer>
##   [1]    chr18 [2697829, 2698047]      * |           .         0
##   [2]    chr18 [2700711, 2700908]      * |           .         0
##   [3]    chr18 [2707563, 2707636]      * |           .         0
##       signalValue    pValue    qValue
##         <numeric> <numeric> <numeric>
##   [1]     354.483        -1        -1
##   [2]     485.956        -1        -1
##   [3]     182.056        -1        -1
##   -------
##   seqinfo: 25 sequences (1 circular) from hg19 genome

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

Annotating Variants

<h4 id=amino-acid-coding-changes”>Amino acid coding changes</h4> 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.3.1 (2016-06-21)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 14.04.3 LTS
## 
## 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=en_US.UTF-8   
##  [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  stats     graphics  grDevices utils     datasets 
## [8] methods   base     
## 
## other attached packages:
##  [1] BSgenome.Hsapiens.UCSC.hg19_1.4.0      
##  [2] BSgenome_1.42.0                        
##  [3] rtracklayer_1.34.0                     
##  [4] org.Mm.eg.db_3.4.0                     
##  [5] org.Hs.eg.db_3.4.0                     
##  [6] TxDb.Mmusculus.UCSC.mm10.ensGene_3.4.0 
##  [7] TxDb.Hsapiens.UCSC.hg19.knownGene_3.2.2
##  [8] GenomicFeatures_1.26.0                 
##  [9] AnnotationDbi_1.36.0                   
## [10] AnnotationHub_2.6.0                    
## [11] VariantAnnotation_1.20.0               
## [12] Rsamtools_1.26.0                       
## [13] Biostrings_2.42.0                      
## [14] XVector_0.14.0                         
## [15] SummarizedExperiment_1.4.0             
## [16] Biobase_2.34.0                         
## [17] GenomicRanges_1.26.1                   
## [18] GenomeInfoDb_1.10.0                    
## [19] IRanges_2.8.0                          
## [20] S4Vectors_0.12.0                       
## [21] BiocGenerics_0.20.0                    
## [22] BiocStyle_2.3.1                        
## [23] rmarkdown_1.1                          
## [24] knitr_1.14                             
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.7                   BiocInstaller_1.24.0         
##  [3] formatR_1.4                   bitops_1.0-6                 
##  [5] tools_3.3.1                   zlibbioc_1.20.0              
##  [7] biomaRt_2.30.0                digest_0.6.10                
##  [9] evaluate_0.10                 RSQLite_1.0.0                
## [11] tibble_1.2                    lattice_0.20-34              
## [13] Matrix_1.2-6                  shiny_0.14.1                 
## [15] DBI_0.5-1                     curl_2.2                     
## [17] yaml_2.1.13                   stringr_1.1.0                
## [19] httr_1.2.1                    grid_3.3.1                   
## [21] R6_2.2.0                      XML_3.98-1.4                 
## [23] BiocParallel_1.8.0            magrittr_1.5                 
## [25] htmltools_0.3.5               GenomicAlignments_1.10.0     
## [27] assertthat_0.1                xtable_1.8-2                 
## [29] mime_0.5                      interactiveDisplayBase_1.12.0
## [31] httpuv_1.3.3                  stringi_1.1.2                
## [33] RCurl_1.95-4.8

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 ]