4. Creating an Interactive Line Chart with shiny.gosling

In this tutorial, we’ll explore how to create an interactive line chart visualization of genomic data using the shiny and shiny.gosling packages in R. This visualization will enable users to explore trends and patterns within genomic datasets.

Introduction

The shiny and shiny.gosling packages offer powerful tools for building interactive data visualizations within Shiny apps. In this tutorial, we’ll demonstrate the process of constructing an interactive line chart visualization using genomic data. This example leverages the shiny package for user interface design and the shiny.gosling package for generating the visualizations.

Understanding Multivec Data

Multivec is a file format introduced by Higlass suitable for visualizing multi-dimensional numerical data across genomic coordinates. It’s widely utilized for representing various genomic experiments such as ChIP-seq, ATAC-seq, and Hi-C, where measurements are taken at different genomic positions.

Multivec data is essentially a matrix where rows correspond to genomic positions or regions, and columns correspond to different samples or experiments. Each matrix entry represents a value associated with a specific genomic position and sample. The genomic positions are typically represented as chromosomal coordinates (chromosome name and base pair position), allowing accurate alignment with the genome. Tools like the UCSC Genome Browser, IGV, and libraries like shiny.gosling can visualize multivec data effectively.

To delve deeper into multivec data and its application in genomics, explore these resources:

UCSC Genome Browser: A versatile tool for visualizing genomic data, including multivec data. Learn how to visualize multivec data in the UCSC Genome Browser.

Integrative Genomics Viewer (IGV): Another popular genome visualization tool supporting multivec data. Discover how to load and visualize multivec data in IGV.

BedGraph and BigWig Formats: These common formats are used to represent multivec data. Explore explanations of the BedGraph and BigWig formats.

Call library libraries.

library(shiny)
library(shiny.gosling)

Creating the Data Object

We’ll begin by fetching the multivec data using the track_data function. This function retrieves data from a specified URL, where the data is organized as a matrix with rows representing samples and columns representing positions. We’ll define the row, column, and value attributes along with the sample categories. The categories parameter should contain a list of category names (e.g., “sample 1”).


# Create data object ----
view1_data <- track_data(
  url = "https://resgen.io/api/v1/tileset_info/?d=UvVPeLHuRDiYA3qwFlm7xQ",
  type = "multivec",
  row = "sample",
  column = "position",
  value = "peak",
  categories = list("sample 1")
)

Constructing the Line Chart Track

To visualize the data, we’ll define the visual channels for the line chart. We’ll specify that the x-axis should represent genomic positions and the y-axis should represent peak values. The add_single_track function creates a line chart visualization using these visual channels. Additionally, we’ll set the width and height of the plot and specify the data source.


# Create visual channels ----
view1_x <- visual_channel_x(
  field = "position", type = "genomic", axis = "bottom"
)

view1_y <- visual_channel_y(
  field = "peak", type = "quantitative", axis = "right"
)

# Create single track ----
single_track <- add_single_track(
  width = 800,
  height = 180,
  data = view1_data,
  mark = "line",
  x = view1_x,
  y = view1_y,
  size = visual_channel_size(
    value = 2
  )
)

Composing and Arranging the View

The compose_view function is used to create a composed view that contains the single line chart track. The layout of the view is specified as “linear”.

The arrange_views function arranges the composed view created earlier. The title and subtitle for the arrangement are specified. The arranged view is named single_composed_views.


# Compose the track ----
single_composed_view <- compose_view(
  tracks = single_track,
  layout = "linear"
)

# Arrange the view above ----
single_composed_views <- arrange_views(
  title = "Basic Marks: line",
  subtitle = "This is a simple line chart.",
  views = single_composed_view
)

Shiny App ui

The navbarPage function creates a navigation bar at the top of the user interface. Within the navigation bar, there is a single tab panel named “Line Chart.” Inside the tab panel, the use_gosling function is used to incorporate the shiny.gosling package. The fluidPage function is used to create a fluid layout within the tab panel. Two columns are defined: one for the goslingOutput and another for action buttons.


ui <- navbarPage(
  title = "shiny.gosling",
  tabPanel(
    "Line Chart",
    use_gosling(),
    fluidPage(
      width = 12,
      fluidRow(
        column(
          width = 8,
          goslingOutput("gosling_plot_test")
        ),
        column(
          width = 4,
          fluidRow(
            column(
              2,
              actionButton(
                "download_png",
                "PNG",
                icon = icon("cloud-arrow-down")
              )
            ),
            column(
              2,
              actionButton(
                "download_pdf",
                "PDF",
                icon = icon("cloud-arrow-down")
              )
            )
          )
        )
      )
    )
  )
)

Shiny App server

The server logic is defined using the server function. The observeEvent functions are used to respond to button clicks and user interactions. Clicking the “PNG” or “PDF” buttons triggers the respective download action for the visualization. The output$gosling_plot_test function uses renderGosling to render the composed view using the gosling function from the shiny.gosling package.


server <- function(input, output, session) {

  observeEvent(input$download_png, {
    export_png(component_id = "sars_cov2")
  })

  observeEvent(input$download_pdf, {
    export_pdf(component_id = "sars_cov2")
  })

  observeEvent(input$zoom_out, {
    zoom_to_extent(
      component_id = "sars_cov2",
      view_id = "view2_track1"
    )
  })

  output$gosling_plot_test <- renderGosling({
    gosling(
      component_id = "sars_cov2",
      single_composed_views,
      clean_braces = TRUE
    )
  })
}

