Package version: trackViewer 1.8.4

Contents

1 Introduction

There are two packages available in Bioconductor for visualizing genomic data: rtracklayer and Gviz. rtracklayer provides an interface to genome browsers and associated annotation tracks. Gviz plots data and annotation information along genomic coordinates. TrackViewer is a light-weighted visualization tool for generating neat figures for publication. It utilizes Gviz, is easy to use, and has a low memory and cpu consumption.

library(Gviz)
library(rtracklayer)
library(trackViewer)
extdata <- system.file("extdata", package="trackViewer",
                       mustWork=TRUE)
gr <- GRanges("chr11", IRanges(122929275, 122930122), strand="-")
fox2 <- importScore(file.path(extdata, "fox2.bed"), format="BED",
                    ranges=gr)
fox2$dat <- coverageGR(fox2$dat)

viewTracks(trackList(fox2), gr=gr, autoOptimizeStyle=TRUE, newpage=FALSE)

dt <- DataTrack(range=fox2$dat[strand(fox2$dat)=="-"] , 
                genome="hg19", type="hist", name="fox2", 
                window=-1, chromosome="chr11", 
                fill.histogram="black", col.histogram="NA",
                background.title="white",
                col.frame="white", col.axis="black",
                col="black", col.title="black")
plotTracks(dt, from=122929275, to=122930122, strand="-")

Plot data with Gviz and trackViewer. Note that trackViewer can generate similar figure as Gviz with several lines of simple codes.

TrackViewer not only has the functionalities to plot the figures generated by Gviz, as shown in Figure above, but also provides additional plotting styles as shown in Figure below. The mimimalist design requires minimum input from users while retaining the flexibility to change output style easily.

gr <- GRanges("chr1", IRanges(c(1, 6, 10), c(3, 6, 12)), score=c(3, 4, 1))
dt <- DataTrack(range=gr, data="score", type="hist")
plotTracks(dt, from=2, to=11)
tr <- new("track", dat=gr, type="data", format="BED")
viewTracks(trackList(tr), chromosome="chr1", start=2, end=11)

Plot data with Gviz and trackViewer. Note that trackViewer is not only including more details but also showing all the data involved in the given range.

It requires huge memory space to handle big wig files. To solve this problem, trackViewer rewrote the import function to import whole file first and parse it later when plot. trackViewer provides higher import speed (21 min vs. over 180 min) and acceptable memory cost (5.32G vs. over 10G) for a half giga wig file (GSM917672) comparing to Gviz.

2 Steps of using

2.1 step1 import data

Function importScore is used to import BED, WIG, bedGraph or BigWig files. Function importBam is employed to import bam file. Here is the example.

library(trackViewer)
extdata <- system.file("extdata", package="trackViewer",
                       mustWork=TRUE)
repA <- importScore(file.path(extdata, "cpsf160.repA_-.wig"),
                    file.path(extdata, "cpsf160.repA_+.wig"),
                    format="WIG")
## because the wig file does not contain strand info, 
## we need to set it manually
strand(repA$dat) <- "-"
strand(repA$dat2) <- "+"

Function coverageGR could be used to calculate coverage after importing if needed.

fox2 <- importScore(file.path(extdata, "fox2.bed"), format="BED",
                    ranges=GRanges("chr11", IRanges(122929000, 122931000)))
dat <- coverageGR(fox2$dat)
## we can split the data by strand into two different track channels
## here we set the dat2 slot to save the negative strand info, 
## reverse order as previous. 
fox2$dat <- dat[strand(dat)=="+"]
fox2$dat2 <- dat[strand(dat)=="-"]

2.2 step2 build gene model

The gene model can be built for a given genomic range using geneModelFromTxdb function which uses TranscriptDb object as input.

library(TxDb.Hsapiens.UCSC.hg19.knownGene)
library(org.Hs.eg.db)
gr <- GRanges("chr11", IRanges(122929275, 122930122), strand="-")
trs <- geneModelFromTxdb(TxDb.Hsapiens.UCSC.hg19.knownGene,
                         org.Hs.eg.db,
                         gr=gr)

2.3 step3 view the tracks

Use viewTracks function to plot data and annotation information along genomic coordinates. addGuideLine or addArrowMark can be used to highlight the peaks.

viewerStyle <- trackViewerStyle()
setTrackViewerStyleParam(viewerStyle, "margin", c(.1, .05, .02, .02))
vp <- viewTracks(trackList(repA, fox2, trs), 
                 gr=gr, viewerStyle=viewerStyle, 
                 autoOptimizeStyle=TRUE)
