1 Getting started

GenomicScores is an R package distributed as part of the Bioconductor project. To install the package, start R and enter:

source("http://bioconductor.org/biocLite.R")
biocLite("GenomicScores")

Once GenomicScores is installed, it can be loaded with the following command.

library(GenomicScores)

Often, however, GenomicScores will be automatically loaded when working with an annotation package that uses GenomicScores, such as phastCons100way.UCSC.hg19.

2 Genomewide position-specific scores

Genomewide scores assign each genomic position a numeric value denoting an estimated measure of constraint or impact on variation at that position. They are commonly used to filter single nucleotide variants or assess the degree of constraint or functionality of genomic features. Genomic scores are built on the basis of different sources of information such as sequence homology, functional domains, physical-chemical changes of amino acid residues, etc.

One particular example of genomic scores are phastCons scores. They provide a measure of conservation obtained from genomewide alignments using the program phast (Phylogenetic Analysis with Space/Time models) from Siepel et al. (2005). The GenomicScores package allows one to retrieve these scores through annotation packages (Section 4) or as AnnotationHub resources (Section 5).

Often, genomic scores such as phastCons are used within workflows running on top of R and Bioconductor. The purpose of the GenomicScores package is to enable an easy and interactive access to genomic scores within those workflows.

3 Lossy storage of genomic scores with compressed vectors

Storing and accessing genomic scores within R is challenging when their values cover large regions of the genome, resulting in gigabytes of double-precision numbers. This is the case, for instance, for phastCons (Siepel et al. 2005), CADD (Kircher et al. 2014) or M-CAP (Jagadeesh et al. 2016) scores.

We address this problem by using lossy compression, also called quantization, coupled with run-length encoding (Rle) vectors. Lossy compression attempts to trade off precision for compression without compromising the scientific integrity of the data (Zender 2016).

Sometimes, measurements and statistical estimates under certain models generate false precision. False precision is essentialy noise that wastes storage space and it is meaningless from the scientific point of view (Zender 2016). In those circumstances, lossy compression not only saves storage space, but also removes false precision.

The use of lossy compression leads to a subset of quantized values much smaller than the original set of genomic scores, resulting in long runs of identical values along the genome. These runs of identical values can be further compressed using the implementation of Rle vectors available in the S4Vectors Bioconductor package.

To enable a seamless access to genomic scores stored with quantized values in compressed vectors the GenomicScores defines the GScores class of objects. This class manages the location, loading and dequantization of genomic scores stored separately on each chromosome.

4 Retrieval of genomic scores through annotation packages

There are currently four different annotation packages that store genomic scores and can be accessed using the GenomicScores package; see Table 1.


Table 1: Bioconductor annotation packages storing genomic scores
Annotation Package Description
phastCons100way.UCSC.hg19 phastCons scores derived from the alignment of the human genome (hg19) to other 99 vertebrate species.
phastCons100way.UCSC.hg38 phastCons scores derived from the alignment of the human genome (hg38) to other 99 vertebrate species.
phastCons7way.UCSC.hg38 phastCons scores derived from the alignment of the human genome (hg38) to other 6 mammal species.
fitCons.UCSC.hg19 fitCons scores: fitness consequences of functional annotation for the human genome (hg19).

This is an example of how genomic scores can be retrieved using the phastCons100way.UCSC.hg19 package. Here, a GScores object is created when the package is loaded.

library(phastCons100way.UCSC.hg19)
phast <- phastCons100way.UCSC.hg19
class(phast)
## [1] "GScores"
## attr(,"package")
## [1] "GenomicScores"

The help page of the GScores class describes the different methods to access the information and metadata stored in a GScores object. To retrieve genomic scores for specific positions we should use the function gscores(), as follows.

gscores(phast, GRanges(seqnames="chr22", IRanges(start=50967020:50967025, width=1)))
## GRanges object with 6 ranges and 1 metadata column:
##       seqnames    ranges strand |   default
##          <Rle> <IRanges>  <Rle> | <numeric>
##   [1]    chr22  50967020      * |         1
##   [2]    chr22  50967021      * |         1
##   [3]    chr22  50967022      * |       0.8
##   [4]    chr22  50967023      * |         1
##   [5]    chr22  50967024      * |         1
##   [6]    chr22  50967025      * |         0
##   -------
##   seqinfo: 1 sequence from Genome Reference Consortium GRCh37 genome; no seqlengths

The GenomicScores package only loads the scores data from one sequence to retrieve metadata and from the sequences that are being queried. Note that now the GScores object has loaded the scores from chr22.

