visualize mapped reads along with annotation as track layers for NGS dataset such as ChIP-seq, RNA-seq, miRNA-seq, DNA-seq.
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.
suppressPackageStartupMessages(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="-")
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)
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.
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)=="-"]
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)
## Loading required package: GenomicFeatures
## Loading required package: AnnotationDbi
## Loading required package: Biobase
## Welcome to Bioconductor
##
## Vignettes contain introductory material; view with
## 'browseVignettes()'. To cite Bioconductor, see
## 'citation("Biobase")', and for packages 'citation("pkgname")'.
trs <- geneModelFromTxdb(TxDb.Hsapiens.UCSC.hg19.knownGene,
"chr11", 122929275, 122930122, "-")
Use viewTracks function to plot data and annotation information along genomic coordinates. addGuideLine or addArrowMark can be used to highlight the peaks.
gr <- GRanges("chr11", IRanges(122929275, 122930122), strand="-")
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)
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)
y-axis can be put to right side of the track by setting main slot to FALSE in y-axis slot of each track.
setTrackViewerStyleParam(viewerStyle, "margin", c(.01, .05, .01, .05))
setTrackYaxisParam(trackList[[1]], "main", FALSE)
setTrackYaxisParam(trackList[[2]], "main", FALSE)
viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)
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)
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)
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)
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)
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)
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)
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)
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)
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)
optSty <- optimizeStyle(trackList(repA, fox2, trs), theme="col")
trackList <- optSty$tracks
viewerStyle <- optSty$style
vp <- viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)
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="+")
viewTracks(trackList(newtrack, trs), gr=gr, viewerStyle=viewerStyle, 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)
sessionInfo()
R version 3.2.2 (2015-08-14) 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=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 parallel stats4 stats graphics grDevices utils
[8] datasets methods base
other attached packages: [1] TxDb.Hsapiens.UCSC.hg19.knownGene_3.2.2 [2] GenomicFeatures_1.22.0
[3] AnnotationDbi_1.32.0
[4] Biobase_2.30.0
[5] trackViewer_1.6.1
[6] rtracklayer_1.30.1
[7] Gviz_1.14.0
[8] GenomicRanges_1.22.0
[9] GenomeInfoDb_1.6.0
[10] IRanges_2.4.1
[11] S4Vectors_0.8.0
[12] BiocGenerics_0.16.0
[13] BiocStyle_1.8.0
loaded via a namespace (and not attached): [1] SummarizedExperiment_1.0.0 pbapply_1.1-1
[3] VariantAnnotation_1.16.1 reshape2_1.4.1
[5] splines_3.2.2 lattice_0.20-33
[7] colorspace_1.2-6 htmltools_0.2.6
[9] yaml_2.1.13 XML_3.98-1.3
[11] survival_2.38-3 foreign_0.8-66
[13] DBI_0.3.1 BiocParallel_1.4.0
[15] RColorBrewer_1.1-2 lambda.r_1.1.7
[17] matrixStats_0.15.0 plyr_1.8.3
[19] stringr_1.0.0 zlibbioc_1.16.0
[21] Biostrings_2.38.0 munsell_0.4.2
[23] gtable_0.1.2 futile.logger_1.4.1
[25] evaluate_0.8 latticeExtra_0.6-26
[27] knitr_1.11 biomaRt_2.26.0
[29] proto_0.3-10 Rcpp_0.12.1
[31] acepack_1.3-3.3 BSgenome_1.38.0
[33] scales_0.3.0 formatR_1.2.1
[35] Hmisc_3.17-0 XVector_0.10.0
[37] Rsamtools_1.22.0 gridExtra_2.0.0
[39] ggplot2_1.0.1 digest_0.6.8
[41] stringi_1.0-1 biovizBase_1.18.0
[43] tools_3.2.2 bitops_1.0-6
[45] magrittr_1.5 RCurl_1.95-4.7
[47] RSQLite_1.0.0 dichromat_2.0-0
[49] Formula_1.2-1 cluster_2.0.3
[51] futile.options_1.0.0 MASS_7.3-44
[53] rmarkdown_0.8.1 rpart_4.1-10
[55] GenomicAlignments_1.6.1 nnet_7.3-11