Skip to content

Commit d67ac7b

Browse files
authored
Merge pull request #97 from Nick-Eagles/cap_vis_gene
Add `vis_gene()` parameter `cap_percentile` to allow capping maximum expression
2 parents 459af8e + 47ed001 commit d67ac7b

File tree

7 files changed

+89
-23
lines changed

7 files changed

+89
-23
lines changed

R/app_server.R

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ app_server <- function(input, output, session) {
227227
alpha = input$alphalevel,
228228
point_size = input$pointsize,
229229
auto_crop = input$auto_crop,
230-
is_stitched = is_stitched
230+
is_stitched = is_stitched,
231+
cap_percentile = input$cap_percentile
231232
)
232233
if (!input$side_by_side_gene) {
233234
p_result <- p
@@ -276,7 +277,8 @@ app_server <- function(input, output, session) {
276277
point_size = isolate(input$pointsize),
277278
sample_order = isolate(input$gene_grid_samples),
278279
auto_crop = isolate(input$auto_crop),
279-
is_stitched = is_stitched
280+
is_stitched = is_stitched,
281+
cap_percentile = isolate(input$cap_percentile)
280282
)
281283
},
282284
warning = function(w) {
@@ -725,6 +727,16 @@ app_server <- function(input, output, session) {
725727
plot_title <- paste(sampleid, "PC1 min >", minCount)
726728
}
727729
}
730+
731+
# Cap the expression values at the given percentile, if applicable
732+
if (input$cap_percentile < 1) {
733+
sorted_count = sort(d$COUNT)
734+
cap = sorted_count[
735+
as.integer(round(length(sorted_count) * input$cap_percentile))
736+
]
737+
d$COUNT[d$COUNT > cap] = cap
738+
}
739+
728740
d$COUNT[d$COUNT <= minCount] <- NA
729741

