forked from GabrielHoffman/dreamlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotVoom.R
154 lines (131 loc) · 3.93 KB
/
plotVoom.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Gabriel Hoffman
# April 6, 2021
#
# plot voom applied to pseudo-bulk from each cell type
#' Plot voom curves from each cell type
#'
#' Plot voom curves from each cell type
#'
#' @param x dreamletProcessedData
#' @param ncol number of columns in the plot
#' @param alpha transparency of points
#' @param assays which assays to plot
#' @param ... other arguments
#'
#' @return Plot of mean-variance trend
#'
#' @examples
#' library(muscat)
#' library(SingleCellExperiment)
#'
#' data(example_sce)
#'
#' # create pseudobulk for each sample and cell cluster
#' pb <- aggregateToPseudoBulk(example_sce,
#' assay = "counts",
#' cluster_id = "cluster_id",
#' sample_id = "sample_id",
#' verbose = FALSE
#' )
#'
#' # voom-style normalization
#' res.proc <- processAssays(pb, ~group_id)
#'
#' # Show mean-variance trend from voom
#' plotVoom(res.proc)
#'
#' # plot for first two cell types
#' plotVoom(res.proc[1:2])
#'
#' @export
#' @docType methods
#' @rdname plotVoom-methods
setGeneric(
"plotVoom",
function(x, ncol = 3, alpha = .5, ...) {
standardGeneric("plotVoom")
}
)
#' @rdname plotVoom-methods
#' @aliases plotVoom,dreamletProcessedData,dreamletProcessedData-method
setMethod(
"plotVoom", "dreamletProcessedData",
function(x, ncol = 3, alpha = .5, assays = names(x)) {
# Pass R CMD check
y <- NULL
# intersect preserving order from assays
assays <- intersect(assays, names(x))
if (length(assays) == 0) stop("No valid assays selected")
# get common range across all plots
###################################
df_range <- lapply(assays, function(id) {
if (!is.null(x[[id]]$voom.xy)) {
res <- with(x[[id]]$voom.xy, data.frame(range(x), range(y), id = id))
} else {
res <- NULL
}
res
})
df_range <- do.call(rbind, df_range)
if (is.null(df_range)) {
stop("Voom was not run on this object")
}
xlim <- range(df_range$range.x.)
ylim <- c(0, max(df_range$range.y.))
xlab <- bquote(log[2](counts + 0.5))
ylab <- bquote(sqrt(standard ~ deviation))
# only included assays were voom succeeded
validAssays <- droplevels(factor(unique(df_range)$id, assays))
# make data.frame of points
df.list <- lapply(levels(validAssays), function(id) {
with(x[[id]]$voom.xy, data.frame(id, x, y))
})
df_points <- do.call(rbind, df.list)
df_points$id <- droplevels(factor(df_points$id, assays))
df_points <- df_points[order(df_points$id), ]
# make data.frame of curves
df.list <- lapply(levels(validAssays), function(id) {
with(x[[id]]$voom.line, data.frame(id, x, y))
})
df_curve <- do.call(rbind, df.list)
ggplot(df_points, aes(x, y)) +
geom_point(size = 0.1, alpha = alpha) +
theme_classic() +
theme(aspect.ratio = 1, plot.title = element_text(hjust = 0.5)) +
facet_wrap(~id, ncol = ncol) +
xlab(xlab) +
ylab(ylab) +
xlim(xlim) +
ylim(ylim) +
geom_line(data = df_curve, aes(x, y), color = "red")
}
)
#' @rdname plotVoom-methods
#' @aliases plotVoom,list,list-method
setMethod(
"plotVoom", "EList",
function(x, ncol = 3, alpha = .5) {
# Pass R CMD check
y <- NULL
# get common range across all plots
###################################
df_range <- with(x$voom.xy, data.frame(range(x), range(y)))
xlim <- range(df_range$range.x.)
ylim <- c(0, max(df_range$range.y.))
xlab <- bquote(log[2](counts + 0.5))
ylab <- bquote(sqrt(standard ~ deviation))
# make data.frame of points
df_points <- with(x$voom.xy, data.frame(x, y))
# make data.frame of curves
df_curve <- with(x$voom.line, data.frame(x, y))
ggplot(df_points, aes(x, y)) +
geom_point(size = 0.1, alpha = alpha) +
theme_classic() +
theme(aspect.ratio = 1, plot.title = element_text(hjust = 0.5)) +
xlab(xlab) +
ylab(ylab) +
xlim(xlim) +
ylim(ylim) +
geom_line(data = df_curve, aes(x, y), color = "red")
}
)