---
title: "trackViewer Vignette: change the track styles"
author: "Jianhong Ou, Lihua Julie Zhu"
date: "`r BiocStyle::doc_date()`"
package: "`r BiocStyle::pkg_ver('trackViewer')`"
abstract: >
  Visualize mapped reads along with annotation as track layers for NGS dataset 
  such as ChIP-seq, RNA-seq, miRNA-seq, DNA-seq, SNPs and methylation data.
vignette: >
  %\VignetteIndexEntry{trackViewer Vignette: change the track styles}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
output:
  html_document:
    theme: simplex
    toc: true
    toc_float: true
    toc_depth: 4
    fig_caption: true
---

```{r, echo=FALSE, results="hide", warning=FALSE}
suppressPackageStartupMessages({
    library(trackViewer)
    library(rtracklayer)
    library(Gviz)
    library(TxDb.Hsapiens.UCSC.hg19.knownGene)
    library(org.Hs.eg.db)
    library(VariantAnnotation)
  library(httr)
})
knitr::opts_chunk$set(warning=FALSE, message=FALSE)
```

* [Plot methylation/mutation/variant data by lollipop plot](lollipopPlot.html)

* [Plot high dense methylation/mutation/variant data by dandelion plot](dandelionPlot.html)

* [Plot genomic interaction data (HiC, cooler, etc) by viewTracks](dandelionPlot.html)

## Prepare toy data

First, we import the sample data.

```{r}
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 any strand info, 
## we need to set it manually.
strand(repA$dat) <- "-"
strand(repA$dat2) <- "+"

fox2 <- importScore(file.path(extdata, "fox2.bed"), format="BED",
                    ranges=GRanges("chr11", IRanges(122830799, 123116707)))
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. 
 
fox2$dat <- dat[strand(dat)=="+"]
fox2$dat2 <- dat[strand(dat)=="-"]

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)

```


## Adjust the x-axis or the X scale
In most cases, researchers are interested in the relative position of the peaks in the gene. 
Sometimes, margin needs to be adjusted to be able to show the entire  gene model. 
The Figure below shows how to add an X scale (x-scale) and 
remove the x-axis using the **setTrackXscaleParam** and **setTrackViewerStyleParam** functions. 

```{r optSty}
optSty <- optimizeStyle(trackList(repA, fox2, trs))
trackList <- optSty$tracks
viewerStyle <- optSty$style
```
```{r viewTracksXaxis,fig.cap='plot data with x-scale',fig.width=8,fig.height=3}
setTrackViewerStyleParam(viewerStyle, "xaxis", FALSE)
setTrackViewerStyleParam(viewerStyle, "margin", c(.01, .05, .01, .01))
setTrackXscaleParam(trackList[[1]], "draw", TRUE)
setTrackXscaleParam(trackList[[1]], "gp", list(cex=0.8))
viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)
setTrackXscaleParam(trackList[[1]], attr="position", 
                    value=list(x=122929700, y=3, label=200))
viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)
```


## Adjust the y-axis
The y-axis can be put to the right side of the track by setting the main slot to FALSE in the y-axis slot of each track. 
In addition, the limit of y-axis (ylim) can be set by **setTrackStyleParam**. 
```{r viewTracksYaxis,fig.cap='plot data with y-axis in right side',fig.width=8,fig.height=3}
setTrackViewerStyleParam(viewerStyle, "margin", c(.01, .05, .01, .05))
for(i in 1:2){
    setTrackYaxisParam(trackList[[i]], "main", FALSE)
}
## Adjust the limit of y-axis
setTrackStyleParam(trackList[[1]], "ylim", c(0, 25))
setTrackStyleParam(trackList[[2]], "ylim", c(-25, 0))
viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)
```

The function `setTrackYaxisParam` can be sued to adjust the track color/size/etc of y-axis and the y-axis labels.

```{r viewTracksYaxisCol,fig.cap='plot data with adjusted track color',fig.width=8,fig.height=3}
## change the 
setTrackYaxisParam(trackList[[1]], "gp", list(cex=.8, col="green"))
setTrackYaxisParam(trackList[[2]], "gp", list(cex=.8, col="blue"))
viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)
```

## Adjust the label of y-axis
The style of y-axis can be changed by setting the ylabgp slot in the style of each track.
```{r viewTracksYlab,fig.cap='plot data with adjusted color and size of y-axis label',fig.width=8,fig.height=3}
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)
```

The y-axis label can be put at the top or the bottom of each track.
```{r viewTracksYlabTopBottom,fig.cap='plot data with adjusted y-axis label position',fig.width=8,fig.height=3}
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 on the upstream or downstream of the transcript.

```{r viewTracksYlabUpsDown,fig.cap='plot data with adjusted transcripts name position',fig.width=8,fig.height=3}
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(unname(unlist(GRangesList(sapply(trs, function(.ele) .ele$dat)))))
start(gr1) <- start(gr1) - 2000
end(gr1) <- end(gr1) + 2000
viewTracks(trackListN, gr=gr1, viewerStyle=viewerStyle)
```


## Adjust the track color
The track color can be changed by setting the color slot in the style of each track.
The first color is for the dat slot of 
**track** and the second color is for the dat2 slot.
```{r viewTracksCol,fig.cap='plot data with adjusted track color',fig.width=8,fig.height=3}
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)
```

## Adjust the track height
The track height can be changed by setting the height slot in the style of each track. However, the total height for all 
the tracks should be 1.
```{r viewTracksHeight,fig.cap='plot data with adjusted track height',fig.width=8,fig.height=3}
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)
```

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

```{r viewTracksNames,fig.cap='change the track names',fig.width=8,fig.height=3}
names(trackList) <- c("cpsf160", "fox2", rep("HSPA8", 5))
viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)
```

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

```{r viewTracksPaired,fig.cap='show two data in the same track',fig.width=8,fig.height=2.4}
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)
```

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

```{r viewTracksFlipped,fig.cap='show data in the flipped track',fig.width=8,fig.height=4}
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=2),
             label="label",
             col="blue",
             vp=vp)
```

## Optimize the theme

Currently, we support two themes: bw (black and white) and col (colored).
```{r themeBW,fig.cap='balck & white theme',fig.width=8,fig.height=4}
optSty <- optimizeStyle(trackList(repA, fox2, trs), theme="bw")
trackList <- optSty$tracks
viewerStyle <- optSty$style
vp <- viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)
```

```{r themeCol,fig.cap='colorful theme',fig.width=8,fig.height=4}
optSty <- optimizeStyle(trackList(repA, fox2, trs), theme="col")
trackList <- optSty$tracks
viewerStyle <- optSty$style
vp <- viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)
```

```{r themeSafe,fig.cap='safe theme',fig.width=8,fig.height=4}
optSty <- optimizeStyle(trackList(repA, fox2, trs), theme="safe")
trackList <- optSty$tracks
viewerStyle <- optSty$style
vp <- viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)
```

## Plot with breaks

We could plot the tracks with breaks by setting multiple genomic ranges.
```{r axisBreak,fig.cap='axis with breaks',fig.width=8,fig.height=4}
gr.breaks <- GRanges("chr11", 
                     IRanges(c(122929275, 122929575, 122929775), 
                             c(122929555, 122929725, 122930122)), 
                     strand="-", percentage=c(.4, .2, .4))
vp <- viewTracks(trackList, gr=gr.breaks, viewerStyle=viewerStyle)
```

## Session Info
```{r sessionInfo, results='asis'}
sessionInfo()
```

