File size: 19,294 Bytes
977cc7d
 
 
 
 
 
3498741
977cc7d
 
1b4ea86
 
977cc7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c162a6e
e55e859
3498741
977cc7d
 
 
 
 
ba4aba0
1b4ea86
977cc7d
 
 
c162a6e
 
 
 
977cc7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba4aba0
977cc7d
ba4aba0
977cc7d
 
ba4aba0
977cc7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7b3428
 
977cc7d
 
 
 
3498741
 
977cc7d
 
 
 
 
 
 
 
 
ba4aba0
e7b3428
977cc7d
 
 
 
 
e7b3428
 
 
977cc7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7b3428
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
977cc7d
e7b3428
 
 
 
 
 
 
 
 
 
 
 
 
 
977cc7d
 
 
 
 
 
 
3498741
977cc7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3498741
977cc7d
 
 
 
 
 
3498741
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
977cc7d
 
 
 
 
 
 
 
 
 
 
 
 
73d003b
3498741
 
 
 
 
 
 
 
 
977cc7d
 
 
 
 
 
 
 
26a24b9
 
977cc7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3498741
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
977cc7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284391c
 
 
 
 
 
e55e859
 
4064c23
e55e859
 
 
4064c23
 
 
e55e859
 
284391c
 
977cc7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284391c
 
 
 
 
 
 
977cc7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e55e859
1b4ea86
e55e859
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# rtemisseq
# ::rtemislive::
# 2024 EDG rtemis.org

# shiny::runApp("./")
# Setup
rtemisseq_version <- "0.3.0"
library(rtemis)
library(rtemisbio)
library(shiny)
library(bslib)
library(htmltools)
library(plotly)
source("globals.R")
source("data.R")

#' Create protein visualization shinylive app
#'
#' Visualize protein amino acid sequence and annotations using
#' `dplot3_protein()`
#'
#' Set verbosity to 1 to monitor app progress
#'
#' @param default_theme Character: "dark" or "light" theme
#' @param verbosity Integer: 0 = silent, 1 = verbose
#'
#' @author EDG
#' @export
#' @return A shiny app that can be converted to a shinylive app