addGuideLine(c(122929767, 122929969), vp=vp)
addArrowMark(list(x=122929650, 
                  y=unit(.39, "npc")),
             label="label",
             col="blue",
             vp=vp)

plot data and annotation information along genomic coordinates

3 Adjust the styles

3.1 adjust x axis or x-scale

In most cases, researchers are interested in the relative position of peaks in the gene. Sometimes, margin needs to be adjusted to be able to show the entire gene model. Figure below shows how to add an x-scale and remove x-axis using addGuideLine Function .

optSty <- optimizeStyle(trackList(repA, fox2, trs))
trackList <- optSty$tracks
viewerStyle <- optSty$style
setTrackViewerStyleParam(viewerStyle, "xaxis", FALSE)
setTrackViewerStyleParam(viewerStyle, "margin", c(.01, .05, .01, .01))
setTrackXscaleParam(trackList[[1]], "draw", TRUE)
setTrackXscaleParam(trackList[[1]], "gp", list(cex=.5))
viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

plot data with x-scale

3.2 adjust y axis

y-axis can be put to right side of the track by setting main slot to FALSE in y-axis slot of each track. And ylim can be set by setTrackStyleParam.

setTrackViewerStyleParam(viewerStyle, "margin", c(.01, .05, .01, .05))
for(i in 1:2){
    setTrackYaxisParam(trackList[[i]], "main", FALSE)
}
## adjust y scale
setTrackStyleParam(trackList[[1]], "ylim", c(0, 25))
setTrackStyleParam(trackList[[2]], "ylim", c(-25, 0))

viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

plot data with y-axis in right side

3.3 adjust y label

Y label style can be changed by setting the ylabgp slot in style of each track.

setTrackStyleParam(trackList[[1]], "ylabgp", list(cex=.8, col="green"))
## set cex to avoid automatic adjust
setTrackStyleParam(trackList[[2]], "ylabgp", list(cex=.8, col="blue"))
setTrackStyleParam(trackList[[2]], "marginBottom", .2)
viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

plot data with adjusted color and size of y label

Y label can be also put to top or bottom of each track.

setTrackStyleParam(trackList[[1]], "ylabpos", "bottomleft")
setTrackStyleParam(trackList[[2]], "ylabpos", "topright")
setTrackStyleParam(trackList[[2]], "marginTop", .2)
viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

plot data with adjusted y label position

For each transcript, the transcript name can be put to upstream or downstream of the transcript.

trackListN <- trackList
setTrackStyleParam(trackListN[[3]], "ylabpos", "upstream")
setTrackStyleParam(trackListN[[4]], "ylabpos", "downstream")
## set cex to avoid automatic adjust
setTrackStyleParam(trackListN[[3]], "ylabgp", list(cex=.6))
setTrackStyleParam(trackListN[[4]], "ylabgp", list(cex=.6))
gr1 <- range(unlist(GRangesList(sapply(trs, function(.ele) .ele$dat))))
start(gr1) <- start(gr1) - 2000
end(gr1) <- end(gr1) + 2000
viewTracks(trackListN, gr=gr1, viewerStyle=viewerStyle)

plot data with adjusted transcripts name position

3.4 adjust track color

The track color can be changed by setting the color slot in style of each track. The first color is for dat slot of track and seconde color is for dat2 slot.

setTrackStyleParam(trackList[[1]], "color", c("green", "black"))
setTrackStyleParam(trackList[[2]], "color", c("black", "blue"))
for(i in 3:length(trackList)) 
    setTrackStyleParam(trackList[[i]], "color", "black")
viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

plot data with adjusted track color

3.5 adjust track height

The track height can be changed by setting the height slot in style of each track. However, the total height for all the tracks should be 1.

trackListH <- trackList
setTrackStyleParam(trackListH[[1]], "height", .1)
setTrackStyleParam(trackListH[[2]], "height", .44)
for(i in 3:length(trackListH)){
    setTrackStyleParam(trackListH[[i]], "height", 
                       (1-(0.1+0.44))/(length(trackListH)-2))
}
viewTracks(trackListH, gr=gr, viewerStyle=viewerStyle)

plot data with adjusted track height

3.6 change track names

The track names such as gene model names can also be edited easily by changing the names of trackList.

names(trackList) <- c("cpsf160", "fox2", rep("HSPA8", 5))
viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

change the track names

3.7 show paired data in the same track

trackViewer can be used to show to-be-compared data in the same track side by side.

cpsf160 <- importScore(file.path(extdata, "cpsf160.repA_-.wig"),
                       file.path(extdata, "cpsf160.repB_-.wig"),
                       format="WIG")
