Skip to content

Commit

Permalink
update paper figs w paths/load libs
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Geuenich committed Jun 4, 2023
1 parent 6e8f7ef commit e39fcf1
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 212 deletions.
15 changes: 8 additions & 7 deletions pipeline/figures/AR-params.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
suppressPackageStartupMessages(
suppressPackageStartupMessages({
library(tidyverse)
)
library(patchwork)
})
source("pipeline/whatsthatcell-helpers.R")


cytof_acc <- read_tsv("output/v7/results/overall-CyTOF-benchmarking-accuracies.tsv") |>
cytof_acc <- read_tsv(snakemake@input$cytof_acc) |>
mutate(cohort = "CyTOF")
scrna_acc <- read_tsv("output/v7/results/overall-scRNASeq-benchmarking-accuracies.tsv") |>
scrna_acc <- read_tsv(snakemake@input$scrna_acc) |>
mutate(cohort = "scRNASeq")
snrna_acc <- read_tsv("output/v7/results/overall-snRNASeq-benchmarking-accuracies.tsv") |>
snrna_acc <- read_tsv(snakemake@input$snrna_acc) |>
mutate(cohort = "snRNASeq")

acc <- bind_rows(cytof_acc, scrna_acc, snrna_acc)
Expand Down Expand Up @@ -42,15 +43,15 @@ plot_knn_res <- function(acc, fill, cohort){
}


pdf("output/v7/paper-figures/supp-AR-res.pdf", height = 14, width = 9)
pdf(snakemake@output$res, height = 14, width = 9)
(plot_knn_res(cytof_acc, "res", "CyTOF") /
plot_knn_res(scrna_acc, "res", "scRNASeq") /
plot_knn_res(snrna_acc, "res", "snRNASeq")) +
plot_layout(guides = "collect", heights = c(1, 2, 2))
dev.off()


pdf("output/v7/paper-figures/supp-AR-knn.pdf", height = 14, width = 9)
pdf(snakemake@output$knn, height = 14, width = 9)
(plot_knn_res(cytof_acc, "knn", "CyTOF") /
plot_knn_res(scrna_acc, "knn", "scRNASeq") /
plot_knn_res(snrna_acc, "knn", "snRNASeq")) +
Expand Down
12 changes: 6 additions & 6 deletions pipeline/figures/cell-type-similarity.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
library(tidyverse)
library(ComplexHeatmap)

cytof_sim <- read_tsv("output/v7/results/cell-type-similarity/similarity-CyTOF-seed-0.tsv")
scrna_sim <- read_tsv("output/v7/results/cell-type-similarity/similarity-scRNASeq-seed-0.tsv")
snrna_sim <- read_tsv("output/v7/results/cell-type-similarity/similarity-snRNASeq-seed-0.tsv")
cytof_sim <- read_tsv(snakemake@input$cytof_sim)
scrna_sim <- read_tsv(snakemake@input$scrna_sim)
snrna_sim <- read_tsv(snakemake@input$snrna_sim)

