Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
278 lines (218 sloc) 7.96 KB
title author output
Annotating Genomic Ranges
Valerie Obenchain
toc_depth number_sections


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 and imported with readVcf() from the VariantAnnotation package. Alternatively, the file is available as a pre-parsed VCF object in the AnnotationHub.


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

setAnnotationHubOption("CACHE", "/liftrroot/")
hub <- AnnotationHub()

Query the Hub for clinvar VCF files build GRCh37:

mcols(query(hub, "clinvar.vcf", "GRCh37"))[,"sourceurl", drop=FALSE]

Retrieve one of the files:

fl <- query(hub, "clinvar.vcf", "GRCh37")[[1]]

Read the data into a VCF object:

vcf <- readVcf(fl, "hg19")

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

txdb_hg19 <- TxDb.Hsapiens.UCSC.hg19.knownGene
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))

The genomes already match so no change is needed.


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.

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.

gr_mm10 <- GRanges("chr4", IRanges(c(4000000, 107889000), width=1000))

Now assign the genome.

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())
loc_mm10 <- locateVariants(gr_mm10, txdb_mm10, AllVariants()) 

Annotate by ID

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

cols <- c("UNIPROT", "PFAM")
keys <- na.omit(unique(loc_hg19$GENEID))
head(select(, keys, cols, keytype="ENTREZID"))

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

keys <- unique(loc_mm10$GENEID)
head(select(, keys, cols, keytype="ENSEMBL"))

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()
hub_hg19 <- subset(hub, 
                  (hub$species == "Homo sapiens") & (hub$genome == "hg19"))

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

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

Inspect the results.

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

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

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.
head(predictCoding(vcf, txdb_hg19, Hsapiens), 3)


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 ]