phast
## GScores object 
## # organism: Homo sapiens (UCSC, hg19)
## # provider: UCSC
## # provider version: 09Feb2014
## # download date: Apr 10, 2018
## # loaded sequences: chr19_gl000208_random, chr22
## # number of sites: 2858 millions
## # maximum abs. error: 0.05
## # use 'citation()' to cite these data in publications

The bibliographic reference to cite the genomic scores stored in a GScores object can be accessed using the citation() method either on the package name or on the GScores object. The latter is implemented in the GenomicScores package and provides a bibentry object.

citation(phast)
## Adam Siepel, Gill Berejano, Jakob S. Pedersen, Angie S. Hinrichs,
## Minmei Hou, Kate Rosenbloom, Hiram Clawson, John Spieth, LaDeana W.
## Hillier, Stephen Richards, George M. Weinstock, Richard K. Wilson,
## Richard A. Gibbs, W. James Kent, Webb Miller, David Haussler (2005).
## "Evolutionarily conserved elements in vertebrate, insect, worm, and
## yeast genomes." _Genome Research_, *15*, 1034-1050. doi:
## 10.1101/gr.3715005 (URL: http://doi.org/10.1101/gr.3715005).

Other methods tracing provenance and other metadata are provider(), providerVersion(), organism() and seqlevelsStyle(); please consult the help page of the GScores class for a comprehensive list of available methods.

provider(phast)
## [1] "UCSC"
providerVersion(phast)
## [1] "09Feb2014"
organism(phast)
## [1] "Homo sapiens"
seqlevelsStyle(phast)
## [1] "UCSC"

5 Retrieval of genomic scores through AnnotationHub resources

Another way to retrieve genomic scores is by using the AnnotationHub, which is a web resource that provides a central location where genomic files (e.g., VCF, bed, wig) and other resources from standard (e.g., UCSC, Ensembl) and distributed sites, can be found. A Bioconductor AnnotationHub web resource creates and manages a local cache of files retrieved by the user, helping with quick and reproducible access.

The first step to retrieve genomic scores is to check the ones available to download.

availableGScores()
##  [1] "cadd.v1.3.hg19"            "fitCons.UCSC.hg19"        
##  [3] "linsight.UCSC.hg19"        "mcap.v1.0.hg19"           
##  [5] "phastCons7way.UCSC.hg38"   "phastCons27way.UCSC.dm6"  
##  [7] "phastCons60way.UCSC.mm10"  "phastCons100way.UCSC.hg19"
##  [9] "phastCons100way.UCSC.hg38" "phyloP60way.UCSC.mm10"    
## [11] "phyloP100way.UCSC.hg19"    "phyloP100way.UCSC.hg38"

The selected resource can be downloaded with the function getGScores(). After the resource is downloaded the first time, the cached copy will enable a quicker retrieval later.

phast <- getGScores("phastCons100way.UCSC.hg19")

Finally, the phastCons score of a particular genomic position is retrieved exactly in the same we did with the annotation package.

gscores(phast, GRanges(seqnames="chr22", IRanges(start=50967020:50967025, width=1)))
## GRanges object with 6 ranges and 1 metadata column:
##       seqnames    ranges strand |   default
##          <Rle> <IRanges>  <Rle> | <numeric>
##   [1]    chr22  50967020      * |         1
##   [2]    chr22  50967021      * |         1
##   [3]    chr22  50967022      * |       0.8
##   [4]    chr22  50967023      * |         1
##   [5]    chr22  50967024      * |         1
##   [6]    chr22  50967025      * |         0
##   -------
##   seqinfo: 1 sequence from Genome Reference Consortium GRCh37 genome; no seqlengths

5.1 Building an annotation package from a GScores object

Retrieving genomic scores through AnnotationHub resources requires an internet connection and we may want to work with such resources offline. For that purpose, we can create ourselves an annotation package, such as phastCons100way.UCSC.hg19, from a GScores object corresponding to a downloaded AnnotationHub resource. To do that we use the function makeGScoresPackage() as follows:

makeGScoresPackage(phast, maintainer="Me <me@example.com>", author="Me", version="1.0.0")
## Creating package in ./phastCons100way.UCSC.hg19

An argument, destDir, which by default points to the current working directory, can be used to change where in the filesystem the package is created. Afterwards, we should still build and install the package via, e.g., R CMD build and R CMD INSTALL, to be able to use it offline.

6 Retrieval of multiple scores per genomic position

Among the score sets available as AnnotationHub web resources shown in the previous section, two of them, CADD (Kircher et al. 2014) and M-CAP (Jagadeesh et al. 2016), provide multiple scores per genomic position that capture the tolerance to mutations of single nucleotides. Using M-CAP scores, we will illustrate how this type of scores are retrieved by default.

