Skip to content
Snippets Groups Projects
Commit ade33a18 authored by nfontrod's avatar nfontrod
Browse files

R/plots.R: change in make_html_pca and make_pca and create_plots to handle more than 1 factor

parent 027e8413
No related branches found
No related tags found
No related merge requests found
......@@ -4,18 +4,47 @@
#' @param percent_var Vector containing the percentage of variance
#' of PC1 and PC2
#' @param output_folder Folder where the figure will be created
#' @param columns The design columns
#' @import plotly
#' @import htmlwidgets
make_html_pca <- function(pca_data, percent_var, output_folder) {
n <- length(unique(pca_data$condition))
make_html_pca <- function(pca_data, percent_var, output_folder, columns) {
n <- length(unique(pca_data[[columns[1]]]))
hues <- seq(15, 375, length = n + 1)
color_list <- hcl(h = hues, l = 65, c = 100)[1:n]
hover <- NULL
for (c in columns) {
hover <- paste0(hover, "<br>", c, " : ", pca_data[[c]])
}
pca_data$hover <- hover
if (length(columns) == 1) {
ppca <- plot_ly(
data = pca_data, x = ~PC1, y = ~PC2,
color = as.formula(paste0("~", columns[1])),
colors = color_list, type = "scatter",
text = ~hover, mode = "markers",
marker = list(size = 30)
)
} else if (length(columns) == 2) {
ppca <- plot_ly(
data = pca_data, x = ~PC1, y = ~PC2,
color = ~condition, colors = color_list, type = "scatter",
text = ~name, mode = "markers",
color = as.formula(paste0("~", columns[1])),
symbol = as.formula(paste0("~", columns[2])),
colors = color_list, type = "scatter",
text = ~hover, mode = "markers",
marker = list(size = 30)
) %>%
)
} else {
pca_data[["tmp"]] <- (as.numeric(pca_data[[columns[3]]]) * 7)
ppca <- plot_ly(
data = pca_data, x = ~PC1, y = ~PC2,
color = as.formula(paste0("~", columns[1])),
symbol = as.formula(paste0("~", columns[2])),
marker = list(size = pca_data$tmp),
colors = color_list, type = "scatter",
text = ~hover, mode = "markers"
)
}
ppca <- ppca %>%
layout(
title = paste0("PCA plot"),
margin = list(l = 100, r = 100, b = 100, t = 100, pad = 4),
......@@ -44,6 +73,59 @@ make_html_pca <- function(pca_data, percent_var, output_folder) {
}
#' Make ggplot PCA
#'
#' @param pca_data Object returned by DESeq2::plotPCA containing PCA table
#' @param percent_var Vector containing the percentage of variance
#' of PC1 and PC2
#' @param columns A vector containing the columns to display in PCA data
#' @param dds a DESeqDataSet object
#' @import ggplot2
make_pca <- function(pca_data, percent_var, columns, dds) {
if (length(columns) == 1) {
pggpca <- ggplot2::ggplot(pca_data, aes_string("PC1", "PC2", color = columns[1]))
} else if (length(columns) == 2) {
pggpca <- ggplot2::ggplot(
pca_data,
aes_string("PC1", "PC2", color = columns[1], shape = columns[2])
)
} else {
list_plots <- list()
my_combinations <- combn(columns, 2)
counter <- 1
for (c in seq_len(ncol(my_combinations))) {
cond1 <- my_combinations[1, c]
cond2 <- my_combinations[2, c]
i <- which(columns == cond1)
n <- length(unique(dds[[columns[i]]]))
hues <- seq(round(15 + 70 * (i - 1)), round(375 + 150 * (i - 1)),
length = n + 1
)
tmpcols <- hcl(h = hues, l = 65 - (i - 1) * 10, c = 100)[1:n]
tmp <- ggplot2::ggplot(
pca_data,
aes_string("PC1", "PC2", color = cond1, shape = cond2)
) +
scale_color_manual(values = tmpcols) +
geom_point(size = 3) +
xlab(paste0("PC1: ", percent_var[1], "% variance")) +
ylab(paste0("PC2: ", percent_var[2], "% variance")) +
coord_fixed()
list_plots[[counter]] <- tmp
counter <- counter + 1
}
return(list_plots)
}
if (length(columns) == 1 || length(columns) == 2) {
pggpca <- pggpca +
geom_point(size = 3) +
xlab(paste0("PC1: ", percent_var[1], "% variance")) +
ylab(paste0("PC2: ", percent_var[2], "% variance")) +
coord_fixed()
return(list(pggpca))
}
}
#' Plots of the experiment
#'
......@@ -55,17 +137,21 @@ make_html_pca <- function(pca_data, percent_var, output_folder) {
#' @import plotly
#' @import htmlwidgets
#' @import RColorBrewer
#' @import stringr
#' @export
create_plots <- function(dds, output_folder) {
my_formula <- paste(dds@design, collapse = "")
columns <- unique(str_split(str_replace_all(my_formula, "[~\\s]", ""),
"([\\+\\*\\:\\-\\^]|%in%)")[[1]])
rld <- DESeq2::rlogTransformation(dds, blind = TRUE)
n <- length(unique(dds$condition))
n <- length(unique(dds[[columns[1]]]))
hues <- seq(15, 375, length = n + 1)
condcols <- hcl(h = hues, l = 65, c = 100)[1:n]
counts_normalise <- DESeq2::counts(dds, normalized = T)
names(condcols) <- unique(dds$condition)
names(condcols) <- unique(dds[[columns[1]]])
pca_data <- DESeq2::plotPCA(rld,
intgroup = c("condition"),
intgroup = columns,
returnData = TRUE
)
percent_var <- round(100 * attr(pca_data, "percentVar"))
......@@ -75,37 +161,38 @@ create_plots <- function(dds, output_folder) {
colours <- colorRampPalette(rev(RColorBrewer::brewer.pal(9, "Blues")))(255)
cts_norm_df <- as.data.frame(counts_normalise)
annotation_col <- data.frame(condition = dds$condition)
rownames(annotation_col) <- colnames(dds)
mat_colors <- list(
condition = condcols
annotation_col <- data.frame("row_names" = colnames(dds))
mat_colors <- NULL
for (i in seq_len(length(columns))) {
n <- length(unique(dds[[columns[i]]]))
hues <- seq(round(15 + 70 * (i - 1)), round(375 + 150 * (i - 1)),
length = n + 1
)
names(mat_colors$condition) <- sort(unique(dds$condition))
tmpcols <- hcl(h = hues, l = 65 - (i - 1) * 10, c = 100)[1:n]
mat_colors[[columns[i]]] <- tmpcols
names(mat_colors[[columns[i]]]) <- sort(unique(dds[[columns[i]]]))
annotation_col[columns[i]] <- dds[[columns[i]]]
}
rownames(annotation_col) <- colnames(dds)
annotation_col$row_names <- NULL
pdf(paste0(output_folder, "/plots_general_view.pdf"))
barplot(colSums(DESeq2::counts(dds, normalized = F)),
col = condcols[as.factor(dds$condition)],
col = condcols[as.factor(dds[[columns[1]]])],
las = 2, cex.names = 0.4,
main = "Pre Normalised Counts"
)
barplot(colSums(DESeq2::counts(dds, normalized = T)),
col = condcols[as.factor(dds$condition)],
col = condcols[as.factor(dds[[columns[1]]])],
las = 2, cex.names = 0.4,
main = "Post Normalised Counts"
)
pggpca <- ggplot2::ggplot(pca_data, aes(PC1, PC2, color = condition)) +
geom_point(size = 3) +
xlab(paste0("PC1: ", percent_var[1], "% variance")) +
ylab(paste0("PC2: ", percent_var[2], "% variance")) +
coord_fixed() +
geom_label_repel(
data = pca_data, alpha = 0.7,
mapping = aes(PC1, PC2, label = name), size = 1.2,
)
plot(pggpca)
pca_plots <- make_pca(pca_data, percent_var, columns, dds)
for (p_pca in pca_plots) {
print(p_pca)
}
gplots::heatmap.2(sample_dist_matrix,
trace = "none", col = colours,
......@@ -119,9 +206,8 @@ create_plots <- function(dds, output_folder) {
annotation_colors = mat_colors, angle_col = "45"
)
DESeq2::plotDispEsts(dds)
dev.off()
make_html_pca(pca_data, percent_var, output_folder)
make_html_pca(pca_data, percent_var, output_folder, columns)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment