Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 90 additions & 12 deletions R/annotate_integrated_sce.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ annotate_integrated_sce <- function(sce,
"\r\n"
)

sce@metadata$dataset_integration$annotation$input_reduced_dim <- input_reduced_dim
sce@metadata$dataset_integration$annotation$input_reduced_dim <-
input_reduced_dim

pca_reducedDim_plots <- list()
liger_reducedDim_plots <- list()
Expand All @@ -77,24 +78,22 @@ annotate_integrated_sce <- function(sce,
feature_dim = variable,
reduced_dim = sprintf("%s_PCA",
input_reduced_dim),
size = .1, alpha = 0.2)
size = 1, alpha = 0.3)
# reducedDim_pca <- plot_pca +
# ggtitle(sprintf("%s using PCA data", input_reduced_dim)) +
# theme(plot.title = element_text(size = 15, face = "bold", hjust = 0.8))
# pca_reducedDim_plots[[variable]] <- reducedDim_pca
plot_pca <- .grobify_ggplot(plot_pca)
pca_reducedDim_plots[[variable]] <- plot_pca

plot_liger <- plot_reduced_dim(sce,
feature_dim = variable,
reduced_dim = sprintf("%s_Liger",
input_reduced_dim),
size = 0.1, alpha = 0.2)
size = 0.75, alpha = 0.3)
# reducedDim_liger <- plot_liger +
# ggtitle(sprintf("%s using LIGER data", input_reduced_dim)) +
# theme(plot.title = element_text(size = 15, face = "bold", hjust = 0.8))
# liger_reducedDim_plots[[variable]] <- reducedDim_liger
plot_liger <- .grobify_ggplot(plot_liger)
liger_reducedDim_plots[[variable]] <- plot_liger

data <- SingleCellExperiment::reducedDim(sce, "PCA")
Expand Down Expand Up @@ -133,7 +132,6 @@ annotate_integrated_sce <- function(sce,
) +
ggtitle("kBET test results - PCA") +
theme(plot.title = element_text(hjust = 0.5, size = 15, face = "bold"))
kbet_pca <- .grobify_ggplot(kbet_pca)
pca_kbet_plots[[variable]] <- kbet_pca

data <- SingleCellExperiment::reducedDim(sce, "Liger")
Expand Down Expand Up @@ -174,7 +172,6 @@ annotate_integrated_sce <- function(sce,
) +
ggtitle("kBET test results - LIGER") +
theme(plot.title = element_text(hjust = 0.5, size = 15, face = "bold"))
kbet_liger <- .grobify_ggplot(kbet_liger)
liger_kbet_plots[[variable]] <- kbet_liger
}

Expand All @@ -196,35 +193,116 @@ annotate_integrated_sce <- function(sce,
)

cli::cli_text("Generating tSNE/UMAP plots for PCA and LIGER data...")
sce <- cluster_sce(sce, k = 50, reduction_method = sprintf("%s_PCA",
input_reduced_dim))
pca_clusters <- sce$clusters

plot_pca <- plot_reduced_dim(sce,
feature_dim = "clusters",
reduced_dim = sprintf("%s_PCA",
input_reduced_dim),
size = 0.1, alpha = 0.2, label_clusters = TRUE
size = 0.75, alpha = 0.3, label_clusters = TRUE
)
# cluster_pca <- plot_pca +
# ggtitle(sprintf("%s using PCA data (colored by cluster)",
# input_reduced_dim)) +
# theme(plot.title = element_text(size = 15, face = "bold"))

cluster_pca <- plot_pca
cluster_pca <- .grobify_ggplot(cluster_pca)
sce@metadata$dataset_integration$clustering_plots$cluster_pca <- cluster_pca

sce <- cluster_sce(sce, k = 50, reduction_method = sprintf("%s_Liger",
input_reduced_dim))

liger_clusters <- sce$clusters

plot_liger <- plot_reduced_dim(sce,
feature_dim = "clusters",
reduced_dim = sprintf("%s_Liger",
input_reduced_dim),
size = 0.1, alpha = 0.2,
size = 0.75, alpha = 0.3,
label_clusters = TRUE
)
# cluster_liger <- plot_liger +
# ggtitle(sprintf("%s using LIGER data (colored by cluster)",
# input_reduced_dim)) +
# theme(plot.title = element_text(size = 15, face = "bold"))
cluster_liger <- plot_liger
cluster_liger <- .grobify_ggplot(cluster_liger)

sce@metadata$dataset_integration$clustering_plots$cluster_liger <- cluster_liger
sce@metadata$dataset_integration$clustering_plots$cluster_liger <-
cluster_liger

sankey_data <- as.data.frame.matrix(table(pca_clusters, liger_clusters)) %>%
rownames_to_column %>%
tidyr::gather(key = "key", value = "value", -rowname) %>%
dplyr::filter(value > 0)
colnames(sankey_data) <- c("pca_cluster", "liger_cluster", "overlap")
sankey_data$liger_cluster <- paste(sankey_data$liger_cluster, " ", sep = "")
nodes <- data.frame(name = c(as.character(sankey_data$pca_cluster),
as.character(sankey_data$liger_cluster)) %>%
unique())
sankey_data$pca_cluster_id <-
match(sankey_data$pca_cluster, nodes$name) - 1
sankey_data$liger_cluster_id <-
match(sankey_data$liger_cluster, nodes$name) - 1
my_colour_scale <-
'd3.scaleOrdinal() .range(["red","cornflowerblue","green","blue","darkorange","grey"])'
sankey_plot <- networkD3::sankeyNetwork(Links = sankey_data, Nodes = nodes,
Source = "pca_cluster_id",
Target = "liger_cluster_id",
Value = "overlap", NodeID = "name",
sinksRight = TRUE,
colourScale = my_colour_scale,
nodeWidth = 80, fontSize = 13,
nodePadding = 20)