mcap <- getGScores("mcap.v1.0.hg19")
mcap
## GScores object 
## # organism: Homo sapiens (UCSC, hg19)
## # provider: Stanford
## # provider version: v1.0
## # download date: Mar 15, 2017
## # loaded sequences: default
## # maximum abs. error: 0.005
## # use 'citation()' to cite these data in publications
citation(mcap)
## Karthik A. Jagadeesh, Aaron M. Wenger, Mark J. Berger, Harendra Guturu,
## Peter D. Stenson, David N. Cooper, Jonathan A. Bernstein, Gill Berejano
## (2016). "M-CAP eliminates a majority of variants of uncertain
## significance in clinical exomes at high sensitivity." _Nature
## Genetics_, *48*, 1581-1586. doi: 10.1038/ng.3703 (URL:
## http://doi.org/10.1038/ng.3703).
gr <- GRanges(seqnames="chr22", IRanges(50967020:50967025, width=1))
gscores(mcap, gr)
## GRanges object with 6 ranges and 1 metadata column:
##       seqnames    ranges strand |        default
##          <Rle> <IRanges>  <Rle> |       <matrix>
##   [1]    chr22  50967020      * | 0.54:0.55:0.34
##   [2]    chr22  50967021      * | 0.47:0.64:0.64
##   [3]    chr22  50967022      * |       NA:NA:NA
##   [4]    chr22  50967023      * | 0.57:0.39:0.46
##   [5]    chr22  50967024      * |    NA:0.5:0.49
##   [6]    chr22  50967025      * |   NA:0.59:0.59
##   -------
##   seqinfo: 1 sequence from Genome Reference Consortium GRCh37 genome; no seqlengths

The previous call to the gscores() method returns three scores per position corresponding to the M-CAP scores that estimate the tolerance of mutating the reference nucleotide at that position to each of the possible alternative alleles, in alphabetical order. One may directly retrieve the scores for combinations of reference and alternative alelles using the ref and alt arguments of the gscores() method. See the following example with the the previous genomic positions and some randomly selected alternative alleles.

library(BSgenome.Hsapiens.UCSC.hg19)

refAlleles <- as.character(getSeq(Hsapiens, gr))
altAlleles <- DNA_BASES[(match(refAlleles, DNA_BASES)) %% 4 + 1]
cbind(REF=refAlleles, ALT=altAlleles)
##      REF ALT
## [1,] "C" "G"
## [2,] "G" "T"
## [3,] "T" "A"
## [4,] "C" "G"
## [5,] "C" "G"
## [6,] "G" "T"
gscores(mcap, gr, ref=refAlleles, alt=altAlleles)
## GRanges object with 6 ranges and 1 metadata column:
##       seqnames    ranges strand |   default
##          <Rle> <IRanges>  <Rle> | <numeric>
##   [1]    chr22  50967020      * |      0.55
##   [2]    chr22  50967021      * |      0.64
##   [3]    chr22  50967022      * |      <NA>
##   [4]    chr22  50967023      * |      0.39
##   [5]    chr22  50967024      * |       0.5
##   [6]    chr22  50967025      * |      0.59
##   -------
##   seqinfo: 1 sequence from Genome Reference Consortium GRCh37 genome; no seqlengths

7 Summarization of genomic scores

The input genomic ranges to the gscores() method may have widths larger than one nucleotide. In those cases, and when there is only one score per position, the gscores() method calculates, by default, the arithmetic mean of the scores across each range.

gr1 <- GRanges(seqnames="chr22", IRanges(start=50967020:50967025, width=1))
gr1sco <- gscores(phast, gr1)
gr1sco
## GRanges object with 6 ranges and 1 metadata column:
##       seqnames    ranges strand |   default
##          <Rle> <IRanges>  <Rle> | <numeric>
##   [1]    chr22  50967020      * |         1
##   [2]    chr22  50967021      * |         1
##   [3]    chr22  50967022      * |       0.8
##   [4]    chr22  50967023      * |         1
##   [5]    chr22  50967024      * |         1
##   [6]    chr22  50967025      * |         0
##   -------
##   seqinfo: 1 sequence from Genome Reference Consortium GRCh37 genome; no seqlengths
mean(gr1sco$default)
## [1] 0.8
gr2 <- GRanges("chr22:50967020-50967025")
gscores(phast, gr2)
## GRanges object with 1 range and 1 metadata column:
##       seqnames            ranges strand |   default
##          <Rle>         <IRanges>  <Rle> | <numeric>
##   [1]    chr22 50967020-50967025      * |       0.8
##   -------
##   seqinfo: 1 sequence from Genome Reference Consortium GRCh37 genome; no seqlengths

However, we may change the way in which scores from multiple-nucleotide ranges are summarized with the argument summaryFun, as follows.

gscores(phast, gr2, summaryFun=max)
## GRanges object with 1 range and 1 metadata column:
##       seqnames            ranges strand |   default
##          <Rle>         <IRanges>  <Rle> | <numeric>
##   [1]    chr22 50967020-50967025      * |         1
##   -------
##   seqinfo: 1 sequence from Genome Reference Consortium GRCh37 genome; no seqlengths
gscores(phast, gr2, summaryFun=min)
## GRanges object with 1 range and 1 metadata column:
##       seqnames            ranges strand |   default
##          <Rle>         <IRanges>  <Rle> | <numeric>
##   [1]    chr22 50967020-50967025      * |         0
##   -------
##   seqinfo: 1 sequence from Genome Reference Consortium GRCh37 genome; no seqlengths
gscores(phast, gr2, summaryFun=median)
## GRanges object with 1 range and 1 metadata column:
##       seqnames            ranges strand |   default
##          <Rle>         <IRanges>  <Rle> | <numeric>
##   [1]    chr22 50967020-50967025      * |         1
##   -------
##   seqinfo: 1 sequence from Genome Reference Consortium GRCh37 genome; no seqlengths

8 Retrieval of quantized genomic scores

The specific quantization and dequantization functions are stored as part of the metadata of a GScores object and they can be examined with the methods qfun() and dqfun(), respectively. The latter is called by the gscores() method to retrieve genomic scores.

phastqfun <- qfun(phast)
phastqfun
## function (x) 
## {
##     q <- as.integer(sprintf("%.0f", 10 * x))
##     q <- q + 1L
##     q
## }
## attr(,"description")
## [1] "multiply by 10, round to nearest integer, add up one"
phastdqfun <- dqfun(phast)
phastdqfun
## function (q) 
## {
##     x <- as.numeric(q)
##     x[x == 0] <- NA
##     x <- (x - 1)/10
##     x
## }
## <bytecode: 0x2402cad0>
## attr(,"description")
## [1] "subtract one integer unit, divide by 10"

For single-nucleotide ranges, we can retrieve the quantized genomic scores using the argument quantized=TRUE.

gr1sco <- gscores(phast, gr1, quantized=TRUE)
gr1sco
## GRanges object with 6 ranges and 1 metadata column:
##       seqnames    ranges strand | default
##          <Rle> <IRanges>  <Rle> |   <Rle>
##   [1]    chr22  50967020      * |      0b
##   [2]    chr22  50967021      * |      0b
##   [3]    chr22  50967022      * |      09
##   [4]    chr22  50967023      * |      0b
##   [5]    chr22  50967024      * |      0b
##   [6]    chr22  50967025      * |      01
##   -------
##   seqinfo: 1 sequence from Genome Reference Consortium GRCh37 genome; no seqlengths

Using the dequantization function we can obtain later the genomic scores.

phastdqfun(gr1sco$default)
## [1] 1.0 1.0 0.8 1.0 1.0 0.0

9 Populations of scores

A single GScores object may store multiple populations of scores of the same kind. One such case is when scores are stored with different precision levels. This is the case for the package phastCons100way.UCSC.hg19, in which phastCons scores are compressed rounding values to 1 and 2 decimal places. To figure out what are the available score populations in a GScores object we should use the method populations():

populations(phast)
## [1] "default" "DP2"

Whenever one of these populations is called default, this is the one used by default. In other cases we can find out which is the default population as follows:

defaultPopulation(phast)
## [1] "default"

To use one of the available score populations we should use the argument pop in the corresponding methods, as follows:

gscores(phast, gr1, pop="DP2")
## GRanges object with 6 ranges and 1 metadata column:
##       seqnames    ranges strand |       DP2
##          <Rle> <IRanges>  <Rle> | <numeric>
##   [1]    chr22  50967020      * |      0.98
##   [2]    chr22  50967021      * |      0.98
##   [3]    chr22  50967022      * |      0.83
##   [4]    chr22  50967023      * |      0.99
##   [5]    chr22  50967024      * |      0.96
##   [6]    chr22  50967025      * |         0
##   -------
##   seqinfo: 1 sequence from Genome Reference Consortium GRCh37 genome; no seqlengths
qfun(phast, pop="DP2")
## function (x) 
## {
##     q <- as.integer(sprintf("%.0f", 100 * x))
##     q <- q + 1L
##     q
## }
## attr(,"description")
## [1] "multiply by 100, round to nearest integer, add up one"
dqfun(phast, pop="DP2")
## function (q) 
## {
##     x <- as.numeric(q)
##     x[x == 0] <- NA
##     x <- (x - 1)/100
##     x
## }
## attr(,"description")
## [1] "subtract one integer unit, divide by 100"