strand(cpsf160$dat) <- strand(cpsf160$dat2) <- "-"
setTrackStyleParam(cpsf160, "color", c("black", "red"))
viewTracks(trackList(trs, cpsf160), gr=gr, viewerStyle=viewerStyle)

show two data in the same track

3.8 flip the x-axis

The x-axis can be horizotally flipped for the genes in negative strand.

viewerStyleF <- viewerStyle
setTrackViewerStyleParam(viewerStyleF, "flip", TRUE)
setTrackViewerStyleParam(viewerStyleF, "xaxis", TRUE)
setTrackViewerStyleParam(viewerStyleF, "margin", c(.1, .05, .01, .01))
vp <- viewTracks(trackList, gr=gr, viewerStyle=viewerStyleF)
addGuideLine(c(122929767, 122929969), vp=vp)
addArrowMark(list(x=122929650,
                  y=unit(.39, "npc")),
             label="label",
             col="blue",
             vp=vp)

show data in the flipped track

3.9 optimize with theme

We support two themes now: bw and col.

optSty <- optimizeStyle(trackList(repA, fox2, trs), theme="bw")
trackList <- optSty$tracks
viewerStyle <- optSty$style
vp <- viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

theme_bw

optSty <- optimizeStyle(trackList(repA, fox2, trs), theme="col")
trackList <- optSty$tracks
viewerStyle <- optSty$style
vp <- viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

theme_col

4 Operators

If there are two tracks and we want to draw the two track by adding or substract one from another, we can try operators.

newtrack <- repA
## must keep same format for dat and dat2
newtrack <- parseWIG(newtrack, "chr11", 122929275, 122930122)
newtrack$dat2 <- newtrack$dat
newtrack$dat <- fox2$dat2
setTrackStyleParam(newtrack, "color", c("blue", "red"))
viewTracks(trackList(newtrack, trs), 
           gr=gr, viewerStyle=viewerStyle, operator="+")

show data with operator +

viewTracks(trackList(newtrack, trs), gr=gr, viewerStyle=viewerStyle, operator="-")

show data with operator -

Or try GRoperator before view tracks.

newtrack$dat <- GRoperator(newtrack$dat, newtrack$dat2, col="score", operator="-")
newtrack$dat2 <- GRanges()
viewTracks(trackList(newtrack, trs), gr=gr, viewerStyle=viewerStyle)

show data with operator -

5 Lolliplot

Lolliplot is a mutation distribution graphics tool.

SNP <- c(10, 100, 105, 108, 400, 410, 420, 600, 700, 805, 840, 1400, 1402)
x <- sample.int(100, length(SNP))
sample.gr <- GRanges("chr1", IRanges(SNP, width=1, names=paste0("snp", SNP)))
features <- GRanges("chr1", IRanges(c(1, 501, 1001), 
                                    width=c(120, 400, 405),
                                    names=paste0("block", 1:3)))
lolliplot(sample.gr, features)

5.1 Change lolliplot colors

5.1.1 Change the color of features.

features$fill <- c("#FF8833", "#51C6E6", "#DFA32D")
lolliplot(sample.gr, features)

5.1.2 Change the color of lollipop.

sample.gr$color <- sample.int(6, length(SNP), replace=TRUE)
sample.gr$border <- sample(c("gray80", "gray30"), length(SNP), replace=TRUE)
lolliplot(sample.gr, features)

5.2 Add index labels in the node

sample.gr$label <- as.character(1:length(sample.gr))
sample.gr$label.col <- "white"
lolliplot(sample.gr, features)

5.3 Change the height of features

features$height <- c(0.02, 0.05, 0.08)
lolliplot(sample.gr, features)

5.4 Change the height of lolliplot

#Note: the score value is integer less than 10
sample.gr$score <- sample.int(5, length(sample.gr), replace = TRUE)
lolliplot(sample.gr, features)

##remove yaxis
lolliplot(sample.gr, features, yaxis=FALSE)

#Try score value greater than 10
sample.gr$score <- sample.int(20, length(sample.gr), replace=TRUE)
lolliplot(sample.gr, features)

#Try float numeric score
sample.gr$score <- runif(length(sample.gr))*10
lolliplot(sample.gr, features)

# score should not be smaller than 1

5.5 Customize xaxis label positions

xaxis <- c(1, 200, 400, 701, 1000, 1200, 1402) ## define the position
lolliplot(sample.gr, features, xaxis=xaxis)

names(xaxis) <- xaxis # define the labels
names(xaxis)[4] <- "center" 
lolliplot(sample.gr, features, xaxis=xaxis)

