Skip to content

Commit bf6c637

Browse files
committed
Merge branch 'devel' of github.com:LieberInstitute/spatialLIBD into devel
2 parents 120803f + a87428b commit bf6c637

File tree

4 files changed

+125
-60
lines changed

4 files changed

+125
-60
lines changed

R/app_server.R

Lines changed: 101 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -224,64 +224,80 @@ app_server <- function(input, output, session) {
224224
})
225225

226226
static_gene <- reactive({
227-
p <- vis_gene(
228-
spe,
229-
sampleid = input$sample,
230-
geneid = input$geneid,
231-
multi_gene_method = input$multi_gene_method,
232-
assayname = input$assayname,
233-
minCount = input$minCount,
234-
cont_colors = cont_colors(),
235-
image_id = input$imageid,
236-
alpha = input$alphalevel,
237-
point_size = input$pointsize,
238-
auto_crop = input$auto_crop,
239-
is_stitched = is_stitched
240-
)
241-
if (!input$side_by_side_gene) {
242-
return(p)
243-
} else {
244-
p_no_spots <- p
245-
p_no_spots$layers[[2]] <- NULL
246-
247-
p_no_spatial <- p
248-
p_no_spatial$layers[[1]] <- NULL
249-
cowplot::plot_grid(
250-
plotlist = list(
251-
p_no_spots,
252-
p_no_spatial + ggplot2::theme(legend.position = "none")
253-
),
254-
nrow = 1,
255-
ncol = 2
227+
gene_warning = NULL
228+
withCallingHandlers({
229+
p <- vis_gene(
230+
spe,
231+
sampleid = input$sample,
232+
geneid = input$geneid,
233+
multi_gene_method = input$multi_gene_method,
234+
assayname = input$assayname,
235+
minCount = input$minCount,
236+
cont_colors = cont_colors(),
237+
image_id = input$imageid,
238+
alpha = input$alphalevel,
239+
point_size = input$pointsize,
240+
auto_crop = input$auto_crop,
241+
is_stitched = is_stitched
256242
)
257-
}
243+
if (!input$side_by_side_gene) {
244+
p_result = p
245+
} else {
246+
p_no_spots <- p
247+
p_no_spots$layers[[2]] <- NULL
248+
249+
p_no_spatial <- p
250+
p_no_spatial$layers[[1]] <- NULL
251+
p_result = cowplot::plot_grid(
252+
plotlist = list(
253+
p_no_spots,
254+
p_no_spatial + ggplot2::theme(legend.position = "none")
255+
),
256+
nrow = 1,
257+
ncol = 2
258+
)
259+
}
260+
}, warning = function(w) {
261+
gene_warning <<- conditionMessage(w)
262+
invokeRestart("muffleWarning")
263+
})
264+
return(list(p = p_result, gene_warning = gene_warning))
258265
})
259266

260267
static_gene_grid <- reactive({
261268
input$gene_grid_update
262269

263-
plots <-
264-
vis_grid_gene(
265-
spe,
266-
geneid = isolate(input$geneid),
267-
multi_gene_method = input$multi_gene_method,
268-
assayname = isolate(input$assayname),
269-
minCount = isolate(input$minCount),
270-
return_plots = TRUE,
271-
spatial = isolate(input$grid_spatial_gene),
272-
cont_colors = isolate(cont_colors()),
273-
image_id = isolate(input$imageid),
274-
alpha = isolate(input$alphalevel),
275-
point_size = isolate(input$pointsize),
276-
sample_order = isolate(input$gene_grid_samples),
277-
auto_crop = isolate(input$auto_crop),
278-
is_stitched = is_stitched
279-
)
280-
cowplot::plot_grid(
270+
gene_grid_warnings = NULL
271+
withCallingHandlers({
272+
plots <-
273+
vis_grid_gene(
274+
spe,
275+
geneid = isolate(input$geneid),
276+
multi_gene_method = input$multi_gene_method,
277+
assayname = isolate(input$assayname),
278+
minCount = isolate(input$minCount),
279+
return_plots = TRUE,
280+
spatial = isolate(input$grid_spatial_gene),
281+
cont_colors = isolate(cont_colors()),
282+
image_id = isolate(input$imageid),
283+
alpha = isolate(input$alphalevel),
284+
point_size = isolate(input$pointsize),
285+
sample_order = isolate(input$gene_grid_samples),
286+
auto_crop = isolate(input$auto_crop),
287+
is_stitched = is_stitched
288+
)
289+
}, warning = function(w) {
290+
gene_grid_warnings <<- c(gene_grid_warnings, conditionMessage(w))
291+
invokeRestart("muffleWarning")
292+
})
293+
294+
p_result = cowplot::plot_grid(
281295
plotlist = plots,
282296
nrow = isolate(input$gene_grid_nrow),
283297
ncol = isolate(input$gene_grid_ncol)
284298
)
299+
300+
return(list(p = p_result, gene_grid_warnings = gene_grid_warnings))
285301
})
286302

287303
editImg_manipulations <- reactive({
@@ -439,7 +455,7 @@ app_server <- function(input, output, session) {
439455
height = 8,
440456
width = 8 * ifelse(input$side_by_side_gene, 2, 1)
441457
)
442-
print(static_gene())
458+
print(static_gene()[['p']])
443459
dev.off()
444460
}
445461
)
@@ -467,7 +483,7 @@ app_server <- function(input, output, session) {
467483
height = 8 * isolate(input$gene_grid_nrow),
468484
width = 8 * isolate(input$gene_grid_ncol)
469485
)
470-
print(static_gene_grid())
486+
print(static_gene_grid()[['p']])
471487
dev.off()
472488
}
473489
)
@@ -530,17 +546,32 @@ app_server <- function(input, output, session) {
530546
height = "auto"
531547
)
532548

