-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodeReducedDim.R
141 lines (130 loc) · 4.69 KB
/
modeReducedDim.R
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
#' App pre-configured to compare multiple reduced dimension plots
#'
#' This mode launches a Shiny App preconfigured with multiple linked reduced
#' dimension plots for interactive data exploration of the
#' \code{\link{SingleCellExperiment}} object.
#'
#' @param se An object that coercible to \linkS4class{SingleCellExperiment}
#' @param includeNames Character vector with the names of reduced dimensions
#' to display as individual panels. The default uses all available in
#' \code{reducedDimNames(se)}.
#' @param colorBy Character scalar controlling coloring of cells. Must match either
#' to one of \code{colnames(colData(se))} or \code{rownames(se)}. If coloring
#' by a colData column, a column data plot is opened in addition to the
#' reduced dimension panels. If coloring by a feature, a row statistics table
#' is openend in addition to the reduced dimension panels, from which the
#' latter are receiving the color.
#' @param ... Additional arguments passed to \code{\link{iSEE}}.
#' @param plotWidth The grid width of linked plots (numeric vector of length
#' either 1 or equal to \code{length(includeNames)}). The total width of
#' the window is 12, so \code{plotWidth = 4} for example will show three
#' panels per row. If \code{plotWidth = NULL} (the default), a value will be
#' estimated depending on the number of reduced dimension panels.
#'
#' @return A Shiny app object is returned.
#'
#' @export
#' @importFrom methods is
#' @importFrom SingleCellExperiment reducedDimNames colData
#' @importFrom iSEE ReducedDimensionPlot ColumnDataPlot RowDataTable iSEE
#'
#' @examples
#' library(scRNAseq)
#'
#' # Example data ----
#' sce <- ReprocessedAllenData(assays="tophat_counts")
#' class(sce)
#'
#' library(scater)
#' sce <- logNormCounts(sce, exprs_values="tophat_counts")
#' sce <- runPCA(sce, ncomponents = 30)
#' sce <- runTSNE(sce)
#' sce <- runUMAP(sce)
#' reducedDimNames(sce)
#'
#' # launch the app ----
#' # ... coloring by a column data variable
#' app <- modeReducedDim(sce, colorBy = "Primary.Type")
#' if (interactive()) {
#' shiny::runApp(app, port=1234)
#' }
#' # ... coloring by a feature
#' app <- modeReducedDim(sce, colorBy = "Scnn1a")
#' if (interactive()) {
#' shiny::runApp(app, port=1234)
#' }
modeReducedDim <- function(
se,
includeNames = reducedDimNames(se),
colorBy = NULL,
...,
plotWidth = NULL) {
# This mode is only for SingleCellExperiments
stopifnot(exprs = {
is(se, "SingleCellExperiment")
is.character(includeNames)
length(includeNames) > 0L
all(includeNames %in% reducedDimNames(se))
is.null(plotWidth) ||
(is.numeric(plotWidth) && (length(plotWidth) == 1L || length(plotWidth) == length(includeNames)))
is.null(colorBy) ||
(is.character(colorBy) && length(colorBy) == 1L &&
(colorBy %in% colnames(colData(se)) || colorBy %in% rownames(se)))
})
# determine plotWidth
n <- length(includeNames)
if (is.null(plotWidth)) {
nr <- floor(sqrt(n))
nc <- ceiling(n / nr)
plotWidth <- 12 / max(nc, 3)
}
initial <- lapply(seq_len(n), function(i) {
iSEE::ReducedDimensionPlot(
Type = includeNames[i],
PanelWidth = as.integer(plotWidth)
)
})
# add color
if (!is.null(colorBy)) {
if (colorBy %in% colnames(colData(se))) {
# coloring of reduced dimension panels
initial <- lapply(initial, function(rdp) {
rdp[["ColorBy"]] <- "Column data"
rdp[["ColorByColumnData"]] <- colorBy
rdp
})
cdp <- list(
iSEE::ColumnDataPlot(
YAxis = colorBy,
XAxis = "None",
ColorBy = "Column data",
ColorByColumnData = colorBy,
PanelWidth = as.integer(plotWidth[1])
)
)
initial <- c(initial, cdp)
} else {
# coloring of reduced dimension panels
initial <- lapply(initial, function(rdp) {
rdp[["ColorBy"]] <- "Feature name"
rdp[["ColorByFeatureSource"]] <- "RowDataTable1"
rdp
})
rdt <- list(
iSEE::RowDataTable(
Selected = colorBy,
Search = colorBy,
PanelWidth = as.integer(plotWidth[1])
)
)
initial <- c(initial, rdt)
}
}
# Preconfigure an app
app <- iSEE::iSEE(
se = se,
initial = initial,
...
)
return(app)
}