shinyApp(ui, server)

Session Info


sessionInfo()
#> R version 4.4.0 RC (2024-04-16 r86468)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 22.04.4 LTS
#> 
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.20-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_GB              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       
#> 
#> time zone: America/New_York
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats4    stats     graphics  grDevices utils     datasets  methods  
#> [8] base     
#> 
#> other attached packages:
#>  [1] sessioninfo_1.2.2                  ggbio_1.53.0                      
#>  [3] ggplot2_3.5.1                      StructuralVariantAnnotation_1.21.0
#>  [5] VariantAnnotation_1.51.0           Rsamtools_2.21.0                  
#>  [7] Biostrings_2.73.0                  XVector_0.45.0                    
#>  [9] SummarizedExperiment_1.35.0        Biobase_2.65.0                    
#> [11] MatrixGenerics_1.17.0              matrixStats_1.3.0                 
#> [13] rtracklayer_1.65.0                 GenomicRanges_1.57.0              
#> [15] GenomeInfoDb_1.41.0                IRanges_2.39.0                    
#> [17] S4Vectors_0.43.0                   BiocGenerics_0.51.0               
#> [19] shiny_1.8.1.1                      shiny.gosling_1.1.0               
#> 
#> loaded via a namespace (and not attached):
#>   [1] RColorBrewer_1.1-3       rstudioapi_0.16.0        jsonlite_1.8.8          
#>   [4] magrittr_2.0.3           GenomicFeatures_1.57.0   rmarkdown_2.26          
#>   [7] fs_1.6.4                 BiocIO_1.15.0            zlibbioc_1.51.0         
#>  [10] vctrs_0.6.5              memoise_2.0.1            RCurl_1.98-1.14         
#>  [13] base64enc_0.1-3          progress_1.2.3           htmltools_0.5.8.1       
#>  [16] S4Arrays_1.5.0           curl_5.2.1               SparseArray_1.5.0       
#>  [19] Formula_1.2-5            sass_0.4.9               bslib_0.7.0             
#>  [22] fontawesome_0.5.2        htmlwidgets_1.6.4        httr2_1.0.1             
#>  [25] plyr_1.8.9               cachem_1.0.8             GenomicAlignments_1.41.0
#>  [28] shiny.react_0.3.0        mime_0.12                lifecycle_1.0.4         
#>  [31] pkgconfig_2.0.3          Matrix_1.7-0             R6_2.5.1                
#>  [34] fastmap_1.1.1            GenomeInfoDbData_1.2.12  digest_0.6.35           
#>  [37] colorspace_2.1-0         GGally_2.2.1             AnnotationDbi_1.67.0    
#>  [40] OrganismDbi_1.47.0       Hmisc_5.1-2              RSQLite_2.3.6           
#>  [43] filelock_1.0.3           fansi_1.0.6              httr_1.4.7              
#>  [46] abind_1.4-5              compiler_4.4.0           bit64_4.0.5             
#>  [49] withr_3.0.0              htmlTable_2.4.2          backports_1.4.1         
#>  [52] BiocParallel_1.39.0      DBI_1.2.2                ggstats_0.6.0           
#>  [55] biomaRt_2.61.0           rappdirs_0.3.3           DelayedArray_0.31.0     
#>  [58] rjson_0.2.21             tools_4.4.0              foreign_0.8-86          
#>  [61] httpuv_1.6.15            nnet_7.3-19              glue_1.7.0              
#>  [64] restfulr_0.0.15          promises_1.3.0           grid_4.4.0              
#>  [67] checkmate_2.3.1          cluster_2.1.6            reshape2_1.4.4          
#>  [70] generics_0.1.3           gtable_0.3.5             BSgenome_1.73.0         
#>  [73] tidyr_1.3.1              ensembldb_2.29.0         hms_1.1.3               
#>  [76] data.table_1.15.4        xml2_1.3.6               utf8_1.2.4              
#>  [79] pillar_1.9.0             stringr_1.5.1            later_1.3.2             
#>  [82] dplyr_1.1.4              BiocFileCache_2.13.0     lattice_0.22-6          
#>  [85] bit_4.0.5                biovizBase_1.53.0        RBGL_1.81.0             
#>  [88] tidyselect_1.2.1         knitr_1.46               gridExtra_2.3           
#>  [91] ProtGenerics_1.37.0      xfun_0.43                stringi_1.8.3           
#>  [94] UCSC.utils_1.1.0         lazyeval_0.2.2           yaml_2.3.8              
#>  [97] evaluate_0.23            codetools_0.2-20         tibble_3.2.1            
#> [100] graph_1.83.0             BiocManager_1.30.22      cli_3.6.2               
#> [103] rpart_4.1.23             xtable_1.8-4             munsell_0.5.1           
#> [106] jquerylib_0.1.4          dichromat_2.0-0.1        Rcpp_1.0.12             
#> [109] dbplyr_2.5.0             png_0.1-8                XML_3.99-0.16.1         
#> [112] parallel_4.4.0           assertthat_0.2.1         blob_1.2.4              
#> [115] prettyunits_1.2.0        AnnotationFilter_1.29.0  bitops_1.0-7            
#> [118] txdbmaker_1.1.0          pwalign_1.1.0            scales_1.3.0            
#> [121] purrr_1.0.2              crayon_1.5.2             rlang_1.1.3             
#> [124] KEGGREST_1.45.0