# make metagenes:
unique_cuts <- unique(cuts)
result_list <- vector("list", length(unique_cuts))
names(result_list) <- unique_cuts
for (i in seq_along(unique_cuts)) {
cutnum <- unique_cuts[i]
tempgenes <- names(cuts)[cuts == cutnum]
result_list[[i]] <- Matrix::rowMeans(norm.s[, tempgenes, drop = FALSE])
}
mscores <- do.call(cbind, result_list)
colnames(mscores) <- unique_cuts
for (i in 1:ncol(mscores)) {
colnames(mscores)[i] <- substr(paste0(names(cuts[cuts == i]), collapse = "_"), 1, 20)
}
# how often is the major metagene significant?
genes <- names(cuts)[cuts == 2]
rowMeans(ps[genes, ] < 1e-2)
## customized plots for 2 selected metagenes:
name = "ADM_ANGPTL4_BNIP3_EG"
xlim = c(6.75,8.25); ylim = c(0.75,2.25)
png(paste0(prolif_dir, "/Plots/patchDE metagene ", name,".png"),
res = 300, units = "in", width = 15, height = 7.5)
par(mar = c(0,0,0,0))
plot(xy21[use, ], pch = 16, cex = 0.25, asp = 1,
col = colorRampPalette(c("grey80", "#4B0082"))(100)[
round(1 + 100 * pmin(mscores[, name][use] / quantile(mscores[, name][use], 0.995), 1))
])
for (i in 1:length(qpolys)) {
plot(qpolys[[i]], add = TRUE, wpoints = F,
col = "cornflowerblue")
}
rect(xlim[1], ylim[1], xlim[2], ylim[2], border = "grey20")
dev.off()
png(paste0(prolif_dir, "/Plots/patchDE zoomed metagene ", name,".png"),
res = 300, units = "in", width = 4, height = 4)
par(mar = c(0,0,0,0))
plot(xy21[use, ], pch = 16, cex = 0.5, asp = 1,
col = colorRampPalette(c("grey80", "#4B0082"))(100)[
round(1 + 100 * pmin(mscores[, name][use] / quantile(mscores[, name][use], 0.995), 1))
],
xlim = xlim, ylim = ylim)
for (i in 1:length(qpolys)) {
plot(qpolys[[i]], add = TRUE, wpoints = F,
col = "cornflowerblue")
}
dev.off()
name = "CYP4F8_PIP"
xlim = c(4.25, 5.575); ylim = c(2.25,3.75)
png(paste0(prolif_dir, "/Plots/patchDE metagene ", name,".png"),
res = 300, units = "in", width = 15, height = 7.5)
par(mar = c(0,0,0,0))
plot(xy21[use, ], pch = 16, cex = 0.25, asp = 1,
col = colorRampPalette(c("grey80", "#4B0082"))(100)[
round(1 + 100 * pmin(mscores[, name][use] / quantile(mscores[, name][use], 0.995), 1))
])
for (i in 1:length(qpolys)) {
plot(qpolys[[i]], add = TRUE, wpoints = F,
col = "cornflowerblue")
}
rect(xlim[1], ylim[1], xlim[2], ylim[2], border = "grey20")
dev.off()
png(paste0(prolif_dir, "/Plots/patchDE zoomed metagene ", name,".png"),
res = 300, units = "in", width = 4, height = 4)
par(mar = c(0,0,0,0))
plot(xy21[use, ], pch = 16, cex = 0.5, asp = 1,
col = colorRampPalette(c("grey80", "#4B0082"))(100)[
round(1 + 100 * pmin(mscores[, name][use] / quantile(mscores[, name][use], 0.995), 1))
],
xlim = xlim, ylim = ylim)
for (i in 1:length(qpolys)) {
plot(qpolys[[i]], add = TRUE, wpoints = F,
col = "cornflowerblue")
}
dev.off()