We can also change the default scores population to use, as follows:

defaultPopulation(phast) <- "DP2"
phast
## GScores object 
## # organism: Homo sapiens (UCSC, hg19)
## # provider: UCSC
## # provider version: 09Feb2014
## # download date: Apr 10, 2018
## # loaded sequences: chr19_gl000208_random, chr22
## # loaded populations: default, DP2
## # default scores population: DP2
## # number of sites: 2858 millions
## # maximum abs. error (def. pop.): 0.005
## # use 'citation()' to cite these data in publications
gscores(phast, gr1)
## GRanges object with 6 ranges and 1 metadata column:
##       seqnames    ranges strand |       DP2
##          <Rle> <IRanges>  <Rle> | <numeric>
##   [1]    chr22  50967020      * |      0.98
##   [2]    chr22  50967021      * |      0.98
##   [3]    chr22  50967022      * |      0.83
##   [4]    chr22  50967023      * |      0.99
##   [5]    chr22  50967024      * |      0.96
##   [6]    chr22  50967025      * |         0
##   -------
##   seqinfo: 1 sequence from Genome Reference Consortium GRCh37 genome; no seqlengths
qfun(phast)
## function (x) 
## {
##     q <- as.integer(sprintf("%.0f", 100 * x))
##     q <- q + 1L
##     q
## }
## attr(,"description")
## [1] "multiply by 100, round to nearest integer, add up one"
dqfun(phast)
## function (q) 
## {
##     x <- as.numeric(q)
##     x[x == 0] <- NA
##     x <- (x - 1)/100
##     x
## }
## <bytecode: 0x2b53c3b0>
## attr(,"description")
## [1] "subtract one integer unit, divide by 100"

10 Retrieval of minor allele frequency data

One particular type of genomic scores that are accessible through the GScores class is minor allele frequency (MAF) data. There are currently 9 different annotation packages that store MAF values using the GenomicScores package; see Table 2 below.

Table 2: Bioconductor annotation packages storing MAF data
Annotation Package Description
MafDb.1Kgenomes.phase1.hs37d5 MAF data from the 1000 Genomes Project Phase 1 for the human genome version GRCh37.
MafDb.1Kgenomes.phase3.hs37d5 MAF data from the 1000 Genomes Project Phase 3 for the human genome version GRCh37.
MafDb.ESP6500SI.V2.SSA137.hs37d5 MAF data from NHLBI ESP 6500 exomes for the human genome version GRCh37.
MafDb.ESP6500SI.V2.SSA137.GRCh38 MAF data from NHLBI ESP 6500 exomes for the human genome version GRCh38.
MafDb.ExAC.r1.0.hs37d5 MAF data from ExAC 60706 exomes for the human genome version GRCh37.
MafDb.ExAC.r1.0.nonTCGA.hs37d5 MAF data from ExAC 53105 nonTCGA exomes for the human genome version GRCh37.
MafDb.gnomAD.r2.0.1.hs37d5 MAF data from gnomAD 15496 genomes for the human genome version GRCh37.
MafDb.gnomADex.r2.0.1.hs37d5 MAF data from gnomADex 123136 exomes for the human genome version GRCh37.
MafDb.TOPMed.freeze5.hg38 MAF data from NHLBI TOPMed 62784 genomes for the human genome version GRCh38.

In this context, the scores populations correspond to populations of individuals from which the MAF data were derived, while all MAF data were compressed using a precision of one significant figure for MAF < 0.1 and two significant figures for MAF >= 0.1.

library(MafDb.1Kgenomes.phase1.hs37d5)

mafdb <- MafDb.1Kgenomes.phase1.hs37d5
mafdb
## GScores object 
## # organism: Homo sapiens (1000genomes, hs37d5)
## # provider: IGSR
## # provider version: Phase1
## # download date: Mar 30, 2018
## # loaded sequences (SNRs): 22
## # loaded sequences (nonSNRs): none
## # default scores population: AF
## # number of sites: 40 millions
## # maximum abs. error (def. pop.): 0.000141
## # use 'citation()' to cite these data in publications
populations(mafdb)
## [1] "AF"     "AFR_AF" "AMR_AF" "ASN_AF" "EUR_AF"

