-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotTotalCounts-methods.R
121 lines (110 loc) · 3.24 KB
/
plotTotalCounts-methods.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
#' @name plotTotalCounts
#' @inherit AcidGenerics::plotTotalCounts
#' @note Updated 2022-03-04.
#'
#' @inheritParams AcidRoxygen::params
#' @param ... Additional arguments.
#'
#' @examples
#' data(
#' RangedSummarizedExperiment,
#' SingleCellExperiment_splatter,
#' package = "AcidTest"
#' )
#'
#' ## SummarizedExperiment ====
#' object <- RangedSummarizedExperiment
#' plotTotalCounts(object)
#'
#' ## SingleCellExperiment ====
#' object <- SingleCellExperiment_splatter
#' plotTotalCounts(object)
NULL
## Updated 2021-09-10.
`plotTotalCounts,SE` <- # nolint
function(object,
assay = 1L,
interestingGroups = NULL,
perMillion = FALSE,
labels = list(
"title" = "Total counts",
"subtitle" = NULL,
"x" = NULL,
"y" = "counts"
),
flip) {
validObject(object)
assert(
isScalar(assay),
isFlag(perMillion),
isFlag(flip)
)
labels <- matchLabels(labels)
interestingGroups(object) <-
matchInterestingGroups(object, interestingGroups)
interestingGroups <- interestingGroups(object)
metricCol <- "totalCounts"
counts <- assay(object, i = assay)
data <- sampleData(object)
if (is(counts, "Matrix")) {
assert(requireNamespaces("Matrix"))
colSums <- Matrix::colSums
}
data[[metricCol]] <- colSums(counts)
if (isTRUE(perMillion)) {
data[[metricCol]] <- data[[metricCol]] / 1e6L
labels[["y"]] <- paste(labels[["y"]], "(per million)")
}
## Plot.
p <- ggplot(
data = as.data.frame(data),
mapping = aes(
x = .data[["sampleName"]],
y = .data[[metricCol]],
fill = .data[["interestingGroups"]]
)
) +
acid_geom_bar() +
acid_scale_y_continuous_nopad()
## Labels.
labels[["fill"]] <- paste(interestingGroups, collapse = ":\n")
p <- p + do.call(what = labs, args = labels)
## Color palette.
p <- p + acid_scale_fill_discrete()
## Flip.
if (isTRUE(flip)) {
p <- p + acid_discrete_coord_flip()
}
## Hide sample name legend.
if (identical(interestingGroups, "sampleName")) {
p <- p + guides(fill = "none")
}
## Return.
p
}
formals(`plotTotalCounts,SE`)[["flip"]] <- # nolint
.formalsList[["flip"]]
## Updated 2019-09-15.
`plotTotalCounts,SCE` <- # nolint
function(object, ...) {
plotTotalCounts(
object = aggregateCellsToSamples(object),
...
)
}
#' @describeIn plotTotalCounts Applies `aggregateCellsToSamples()` calculation
#' to summarize at sample level prior to plotting.\cr
#' Passes `...` to `SummarizedExperiment` method.
#' @export
setMethod(
f = "plotTotalCounts",
signature = signature(object = "SingleCellExperiment"),
definition = `plotTotalCounts,SCE`
)
#' @rdname plotTotalCounts
#' @export
setMethod(
f = "plotTotalCounts",
signature = signature(object = "SummarizedExperiment"),
definition = `plotTotalCounts,SE`
)