<jupyter_start><jupyter_text><jupyter_code>install.packages("BiocManager")
BiocManager::install(c("ComplexHeatmap", "circlize"))
library(ComplexHeatmap)
library(circlize)
library(magrittr)
library(dplyr)
library(ggplot2)
library(tidyr)
library(tidyverse)
system("wget -x -c -nH https://s3.msi.umn.edu/skiex003/datasets/EnsembleMerge/fig9.csv")
dat = read.table(file = "skiex003/datasets/EnsembleMerge/fig9.csv", header = T, sep = ",") %>% select(score, score_method, method, Dataset, Datasets)
system("wget -x -c -nH https://s3.msi.umn.edu/skiex003/datasets/EnsembleMerge/scVI.csv")
scvi = read.table(file = "skiex003/datasets/EnsembleMerge/scVI.csv", header = T, sep = ",")
scvi$Dataset = recode_factor(scvi$Datasets, Dataset_1 = "Villani 2017", Dataset_2 = "Han 2018", Dataset_3 = "Chen 2020", Dataset_4 = "Hemberg Panc",
                                                                          Dataset_5 = "PBMC", Dataset_6 = "293t_Jurkat", Dataset_7 = "Hemberg Retina", Dataset_8 = "Saunders 2018", Dataset_9 = "HCA Blood",
                                                                          Dataset_10 = "Paul 2015", Dataset_11 = "Human/mouse Retina", Dataset_12 = "Zilionis 2019", Dataset_13 = "Shekhar 2016", Dataset_14 = "Nestorowa 2016",
                                                                          Dataset_15 = "Zheng 2017", Dataset_16 = "Polanski 2019", Dataset_17 = "HBDC", Dataset_18 = "Panc8")
scvi = scvi[,c("score", "score_method", "method", "Dataset", "Datasets")]
system("wget -x -c -nH https://s3.msi.umn.edu/skiex003/datasets/EnsembleMerge/fastMNN.csv")
mnn = read.table(file = "skiex003/datasets/EnsembleMerge/fastMNN.csv", header = T, sep = ",")
mnn$Dataset = recode_factor(mnn$Datasets, Dataset_1 = "Villani 2017", Dataset_2 = "Han 2018", Dataset_3 = "Chen 2020", Dataset_4 = "Hemberg Panc",
                                                                          Dataset_5 = "PBMC", Dataset_6 = "293t_Jurkat", Dataset_7 = "Hemberg Retina", Dataset_8 = "Saunders 2018", Dataset_9 = "HCA Blood",
                                                                          Dataset_10 = "Paul 2015", Dataset_11 = "Human/mouse Retina", Dataset_12 = "Zilionis 2019", Dataset_13 = "Shekhar 2016", Dataset_14 = "Nestorowa 2016",
                                                                          Dataset_15 = "Zheng 2017", Dataset_16 = "Polanski 2019", Dataset_17 = "HBDC", Dataset_18 = "Panc8")
mnn = mnn[,c("score", "score_method", "method", "Dataset", "Datasets")]
dat = rbind(dat, mnn)
dat = rbind(dat, scvi)


dat$Datasets = factor(dat$Datasets, levels = c("Dataset_1", "Dataset_2", "Dataset_3", "Dataset_4", #reorder datasets in ascending order
                                                                          "Dataset_5", "Dataset_6", "Dataset_7", "Dataset_8", "Dataset_9",
                                                                          "Dataset_10", "Dataset_11", "Dataset_12", "Dataset_13", "Dataset_14",
                                                                          "Dataset_15", "Dataset_16","Dataset_17","Dataset_18"))
dat$Dataset = recode_factor(dat$Datasets, Dataset_1 = "Villani 2017", Dataset_2 = "Han 2018", Dataset_3 = "Chen 2020", Dataset_4 = "Hemberg Panc",
                                                                          Dataset_5 = "PBMC", Dataset_6 = "293t_Jurkat", Dataset_7 = "Hemberg Retina", Dataset_8 = "Saunders 2018", Dataset_9 = "HCA Blood", # reassingn dataset names to manuscript names
                                                                          Dataset_10 = "Paul 2015", Dataset_11 = "Human/mouse Retina", Dataset_12 = "Zilionis 2019", Dataset_13 = "Shekhar 2016", Dataset_14 = "Nestorowa 2016",
                                                                          Dataset_15 = "Zheng 2017", Dataset_16 = "Polanski 2019", Dataset_17 = "HBDC",Dataset_18 = "Panc8")

uncorrected = dat %>% filter(method == "Uncorrected") # seperate baseline methods from results
options(repr.plot.width = 10, repr.plot.height = 15)
oldw <- getOption("warn")
options(warn = -1)
mat = dat
mat %<>%
  distinct() %>%
  select(score, method, Dataset) %>% filter(method != "Uncorrected") %>%
  group_by(Dataset, method) %>% 
  mutate(p_value = (sum(abs(uncorrected$score[uncorrected$Dataset == Dataset]) >= abs(mat$score[mat$method == method & mat$Dataset == Dataset]))+1)/(length(uncorrected$score[uncorrected$Dataset == Dataset]))) %>% 
  ungroup() %>% 
  select(Dataset, method, p_value) %>% 
  distinct() %>%  
  filter(method != "Uncorrected" & !Dataset %in% c("HCA Blood", "Chen 2020", "HBDC", "Nestorowa 2016", "Villani 2017", "Human/mouse Retina")) %>% #remove baseline results and datasets with no significant results
  pivot_wider(names_from = method, values_from = p_value) %>% column_to_rownames(var = "Dataset") %>% 
  as.matrix()
mat = mat[,c("EnsembleMerge", "Seurat", "Harmony", "Liger", "Scanorama", "BBKNN", "fastMNN", "scVI")] # change rownames for figure
mat = -log(mat) #negative log scaling p value for interpretability

col_fun <- colorRamp2(quantile(mat, c(0,1)), c("gray", "#AD5F76"))
Heatmap(mat, column_title = "Significantly Improved Cell ARI",
        name = "-log P value",
        column_title_gp = gpar(fontsize = 20, fontface = "bold"),
        rect_gp = gpar(lwd = 2, col = "white"),
        show_column_dend = FALSE,
        show_row_dend = FALSE,
        cluster_rows = TRUE,
        cluster_columns = FALSE,
        col = col_fun,
        row_labels = rownames(mat)) +
  rowAnnotation(rn = anno_text(unique(rownames(mat))))
options(warn = oldw)
sessionInfo()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14029797800638524, 'fraction_numerical': 0.05161404753458673, 'mean_word_length': 2.5577287066246055, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 20, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '262861', 'license': 'MIT', 'n_tokens_mistral': 2008, 'n_tokens_neox': 1776, 'n_words': 387, 'path': 'Supplementary_Fig9.ipynb', 'repo_name': 'erikjskie/ensemblemerge_manuscript', 'size': '5638'}