Consider the following example in which we are interested in MAF values for variants associated with eye color. We can start by fetching the corresponding variant identifers from the GWAS catalog using the gwascat package, as follows.

library(gwascat)
data(ebicat37)
eyersids <- unique(subsetByTraits(ebicat37, tr="Eye color")$SNPS)
eyersids
##  [1] "rs12913832" "rs7173419"  "rs3002288"  "rs12896399" "rs1408799" 
##  [6] "rs12520016" "rs288139"   "rs1667394"  "rs4596632"  "rs12203592"
## [11] "rs16891982" "rs1393350"  "rs1847134"

We query the genomic position of the variant identifier through the SNPlocs.Hsapiens.dbSNP144.GRCh37 package, which stores the build 144 of the dbSNP database.

library(SNPlocs.Hsapiens.dbSNP144.GRCh37)
rng <- snpsById(SNPlocs.Hsapiens.dbSNP144.GRCh37, ids=eyersids)
rng
## GPos object with 13 positions and 2 metadata columns:
##        seqnames       pos strand |   RefSNP_id alleles_as_ambig
##           <Rle> <integer>  <Rle> | <character>      <character>
##    [1]       15  28365618      * |  rs12913832                R
##    [2]       15  28196821      * |   rs7173419                Y
##    [3]        1 213126565      * |   rs3002288                R
##    [4]       14  92773663      * |  rs12896399                K
##    [5]        9  12672097      * |   rs1408799                Y
##    ...      ...       ...    ... .         ...              ...
##    [9]        8 132353735      * |   rs4596632                R
##   [10]        6    396321      * |  rs12203592                Y
##   [11]        5  33951693      * |  rs16891982                S
##   [12]       11  89011046      * |   rs1393350                R
##   [13]       11  89005253      * |   rs1847134                M
##   -------
##   seqinfo: 25 sequences (1 circular) from GRCh37.p13 genome

Finally, fetch the MAF values on those positions.

eyecolormafs <- gscores(mafdb, rng, pop=c("AF", "EUR_AF", "AFR_AF"))
## assuming GRCh37.p13 and hs37d5 represent the same genome build between query ranges and the GScores object, respectively.
eyecolormafs
## GPos object with 13 positions and 5 metadata columns:
##        seqnames       pos strand |   RefSNP_id alleles_as_ambig        AF
##           <Rle> <integer>  <Rle> | <character>      <character> <numeric>
##    [1]       15  28365618      * |  rs12913832                R      0.29
##    [2]       15  28196821      * |   rs7173419                Y      0.44
##    [3]        1 213126565      * |   rs3002288                R      0.33
##    [4]       14  92773663      * |  rs12896399                K      0.29
##    [5]        9  12672097      * |   rs1408799                Y      0.35
##    ...      ...       ...    ... .         ...              ...       ...
##    [9]        8 132353735      * |   rs4596632                R      0.45
##   [10]        6    396321      * |  rs12203592                Y      0.05
##   [11]        5  33951693      * |  rs16891982                S      0.44
##   [12]       11  89011046      * |   rs1393350                R      0.11
##   [13]       11  89005253      * |   rs1847134                M      0.22
##           EUR_AF    AFR_AF
##        <numeric> <numeric>
##    [1]      0.29      0.04
##    [2]      0.25      0.26
##    [3]      0.46      0.08
##    [4]      0.43      0.03
##    [5]      0.34      0.22
##    ...       ...       ...
##    [9]      0.32      0.46
##   [10]      0.11      0.01
##   [11]      0.03      0.05
##   [12]      0.24      0.01
##   [13]       0.3      0.15
##   -------
##   seqinfo: 25 sequences (1 circular) from hs37d5 genome

Sometimes, data producers may annotate identifiers to genomic scores, such as dbSNP rs identifiers to MAF values. In such a case, we can directly attempt to fetch genomic scores using those identifiers as follows.

