[413088]: / R / interactive.R

Download this file

222 lines (196 with data), 6.8 kB

  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
####
# Spatial Interactive Plot (VoltRon) ####
####
####
## Background Shiny App ####
####
#' vrSpatialPlotInteractive
#'
#' @inheritParams shiny::runApp
#' @param plot_g the ggplot plot
#' @param shiny.options a list of shiny options (launch.browser, host, port etc.) passed \code{options} arguement of \link{shinyApp}. For more information, see \link{runApp}
#'
#' @importFrom shinyjs useShinyjs
#'
#' @noRd
vrSpatialPlotInteractive <- function(plot_g = NULL,
shiny.options = list()){
# js for Shiny
shinyjs::useShinyjs()
# UI
ui <- mod_app_ui("app")
# Server
server <- function(input, output, session) {
mod_app_server("app", plot_g = plot_g)
session$onSessionEnded(function() {
stopApp()
})
}
# get shiny options
shiny.options = configure_shiny_options(shiny.options)
# Start Shiny Application
shiny::runApp(
shiny::shinyApp(ui, server, options = list(host = shiny.options[["host"]], port = shiny.options[["port"]], launch.browser = shiny.options[["launch.browser"]]),
onStart = function() {
onStop(function() {
})
})
)
}
#' App UI
#'
#' @param id id of the module
#'
#' @import shiny
#'
#' @noRd
mod_app_ui <- function(id) {
ns <- NS(id)
plotOutput(ns("image_plot"),
height = "1000px",
dblclick = ns("plot_dblclick"),
brush = brushOpts(
id = ns("plot_brush"), fill = "green",
resetOnNew = TRUE
))
}
#' App Server
#'
#' @param id id of the module
#' @param plot_g the ggplot plot
#'
#' @import ggplot2
#'
#' @noRd
mod_app_server <- function(id, plot_g = NULL) {
moduleServer(id, function(input, output, session) {
ranges <- reactiveValues(x = plot_g$coordinates$limits$x, y = plot_g$coordinates$limits$y)
observeEvent(input$plot_dblclick, {
brush <- input$plot_brush
if (!is.null(brush)) {
ranges$x <- c(brush$xmin, brush$xmax)
ranges$y <- c(brush$ymin, brush$ymax)
} else {
ranges$x <- plot_g$coordinates$limits$x
ranges$y <- plot_g$coordinates$limits$y
}
})
# image output
output$image_plot <- renderPlot({
plot_g +
ggplot2::coord_equal(xlim = ranges$x, ylim = ranges$y, ratio = 1)
})
})
}
#' configure_shiny_options
#'
#' @param shiny.options a list of shiny options (launch.browser, host, port etc.) passed \code{options} arguement of \link{shinyApp}. For more information, see \link{runApp}
#'
#' @noRd
configure_shiny_options <- function(shiny.options){
# check package
if (!requireNamespace('rstudioapi'))
stop("Please install rstudioapi package to use RStudio for interactive visualization")
# launch.browser
if("launch.browser" %in% names(shiny.options)){
launch.browser <- shiny.options[["launch.browser"]]
} else {
launch.browser <- "RStudio"
}
if(!is.function(launch.browser)){
if(launch.browser == "RStudio"){
launch.browser <- rstudioapi::viewer
}
}
# host and port
# if "port" is entered, parse "host" (or use default) but ignore "launch.browser"
if("host" %in% names(shiny.options)){
host <- shiny.options[["host"]]
} else {
host <- getOption("shiny.host", "0.0.0.0")
}
if("port" %in% names(shiny.options)){
port <- shiny.options[["port"]]
launch.browser <- TRUE
} else {
port <- getOption("shiny.port")
}
return(list(host = host, port = port, launch.browser = launch.browser))
}
####
# Spatial Interactive Plot (Vitessce) ####
####
#' vrSpatialPlotInteractive
#'
#' Interactive Plotting identification of spatially resolved cells, spots, and ROI on associated images from multiple assays in a VoltRon object.
#'
#' @param zarr.file The zarr file of a VoltRon object
#' @param group.by a grouping label for the spatial entities
#' @param reduction The name of the reduction to visualize an embedding alongside with the spatial plot.
#' @param shiny.options a list of shiny options (host, port etc.) passed \code{options} argument of \link{wc$widget}.
#'
#' @noRd
vrSpatialPlotVitessce <- function(zarr.file, group.by = "Sample", reduction = NULL, shiny.options = NULL) {
# check package
if (!requireNamespace('vitessceR'))
stop("Please install vitessceR package for using interactive visualization!: devtools::install_github('vitessce/vitessceR')")
# check file
if(!dir.exists(zarr.file))
stop(zarr.file, " is not found at the specified location!")
# get embedding
if(is.null(reduction)){
obs_embedding_paths <- c("obsm/spatial")
} else {
obs_embedding_paths <- c(paste0("obsm/", reduction), "obsm/spatial")
}
# initiate vitessceR
vc <- vitessceR::VitessceConfig$new(schema_version = "1.0.15", name = "MBrain")
dataset <- vc$add_dataset("My dataset")
# add ome tiff if exists
ometiff.file <- gsub("zarr[/]?$", "ome.tiff", zarr.file)
if(file.exists(ometiff.file)){
w_img <- vitessceR::MultiImageWrapper$new(
image_wrappers = list(
vitessceR::OmeTiffWrapper$new(name="Test1", img_path=ometiff.file)
)
)
dataset <- dataset$add_object(w_img)
}
# add anndata
w_data <- vitessceR::AnnDataWrapper$new(
adata_path=zarr.file,
obs_set_paths = c(paste0("obs/", group.by)),
obs_set_names = c(group.by),
obs_locations_path = "obsm/spatial",
obs_segmentations_path = "obsm/segmentation",
obs_embedding_paths = obs_embedding_paths
)
dataset <- dataset$add_object(w_data)
# set up vitessce pane
spatial <- vc$add_view(dataset, vitessceR::Component$SPATIAL)
cell_sets <- vc$add_view(dataset, vitessceR::Component$OBS_SETS)
spatial_segmentation_layer_value <- list(opacity = 1, radius = 0, visible = TRUE, stroked = FALSE)
spatial_layers <- vc$add_view(dataset, vitessceR::Component$LAYER_CONTROLLER)
if(is.null(reduction)){
vc$layout(
vitessceR::hconcat(spatial,
vitessceR::hconcat(cell_sets, spatial_layers))
)
} else {
umap <- vc$add_view(dataset, vitessceR::Component$SCATTERPLOT, mapping = reduction)
vc$layout(
vitessceR::hconcat(spatial,
vitessceR::vconcat(umap,
vitessceR::hconcat(cell_sets, spatial_layers)))
)
}
if(length(shiny.options) == 0){
vc$widget(theme = "light")
} else {
if (!requireNamespace('rstudioapi'))
stop("Please install rstudioapi package!: install.packages('rstudioapi')")
message("Listening widget from ", shiny.options[["host"]], ":", shiny.options[["port"]])
base = rstudioapi::translateLocalUrl(paste0(shiny.options[["host"]],":",shiny.options[["port"]]), absolute=TRUE)
vc$widget(theme = "light", base_url=base, port=shiny.options[["port"]])
}
}