---
title: "rGenomeTracks"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{rGenomeTracks}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```
rGenomeTracks package leverages the power of [pyGenomeTracks](https://github.com/deeptools/pyGenomeTracks) software
with the interactivity of R.
[pyGenomeTracks](https://github.com/deeptools/pyGenomeTracks) is a python software
that offers robust method for visualizing
epigenetic data files like narrowPeak, Hic matrix, TADs and arcs, however though,
here is no way currently to use it within R interactive session.
rGenomeTracks wrapped the whole functionality of pyGenomeTracks with additional utilites
to make to more pleasant for R users.

```{r setup}
# loading the rGenomeTracks
library(rGenomeTracks)
# loading example data
#library(rGenomeTracksData)
```


## Installing PyGenomeTracks
You should have pyGenomeTracks installed on R's loading environment. To avoid dependency clash, 
we highly recommend using `install_pyGenomeTracks()`. That way, you ensure using the tested pyGenomeTracks version with the current release.  rGenomeTracks is supposed to automatically prompt you to install this dependency after running `plot_gtracks()`. If this step failed, you can manually install pyGenomeTracks with `install_pyGenomeTracks()`

```{r eval=FALSE}
install_pyGenomeTracks()
```



## Principle
rGenomeTracks deals creates tracks in a class `genome_track`. Currently, there are 14 tracks available:
1. track_bed()
2. track_bedgraph()
3. track_bedgraph_matrix()
4. track_gtf()
5. track_hlines()
6. track_vlines()
7. track_spacer()
8. track_bigwig()
9. track_epilogos()
10. track_narrowPeak()
11. track_domains()
12. track_hic_matrix()
13. track_links()
14. track_scalebar()
15. track_x_axis()

Please refer to the help page for each one of them for details and examples.

We will download .h5 matrix and store the location in temporary directory for demonstration.
```{r eval=FALSE}
# Download h5 example
ah <- AnnotationHub()
query(ah, "rGenomeTracksData")
h5_dir <-  ah[["AH95901"]]

 # Create HiC track from HiC matrix
 h5 <- track_hic_matrix(
   file = h5_dir, depth = 250000, min_value = 5, max_value = 200,
   transform = "log1p", show_masked_bins = FALSE
 )

```

Other demonstration for TADS, arcs and bigwig data will be loaded from the built-in package example data.

```{r}
 # Load other examples
 tads_dir <- system.file("extdata", "tad_classification.bed", package = "rGenomeTracks")
 arcs_dir <- system.file("extdata", "links2.links", package = "rGenomeTracks")
 bw_dir <- system.file("extdata", "bigwig2_X_2.5e6_3.5e6.bw", package = "rGenomeTracks")
 
 # Create TADS track
 tads <- track_domains(
   file = tads_dir, border_color = "black",
   color = "none", height = 5,
   line_width = 5,
   show_data_range = FALSE,
   overlay_previous = "share-y"
 )

 # Create arcs track
 arcs <- track_links(
   file = arcs_dir, links_type = "triangles",
   line_style = "dashed",
   overlay_previous = "share-y",
   color = "darkred",
   line_width = 3,
   show_data_range = FALSE
 )

 # Create bigwig track
 bw <- track_bigwig(
   file = bw_dir, color = "red",
   max_value = 50,
   min_value = 0,
   height = 4,
   overlay_previous = "no",
   show_data_range = FALSE
 )

```



`genome_track` objects can be added together using `+` function. 
 
```{r}
 # Create one object from HiC, arcs and bigwid
 tracks <- tads + arcs + bw
```


The track(s) to be plotted is to be passed to `plot_gtracks()` for the generation of the plot.
Additionally, `plot_gtracks()` requires the genomic region to be plotted. Optionally, you can set plot title, dpi, width, height, fontsize, track-to-label fraction, label alignment position, and directory to save the plot.
 
```{r eval=FALSE}
 # Plot the tracks
## Note to verify installing miniconda if not installed.

 layout(matrix(c(1,1,2,3,4,4), nrow = 3, ncol = 2, byrow = TRUE))
 plot_gtracks(tracks, chr = "X", start = 25 * 10^5, end = 31 * 10^5)

# Plot HiC, TADS and bigwig tracks
 plot_gtracks(h5 + tads + bw, chr = "X", start = 25 * 10^5, end = 31 * 10^5)
```

```{r echo=FALSE}
plot(imager::load.image(system.file("extdata", "images/example1.png", package = "rGenomeTracks")), axes = FALSE)
plot(imager::load.image(system.file("extdata", "images/example2.png", package = "rGenomeTracks")), axes = FALSE)
```

## Tips 
### Quickly create multiple tracks 
If you have tracks with the same format, you can import them quickly by using lapply() and reduce() functions. 
```{r}
dirs <- list.files(system.file("extdata", package = "rGenomeTracks"), full.names = TRUE)

# filter only bed files (without bedgraphs or narrowpeaks)
bed_dirs <- grep(
  dirs,  pattern = ".bed(?!graph)", perl = TRUE, value = TRUE)

bed_list <- lapply(bed_dirs, track_bed)

bed_tracks <- Reduce("+", bed_list)
```
You can repeat this process for tracks of same category then pass the tracks to plot_gtracks()

### Create complex layout figures 
You may choose create a complex figure using layout() and split.screen() function then save the device. 

Note that you cannot make use of dir argument in plot_gtracks() if you used this method as it is passed to pyGenomeTracks. So, you have to capture R's graphic device and save it manually.



## Session Information 
```{r}
sessionInfo()
```