gscores(mafdb, eyersids, pop=c("AF", "EUR_AF", "AFR_AF"))
## Loading first time annotations of identifiers to genomic positions, produced by data provider.
## GRanges object with 13 ranges and 3 metadata columns:
##              seqnames    ranges strand |        AF    EUR_AF    AFR_AF
##                 <Rle> <IRanges>  <Rle> | <numeric> <numeric> <numeric>
##   rs12913832       15  28365618      * |      0.29      0.29      0.04
##    rs7173419       15  28196821      * |      0.44      0.25      0.26
##    rs3002288        1 213126565      * |      0.33      0.46      0.08
##   rs12896399       14  92773663      * |      0.29      0.43      0.03
##    rs1408799        9  12672097      * |      0.35      0.34      0.22
##          ...      ...       ...    ... .       ...       ...       ...
##    rs4596632        8 132353735      * |      0.45      0.32      0.46
##   rs12203592        6    396321      * |      0.05      0.11      0.01
##   rs16891982        5  33951693      * |      0.44      0.03      0.05
##    rs1393350       11  89011046      * |      0.11      0.24      0.01
##    rs1847134       11  89005253      * |      0.22       0.3      0.15
##   -------
##   seqinfo: 23 sequences from hs37d5 genome; no seqlengths

The following code produces the barplot shown in Figure 1 illustrating graphically the differences in MAF values from these variants between the three queried populations.

par(mar=c(3, 5, 1, 1))
bp <- barplot(t(as.matrix(mcols(eyecolormafs)[, c("AF", "EUR_AF", "AFR_AF")])), beside=TRUE,
              col=c("darkblue", "darkgreen", "darkorange"),
              ylim=c(0, 0.55), las=1, ylab="Minor allele frequency")
axis(1, bp[2, ], 1:length(eyecolormafs))
mtext("Variant", side=1, line=2)
legend("topright", colnames(mcols(eyecolormafs)), fill=c("darkblue", "darkgreen", "darkorange"))
Eye color MAFs. Minor allele frequencies (MAFs) of variants associated with eye color for global, european and african populations of the Phase 1 data from the 1000 Genomes Project.

Figure 1: Eye color MAFs
Minor allele frequencies (MAFs) of variants associated with eye color for global, european and african populations of the Phase 1 data from the 1000 Genomes Project.

11 Annotating variants with genomic scores

A typical use case of the GenomicScores package is in the context of annotating variants with genomic scores, such as phastCons conservation scores. For this purpose, we load the VariantAnnotaiton and TxDb.Hsapiens.UCSC.hg19.knownGene packages. The former will allow us to read a VCF file and annotate it, and the latter contains the gene annotations from UCSC that will be used in this process.

library(VariantAnnotation)
library(TxDb.Hsapiens.UCSC.hg19.knownGene)

Let’s load one of the sample VCF files that form part of the VariantAnnotation package.

fl <- system.file("extdata", "chr22.vcf.gz", package="VariantAnnotation")
vcf <- readVcf(fl, "hg19")
seqlevelsStyle(vcf)
## [1] "NCBI"    "Ensembl"
txdb <- TxDb.Hsapiens.UCSC.hg19.knownGene
seqlevelsStyle(txdb)
## [1] "UCSC"

Because the chromosome nomenclature from the VCF file (NCBI) is different from the one with the gene annotations (UCSC) we use the seqlevelsStyle() function to force our variants having the chromosome nomenclature of the gene annotations.

seqlevelsStyle(vcf) <- seqlevelsStyle(txdb)

We annotate the location of variants using the function locateVariants() from the VariantAnnotation package.

loc <- locateVariants(vcf, txdb, AllVariants())
## Warning in valid.GenomicRanges.seqinfo(x, suggest.trim = TRUE): GRanges object contains 2405 out-of-bound ranges located on sequences
##   75253, 74357, 74359, 74360, 74361, 74362, 74363, 74358, 74364, 74365,
##   75254, 75259, 74368, 74369, 74366, 74367, 74370, 74372, 74373, 74374,
##   74375, 74378, 74377, 74380, 74381, 75262, 75263, 75265, 75266, 75268,
##   75269, 75271, 75273, 75276, 75281, 75282, 75283, 74389, 74383, 74384,
##   74385, 74386, 74387, 75287, 75288, 75286, 75289, 74390, 74391, 74392,
##   74393, 74394, 75291, 74395, 74396, 74397, 74398, 75302, 75304, 75305,
##   and 75306. Note that ranges located on a sequence whose length is
##   unknown (NA) or on a circular sequence are not considered out-of-bound
##   (use seqlengths() and isCircular() to get the lengths and circularity
##   flags of the underlying sequences). You can use trim() to trim these
##   ranges. See ?`trim,GenomicRanges-method` for more information.
loc[1:3]
## GRanges object with 3 ranges and 9 metadata columns:
##    seqnames    ranges strand | LOCATION  LOCSTART    LOCEND   QUERYID
##       <Rle> <IRanges>  <Rle> | <factor> <integer> <integer> <integer>
##       chr22  50300078      - |   intron     10763     10763         1
##       chr22  50300086      - |   intron     10755     10755         2
##       chr22  50300101      - |   intron     10740     10740         3
##           TXID         CDSID      GENEID       PRECEDEID        FOLLOWID
##    <character> <IntegerList> <character> <CharacterList> <CharacterList>
##          75253          <NA>       79087            <NA>            <NA>
##          75253          <NA>       79087            <NA>            <NA>
##          75253          <NA>       79087            <NA>            <NA>
##   -------
##   seqinfo: 1 sequence from hg19 genome; no seqlengths
table(loc$LOCATION)
## 
## spliceSite     intron    fiveUTR   threeUTR     coding intergenic   promoter 
##         11      22572        309       1368       2822       2867       2864