rtemisseq <- function(
    default_theme = "dark",
    protein_plotly_height = "900px",
    jsonedit_height = "900px",
    verbosity = 0) {
  # Logo
  logo <- base64enc::dataURI(
    file = "./www/rtemisseq_gray.png", mime = "image/png"
  )
  # Version
  platform <- sessionInfo()[["platform"]]
  svl <- paste0(
    "rtemisseq v", rtemisseq_version,
    " | ", "rtemisbio v", utils::packageVersion("rtemisbio"),
    " | ", "rtemis v", utils::packageVersion("rtemis"),
    " | R v", version$major, ".", version$minor,
    " | running on ", platform
  )

  # Shinylive info
  shinylive_info <- if (substr(platform, 1, 4) == "wasm") {
    paste0(
      "<br><br>This application has been compiled to ",
      as.character(a("WebAssembly", href = "https://webassembly.org/", target = "_blank")),
      " using ",
      as.character(a("shinylive", href = "https://posit-dev.github.io/r-shinylive/", target = "_blank")),
      "<br>and is best viewed with the latest version of Chrome."
    )
  } else {
    NULL
  }

  # UI page_navbar ----
  ui <- function(request) {
    bslib::page_navbar(
      # Title ----
      title = list(
        logo = a(
          img(
            src = logo,
            width = "140px",
            height = "auto",
            alt = "rtemisseq"
          ),
          href = "https://rtemis.org/rtemisseq",
        )
      ),
      id = "rtemisseq",
      selected = "Welcome",
      footer = span(
        svl,
        " © 2024 EDG",
        style = "display: block; text-align: center; margin-top: 1em; margin-bottom: 1em;"
      ),
      # Theme ----
      theme = bslib::bs_theme(
        `tooltip-opacity` = 1,
        `tooltip-border-radius` = "10px",
        `tooltip-padding-x` = "1rem",
        `tooltip-padding-y` = "1rem",
        `tooltip-font-size` = "1rem"
      ) |>
        bs_add_rules(sass::sass_file("www/rtemislive.scss")),
      # Window title ----
      window_title = "rtemisSeq",
      # Language ----
      lang = "en",
      # Nav Panels ----
      ## [] Welcome ----
      bslib::nav_panel(
        title = "Welcome",
        icon = bsicons::bs_icon("stars"),
        bslib::card(
          card_body(
            class = "d-inline text-center",
            h4("Welcome to rtemisSeq", style = "text-align: center;"),
            br(),
            HTML(paste0(
              "rtemisSeq is a web interface for ",
              as.character(a("rtemisbio", href = "https://rtemis.org/rtemisbio", target = "_blank")),
              ", <br>providing interactive visualization of sequence data.",
              "<br>Created for the ",
              as.character(a("FTD CWOW", href = "https://cwow.ucsf.edu/", target = "_blank")),
              ".<br><br>",
              rthelp_inline(
                "To get started, use the navigation tabs at the top.",
                title = "Welcome"
              ),
              shinylive_info
            )),
            br(), br(),
            bslib::card_image(
              file = "./www/rtemisseq-splash.webp",
              alt = "rtemisseq",
              align = "center",
              border_radius = "all",
              fill = FALSE,
              width = "40%",
              class = "mx-auto"
            ) # /card_image
          ) # /card_body
        ) # /card
      ), # /nav_panel Welcome
      ## [] Protein Visualization ----
      bslib::nav_panel(
        title = "Protein Visualization",
        icon = bsicons::bs_icon("body-text"),
        card(
          full_screen = TRUE,
          class = "p-0",
          # allow items side by side ----
          card_header(
            class = "d-flex justify-content-end",
            # tooltip UI output ----
            uiOutput("ui_a3_tooltip"),
            # popover UI output ----
            uiOutput("ui_a3_popover")
          ),
          layout_sidebar(
            fillable = TRUE,
            # Data Load Sidebar ----
            sidebar = bslib::sidebar(
              uiOutput("ui_a3_load_switch"), # Switch between file upload and built-in data
              uiOutput("ui_a3_data_load"), # File upload or built-in data selection depending on switch
              uiOutput("ui_a3_data_info"), # Shows dataset info
              uiOutput("ui_a3_plot_button"), # Click to plot
              # uiOutput("ui_a3_tooltip")
            ),
            # Plot Output ----
            # for shinylive, do not use plotlyOutput directly in ui !!
            # fails when no plot is rendered, unlike shiny
            uiOutput("ui_dplot3_protein")
          )
        )
        # bslib::navset_card_tab(
        #   full_screen = TRUE,
        #   # []] Sequence Viewer ----
        #   bslib::nav_panel(
        #     title = "Sequence Viewer",
        #     layout_sidebar(
        #       # Data Load Sidebar ----
        #       sidebar = bslib::sidebar(
        #         uiOutput("ui_a3_load_switch"), # Switch between file upload and built-in data
        #         uiOutput("ui_a3_data_load"), # File upload or built-in data selection depending on switch
        #         uiOutput("ui_a3_data_info"), # Shows dataset info
        #         uiOutput("ui_a3_plot_button") # Click to plot
        #       ),
        #       # Plot Output ----
        #       # for shinylive, do not use plotlyOutput directly in ui !!
        #       # fails when no plot is rendered, unlike shiny
        #       uiOutput("ui_dplot3_protein")
        #     )
        #   ),
        #   # []] jsonedit ----
        #   bslib::nav_panel(
        #     title = "Raw Data",
        #     bslib::card(
        #       bslib::card_title("Sequence & Annotation data"),
        #       uiOutput("ui_jsonedit")
        #     )
        #   ) # /nav_panel jsonedit
        # ) # /navset_card_tab
      ), # /nav_panel Protein Visualization
      ## [] About ----
      bslib::nav_panel(
        title = "About",
        icon = bsicons::bs_icon("info-square"),
        bslib::card(
          class = "mx-auto",
          bslib::card_body(
            div(
              class = "text-center",
              HTML(paste0(
                "Created by the ",
                as.character(a("FTD CWOW", href = "https://cwow.ucsf.edu/", target = "_blank")),
                " Genomics & Transcriptomics core.<br/>",
                "Powered by rtemis & rtemisbio (",
                as.character(a("rtemis.org", href = "https://rtemis.org", target = "_blank")),
                ")."
              ))
            ), # /div
            card_image(
              file = "./www/rtemisbio.webp",
              href = "https://rtemis.org/rtemisbio",
              alt = "rtemisbio",
              align = "center",
              border_radius = "all",
              fill = FALSE,
              width = "54%",
              class = "mx-auto"
            ),
            div(
              class = "text-center",
              a(
                img(
                  src = "rtemis_gray.png",
                  alt = "rtemis",
                  width = "190px"
                ),
                href = "https://rtemis.org",
                target = "_blank"
              )
            ) # /div
          ) # /card_body
        ) # /card
      ), # /nav_panel About
      bslib::nav_spacer(),
      bslib::nav_item(input_dark_mode(id = "dark_mode", mode = default_theme)),
      header = list(
        # busy indicators ----
        shinybusy::add_busy_spinner(
          spin = "orbit",
          color = "#FF8F28",
          timeout = 200,
          position = "bottom-left",
          onstart = FALSE
        )
      )
    ) # /ui /bslib::page_navbar
  } # /ui function

  # Server ----
  server <- function(input, output, session) {
    # UI a3 load switch ----
    output$ui_a3_load_switch <- shiny::renderUI({
      if (verbosity > 0) {
        message("Rendering ui_a3_load_switch")
      }
      # Radio buttons: built-in data vs upload file ----
      shiny::radioButtons(
        inputId = "a3_load_switch",
        label = "Data source",
        choices = list(
          `Built-in datasets` = "builtin",
          `GitHub repository` = "github",
          `File upload` = "upload"
        ),
        selected = "builtin"
      )
    })

    # GitHub file list ----
    # GitHub file list udpate button ----
    output$ui_github_files_update_button <- shiny::renderUI({
      if (verbosity > 0) {
        message("Rendering ui_github_files_update_button")
      }
      bslib::input_task_button(
        "github_files_update_button",
        "Update GitHub file list",
        icon = bsicons::bs_icon("arrow-clockwise"),
        label_busy = "Updating...",
        icon_busy = bsicons::bs_icon("clock-history"),
        type = "primary",
        auto_reset = TRUE
      )
    }) # /ui_github_files_update_button

    # GitHub files runs when github_files_update_button is clicked ----
    github_files <- reactive({
      get_github_files(repo_owner = "rtemis-org", repo_name = "seq-data", verbosity = verbosity)
    }) |>
      bindEvent(input$github_files_update_button)
    
    # GitHub file list output ----
    output$ui_github_files <- shiny::renderUI({
      req(github_files())
      if (verbosity > 0) {
        message("Rendering ui_github_files")
      }
      shiny::selectizeInput(
        inputId = "github_file",
        label = "Select a file from the GitHub repository",
        choices = github_files()$file_name,
        selected = github_files()$file_name[1]
      )
    }) # /ui_github_files

    # UI a3 Data ----
    output$ui_a3_data_load <- shiny::renderUI({
      req(input$a3_load_switch)
      if (input$a3_load_switch == "upload") {
        # Upload a3 JSON file
        if (verbosity > 0) {
          message("Rendering ui_a3_data_load for file upload")
        }
        shiny::fileInput(
          inputId = "a3_file",
          label = "Upload a3 JSON file",
          buttonLabel = "Browse local files...",
        )
      } else if (input$a3_load_switch == "github") {
        # "Update GitHub file list" button
        if (verbosity > 0) {
          message("Rendering ui_a3_data_load for GitHub file list")
        }
        # UI output with GitHub file list & update button
        shiny::tagList(
          uiOutput("ui_github_files"),
          uiOutput("ui_github_files_update_button")
        )
      } else {
        # Select built-in data stored in ./data/ directory
        if (verbosity > 0) {
          message("Rendering ui_a3_data_load for built-in data selection")
        }
        shiny::selectizeInput(
          inputId = "a3_builtin_data",
          label = "Select built-in a3 dataset",
          choices = c("MAPT_Annot", "MAPT_Cleavage", "MAPT_Citrullination"),
          selected = "MAPT_Annot"
        )
      }
    }) # /ui_a3_data_load

    # UI for a3 data info ----
    output$ui_a3_data_info <- shiny::renderUI({
      req(a3_obj())
      if (verbosity > 0) {
        message("Rendering ui_a3_data_info")
      }
      bslib::card(
        bslib::card_title("a3 Dataset Info", container = htmltools::h6),
        bslib::card_body(
          summarize_a3(a3_obj()),
          fillable = FALSE
        ) # /card_body
      ) # /card
    }) # /ui_a3_data_info

    # UI for plot action button ----
    output$ui_a3_plot_button <- shiny::renderUI({
      req(a3_obj())
      if (verbosity > 0) {
        message("Rendering ui_a3_plot_button")
      }
      bslib::input_task_button(
        "a3_plot_button",
        "Plot dataset",
        icon = bsicons::bs_icon("magic"),
        label_busy = "Drawing...",
        icon_busy = bsicons::bs_icon("clock-history"),
        type = "primary",
        auto_reset = TRUE
      )
    }) # /ui_a3_plot_button

    # Load Dataset ----
    a3_obj <- shiny::reactive({
      req(input$a3_load_switch)
      if (input$a3_load_switch == "upload") {
        req(input$a3_file)
        if (verbosity > 0) {
          message("Loading a3 JSON file '", input$a3_file$datapath, "'")
        }
        dat <- read.a3json(input$a3_file$datapath)
        if (verbosity > 0) {
          message("Loaded dataset of class '", class(dat)[1], "'")
        }
        return(dat)
      } else if (input$a3_load_switch == "github") {
        # Download GitHub file
        req(input$github_file)
        if (verbosity > 0) {
          message("Downloading GitHub file '", input$github_file, "'")
        }
        # index with input$github_file
        jsondat <- httr::GET(
          github_files()[github_files()$file_name == input$github_file, "download_url"]
        ) |> httr::content(as = "text", encoding = "UTF-8")
        dat <- as.a3(jsonlite::parse_json(
          jsondat,
          simplifyVector = TRUE,
          simplifyMatrix = FALSE
        ))
        return(dat)
      } else {
        # Load built-in data from data.R objects
        req(input$a3_builtin_data)
        if (verbosity > 0) {
          message("Loading built-in a3 dataset '", input$a3_builtin_data, "'")
        }
        # read.3.json from data folder works fine in shiny app, not shinylive,
        # use objects from data.R instead
        # assign to dat object of name input$a3_builtin_data
        dat <- get(input$a3_builtin_data)
        if (verbosity > 0) {
          message("Loaded dataset of class '", class(dat)[1], "'")
        }
        return(dat)
      }
    }) # /a3_obj

    # dplot3 theme is "white" or "black" ----
    dplot3_theme <- shiny::reactive({
      req(input$dark_mode)
      if (input$dark_mode == "dark") {
        "black"
      } else {
        "white"
      }
    }) # /dplot3_theme

    # Render plotly ----
    output$dplot3_protein <- plotly::renderPlotly({
      req(a3_obj())
      if (verbosity > 0) {
        message("Rendering dplot3_protein of object with class '", class(a3_obj())[1], "'")
      }
      plot(
        a3_obj(),
        theme = dplot3_theme(),
        marker.size = input$marker.size,
        font.size = input$font.size,
        ptm.marker.size = input$ptm.marker.size,
        clv.marker.size = input$clv.marker.size,
        bg = input$plot.bg, # legend.bg defaults to transparent, this sets paper bg
        plot.bg = input$plot.bg,
        marker.col = input$marker.col,
        n.per.row = if (input$n.per.row == "auto") NULL else as.integer(input$n.per.row)
      )
    }) |> # /dplot3_protein
      bindEvent(input$a3_plot_button, input$a3_plot_update_button)

    # Create variable clicked that is TRUE after input$a3_plot_button is clicked
    clicked <- shiny::reactiveVal(FALSE)
    shiny::observeEvent(input$a3_plot_button, {
      clicked(TRUE)
    })

    # UI a3 tooltip ----
    output$ui_a3_tooltip <- shiny::renderUI({
      bslib::tooltip(
        trigger = span(
          "Plot help", bsicons::bs_icon("info-circle", class = "text-info"),
          style = "text-align: right;",
          class = "rtanihi"
        ),
        rthelplist(
          c(
            "Select Data Source (built-in, GitHub, or upload)",
            "Click 'Plot dataset' to render the plot - repeat after changing datasets.",
            "Click on legend items to toggle visibility of annotations.",
            "Double-click on legend items to isolate a single annotation type.",
            "Hover over plot to see annotations.",
            "Click on top-right gear icon to change plot settings.",
            "Use camera icon (first on top right menu within plot) to download plot."
          )
        ),
        placement = "bottom"
      ) # /tooltip
    }) # /ui_a3_tooltip

    # UI a3 popover ----
    output$ui_a3_popover <- shiny::renderUI({
      if (clicked()) {
        popover(
          trigger = bsicons::bs_icon("gear", class = "ms-auto"),
          # trigger = span(
          #   "Plot settings", bsicons::bs_icon("gear", class = "ms-auto"),
          #   style = "text-align: right;"
          # shiny::selectizeInput("a3.theme", label = "Theme", choices = c("dark", "light"), selected = default_theme),
          shiny::sliderInput("marker.size", label = "Marker size", min = 1, max = 100, value = 28),
          shiny::sliderInput("font.size", label = "Font size", min = 1, max = 72, value = 18),
          if (length(a3_obj()$Annotations$PTM) > 0) shiny::sliderInput("ptm.marker.size", label = "PTM Marker size", min = .1, max = 36, value = 28 / 4.5),
          if (length(a3_obj()$Annotations$Cleavage_site) > 0) shiny::sliderInput("clv.marker.size", label = "Cleavage Site Marker size", min = .1, max = 36, value = 28 / 4),
          shinyWidgets::colorPickr(
            "plot.bg",
            label = "Plot background",
            selected = ifelse(input$dark_mode == "dark", "#191919", "#FFFFFF")
          ),
          shinyWidgets::colorPickr(
            "marker.col",
            label = "Marker color",
            selected = ifelse(input$dark_mode == "dark", "#3f3f3f", "#dfdfdf")
          ),
          textInput("n.per.row", "Number of AAs per row", value = "auto"),
          # tags$i("Click on 'Plot dataset' to update render after changing settings."),
          bslib::input_task_button(
            "a3_plot_update_button",
            "Update rendering",
            icon = bsicons::bs_icon("arrow-clockwise"),
            label_busy = "Drawing...",
            icon_busy = bsicons::bs_icon("clock-history"),
            type = "primary",
            auto_reset = TRUE
          ),
          title = "Plot settings",
          placement = "auto"
          # options = list(trigger = "hover focus click")
        ) # /popover
      } else {
        popover(
          trigger = bsicons::bs_icon("gear", class = "ms-auto"),
          HTML(
            "Please select dataset from the left sidebar and click 'Plot dataset'.<br>Plot settings will appear here after the plot is rendered."
          )
        )
      }
    }) # /ui_a3_popover


    # UI dplot3_protein ----
    output$ui_dplot3_protein <- renderUI({
      if (clicked() == FALSE || is.null(a3_obj())) {
        rthelp(
          "Select Data Source and Click 'Plot dataset' on the left.",
          title = "Protein Visualization "
        )
      } else {
        plotly::plotlyOutput(
          "dplot3_protein",
          width = "100%",
          height = protein_plotly_height
        )
      }
    }) # /ui_dplot3_protein

    # Render JSON ----
    output$jsonedit <- listviewer::renderJsonedit({
      req(a3_obj())
      listviewer::jsonedit(a3_obj())
    }) # /json

    # UI jsonedit ----
    output$ui_jsonedit <- shiny::renderUI({
      listviewer::jsoneditOutput(
        "jsonedit",
        height = jsonedit_height
      )
    })
  } # /server

  # Shiny app ----
  shiny::shinyApp(ui = ui, server = server, enableBookmarking = "url")
} # rtemisseq

rtemisseq()