730742
## Add the reduced dims
@@ -1012,7 +1024,8 @@ app_server <- function(input, output, session) {
10121024
alpha = input$alphalevel,
10131025
point_size = input$pointsize,
10141026
auto_crop = input$auto_crop,
1015-
is_stitched = is_stitched
1027+
is_stitched = is_stitched,
1028+
cap_percentile = input$cap_percentile
10161029
) +
10171030
geom_point(
10181031
shape = 21,

R/app_ui.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ app_ui <- function() {
146146
"You can manually enter any number then press enter in your keyboard. This is useful for extreme values."
147147
),
148148
hr(),
149+
numericInput(
150+
inputId = "cap_percentile",
151+
label = "Maximum count percentile",
152+
value = 1,
153+
min = 0.05,
154+
max = 1,
155+
step = 0.05
156+
),
157+
helpText(
158+
"When set below 1, counts above the given percentile (e.g. 0.95 for 95th) are capped, typically to control outliers"
159+
),
160+
hr(),
149161
selectInput(
150162
inputId = "genecolor",
151163
label = "Gene color scale",

R/vis_gene.R

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
#' the proportion of continuous variables with positive values for each spot is
3737
#' computed. For more details, check the multi gene vignette at
3838
#' <https://research.libd.org/spatialLIBD/articles/multi_gene_plots.html>.
39+
#' @param cap_percentile A `numeric(1)` in (0, 1] determining the maximum
40+
#' percentile (as a proportion) at which to cap expression. For example, a value
41+
#' of 0.95 sets the top 5% of expression values to the 95th percentile value.
42+
#' This can help make the color scale more dynamic in the presence of high
43+
#' outliers. Defaults to `1`, which effectively performs no capping.
3944
#'
4045
#' @return A [ggplot2][ggplot2::ggplot] object.
4146
#' @export
@@ -128,12 +133,13 @@
128133
#' )
129134
#'
130135
#' ## Plot all white matter markers at once using the Z-score combination
131-
#' ## method
136+
#' ## method. Flatten this quantity at the top 5% of values for plotting
132137
#' p6 <- vis_gene(
133138
#' spe = spe,
134139
#' sampleid = "151507",
135140
#' geneid = white_matter_genes,
136-
#' multi_gene_method = "z_score"
141+
#' multi_gene_method = "z_score",
142+
#' cap_percentile = 0.95
137143
#' )
138144
#' print(p6)
139145
#'
@@ -158,22 +164,24 @@
158164
#' print(p8)
159165
#' }
160166
vis_gene <-
161-
function(spe,
162-
sampleid = unique(spe$sample_id)[1],
163-
geneid = rowData(spe)$gene_search[1],
164-
spatial = TRUE,
165-
assayname = "logcounts",
166-
minCount = 0,
167-
viridis = TRUE,
168-
image_id = "lowres",
169-
alpha = NA,
170-
cont_colors = if (viridis) viridisLite::viridis(21) else c("aquamarine4", "springgreen", "goldenrod", "red"),
171-
point_size = 2,
172-
auto_crop = TRUE,
173-
na_color = "#CCCCCC40",
174-
multi_gene_method = c("z_score", "pca", "sparsity"),
175-
is_stitched = FALSE,
176-
...) {
167+
function(
168+
spe,
169+
sampleid = unique(spe$sample_id)[1],
170+
geneid = rowData(spe)$gene_search[1],
171+
spatial = TRUE,
172+
assayname = "logcounts",
173+
minCount = 0,
174+
viridis = TRUE,
175+
image_id = "lowres",
176+
alpha = NA,
177+
cont_colors = if (viridis) viridisLite::viridis(21) else c("aquamarine4", "springgreen", "goldenrod", "red"),
178+
point_size = 2,
179+
auto_crop = TRUE,
180+
na_color = "#CCCCCC40",
181+
multi_gene_method = c("z_score", "pca", "sparsity"),
182+
is_stitched = FALSE,
183+
cap_percentile = 1,
184+
...) {
177185
multi_gene_method <- rlang::arg_match(multi_gene_method)
178186
# Verify existence and legitimacy of 'sampleid'
179187
if (
@@ -201,6 +209,11 @@ vis_gene <-
201209
)
202210
}
203211

212+
# Validate 'cap_percentile'
213+
if (cap_percentile <= 0 || cap_percentile > 1) {
214+
stop("'cap_percentile' must be in (0, 1]", call. = FALSE)
215+
}
216+
204217
spe_sub <- spe[, spe$sample_id == sampleid]
205218

206219
if (is_stitched) {
@@ -282,6 +295,16 @@ vis_gene <-
282295
legend_title <- paste("PC1\n min > ", minCount)
283296
}
284297
}
298+
299+
# Cap the expression values at the given percentile, if applicable
300+
if (cap_percentile < 1) {
301+
sorted_count = sort(d$COUNT)
302+
cap = sorted_count[
303+
as.integer(round(length(sorted_count) * cap_percentile))
304+
]
305+
d$COUNT[d$COUNT > cap] = cap
306+
}
307+
285308
d$COUNT[d$COUNT <= minCount] <- NA
286309

287310
p <- vis_gene_p(

R/vis_grid_gene.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ vis_grid_gene <-
5454
auto_crop = TRUE,
5555
na_color = "#CCCCCC40",
5656
is_stitched = FALSE,
57+
cap_percentile = 1,
5758
...) {
5859
stopifnot(all(sample_order %in% unique(spe$sample_id)))
5960

@@ -73,6 +74,7 @@ vis_grid_gene <-
7374
auto_crop = auto_crop,
7475
na_color = na_color,
7576
is_stitched = is_stitched,
77+
cap_percentile = cap_percentile,
7678
...
7779
)
7880
})

inst/app/www/documentation_spe.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Throughout the rest of this document, we'll refer to this object by the name `sp
5252
* `Image name`: the name of the background image to use. You can edit this image on the `Edit image` tab.
5353
* `Spot transparency level`: the transparency of the spots in the visualizations. It can be useful if the spot colors are blocking the background image.
5454
* `Minimum count value`: Values from the selected `continuous variable to plot` at or below this threshold will not be displayed.
55+
* `Maximum count percentile`: Sets the maximum count to display based on percentile, with acceptable values in (0, 1]. For example, a value of 0.95 sets the top 5% of counts to the value at the 95th percentile. This can be useful to make the color scale more dynamic in the presence of a small set of high outliers. This setting applies to plots of continuous variables only.
5556
* `Gene color scale`: Whether to use the color blind friendly palette (`viridis`) or to use a custom palette that we used for our `paper`. Other options from the [viridisLite R package](https://sjmgarnier.github.io/viridisLite/reference/viridis.html#details) are also supported.
5657
* `Gene color direction`: whether colors should be ordered from darkest to lightest or in the reverse direction.
5758

man/vis_gene.Rd

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/vis_grid_gene.Rd

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)