533-
534549
output$gene <- renderPlot(
535550
{
536-
static_gene()
551+
static_gene()[['p']]
537552
},
538553
width = function() {
539554
600 * ifelse(input$side_by_side_gene, 2, 1)
540555
},
541556
height = 600
542557
)
543558

559+
output$gene_warnings <- renderText({
560+
# Since 'static_gene()' is invoked twice (once also in the assignment
561+
# of 'output$gene'), we silence any errors that occur in this second
562+
# invocation to not duplicate error messages
563+
this_warning = NULL
564+
temp = try(
565+
{ this_warning = static_gene()[['gene_warning']] }, silent = TRUE
566+
)
567+
568+
if (!is.null(this_warning)) {
569+
paste("Warning:", this_warning)
570+
} else {
571+
""
572+
}
573+
})
574+
544575

545576
output$gene_grid_static <- renderUI({
546577
input$gene_grid_update
@@ -554,12 +585,29 @@ app_server <- function(input, output, session) {
554585

555586
output$gene_grid <- renderPlot(
556587
{
557-
print(static_gene_grid())
588+
print(static_gene_grid()[['p']])
558589
},
559590
width = "auto",
560591
height = "auto"
561592
)
562593

594+
output$gene_grid_warnings <- renderText({
595+
# Since 'static_gene_grid()' is invoked twice (once also in the
596+
# assignment of 'output$gene_grid'), we silence any errors that occur
597+
# in this second invocation to not duplicate error messages
598+
these_warnings = NULL
599+
temp = try(
600+
{ these_warnings = static_gene_grid()[['gene_grid_warnings']] },
601+
silent = TRUE
602+
)
603+
604+
if (!is.null(these_warnings)) {
605+
paste("Warnings:", paste(these_warnings, collapse = "; "))
606+
} else {
607+
""
608+
}
609+
})
610+
563611
output$editImg_plot <- renderPlot(
564612
{
565613
plot(editImg_manipulations())

R/app_ui.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ app_ui <- function() {
352352
tags$br(),
353353
tags$br(),
354354
tags$br(),
355-
tags$br()
355+
tags$br(),
356+
textOutput("gene_warnings")
356357
),
357358
tabPanel(
358359
"Gene (interactive)",
@@ -422,6 +423,7 @@ app_ui <- function() {
422423
actionButton("gene_grid_update", label = "Update grid plot"),
423424
downloadButton("downloadPlotGeneGrid", "Download PDF"),
424425
uiOutput("gene_grid_static"),
426+
textOutput("gene_grid_warnings"),
425427
helpText("Click the 'upgrade grid plot' button above to re-make this plot."),
426428
tags$br(),
427429
tags$br(),

R/multi_gene_z_score.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
#' @family functions for summarizing expression of multiple continuous variables simultaneously
1515
#' @keywords internal
1616
multi_gene_z_score <- function(cont_mat) {
17-
# Z-score calculation requires at least 2 features with nonzero variance.
17+
# Z-score calculation requires at least 1 feature with nonzero variance.
1818
# Verify this and drop any zero-variance features
1919
good_indices <- which(colSds(cont_mat, na.rm = TRUE) != 0)
20-
if (length(good_indices) < 2) {
21-
stop("After dropping features with no expression variation, less than 2 features were left. This error can occur when using data from only 1 spot.", call. = FALSE)
20+
if (length(good_indices) < 1) {
21+
stop("After dropping features with no expression variation, no features were left. This error can occur when using data from only 1 spot.", call. = FALSE)
2222
}
2323
if (ncol(cont_mat) - length(good_indices) > 0) {
2424
warning(

tests/testthat/test-multi_gene_z_score.R

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,32 @@ test_that(
1111
)
1212

1313
# NAs should be correctly removed from columns (as long as 2 non-NAs remain
14-
# in at least 2 columns), and the result should have no NAs
14+
# in at least 1 column), and the result should have no NAs
1515
cont_mat <- matrix(c(1, NA, 3, NA, 2, 0), ncol = 2)
1616
colnames(cont_mat) <- c("good1", "good2")
1717
expect_equal(any(is.na(multi_gene_z_score(cont_mat))), FALSE)
1818

19-
# With only one good column, an error should be thrown
19+
# With only one good column, the result should simply be the
20+
# Z-score-normalized good column. A warning should indicate which
21+
# columns were dropped
2022
cont_mat <- matrix(c(1, NA, 3, 4, 2, 2), ncol = 3)
2123
colnames(cont_mat) <- c("bad1", "good", "bad2")
24+
25+
temp = c(3, 4)
26+
expected_result = (temp - mean(temp)) / sd(temp)
27+
28+
expect_warning(
29+
{ actual_result = multi_gene_z_score(cont_mat) },
30+
"Dropping features\\(s\\) 'bad1', 'bad2' which have no expression variation"
31+
)
32+
expect_equal(actual_result, expected_result)
33+
34+
# An error should be thrown if no columns have variation
35+
cont_mat <- matrix(c(1, 1, 0, 0, 2, 2), ncol = 3)
36+
colnames(cont_mat) <- c("bad1", "bad2", "bad3")
2237
expect_error(
2338
multi_gene_z_score(cont_mat),
24-
"After dropping features with no expression variation, less than 2 features were left"
39+
"^After dropping features with no expression variation"
2540
)
2641
}
2742
)

0 commit comments

Comments
 (0)