|
a |
|
b/R/PlotMBpca.R |
|
|
1 |
#' @title Plotting pseudo-time ordering or gene expression in Model-based |
|
|
2 |
#' clustering in PCA |
|
|
3 |
#' @description The PCA representation can either be used to show pseudo-time |
|
|
4 |
#' ordering or the gene expression of a particular gene. |
|
|
5 |
#' @param object \code{DISCBIO} class object. |
|
|
6 |
#' @param type either `order` to plot pseudo-time ordering or `exp` to plot gene |
|
|
7 |
#' expression |
|
|
8 |
#' @param g Individual gene name or vector with a group of gene names |
|
|
9 |
#' corresponding to a subset of valid row names of the \code{ndata} slot of |
|
|
10 |
#' the \code{DISCBIO} object. Ignored if `type="order"`. |
|
|
11 |
#' @param n String of characters representing the title of the plot. Default is |
|
|
12 |
#' NULL and the first element of \code{g} is chosen. Ignored if |
|
|
13 |
#' `type="order"`. |
|
|
14 |
#' @importFrom RColorBrewer brewer.pal |
|
|
15 |
#' @importFrom grDevices colorRampPalette |
|
|
16 |
#' @importFrom graphics layout par image |
|
|
17 |
#' @return A plot of the PCA. |
|
|
18 |
#' @export |
|
|
19 |
|
|
|
20 |
PlotMBpca <- function(object, type = "order", g = NULL, n = NULL) { |
|
|
21 |
# ========================================================================== |
|
|
22 |
# Validation |
|
|
23 |
# ========================================================================== |
|
|
24 |
data <- object@MBclusters |
|
|
25 |
if (type == "exp") { |
|
|
26 |
if (is.null(g)) { |
|
|
27 |
stop('g must be provided if type="exp"') |
|
|
28 |
} |
|
|
29 |
if (length(intersect(g, rownames(object@ndata))) < length(unique(g))) { |
|
|
30 |
stop( |
|
|
31 |
"second argument does not correspond to set of rownames slot", |
|
|
32 |
"ndata of SCseq object" |
|
|
33 |
) |
|
|
34 |
} |
|
|
35 |
if (is.null(n)) { |
|
|
36 |
n <- g[1] |
|
|
37 |
} |
|
|
38 |
logObj <- log(object@ndata) |
|
|
39 |
l <- apply(logObj[g, ] - .1, 2, sum) + .1 |
|
|
40 |
x <- data$pcareduceres |
|
|
41 |
} else if (type == "order") { |
|
|
42 |
MBordertable <- cbind(data$pcareduceres, object@MBordering) |
|
|
43 |
l <- MBordertable[, 3] |
|
|
44 |
x <- MBordertable |
|
|
45 |
} else { |
|
|
46 |
stop("Invalid type. Valid alternatives as 'order' and 'exp'.") |
|
|
47 |
} |
|
|
48 |
# ========================================================================== |
|
|
49 |
# Plotting |
|
|
50 |
# ========================================================================== |
|
|
51 |
mi <- min(l, na.rm = TRUE) |
|
|
52 |
ma <- max(l, na.rm = TRUE) |
|
|
53 |
ColorRamp <- colorRampPalette(rev(brewer.pal(n = 11, name = "RdYlBu")))(100) |
|
|
54 |
ColorLevels <- seq(mi, ma, length = length(ColorRamp)) |
|
|
55 |
v <- round((l - mi) / (ma - mi) * 99 + 1, 0) |
|
|
56 |
layout( |
|
|
57 |
matrix( |
|
|
58 |
data = c(1, 3, 2, 4), |
|
|
59 |
nrow = 2, |
|
|
60 |
ncol = 2 |
|
|
61 |
), |
|
|
62 |
widths = c(5, 1, 5, 1), |
|
|
63 |
heights = c(5, 1, 1, 1) |
|
|
64 |
) |
|
|
65 |
opar <- withr::local_par(mar = c(5, 5, 2.5, 2)) |
|
|
66 |
on.exit(withr::local_par(opar)) |
|
|
67 |
plot( |
|
|
68 |
x[, 1], |
|
|
69 |
x[, 2], |
|
|
70 |
xlab = "PC1", |
|
|
71 |
ylab = "PC2", |
|
|
72 |
pch = 20, |
|
|
73 |
cex = 0, |
|
|
74 |
col = "grey", |
|
|
75 |
las = 1, |
|
|
76 |
main = n |
|
|
77 |
) |
|
|
78 |
for (k in seq_len(length(v))) { |
|
|
79 |
points( |
|
|
80 |
x[k, 1], |
|
|
81 |
x[k, 2], |
|
|
82 |
col = ColorRamp[v[k]], |
|
|
83 |
pch = 20, |
|
|
84 |
cex = 2 |
|
|
85 |
) |
|
|
86 |
} |
|
|
87 |
opar <- withr::local_par(mar = c(3, 2.5, 2.5, 2)) |
|
|
88 |
on.exit(withr::local_par(opar)) |
|
|
89 |
image( |
|
|
90 |
1, |
|
|
91 |
ColorLevels, |
|
|
92 |
matrix( |
|
|
93 |
data = ColorLevels, |
|
|
94 |
ncol = length(ColorLevels), |
|
|
95 |
nrow = 1 |
|
|
96 |
), |
|
|
97 |
col = ColorRamp, |
|
|
98 |
xlab = "", |
|
|
99 |
ylab = "", |
|
|
100 |
las = 2, |
|
|
101 |
xaxt = "n" |
|
|
102 |
) |
|
|
103 |
layout(1) |
|
|
104 |
} |