5.6 Add legend

legend <- 1:6 ## legend fill color
names(legend) <- paste0("legend", letters[1:6]) ## legend labels
lolliplot(sample.gr, features, legend=legend)

## use list to define more attributes. see ?grid::gpar to get more details.
legend <- list(labels=paste0("legend", LETTERS[1:6]), 
               col=palette()[6:1], 
               fill=palette()[legend])
lolliplot(sample.gr, features, legend=legend)

## if you have multiple tracks, try to set the legend by list.
## see more in section [Plot multiple samples](#plot-multiple-samples)
legend <- list(legend)
lolliplot(sample.gr, features, legend=legend)

5.7 Change the type of lolliplot

5.7.1 Google pin

lolliplot(sample.gr, features, type="pin")

sample.gr$color <- lapply(sample.gr$color, function(.ele) c(.ele, sample.int(6, 1)))
sample.gr$border <- sample.int(6, length(SNP), replace=TRUE)
lolliplot(sample.gr, features, type="pin")

5.7.2 Pie plot

sample.gr$score <- NULL ## must be removed, because pie will consider all the numeric columns except column "color", "fill", "lwd", "id" and "id.col".
sample.gr$label <- NULL
sample.gr$label.col <- NULL
x <- sample.int(100, length(SNP))
sample.gr$value1 <- x
sample.gr$value2 <- 100 - x
## the length of color should be no less than the values number
sample.gr$color <- rep(list(c("#87CEFA", '#98CE31')), length(SNP))
sample.gr$border <- "gray30"
lolliplot(sample.gr, features, type="pie")

5.8 Plot multiple samples

SNP2 <- sample(4000:8000, 30)
x2 <- sample.int(100, length(SNP2), replace=TRUE)
sample2.gr <- GRanges("chr3", IRanges(SNP2, width=1, names=paste0("snp", SNP2)), 
             value1=x2, value2=100-x2)
sample2.gr$color <- rep(list(c('#DB7575', '#FFD700')), length(SNP2))
sample2.gr$border <- "gray30"
mcols(sample2.gr) <- mcols(sample2.gr)[, colnames(mcols(sample.gr))]
features2 <- GRanges("chr3", IRanges(c(5001, 5801, 7001), 
                                    width=c(500, 500, 405),
                                    names=paste0("block", 4:6)),
                    fill=c("orange", "gray30", "lightblue"),
                    height=c(0.1, 0.05, 0.08))
legends <- list(list(labels=c("WT", "MUT"), fill=c("#87CEFA", '#98CE31')), 
                list(labels=c("WT", "MUT"), fill=c('#DB7575', '#FFD700')))
lolliplot(GRangesList(A=sample.gr, B=sample2.gr), 
          GRangesList(x=features, y=features2), 
          type="pie", legend=legends)

5.9 Variant Call Format (VCF) data

library(VariantAnnotation)
library(TxDb.Hsapiens.UCSC.hg19.knownGene)
library(org.Hs.eg.db)
fl <- system.file("extdata", "chr22.vcf.gz", package="VariantAnnotation")
gr <- GRanges("22", IRanges(50968014, 50970514, names="TYMP"))
tab <- TabixFile(fl)
vcf <- readVcf(fl, "hg19", param=gr)
mutation.frequency <- rowRanges(vcf)
mcols(mutation.frequency) <- cbind(mcols(mutation.frequency), 
                                   VariantAnnotation::info(vcf))
mutation.frequency$border <- "gray30"
mutation.frequency$color <- 
    ifelse(grepl("^rs", names(mutation.frequency)), "lightcyan", "lavender")
## plot Global Allele Frequency based on AC/AN
mutation.frequency$score <- mutation.frequency$AF*100
seqlevelsStyle(gr) <- seqlevelsStyle(mutation.frequency) <- "UCSC" 
trs <- geneModelFromTxdb(TxDb.Hsapiens.UCSC.hg19.knownGene,
                         org.Hs.eg.db,
                         gr=gr)
features <- c(range(trs[[1]]$dat), range(trs[[5]]$dat))
names(features) <- c(trs[[1]]$name, trs[[5]]$name)
features$fill <- c("lightblue", "mistyrose")
features$height <- c(.02, .04)
lolliplot(mutation.frequency, features, ranges=gr)

5.10 Methylation data

library(rtracklayer)
session <- browserSession()
query <- ucscTableQuery(session, track="HAIB Methyl RRBS", 
                        range=GRangesForUCSCGenome("hg19", seqnames(gr), ranges(gr)))