plot_dist_mat <- function(df){
pivot_wider(df, names_from = "cell_type2", values_from = "cosine_similarity") |>
Expand All @@ -14,14 +14,14 @@ plot_dist_mat <- function(df){
Heatmap(name = "Cosine\nsimilarity")
}

pdf("output/v7/paper-figures/Supp-cell-type-sim-scRNASeq.pdf", height = 5, width = 5.5)
pdf(snakemake@input$scrna, height = 5, width = 5.5)
plot_dist_mat(scrna_sim)
dev.off()

pdf("output/v7/paper-figures/Supp-cell-type-sim-snRNASeq.pdf", height = 5, width = 5.5)
pdf(snakemake@input$snrna, height = 5, width = 5.5)
plot_dist_mat(snrna_sim)
dev.off()

pdf("output/v7/paper-figures/Supp-cell-type-sim-CyTOF.pdf", height = 5, width = 5.5)
pdf(snakemake@input$cytof, height = 5, width = 5.5)
plot_dist_mat(cytof_sim)
dev.off()
45 changes: 19 additions & 26 deletions pipeline/figures/compare-lr-rf-random-and-ranking.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ suppressPackageStartupMessages({
library(magick)
library(ggpubr)
})
devtools::load_all("/ggplot2")
source("pipeline/whatsthatcell-helpers.R")

## Part A: Proportion of cells initially selected
get_proportion_rep <- function(sce_path, selected_cells_path, cohort){
get_proportion_rep <- function(sce_path, rand_files, rank_files, cohort){
sce <- readRDS(sce_path)

cell_types <- unique(sce$cell_type)
Expand All @@ -18,13 +19,6 @@ get_proportion_rep <- function(sce_path, selected_cells_path, cohort){
cell_types <- unique(sce$CellType)
}

rand_files <- list.files(selected_cells_path,
pattern = "random",
full.names = TRUE)
rank_files <- list.files(selected_cells_path,
pattern = "ranking",
full.names = TRUE)

random <- lapply(rand_files, function(x){
sel_cells <- read_tsv(x) |>
filter(iteration == 0) |>
Expand All @@ -48,14 +42,17 @@ get_proportion_rep <- function(sce_path, selected_cells_path, cohort){
}

props <- bind_rows(
get_proportion_rep("data/CyTOF/CyTOF-train-seed-0.rds",
"output/v8/data/CyTOF/Active-Learning_entropy/AL-batches-subset",
get_proportion_rep(snakemake@input$cytof,
snakemake@input$cytof_AL_rand,
snakemake@input$cytof_AL_rank,
"CyTOF"),
get_proportion_rep("data/scRNASeq/scRNASeq-train-seed-0.rds",
"output/v8/data/scRNASeq/Active-Learning_entropy/AL-batches-subset",
get_proportion_rep(snakemake@input$scrna,
snakemake@input$scrna_AL_rand,
snakemake@input$scrna_AL_rank,
"scRNASeq"),
get_proportion_rep("data/snRNASeq/snRNASeq-train-seed-0.rds",
"output/v8/data/snRNASeq/Active-Learning_entropy/AL-batches-subset",
get_proportion_rep(snakemake@input$snrna,
snakemake@input$snrna_AL_rand,
snakemake@input$snrna_AL_rank,
"snRNASeq")
)

Expand All @@ -72,11 +69,7 @@ prop <- props |>


## Part B: heatmaps
f <- c("output/v8/results/overall-CyTOF-benchmarking-accuracies.tsv",
"output/v8/results/overall-scRNASeq-benchmarking-accuracies.tsv",
"output/v8/results/overall-snRNASeq-benchmarking-accuracies.tsv")

acc <- lapply(f, function(x){
acc <- lapply(snakemake@input$accs, function(x){
df <- read_tsv(x) |>
mutate(cohort = case_when(grepl("CyTOF", basename(x)) ~ "CyTOF",
grepl("snRNASeq", basename(x)) ~ "snRNASeq",
Expand Down Expand Up @@ -149,7 +142,7 @@ ranking_vs_random <- create_heatmap(acc, "scRNASeq", "random_vs_ranked") +

ranking <- draw(ranking_vs_random, column_title = "Selecting initial cells based on marker expression")

pdf("output/v8/paper-figures/ranking-random-heatmap.pdf", height = 3.5, width = 7)
pdf(snakemake@output$rank_rand_hm, height = 3.5, width = 7)
ranking
dev.off()

Expand All @@ -159,20 +152,20 @@ lr_vs_rf <- create_heatmap(acc, "scRNASeq", "lr_vs_rf") +

lr_vs_rf <- draw(lr_vs_rf, column_title = "F1-score improvement by random forest compared to logistic regression")

pdf("output/v8/paper-figures/lr-rf-heatmap.pdf", height = 3.5, width = 7)
pdf(snakemake@output$lr_rf_hm, height = 3.5, width = 7)
lr_vs_rf
dev.off()

## Full main figure
ranking <- image_read_pdf("output/v8/paper-figures/ranking-random-heatmap.pdf")
ranking <- image_read_pdf(snakemake@output$rank_rand_hm)
ranking <- ggplot() +
background_image(ranking)

lr_vs_rf <- image_read_pdf("output/v8/paper-figures/lr-rf-heatmap.pdf")
lr_vs_rf <- image_read_pdf(snakemake@output$lr_rf_hm)
lr_vs_rf <- ggplot() +
background_image(lr_vs_rf)

pdf("output/v8/paper-figures/lr-and-rf-heatmaps.pdf", height = 8.8, width = 7)
pdf(snakemake@output$main_fig, height = 8.8, width = 7)
(lr_vs_rf / wrap_elements(full = prop) / ranking) + plot_layout(heights = c(3, 1.2, 3)) + plot_annotation(tag_levels = "A")
dev.off()

Expand Down Expand Up @@ -212,7 +205,7 @@ rf_vs_rf_ranking <- filter(acc, corrupted == 0) |>
whatsthatcell_theme() +
theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust =1))

pdf("output/v8/paper-figures/rf-vs-lr-supplementary.pdf", height = 13, width = 12)
pdf(snakemake@output$rf_lr_supp, height = 13, width = 12)
rf_vs_rf_random / rf_vs_rf_ranking + plot_layout(guides = "collect") +
plot_annotation(tag_levels = "A")
dev.off()
Expand Down Expand Up @@ -254,7 +247,7 @@ rand_vs_rank_rf <- filter(acc, corrupted == 0) |>
whatsthatcell_theme() +
theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust =1))

pdf("output/v8/paper-figures/random-vs-ranking-supplementary.pdf", height = 13, width = 12)
pdf(snakemake@output$rand_rank_supp, height = 13, width = 12)
rand_vs_rank_lr / rand_vs_rank_rf + plot_layout(guides = "collect") +
plot_annotation(tag_levels = "A")
dev.off()
17 changes: 9 additions & 8 deletions pipeline/figures/figure1.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ source("pipeline/whatsthatcell-helpers.R")


### [ FIGURE 1A - BENCHMARKING OVERVIEW ] #####
schematic <- image_read("illustrator-figures/benchmarking-schematic.ai") |>
image_ggplot()
# had to remove because magick causes issues with docker
# schematic <- image_read(snakemake@input$schematic) |>
# image_ggplot()

### [ FIGURE 1B - DATASET COMPOSOTION ] #####
set.seed(42)

CyTOF <- readRDS("data/CyTOF/CyTOF-full.rds")
CyTOF <- readRDS(snakemake@input$cytof)
CyTOF <- runTSNE(CyTOF)
scRNA <- readRDS("data/scRNASeq/scRNASeq-full.rds")
scRNA <- readRDS(snakemake@input$scrna)
scRNA <- runTSNE(scRNA)
snRNA <- readRDS("data/snRNASeq/snRNASeq-full.rds")
snRNA <- readRDS(snakemake@input$snrna)
snRNA <- runTSNE(snRNA)


Expand Down Expand Up @@ -130,10 +131,10 @@ snrna_plot <- ((wrap_elements(full = snrna_tsne, ignore_tag = TRUE) & labs(title
snrna_bar) +
plot_layout(heights = c(3,1))

pdf("output/v8/paper-figures/figure1.pdf", height = 12.5, width = 17.5)
pdf(snakemake@output$fig1, height = 9.25, width = 17.5)
wrap_elements((scrna_plot | snrna_plot | cytof_plot) + plot_layout(widths = c(1, 1.2, 1))) /
wrap_elements(schematic) +
plot_layout(heights = c(2, 0.7)) +
#wrap_elements(schematic) +
#plot_layout(heights = c(2, 0.7)) +
plot_annotation(tag_levels = 'A') &
theme(plot.tag = element_text(size = 22))
dev.off()
Expand Down
10 changes: 3 additions & 7 deletions pipeline/figures/figure3.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ suppressPackageStartupMessages({
library(scales)
library(magick)
})
devtools::load_all("/ggplot2")
source("pipeline/whatsthatcell-helpers.R")

### [ ACCURACIES ] ####
f <- list.files("output/v8/results/", pattern = "overall", full.names = TRUE)

acc <- lapply(f, function(x){
acc <- lapply(snakemake@input$accs, function(x){
df <- read_tsv(x) |>
mutate(cohort = case_when(grepl("CyTOF", basename(x)) ~ "CyTOF",
grepl("snRNASeq", basename(x)) ~ "snRNASeq",
Expand All @@ -34,13 +33,10 @@ acc <- lapply(f, function(x){

sel_meth_cols <- sel_met_cols

schematic <- image_read("illustrator-figures/AR-schematic.ai") |>
image_ggplot()

eval <- full_acc_plot_wrapper(acc, "rf", "ranking", "") &
labs(fill = "Selection method")

pdf("output/v8/paper-figures/figure3-overall-benchmark.pdf", height = 8.4, width = 9)
pdf(snakemake@output$overall_fig, height = 8.4, width = 9)
eval
dev.off()

11 changes: 6 additions & 5 deletions pipeline/figures/imbalance-plots.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
library(tidyverse)
library(scales)
library(patchwork)

sc_acc <- read_tsv("output/v8/imbalance/acc/imbalance-acc-scRNASeq.tsv")
sn_acc <- read_tsv("output/v8/imbalance/acc/imbalance-acc-snRNASeq.tsv")
cy_acc <- read_tsv("output/v8/imbalance/acc/imbalance-acc-CyTOF.tsv")
sc_acc <- read_tsv(snakemake@input$scrna_acc)
sn_acc <- read_tsv(snakemake@input$snrna_acc)
cy_acc <- read_tsv(snakemake@input$cytof_acc)

source("pipeline/whatsthatcell-helpers.R")

Expand Down Expand Up @@ -79,7 +80,7 @@ plotting_order <- filter(comb_imb_score, modality == "snRNASeq" & al == "RF" & i
arrange(mean_estimate) |>
pull(strat)

pdf("output/v8/paper-figures/imbalance-main-figure.pdf", height = 3, width = 7)
pdf(snakemake@output$main_fig, height = 3, width = 7)
filter(comb_imb_score, modality == "snRNASeq" & al == "RF" & init == "ranking") |>
mutate(strat = factor(strat, levels = plotting_order)) |>
ggplot(aes(x = comp, y = diff, fill = strat)) +
Expand All @@ -95,7 +96,7 @@ pdf("output/v8/paper-figures/imbalance-main-figure.pdf", height = 3, width = 7)
dev.off()


pdf("output/v8/paper-figures/imbalance-supplementary.pdf", height = 18, width = 9)
pdf(snakemake@output$sup_fig, height = 18, width = 9)
(scrnaseq / snrnaseq / cytof) +
plot_annotation(tag_levels = "A") +
plot_layout(guides = "collect", heights = c(2, 2, 1))
Expand Down
Loading

0 comments on commit e39fcf1

Please sign in to comment.