sce@metadata$dataset_integration$clustering_plots$sankey_plot <- sankey_plot

pca_proportional_barplots <- list()
liger_proportional_barplots <- list()

for (variable in categorical_covariates) {

pca_barplot_data <-
data.frame("clusters" = pca_clusters, "group" = sce@colData[[variable]])
clusters <- pca_barplot_data$clusters
group <- pca_barplot_data$group

pca_barplot <- ggplot(pca_barplot_data, aes(x = clusters, fill = group)) +
geom_bar(position = "fill") + theme_bw() +
theme(
axis.text = element_text(size = 13),
axis.title = element_text(size = 13),
axis.line = element_line(colour = "black"),
panel.grid.minor = element_blank(),
panel.background = element_blank()) +
theme(legend.text = element_text(size = 13))

pca_proportional_barplots[[variable]] <- pca_barplot

liger_barplot_data <-
data.frame("clusters" = liger_clusters,
"group" = sce@colData[[variable]])
clusters <- liger_barplot_data$clusters
group <- liger_barplot_data$group

liger_barplot <-
ggplot(liger_barplot_data, aes(x = clusters, fill = group)) +
geom_bar(position = "fill") + theme_bw() +
theme(
axis.text = element_text(size = 13),
axis.title = element_text(size = 13),
axis.line = element_line(colour = "black"),
panel.grid.minor = element_blank(),
panel.background = element_blank()) +
theme(legend.text = element_text(size = 13))

liger_proportional_barplots[[variable]] <- liger_barplot
}
sce@metadata$dataset_integration$clustering_plots$pca_proportional_barplots <-
pca_proportional_barplots
sce@metadata$dataset_integration$clustering_plots$liger_proportional_barplots <-
liger_proportional_barplots

return(sce)
}
1 change: 1 addition & 0 deletions R/liger_preprocess.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ liger_preprocess <- function(sce,
manifests <- unique(sce@colData[, unique_id_var])
for (mnft in manifests) {
dataset_name <- paste0("dataset_", mnft)
cli::cli_text("Extracting {.val {dataset_name}}")
dataset_list[[dataset_name]] <-
sce[, sce[[unique_id_var]] == mnft]
mat_list[[dataset_name]] <-
Expand Down
16 changes: 8 additions & 8 deletions R/report_integrated_sce.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ report_integrated_sce <- function(sce,
"rmarkdown/templates/integrate/skeleton/skeleton.Rmd",
package = "scFlow"),
params = list(
metadata_path = metadata_tmp_path,
categorical_covariates = categorical_covariates
),
output_dir = report_folder_path,
output_file = report_file,
knit_root_dir = krd,
intermediates_dir = intd,
quiet = TRUE
metadata_path = metadata_tmp_path,
categorical_covariates = categorical_covariates
),
output_dir = report_folder_path,
output_file = report_file,
knit_root_dir = krd,
intermediates_dir = intd,
quiet = TRUE
)
cli::cli_text(c(
"Report successfully generated: ",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## `r variable`

<div class = "row">
<div class="col-md-6">
<div class = "basicfig">
```{r, fig.align="center", fig.width=4.5, fig.height=4, fig.cap = sprintf("<b>Proportional barplot by %s</b>", variable)}
knitr::opts_chunk$set(echo = FALSE)
metadata$dataset_integration$clustering_plots$pca_proportional_barplots[[variable]]
```
</div>
</div>
<div class="col-md-6">
<div class = "basicfig">
```{r, fig.align="center", fig.width=4.5, fig.height=4, fig.cap = sprintf("<b>Proportional barplot (LIGER) by %s</b>", variable)}
knitr::opts_chunk$set(echo = FALSE)
metadata$dataset_integration$clustering_plots$liger_proportional_barplots[[variable]]
```
</div>
</div>
</div>
17 changes: 17 additions & 0 deletions inst/rmarkdown/templates/integrate/skeleton/skeleton.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,24 @@ paste(out, collapse='\n')

```{r conditional_clustering_block, child='clustering.Rmd', eval = !is.null(metadata$cluster_params)}
```
</div>
</div>
```{r, results='asis', include=FALSE, echo=FALSE}
knitr::opts_chunk$set(echo = FALSE)

show_plot <- function(plot_object) {
div(style="margin:auto;text-align:center", plot_object)
}

out <- NULL
for (variable in names(metadata$dataset_integration$clustering_plots$liger_proportional_barplots)) {
out <- c(out, knitr::knit_child("categorical_covariates_2.Rmd"))
}
paste(out, collapse='\n')
```
`r paste(out, collapse='\n')`
</div>
</div>
# References {-}
<div id="refs"></div>

Expand Down