Annotate phastCons conservation scores on the variants and store those annotations as an additional metadata column of the GRanges object. For this specific purpose we should use the method score() that returns the genomic scores as a numeric vector instead as a metadata column in the input ranges object.

loc$PHASTCONS <- score(phast, loc)
## assuming hg19 and Genome Reference Consortium GRCh37 represent the same genome build between query ranges and the GScores object, respectively.
loc[1:3]
## GRanges object with 3 ranges and 10 metadata columns:
##    seqnames    ranges strand | LOCATION  LOCSTART    LOCEND   QUERYID
##       <Rle> <IRanges>  <Rle> | <factor> <integer> <integer> <integer>
##       chr22  50300078      - |   intron     10763     10763         1
##       chr22  50300086      - |   intron     10755     10755         2
##       chr22  50300101      - |   intron     10740     10740         3
##           TXID         CDSID      GENEID       PRECEDEID        FOLLOWID
##    <character> <IntegerList> <character> <CharacterList> <CharacterList>
##          75253          <NA>       79087            <NA>            <NA>
##          75253          <NA>       79087            <NA>            <NA>
##          75253          <NA>       79087            <NA>            <NA>
##    PHASTCONS
##    <numeric>
##            0
##         0.08
##            0
##   -------
##   seqinfo: 1 sequence from hg19 genome; no seqlengths

Using the following code we can examine the distribution of phastCons conservation scores of variants across the different annotated regions, shown in Figure 2.

x <- split(loc$PHASTCONS, loc$LOCATION)
mask <- elementNROWS(x) > 0
boxplot(x[mask], ylab="phastCons score", las=1, cex.axis=1.2, cex.lab=1.5, col="gray")
points(1:length(x[mask])+0.25, sapply(x[mask], mean, na.rm=TRUE), pch=23, bg="black")
Distribution of phastCons conservation scores in variants across different annotated regions. Diamonds indicate mean values.

Figure 2: Distribution of phastCons conservation scores in variants across different annotated regions
Diamonds indicate mean values.

Next, we can annotate M-CAP and CADD scores as follows. Note that we need to take care to only query positions of single nucleotide variants, and using the QUERYID column of the annotations to fetch back reference and alternative alleles from the original VCF file container.

maskSNVs <- isSNV(vcf)[loc$QUERYID]
loc$MCAP <- rep(NA_real_, length(loc))
loc$MCAP[maskSNVs] <- score(mcap, loc[maskSNVs],
                            ref=ref(vcf)[loc$QUERYID[maskSNVs]],
                            alt=alt(vcf)[loc$QUERYID[maskSNVs]])
## assuming hg19 and Genome Reference Consortium GRCh37 represent the same genome build between query ranges and the GScores object, respectively.
maskSNVs <- isSNV(vcf)[loc$QUERYID]
loc$CADD <- rep(NA_real_, length(loc))
loc$CADD[maskSNVs] <- score(cadd, loc[maskSNVs],
                            ref=ref(vcf)[loc$QUERYID[maskSNVs]],
                            alt=alt(vcf)[loc$QUERYID[maskSNVs]])
## assuming hg19 and Genome Reference Consortium GRCh37 represent the same genome build between query ranges and the GScores object, respectively.

Using the code below we can produce the plot of Figure 3 comparing CADD and M-CAP scores and labeling the location of the variants from which they are derived.

library(RColorBrewer)
par(mar=c(4, 5, 1, 1))
hmcol <- colorRampPalette(brewer.pal(nlevels(loc$LOCATION), "Set1"))(nlevels(loc$LOCATION))
plot(jitter(loc$MCAP, factor=2), jitter(loc$CADD, factor=2), pch=19,
     col=hmcol, xlab="M-CAP scores", ylab="CADD scores",
     las=1, cex.axis=1.2, cex.lab=1.5, panel.first=grid())
legend("bottomright", levels(loc$LOCATION), pch=19, col=hmcol, inset=0.01)