rGenomeTracks

rGenomeTracks package leverages the power of pyGenomeTracks software with the interactivity of R. 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.

# 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()

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.

# 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.

 # 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.

 # 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.

 # 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)

Tips

Quickly create multiple tracks

If you have tracks with the same format, you can import them quickly by using lapply() and reduce() functions.

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

sessionInfo()
#> R version 4.4.0 beta (2024-04-15 r86425)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 22.04.4 LTS
#> 
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.19-bioc/R/lib/libRblas.so 
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [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       
#> 
#> time zone: America/New_York
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] rGenomeTracks_1.10.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] vctrs_0.6.5              cli_3.6.2                knitr_1.46              
#>  [4] rlang_1.1.3              xfun_0.43                highr_0.10              
#>  [7] stringi_1.8.3            tiff_0.1-12              purrr_1.0.2             
#> [10] png_0.1-8                readbitmap_0.1.5         jsonlite_1.8.8          
#> [13] glue_1.7.0               htmltools_0.5.8.1        sass_0.4.9              
#> [16] rmarkdown_2.26           grid_4.4.0               evaluate_0.23           
#> [19] jquerylib_0.1.4          fastmap_1.1.1            yaml_2.3.8              
#> [22] lifecycle_1.0.4          stringr_1.5.1            compiler_4.4.0          
#> [25] bmp_0.3                  igraph_2.0.3             Rcpp_1.0.12             
#> [28] pkgconfig_2.0.3          imager_1.0.1             lattice_0.22-6          
#> [31] digest_0.6.35            R6_2.5.1                 reticulate_1.36.1       
#> [34] magrittr_2.0.3           rGenomeTracksData_0.99.0 Matrix_1.7-0            
#> [37] bslib_0.7.0              tools_4.4.0              jpeg_0.1-10             
#> [40] cachem_1.0.8