Last updated: 2021-12-17

Checks: 7 0

Knit directory: sct2_revision/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20210706) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 8afc486. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    data/raw_data/
    Ignored:    data/rds_filtered/
    Ignored:    data/rds_raw/
    Ignored:    data/sampled_counts/
    Ignored:    output/snakemake_output/

Untracked files:
    Untracked:  code/02_run_seurat_noclip.R
    Untracked:  code/07AA_deseq2_muscat_simulate.R
    Untracked:  code/07A_muscat_simulate.R
    Untracked:  code/07A_simulate_muscat.R
    Untracked:  code/07BB_deseq2_muscat_process.R
    Untracked:  code/07B_muscat_process.R
    Untracked:  code/07B_process_muscat.R
    Untracked:  code/08_run_presto.R
    Untracked:  code/17A_HEK_SS3_dropseq.Rmd
    Untracked:  code/17A_HEK_SS3_dropseq_files/
    Untracked:  code/17C_HEK_Quartzeseq2_dropseq.Rmd
    Untracked:  code/17C_HEK_Quartzeseq2_dropseq_files/
    Untracked:  code/17_HEK_SS3_ChromiumV3.Rmd
    Untracked:  code/17_HEK_SS3_ChromiumV3.nb.html
    Untracked:  code/17_HEK_SS3_ChromiumV3_files/
    Untracked:  code/AA_process_muscat.R
    Untracked:  code/BB_process_muscat.R
    Untracked:  code/DD_simulate_muscat.R
    Untracked:  code/EE_simulate_muscat.R
    Untracked:  code/XX_process_muscat.R
    Untracked:  code/XX_simulate_muscat.R
    Untracked:  code/YY_simulate_muscat.R
    Untracked:  code/ZZ_simulate_muscat.R
    Untracked:  code/kang_muscat.R
    Untracked:  code/prep_sce.R
    Untracked:  code/prep_sce_ss3_dropseq.R
    Untracked:  data/azimuth_predictions/
    Untracked:  junk/
    Untracked:  mamba_update_changes.txt
    Untracked:  output/11C_VST/
    Untracked:  output/AAmuscat_simulated/
    Untracked:  output/BBmuscat_simulated/
    Untracked:  output/CCmuscat_simulated/
    Untracked:  output/CD4_NK_downsampling_DE.rds
    Untracked:  output/DDmuscat_simulated/
    Untracked:  output/EEmuscat_simulated/
    Untracked:  output/KANGmuscat_simulated/
    Untracked:  output/NK_downsampling/
    Untracked:  output/XXmuscat_simulated/
    Untracked:  output/YYmuscat_simulated/
    Untracked:  output/ZZmuscat_simulated/
    Untracked:  output/figures/
    Untracked:  output/kang_prepsce.rds
    Untracked:  output/muscat_simulated/
    Untracked:  output/muscat_simulation/
    Untracked:  output/seu_sct2_sim.rds
    Untracked:  output/simulation_HEK_QuartzSeq2_Dropseq_downsampling/
    Untracked:  output/simulation_HEK_SS3_ChromiumV3_downsampling/
    Untracked:  output/simulation_HEK_SS3_Dropseq_downsampling/
    Untracked:  output/simulation_HEK_downsampling/
    Untracked:  output/simulation_NK_downsampling/
    Untracked:  output/ss3_dropseq_prepsim.rds
    Untracked:  output/tables/
    Untracked:  output/vargenes/
    Untracked:  snakemake/.snakemake/
    Untracked:  snakemake/Snakefile_noclip.smk
    Untracked:  snakemake/Snakefile_presto.smk
    Untracked:  snakemake/cluster.yaml
    Untracked:  snakemake/install_glm.R
    Untracked:  snakemake/jobscript.sh
    Untracked:  snakemake/jobscript_ncells.sh
    Untracked:  snakemake/local_run_downsampling.sh
    Untracked:  snakemake/local_run_glm.sh
    Untracked:  snakemake/local_run_ncells.sh
    Untracked:  snakemake/local_run_noclip.sh
    Untracked:  snakemake/local_run_presto.sh
    Untracked:  snakemake/local_run_time.sh
    Untracked:  snakemake/run_glm.sh
    Untracked:  snakemake/run_ncells.sh
    Untracked:  snakemake/sct2_revision_env.yml
    Untracked:  temp_figures/