tableName(query) <- tableNames(query)[1]
methy <- track(query)
methy <- GRanges(methy)
lolliplot(methy, features, ranges=gr, type="pin")

5.11 Change node size

In above sample, some of the nodes overlap each other. To change the node size, cex prameter could be used.

methy$lwd <- .5
lolliplot(methy, features, ranges=gr, type="pin", cex=.5)

lolliplot(methy, features, ranges=gr, type="circle", cex=.5)

methy$score2 <- max(methy$score) - methy$score
lolliplot(methy, features, ranges=gr, type="pie", cex=.5)

6 Dandelion Plot

Sometimes, there are too many SNPs invoved in one gene. If you want to plot such as more than handred SNPs, Dandelion plot may be the good chioce. Please note, the height of the dandelion means the desity of the SNPs.

dandelion.plot(methy, features, ranges=gr, type="pin")

6.1 change the type of Dandelion plot

There are one more type for dandelion plot, type “fan”. The area of the fan indicate the percentage of methylation or rate of mutation.

methy$color <- 3
methy$border <- "gray"
## score info is required, the score must be a number in [0, 1]
m <- max(methy$score)
methy$score <- methy$score/m
dandelion.plot(methy, features, ranges=gr, type="fan")

methy$color <- rep(list(c(3, 5)), length(methy))
methy$score2 <- methy$score2/m
legends <- list(list(labels=c("s1", "s2"), fill=c(3, 5)))
dandelion.plot(methy, features, ranges=gr, type="pie", legend=legends)

6.2 change the number of dandelions

## want less
dandelion.plot(methy, features, ranges=gr, type="circle", maxgaps=1/10)

## want more
dandelion.plot(methy, features, ranges=gr, type="circle", maxgaps=1/100)

# Session Info

sessionInfo()

R version 3.3.0 (2016-05-03) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 14.04.4 LTS

locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
[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] grid stats4 parallel stats graphics grDevices utils
[8] datasets methods base

other attached packages: [1] VariantAnnotation_1.18.1
[2] Rsamtools_1.24.0
[3] Biostrings_2.40.2
[4] XVector_0.12.0
[5] SummarizedExperiment_1.2.3
[6] org.Hs.eg.db_3.3.0
[7] TxDb.Hsapiens.UCSC.hg19.knownGene_3.2.2 [8] GenomicFeatures_1.24.3
[9] AnnotationDbi_1.34.3
[10] Biobase_2.32.0
[11] Gviz_1.16.1
[12] rtracklayer_1.32.1
[13] trackViewer_1.8.4
[14] GenomicRanges_1.24.2
[15] GenomeInfoDb_1.8.2
[16] IRanges_2.6.1
[17] S4Vectors_0.10.1
[18] BiocGenerics_0.18.0
[19] BiocStyle_2.0.2

loaded via a namespace (and not attached): [1] Rcpp_0.12.5 biovizBase_1.20.0
[3] lattice_0.20-33 digest_0.6.9
[5] mime_0.4 R6_2.1.2
[7] plyr_1.8.4 chron_2.3-47
[9] acepack_1.3-3.3 RSQLite_1.0.0
[11] evaluate_0.9 httr_1.2.0
[13] ggplot2_2.1.0 BiocInstaller_1.22.2
[15] zlibbioc_1.18.0 data.table_1.9.6
[17] rpart_4.1-10 Matrix_1.2-6
[19] rmarkdown_0.9.6 splines_3.3.0
[21] BiocParallel_1.6.2 AnnotationHub_2.4.2
[23] stringr_1.0.0 foreign_0.8-66
[25] RCurl_1.95-4.8 biomaRt_2.28.0
[27] munsell_0.4.3 shiny_0.13.2
[29] httpuv_1.3.3 htmltools_0.3.5
[31] nnet_7.3-12 gridExtra_2.2.1
[33] interactiveDisplayBase_1.10.3 Hmisc_3.17-4
[35] matrixStats_0.50.2 XML_3.98-1.4
[37] GenomicAlignments_1.8.3 bitops_1.0-6
[39] xtable_1.8-2 gtable_0.2.0
[41] DBI_0.4-1 magrittr_1.5
[43] formatR_1.4 scales_0.4.0
[45] pbapply_1.2-1 stringi_1.1.1
[47] latticeExtra_0.6-28 grImport_0.9-0
[49] Formula_1.2-1 RColorBrewer_1.1-2
[51] ensembldb_1.4.6 tools_3.3.0
[53] dichromat_2.0-0 BSgenome_1.40.1
[55] survival_2.39-4 yaml_2.1.13
[57] colorspace_1.2-6 cluster_2.0.4
[59] knitr_1.13