Unstaged changes:
    Deleted:    analysis/04_PBMC68k.Rmd
    Modified:   code/02_run_seurat.R
    Modified:   code/03_run_vst2_downsample.R
    Modified:   code/04_run_vst_ncells.R
    Modified:   code/06_run_sct.R
    Modified:   data/datasets.csv
    Modified:   snakemake/Snakefile_downsampling.smk
    Modified:   snakemake/Snakefile_glm_seurat.smk
    Modified:   snakemake/Snakefile_metacell.smk

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/11B_Figure4.Rmd) and HTML (docs/11B_Figure4.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 8afc486 Saket Choudhary 2021-12-17 workflowr::wflow_publish("analysis/*")

suppressMessages({
  library(Seurat)
  library(ggpubr)
  library(ggridges)
  library(patchwork)
  library(dplyr)
  library(ggupset)
  library(tidyverse)
  library(ggplot2)
  library(xtable)
  library(ComplexUpset)
  library(SeuratData)

})
Warning in if (is.na(desc)) {: the condition has length > 1 and only the first
element will be used

Warning in if (is.na(desc)) {: the condition has length > 1 and only the first
element will be used

Warning in if (is.na(desc)) {: the condition has length > 1 and only the first
element will be used

Warning in if (is.na(desc)) {: the condition has length > 1 and only the first
element will be used

Warning in if (is.na(desc)) {: the condition has length > 1 and only the first
element will be used

Warning in if (is.na(desc)) {: the condition has length > 1 and only the first
element will be used

Warning in if (is.na(desc)) {: the condition has length > 1 and only the first
element will be used

Warning in if (is.na(desc)) {: the condition has length > 1 and only the first
element will be used

Warning in if (is.na(desc)) {: the condition has length > 1 and only the first
element will be used

Warning in if (is.na(desc)) {: the condition has length > 1 and only the first
element will be used

Warning in if (is.na(desc)) {: the condition has length > 1 and only the first
element will be used

Warning in if (is.na(desc)) {: the condition has length > 1 and only the first
element will be used
data("bmcite")
knitr::opts_chunk$set(warning = FALSE, message = FALSE)

theme_set(theme_pubr())

Meta marker coverage

datasets <- read.csv(here::here("data/datasets.csv"))
n_markers <- readRDS(here::here("output/vargenes/n_markers.rds"))
# select datasets where we detect at least 3000 markers
n_markers_3k <- n_markers %>% filter(n_markers > 2999)


var_genes_df <- readRDS(here::here("output/vargenes/compare_variable_genes_df_long.rds"))
var_genes_df <- left_join(var_genes_df, datasets, by = "sample_name")
var_genes_df <- var_genes_df[var_genes_df$sample_name %in% n_markers_3k$sample_name,]

var_genes_df_summary2 <- var_genes_df %>%
  group_by(technology, variable, rank) %>%
  summarise(median_overlap = median(value)) %>%
  ungroup()

var_genes_df_summary3 <- var_genes_df %>%
  group_by(tissue, variable, rank) %>%
  summarise(median_overlap = median(value)) %>%
  ungroup()




var_genes_prop_df <- readRDS(here::here("output/vargenes/compare_variable_genes_prop_df_long.rds"))
var_genes_prop_df <- left_join(var_genes_prop_df, datasets, by = "sample_name")
var_genes_prop_df <- var_genes_prop_df[var_genes_prop_df$sample_name %in% n_markers_3k$sample_name,]

var_genes_prop_df_summary2 <- var_genes_prop_df %>%
  group_by(technology, variable, rank) %>%
  summarise(median_overlap = median(value)) %>%
  ungroup()
var_genes_prop_df_summary3 <- var_genes_prop_df %>%
  group_by(tissue, variable, rank) %>%
  summarise(median_overlap = median(value)) %>%
  ungroup()

var_genes_prop_df_summary2$variable <- factor(as.character(var_genes_prop_df_summary2$variable),
  levels = c("offset100", "offset10", "sct", "sct2")
)

var_genes_prop_df_summary3$variable <- factor(as.character(var_genes_prop_df_summary3$variable),
  levels = c("offset100", "offset10", "sct", "sct2")
)

labels <- c(expression(theta == 100), expression(theta == 10), "SCT v1", "SCT v2")
names(labels) <- c("offset100", "offset10", "sct", "sct2")


p1.tech <- ggplot(var_genes_prop_df_summary2, aes(rank, median_overlap, color = variable)) +
  geom_point(size = 0.1) +
  facet_wrap(~technology, scales = "free_y") +
  # geom_line() +
  scale_color_brewer(type = "qual", palette = "Set1", name = "", labels = labels) +
  guides(colour = guide_legend(override.aes = list(size = 2))) +
  xlab("Variable feature rank") +
  ylab("Median proportion of marker genes") 



p1.system <- ggplot(var_genes_prop_df_summary3, aes(rank, median_overlap, color = variable)) +
  geom_point(size = 0.1) +
  facet_wrap(~tissue, scales = "free_y", ncol = 4) +
  # geom_line() +
  scale_color_brewer(type = "qual", palette = "Set2", name = "", labels = labels) +
  guides(colour = guide_legend(override.aes = list(size = 2))) +
  xlab("Variable feature rank") +
  ylab("Median proportion of marker genes") #+ theme(legend.position = c(0.9, 0.24),legend.background=element_blank(), legend.text.align = 0)
p1.system

summary_df <- var_genes_prop_df_summary3[var_genes_prop_df_summary3$rank %in% c(1000,2000,3000), ]

print(xtable(summary_df, type = "latex", digits=3), include.rownames = FALSE)
% latex table generated in R 4.1.2 by xtable 1.8-4 package
% Fri Dec 17 10:18:45 2021
\begin{table}[ht]
\centering
\begin{tabular}{llrr}
  \hline
tissue & variable & rank & median\_overlap \\ 
  \hline
Bone Marrow & offset100 & 1000 & 0.204 \\ 
  Bone Marrow & offset100 & 2000 & 0.305 \\ 
  Bone Marrow & offset100 & 3000 & 0.367 \\ 
  Bone Marrow & offset10 & 1000 & 0.189 \\ 
  Bone Marrow & offset10 & 2000 & 0.276 \\ 
  Bone Marrow & offset10 & 3000 & 0.337 \\ 
  Bone Marrow & sct & 1000 & 0.194 \\ 
  Bone Marrow & sct & 2000 & 0.279 \\ 
  Bone Marrow & sct & 3000 & 0.337 \\ 
  Bone Marrow & sct2 & 1000 & 0.290 \\ 
  Bone Marrow & sct2 & 2000 & 0.427 \\ 
  Bone Marrow & sct2 & 3000 & 0.536 \\ 
  Cortex & offset100 & 1000 & 0.263 \\ 
  Cortex & offset100 & 2000 & 0.415 \\ 
  Cortex & offset100 & 3000 & 0.544 \\ 
  Cortex & offset10 & 1000 & 0.249 \\ 
  Cortex & offset10 & 2000 & 0.402 \\ 
  Cortex & offset10 & 3000 & 0.529 \\ 
  Cortex & sct & 1000 & 0.216 \\ 
  Cortex & sct & 2000 & 0.353 \\ 
  Cortex & sct & 3000 & 0.470 \\ 
  Cortex & sct2 & 1000 & 0.292 \\ 
  Cortex & sct2 & 2000 & 0.474 \\ 
  Cortex & sct2 & 3000 & 0.586 \\ 
  Fetus & offset100 & 1000 & 0.210 \\ 
  Fetus & offset100 & 2000 & 0.356 \\ 
  Fetus & offset100 & 3000 & 0.453 \\ 
  Fetus & offset10 & 1000 & 0.206 \\ 
  Fetus & offset10 & 2000 & 0.352 \\ 
  Fetus & offset10 & 3000 & 0.446 \\ 
  Fetus & sct & 1000 & 0.222 \\ 
  Fetus & sct & 2000 & 0.394 \\ 
  Fetus & sct & 3000 & 0.520 \\ 
  Fetus & sct2 & 1000 & 0.287 \\ 
  Fetus & sct2 & 2000 & 0.455 \\ 
  Fetus & sct2 & 3000 & 0.542 \\ 
  PBMC & offset100 & 1000 & 0.190 \\ 
  PBMC & offset100 & 2000 & 0.292 \\ 
  PBMC & offset100 & 3000 & 0.385 \\ 
  PBMC & offset10 & 1000 & 0.165 \\ 
  PBMC & offset10 & 2000 & 0.264 \\ 
  PBMC & offset10 & 3000 & 0.345 \\ 
  PBMC & sct & 1000 & 0.145 \\ 
  PBMC & sct & 2000 & 0.217 \\ 
  PBMC & sct & 3000 & 0.287 \\ 
  PBMC & sct2 & 1000 & 0.282 \\ 
  PBMC & sct2 & 2000 & 0.459 \\ 
  PBMC & sct2 & 3000 & 0.571 \\ 
   \hline
\end{tabular}
\end{table}
summary_df <- tidyr::spread(summary_df, key = "variable", value = "median_overlap")
print(xtable(summary_df, type = "latex", digits=3), include.rownames = FALSE)
% latex table generated in R 4.1.2 by xtable 1.8-4 package
% Fri Dec 17 10:18:45 2021
\begin{table}[ht]
\centering
\begin{tabular}{lrrrrr}
  \hline
tissue & rank & offset100 & offset10 & sct & sct2 \\ 
  \hline
Bone Marrow & 1000 & 0.204 & 0.189 & 0.194 & 0.290 \\ 
  Bone Marrow & 2000 & 0.305 & 0.276 & 0.279 & 0.427 \\ 
  Bone Marrow & 3000 & 0.367 & 0.337 & 0.337 & 0.536 \\ 
  Cortex & 1000 & 0.263 & 0.249 & 0.216 & 0.292 \\ 
  Cortex & 2000 & 0.415 & 0.402 & 0.353 & 0.474 \\ 
  Cortex & 3000 & 0.544 & 0.529 & 0.470 & 0.586 \\ 
  Fetus & 1000 & 0.210 & 0.206 & 0.222 & 0.287 \\ 
  Fetus & 2000 & 0.356 & 0.352 & 0.394 & 0.455 \\ 
  Fetus & 3000 & 0.453 & 0.446 & 0.520 & 0.542 \\ 
  PBMC & 1000 & 0.190 & 0.165 & 0.145 & 0.282 \\ 
  PBMC & 2000 & 0.292 & 0.264 & 0.217 & 0.459 \\ 
  PBMC & 3000 & 0.385 & 0.345 & 0.287 & 0.571 \\ 
   \hline
\end{tabular}
\end{table}
summary_df <- var_genes_df_summary3[var_genes_df_summary3$rank %in% c(1000,2000,3000), ]

summary_df <- tidyr::spread(summary_df, key = "variable", value = "median_overlap")
print(xtable(summary_df, type = "latex", digits=1), include.rownames = FALSE)
% latex table generated in R 4.1.2 by xtable 1.8-4 package
% Fri Dec 17 10:18:45 2021
\begin{table}[ht]
\centering
\begin{tabular}{lrrrrr}
  \hline
tissue & rank & offset100 & offset10 & sct & sct2 \\ 
  \hline
Bone Marrow & 1000 & 611.0 & 567.0 & 582.0 & 871.0 \\ 
  Bone Marrow & 2000 & 916.0 & 829.0 & 836.0 & 1281.0 \\ 
  Bone Marrow & 3000 & 1100.0 & 1012.0 & 1012.0 & 1607.0 \\ 
  Cortex & 1000 & 788.5 & 747.5 & 649.5 & 876.5 \\ 
  Cortex & 2000 & 1245.0 & 1205.0 & 1058.5 & 1422.0 \\ 
  Cortex & 3000 & 1633.0 & 1586.5 & 1408.5 & 1757.5 \\ 
  Fetus & 1000 & 631.0 & 618.0 & 667.0 & 862.0 \\ 
  Fetus & 2000 & 1068.0 & 1056.0 & 1182.0 & 1366.0 \\ 
  Fetus & 3000 & 1360.0 & 1337.0 & 1559.0 & 1626.0 \\ 
  PBMC & 1000 & 569.0 & 495.0 & 436.0 & 847.0 \\ 
  PBMC & 2000 & 877.0 & 793.0 & 650.0 & 1376.0 \\ 
  PBMC & 3000 & 1155.0 & 1035.0 & 860.0 & 1712.0 \\ 
   \hline
\end{tabular}
\end{table}

Contrast feature lists

datasets <- read.csv(here::here("data/datasets.csv"))
read_gene_attr <- function(filepath) {
  gene_attr <- read.csv(filepath, row.names = 1)
  gene_attr$gene <- rownames(gene_attr)
  gene_attr <- gene_attr %>%
    arrange(desc(residual_variance)) %>%
    pull(gene)
  return(gene_attr[1:3000])
}

dataset_sub <- datasets[datasets$key == "PBMC__ChromiumV3", ]

overlap_list <- list()
overlap_list_prop <- list()
sample_name <- unique(dataset_sub$sample_name)
dataset <- unique(dataset_sub$key)
all_genes <- rownames(read.csv(here::here("output/snakemake_output/seurat_output", dataset, "glmGamPoi", "gene_attr.csv"), row.names = 1))
gene_attr <- read.csv(here::here("output/snakemake_output/seurat_output", dataset, "glmGamPoi", "gene_attr.csv"), row.names = 1)
gene_attr$gene <- rownames(gene_attr)
sct <- read_gene_attr(here::here("output/snakemake_output/seurat_output", dataset, "glmGamPoi", "gene_attr.csv"))

offset100 <- read_gene_attr(here::here("output/snakemake_output/seurat_output", dataset, "offset-100", "gene_attr.csv"))
offset10 <- read_gene_attr(here::here("output/snakemake_output/seurat_output", dataset, "offset-10", "gene_attr.csv"))

sct2 <- read_gene_attr(here::here("output/snakemake_output/seurat_output", dataset, "vst2", "gene_attr.csv"))


presto_output <- readRDS(here::here("output/snakemake_output/presto_output", dataset, "presto_markers.rds"))
#marker_genes <- unique(presto_output %>% filter(p_val_adj < 0.01) %>% filter(avg_log2FC > 0.5) %>% arrange(p_val_adj) %>% pull(gene))

presto_subset <- presto_output %>%
    group_by(gene) %>%
    arrange(desc(avg_log2FC)) %>%
    filter(row_number() == 1) %>%
    ungroup() %>%
    filter(p_val < 0.05) %>%
    filter(avg_log2FC > 0.25)

  # marker_genes <- unique(presto_output %>% filter(p_val_adj < 0.01) %>% filter(avg_log2FC > 0.5) %>% arrange(p_val_adj) %>% pull(gene))
  marker_genes <- presto_subset %>%
    arrange(desc(avg_log2FC), p_val_adj) %>%
    pull(gene)
  if (length(marker_genes) > length(sct2)) {
    marker_genes <- marker_genes[1:length(sct2)]
  }
  
all_methods <- list(sct2 = sct2, offset10 = offset10, offset100 = offset100, sct = sct)

gene_methods_list <- list()
index <- 1
for (gene in unique(unlist(all_methods))) {
  gene_methods <- c()
  for (method in names(all_methods)) {
    if (gene %in% all_methods[[method]]) {
      gene_methods <- c(gene_methods, method)
    }
  }
  gene_methods_list[[index]] <- list(gene = gene, methods = gene_methods)
  index <- index + 1
}
gene_methods_list_df <- data.frame(gene = names(gene_methods_list))
gene_methods_list_df$methods <- gene_methods_list[gene_methods_list_df$gene]


gene_methods_df <- data.frame(gene = unique(unlist(all_methods)))
rownames(gene_methods_df) <- gene_methods_df$gene
for (method in names(all_methods)) {
  gene_methods_df[, method] <- 0
  gene_methods_df[all_methods[[method]], method] <- 1
}
gene_methods_df <- left_join(gene_methods_df, gene_attr, by = "gene")
gene_methods_df$marker_prop <- 0
gene_methods_df$marker_prop[gene_methods_df$gene %in% marker_genes] <- 100*1/length(marker_genes)

gene_methods_df$is_marker <- "No"
gene_methods_df$is_marker[gene_methods_df$gene %in% marker_genes] <- "Yes"

gene_methods_df <- as_tibble(gene_methods_df)

gene_methods_df[, "SCT v1"] <- gene_methods_df$sct
gene_methods_df[, "SCT v2"] <- gene_methods_df$sct2
gene_methods_df[, "Offset 100"] <- gene_methods_df$offset100
gene_methods_df[, "Offset 10"] <- gene_methods_df$offset10
sct_sp_genes <- setdiff(sct, union(sct2, union(offset10, offset100)))
summary(gene_methods_df[gene_methods_df$gene %in% sct_sp_genes,] %>% pull(gmean))
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.003539 0.004248 0.004958 0.005965 0.006795 0.028845 
sct2_sp_genes <- setdiff(sct2, union(sct, union(offset10, offset100)))
summary(gene_methods_df[gene_methods_df$gene %in% sct2_sp_genes,] %>% pull(gmean))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.02746 0.06597 0.10432 0.12552 0.16410 0.43309 
length(intersect(sct2_sp_genes, marker_genes))
[1] 269
p2 <- upset(
  gene_methods_df,
  c("SCT v1", "SCT v2", "Offset 100", "Offset 10"),
  base_annotations=list(
        'Intersection size'=intersection_size(
            counts=TRUE,
            mapping=aes(fill=is_marker), 

        ) + scale_fill_brewer(type="qual", palette =  "Dark2", name = "Top 3000 marker")
  ),
  annotations = list(
    "log2(1 + Gene mean)" = ggplot(mapping = aes(x = intersection, y = log(1+gmean, base = 2)))
    +
      ggbeeswarm::geom_quasirandom(groupOnX = TRUE, size = 0.1) +
      stat_summary(
        geom = "crossbar", color = "red",
        fun = median, fun.min = median, fun.max = median,
        fatten = 1.2, width = 0.5
      ) + ylim(0,3)
  ),
 
  min_size = 10,
  width_ratio = 0.1,
  set_sizes = FALSE
)
p2 

all_fdr_tpr_df_melt <- readRDS(here::here("output/muscat_simulation/results/all_fdr_tpr_df_melt.rds"))
all_fdr_tpr_df_melt[all_fdr_tpr_df_melt$method == "SCT2", "method"] <- "SCT v2"
all_fdr_tpr_df_melt[all_fdr_tpr_df_melt$method == "SCT", "method"] <- "SCT v1"
all_fdr_tpr_df_melt$method <- factor(all_fdr_tpr_df_melt$method, levels = c("lognorm", "scran", "SCT v1", "SCT v2"))

labels <- c("LogNorm", "Scran", "SCT v1", "SCT v2")
names(labels) <- c("LogNorm", "Scran", "SCT v1", "SCT v2")


p3 <- ggplot(all_fdr_tpr_df_melt[all_fdr_tpr_df_melt$de_percent > 0.01, ], aes(FDR, TPR, color = method)) +
  geom_point() +
  geom_line() +
  geom_vline(xintercept = c(0.01, 0.05, 0.1), linetype = "dashed") +
  xlim(0, 1) +
  scale_x_continuous(trans = "log", breaks = c(0.01, 0.05, 0.1, 0.5,1), guide = guide_axis(angle = 50)) +
  facet_wrap(~de_percent, ncol = 4) +
  theme_pubr() +
  theme(
    panel.grid.minor = element_line(size = (0.2), colour = "grey")
  ) +
  scale_color_brewer(type = "qual", palette = "Dark2", name = "", labels = labels) + 
  theme(axis.text.x=element_text(size=rel(0.7), angle=45))
p3

nk_markers <- readRDS(here::here("output/simulation_NK_downsampling/NK_downsampling_DE_sig.rds"))

nk_markers$method <- factor(nk_markers$method, levels = c("LogNorm", "Scran", "SCT v1", "SCT v2"))
nk_markers_summary <- nk_markers %>%
  group_by(method) %>%
  summarise(count = n())
nk_markers_empty <- data.frame(method = c("LogNorm", "Scran", "SCT v1", "SCT v2"))
nk_markers_empty$count <- 1
nk_markers_summary2 <- rbind(nk_markers_empty, nk_markers_summary)
nk_markers_summary2 <- nk_markers_summary2 %>%
  group_by(method) %>%
  summarise(count = sum(count))


p4 <- ggplot(nk_markers_summary2, aes(method, y = count, fill = method)) +
  geom_bar(width = 0.5, stat = "identity") +
  scale_y_log10() +
  scale_fill_brewer(type = "qual", palette = "Dark2", name = "") +
  ylab("Number of DE genes") +
  xlab("") +
  scale_x_discrete(guide = guide_axis(angle = 30)) +
  NoLegend()
p4

layout <- "
AAABB
CCDBB"

p <- p1.system + p2 + p3 + p4
p + plot_layout(design = layout) & plot_annotation(tag_levels = "A") & theme(plot.tag = element_text(face = "bold"))

ggsave(here::here("output/figures/Figure4.pdf"))#, width = 14, height = 8)
ggsave(here::here("output/figures/Figure4.png"))#, width = 12, height = 8)
nbfits.sct <- readRDS(here::here("output/11C_VST/nbfits.sct.rds"))
nbfits.sct2 <- readRDS(here::here("output/11C_VST/nbfits.sct2.rds"))

pl <- VlnPlot(bmcite, "RP11-290C10.1", slot = "counts", group.by = "celltype.l2") + NoLegend()
pr <- VlnPlot(bmcite, "CD86", slot = "counts", group.by = "celltype.l2") + NoLegend()
px <- pl | pr

p1 <- ggplot(nbfits.sct, aes(umi, res, color = gene)) +
  geom_jitter(alpha = 0.5, size=0.5) +
  scale_color_manual(values = RColorBrewer::brewer.pal(3, "Set1"), name = "") +
  ylab("Pearson residual") + xlab("UMI")
p3 <- ggplot(nbfits.sct, aes(sd, res, color = gene)) +
  geom_jitter(alpha = 0.5, size=0.5) +
  scale_color_manual(values = RColorBrewer::brewer.pal(3, "Set1"), name = "") +
  ylab("Pearson residual") + xlab("Standard deviation (model)")
pup <- p1 | p3 + patchwork::plot_annotation(title = "sctransform (v1)")

p1 <- ggplot(nbfits.sct2, aes(umi, res, color = gene)) +
  geom_jitter(alpha = 0.5, size=0.5) +
  scale_color_manual(values = RColorBrewer::brewer.pal(3, "Set1"), name = "") +
  ylab("Pearson residual") + xlab("UMI")
p3 <- ggplot(nbfits.sct2, aes(sd, res, color = gene)) +
  geom_jitter(alpha = 0.5, size=0.5) +
  scale_color_manual(values = RColorBrewer::brewer.pal(3, "Set1"), name = "") +
  ylab("Pearson residual") + xlab("Standard deviation (model)")
pdown <- p1 | p3 + patchwork::plot_annotation(title = "sctransform (v2)")

#px / pup / pdown
layout <- "
AB
CD
EF"

p <- px / pup / pdown
p & plot_annotation(tag_levels = "A") & theme(plot.tag = element_text(face = "bold"))

ggsave(here::here("output/figures/varcomp.pdf"))
ggsave(here::here("output/figures/varcomp.png"))
sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] thp1.eccite.SeuratData_3.1.5 pbmcsca.SeuratData_3.0.0    
 [3] pbmc3k.SeuratData_3.1.4      panc8.SeuratData_3.0.2      
 [5] hcabm40k.SeuratData_3.0.0    bmcite.SeuratData_0.3.0     
 [7] SeuratData_0.2.1             ComplexUpset_1.3.3          
 [9] xtable_1.8-4                 forcats_0.5.1               
[11] stringr_1.4.0                purrr_0.3.4                 
[13] readr_2.1.1                  tidyr_1.1.4                 
[15] tibble_3.1.6                 tidyverse_1.3.1             
[17] ggupset_0.3.0                dplyr_1.0.7                 
[19] patchwork_1.1.1              ggridges_0.5.3              
[21] ggpubr_0.4.0                 ggplot2_3.3.5               
[23] SeuratObject_4.0.4           Seurat_4.0.5                
[25] workflowr_1.6.2             

loaded via a namespace (and not attached):
  [1] readxl_1.3.1           backports_1.4.1        plyr_1.8.6            
  [4] igraph_1.2.9           lazyeval_0.2.2         splines_4.1.2         
  [7] listenv_0.8.0          scattermore_0.7        digest_0.6.29         
 [10] htmltools_0.5.2        fansi_0.5.0            magrittr_2.0.1        
 [13] tensor_1.5             cluster_2.1.2          ROCR_1.0-11           
 [16] tzdb_0.2.0             globals_0.14.0         modelr_0.1.8          
 [19] matrixStats_0.61.0     spatstat.sparse_2.0-0  colorspace_2.0-2      
 [22] rappdirs_0.3.3         rvest_1.0.2            ggrepel_0.9.1         
 [25] haven_2.4.3            xfun_0.28              crayon_1.4.2          
 [28] jsonlite_1.7.2         spatstat.data_2.1-0    survival_3.2-13       
 [31] zoo_1.8-9              glue_1.5.1             polyclip_1.10-0       
 [34] gtable_0.3.0           leiden_0.3.9           car_3.0-12            
 [37] future.apply_1.8.1     abind_1.4-5            scales_1.1.1          
 [40] DBI_1.1.1              rstatix_0.7.0          miniUI_0.1.1.1        
 [43] Rcpp_1.0.7             viridisLite_0.4.0      reticulate_1.22       
 [46] spatstat.core_2.3-2    htmlwidgets_1.5.4      httr_1.4.2            
 [49] RColorBrewer_1.1-2     ellipsis_0.3.2         ica_1.0-2             
 [52] farver_2.1.0           pkgconfig_2.0.3        sass_0.4.0            
 [55] uwot_0.1.11            dbplyr_2.1.1           deldir_1.0-6          
 [58] here_1.0.1             utf8_1.2.2             labeling_0.4.2        
 [61] tidyselect_1.1.1       rlang_0.4.12           reshape2_1.4.4        
 [64] later_1.3.0            cellranger_1.1.0       munsell_0.5.0         
 [67] tools_4.1.2            cli_3.1.0              generics_0.1.1        
 [70] broom_0.7.10           evaluate_0.14          fastmap_1.1.0         
 [73] yaml_2.2.1             goftest_1.2-3          knitr_1.36            
 [76] fs_1.5.2               fitdistrplus_1.1-6     RANN_2.6.1            
 [79] pbapply_1.5-0          future_1.23.0          nlme_3.1-152          
 [82] whisker_0.4            mime_0.12              xml2_1.3.3            
 [85] rstudioapi_0.13        compiler_4.1.2         beeswarm_0.4.0        
 [88] plotly_4.10.0          png_0.1-7              ggsignif_0.6.3        
 [91] spatstat.utils_2.3-0   reprex_2.0.1           bslib_0.3.1           
 [94] stringi_1.7.6          highr_0.9              lattice_0.20-45       
 [97] Matrix_1.4-0           vctrs_0.3.8            pillar_1.6.4          
[100] lifecycle_1.0.1        spatstat.geom_2.3-1    lmtest_0.9-39         
[103] jquerylib_0.1.4        RcppAnnoy_0.0.19       data.table_1.14.2     
[106] cowplot_1.1.1          irlba_2.3.5            httpuv_1.6.3          
[109] R6_2.5.1               promises_1.2.0.1       KernSmooth_2.23-20    
[112] gridExtra_2.3          vipor_0.4.5            parallelly_1.29.0     
[115] codetools_0.2-18       MASS_7.3-54            assertthat_0.2.1      
[118] rprojroot_2.0.2        withr_2.4.3            sctransform_0.3.2.9008
[121] mgcv_1.8-38            parallel_4.1.2         hms_1.1.1             
[124] grid_4.1.2             rpart_4.1-15           rmarkdown_2.11        
[127] carData_3.0-4          Rtsne_0.15             git2r_0.29.0          
[130] shiny_1.7.1            lubridate_1.8.0        ggbeeswarm_0.6.0      

sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] thp1.eccite.SeuratData_3.1.5 pbmcsca.SeuratData_3.0.0    
 [3] pbmc3k.SeuratData_3.1.4      panc8.SeuratData_3.0.2      
 [5] hcabm40k.SeuratData_3.0.0    bmcite.SeuratData_0.3.0     
 [7] SeuratData_0.2.1             ComplexUpset_1.3.3          
 [9] xtable_1.8-4                 forcats_0.5.1               
[11] stringr_1.4.0                purrr_0.3.4                 
[13] readr_2.1.1                  tidyr_1.1.4                 
[15] tibble_3.1.6                 tidyverse_1.3.1             
[17] ggupset_0.3.0                dplyr_1.0.7                 
[19] patchwork_1.1.1              ggridges_0.5.3              
[21] ggpubr_0.4.0                 ggplot2_3.3.5               
[23] SeuratObject_4.0.4           Seurat_4.0.5                
[25] workflowr_1.6.2             

loaded via a namespace (and not attached):
  [1] readxl_1.3.1           backports_1.4.1        plyr_1.8.6            
  [4] igraph_1.2.9           lazyeval_0.2.2         splines_4.1.2         
  [7] listenv_0.8.0          scattermore_0.7        digest_0.6.29         
 [10] htmltools_0.5.2        fansi_0.5.0            magrittr_2.0.1        
 [13] tensor_1.5             cluster_2.1.2          ROCR_1.0-11           
 [16] tzdb_0.2.0             globals_0.14.0         modelr_0.1.8          
 [19] matrixStats_0.61.0     spatstat.sparse_2.0-0  colorspace_2.0-2      
 [22] rappdirs_0.3.3         rvest_1.0.2            ggrepel_0.9.1         
 [25] haven_2.4.3            xfun_0.28              crayon_1.4.2          
 [28] jsonlite_1.7.2         spatstat.data_2.1-0    survival_3.2-13       
 [31] zoo_1.8-9              glue_1.5.1             polyclip_1.10-0       
 [34] gtable_0.3.0           leiden_0.3.9           car_3.0-12            
 [37] future.apply_1.8.1     abind_1.4-5            scales_1.1.1          
 [40] DBI_1.1.1              rstatix_0.7.0          miniUI_0.1.1.1        
 [43] Rcpp_1.0.7             viridisLite_0.4.0      reticulate_1.22       
 [46] spatstat.core_2.3-2    htmlwidgets_1.5.4      httr_1.4.2            
 [49] RColorBrewer_1.1-2     ellipsis_0.3.2         ica_1.0-2             
 [52] farver_2.1.0           pkgconfig_2.0.3        sass_0.4.0            
 [55] uwot_0.1.11            dbplyr_2.1.1           deldir_1.0-6          
 [58] here_1.0.1             utf8_1.2.2             labeling_0.4.2        
 [61] tidyselect_1.1.1       rlang_0.4.12           reshape2_1.4.4        
 [64] later_1.3.0            cellranger_1.1.0       munsell_0.5.0         
 [67] tools_4.1.2            cli_3.1.0              generics_0.1.1        
 [70] broom_0.7.10           evaluate_0.14          fastmap_1.1.0         
 [73] yaml_2.2.1             goftest_1.2-3          knitr_1.36            
 [76] fs_1.5.2               fitdistrplus_1.1-6     RANN_2.6.1            
 [79] pbapply_1.5-0          future_1.23.0          nlme_3.1-152          
 [82] whisker_0.4            mime_0.12              xml2_1.3.3            
 [85] rstudioapi_0.13        compiler_4.1.2         beeswarm_0.4.0        
 [88] plotly_4.10.0          png_0.1-7              ggsignif_0.6.3        
 [91] spatstat.utils_2.3-0   reprex_2.0.1           bslib_0.3.1           
 [94] stringi_1.7.6          highr_0.9              lattice_0.20-45       
 [97] Matrix_1.4-0           vctrs_0.3.8            pillar_1.6.4          
[100] lifecycle_1.0.1        spatstat.geom_2.3-1    lmtest_0.9-39         
[103] jquerylib_0.1.4        RcppAnnoy_0.0.19       data.table_1.14.2     
[106] cowplot_1.1.1          irlba_2.3.5            httpuv_1.6.3          
[109] R6_2.5.1               promises_1.2.0.1       KernSmooth_2.23-20    
[112] gridExtra_2.3          vipor_0.4.5            parallelly_1.29.0     
[115] codetools_0.2-18       MASS_7.3-54            assertthat_0.2.1      
[118] rprojroot_2.0.2        withr_2.4.3            sctransform_0.3.2.9008
[121] mgcv_1.8-38            parallel_4.1.2         hms_1.1.1             
[124] grid_4.1.2             rpart_4.1-15           rmarkdown_2.11        
[127] carData_3.0-4          Rtsne_0.15             git2r_0.29.0          
[130] shiny_1.7.1            lubridate_1.8.0        ggbeeswarm_0.6.0