write.csv(idx - 1,
file = file.path(grad_input_dir, paste0("day", d_to, "_cluster", cl,
"Others_from_day", d_from, "_cluster", cl_from, ".csv")),
row.names = FALSE)
}
peak_scale <- counts(altExp(sce[, sce$day == d_from & sce$cell_type == cl_from]))[peaks, ]
altExp(sce[, sce$day == d_from & sce$cell_type == cl_from])
peak_scale <- assay(altExp(sce[, sce$day == d_from & sce$cell_type == cl_from]))[peaks, ]
peak_scale <- assay(altExp(sce[, sce$day == d_from & sce$cell_type == cl_from])[peaks, ])
peaks_prop <- rowMeans(peak_scale != 0)
summary(peaks_prop)
peaks_prop_all <- rowMeans(assay(altExp(sce)[peaks, ]))
peaks_prop_ratio <- peaks_prop/peaks_prop_all
table(peaks_prop_ratio < 1.2)
table(peaks_prop_ratio < 1.1 | peaks_prop < 0.1)
peaks_prop
plot(peaks_prop)
plot(peaks_prop, peaks_prop_all)
hist(peaks_prop_ratio)
table(peaks_prop_ratio < 1 | peaks_prop < 0.1)
peak_scale <- assay(altExp(sce[, sce$cell_type == cl_from])[peaks, ])
peaks_prop <- rowMeans(peak_scale != 0)
summary(peaks_prop)
peaks_prop_all <- rowMeans(assay(altExp(sce)[peaks, ]))
peaks_prop_ratio <- peaks_prop/peaks_prop_all
table(peaks_prop_ratio < 1.2)
table(peaks_prop_ratio < 1 | peaks_prop < 0.1)
remove_peaks <- which(peaks_prop_ratio < 1.1 | peaks_prop < 0.1)
peaks_prop <- rowMeans(peak_scale >= 0)
summary(peaks_prop)
peak_scale <- assay(altExp(sce[, sce$cell_type == cl_from])[peaks, ])
peaks_prop <- rowMeans(peak_scale >= 0)
summary(peaks_prop)
peaks_prop_all <- rowMeans(assay(altExp(sce)[peaks, ]) >= 0)
peaks_prop_ratio <- peaks_prop/peaks_prop_all
table(peaks_prop_ratio < 1.2)
peak_scale
peak_scale <- assay(altExp(sce[, sce$cell_type == cl_from])[peaks, ])
peaks_prop <- rowMeans(peak_scale > 0)
summary(peaks_prop)
peaks_prop_all <- rowMeans(assay(altExp(sce)[peaks, ]) > 0)
peaks_prop_ratio <- peaks_prop/peaks_prop_all
table(peaks_prop_ratio < 1.2)
table(peaks_prop_ratio < 1.1 | peaks_prop < 0.1)
peak_scale <- assay(altExp(sce[, sce$cell_type == cl_from & sce$day == d_from])[peaks, ])
peaks_prop <- rowMeans(peak_scale > 0)
summary(peaks_prop)
peaks_prop_all <- rowMeans(assay(altExp(sce)[peaks, ]) > 0)
peaks_prop_ratio <- peaks_prop/peaks_prop_all
table(peaks_prop_ratio < 1.2)
table(peaks_prop_ratio < 1.1 | peaks_prop < 0.1)
table(peaks_prop_ratio < 1 | peaks_prop < 0.1)
hist(peaks_prop)
plot(peaks_prop, peaks_prop_ratio)
table(peaks_prop_ratio < 1 | peaks_prop < 0.05)
table(peaks_prop_ratio < 1 | peaks_prop < 0.01)
table(peaks_prop_ratio < 1.1 | peaks_prop < 0.01)
table(peaks_prop_ratio < 1.1 | peaks_prop < 0.05)
write.csv(remove_peaks, file = file.path(grad_input_dir, "remove_peaks_by_accessibility_idx_prop_1.1.csv"))
write.csv(remove_peaks - 1, file = file.path(grad_input_dir, "remove_peaks_by_accessibility_idx_prop_1.1.csv"))
for (d_to in c("3", "4", "7")) {
idx <- which(descendant_day2[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ] > median(descendant_day2[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ]) &
cluster_list[[d_to]] == cl_to)
print(table(celltype[names(idx)]))
print(table(day[names(idx)]))
names(idx) <- NULL
write.csv(idx - 1,
file = file.path(grad_input_dir, paste0("day", d_to, "_", cl,
"_from_day", d_from, "_", cl_from, ".csv")),
row.names = FALSE)
}
for (d_to in c("3", "4", "7")) {
idx <- which(descendant_day2[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ] > median(descendant_day2[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ]) &
cluster_list[[d_to]] != cl_to)
print(table(celltype[names(idx)]))
print(table(day[names(idx)]))
names(idx) <- NULL
write.csv(idx - 1,
file = file.path(grad_input_dir, paste0("day", d_to, "_", cl,
"Others_from_day", d_from, "_", cl_from, ".csv")),
row.names = FALSE)
}
grad_input_dir
table(peaks_prop_ratio < 1.1 | peaks_prop < 0.05)
remove_peaks <- which(peaks_prop_ratio < 1.1 | peaks_prop < 0.05)
write.csv(remove_peaks - 1, file = file.path(grad_input_dir, "remove_peaks_by_accessibility_idx_prop_1.1.csv"))
Config
rowSds(logcounts(sce))
head(rowSds(logcounts(sce)))
sce
table(sce$day)
head(rowSds(logcounts(sce)))
sd(c(1, 2, 3, 4))
x <- c(1, 2, 3, 4)
sum(x - mean(x))
x
x - mean(x)
sum((x - mean(x))^2)
sum((x - mean(x))^2)/length(x)
sqrt(sum((x - mean(x))^2)/length(x))
sqrt(sum((x - mean(x))^2)/c(length(x) - 1))
reticulate::repl_python()
sce_mESC_multiome_20210617 <- readRDS("/dski/nobackup/yingxinl/ATAC_Multiome/data/multiome/sce_mESC_multiome_20210617.rds")
rowSds(logcounts(sce_mESC_multiome_20210617))
np$load("data/data_multiome_processed/rna_all_days_std.npy")
plot(np$load("data/data_multiome_processed/rna_all_days_std.npy"), rowSds(logcounts(sce_mESC_multiome_20210617)))
abline(b = 1)
plot(np$load("data/data_multiome_processed/rna_all_days_std.npy"), rowSds(logcounts(sce_mESC_multiome_20210617)))
line(x = y)
abline(b = 1, a = 0)
rowSds(log1p(logcounts(sce_mESC_multiome_20210617)) * 10000/sum(logcounts(sce_mESC_multiome_20210617)))))
rowSds(log1p(logcounts(sce_mESC_multiome_20210617)) * 10000/sum(logcounts(sce_mESC_multiome_20210617))))
rowSds(log1p(logcounts(sce_mESC_multiome_20210617)) * 10000/sum(logcounts(sce_mESC_multiome_20210617)))
head(rowSds(log1p(logcounts(sce_mESC_multiome_20210617)) * 10000/sum(logcounts(sce_mESC_multiome_20210617))))
head(rowSds(log1p(logcounts(sce_mESC_multiome_20210617) * 10000/sum(logcounts(sce_mESC_multiome_20210617)))))
np$load("data/data_multiome_processed/rna_all_days_std.npy")
head(np$load("data/data_multiome_processed/rna_all_days_std.npy"))
scale <- apply(logcounts(sce_mESC_multiome_20210617), 2, function(x) x*10000/sum(x))
dim(scale)
rowSds(scale)
scale <- log1p(scale)
head(rowSds(scale))
head(np$load("data/data_multiome_processed/rna_all_days_std.npy"))
plot(rowSds(scale), np$load("data/data_multiome_processed/rna_all_days_std.npy"))
scale <- apply(counts(sce_mESC_multiome_20210617), 2, function(x) x*10000/sum(x))
scale <- log1p(scale); head(rowSds(scale))
scale <- apply(logcounts(sce), 2, function(x) log1p(x*10000/sum(x)))
head(rowSds(scale))
logcounts(sce)[1:6, 1:6]
scale[1:6, 1:6]
colSums(logcounts(sce))
head(rowSds(scale))
reticulate::repl_python()
d_from <- "3"
for (d_to in c("4", "7")) {
idx <- which(descendant_day2[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ] > median(descendant_day2[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ]) &
cluster_list[[d_to]] != cl_to)
print(table(celltype[names(idx)]))
print(table(day[names(idx)]))
names(idx) <- NULL
write.csv(idx - 1,
file = file.path(grad_input_dir, paste0("day", d_to, "_", cl,
"Others_from_day", d_from, "_", cl_from, ".csv")),
row.names = FALSE)
}
idx
descendant_day3 <- lapply(day3_cell_set, function(x) descendant(x, gamma[c(1, 4, 6)]))
day3_cell_set <- split(cellname_list$`3`, cluster_list$`3`)
descendant_day3 <- lapply(day3_cell_set, function(x) descendant(x, gamma[c(1, 4, 6)]))
cellname_list
cellname_list$`3`
cluster_list$`3`
day3_cell_set <- split(cellname_list$`3`, cluster_list$`3`)
names(gamma)
descendant_day3 <- lapply(day3_cell_set, function(x) descendant(x, gamma[c(4, 6)]))
d_from <- "3"
for (d_to in c("4", "7")) {
idx <- which(descendant_day3[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ] > median(descendant_day3[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ]) &
cluster_list[[d_to]] != cl_to)
print(table(celltype[names(idx)]))
print(table(day[names(idx)]))
names(idx) <- NULL
write.csv(idx - 1,
file = file.path(grad_input_dir, paste0("day", d_to, "_", cl,
"Others_from_day", d_from, "_", cl_from, ".csv")),
row.names = FALSE)
}
names(descendant_day3[[cl_from]]) <- paste("3", c("4", "7"), sep = "_")
d_from <- "3"
for (d_to in c("4", "7")) {
idx <- which(descendant_day3[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ] > median(descendant_day3[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ]) &
cluster_list[[d_to]] != cl_to)
print(table(celltype[names(idx)]))
print(table(day[names(idx)]))
names(idx) <- NULL
write.csv(idx - 1,
file = file.path(grad_input_dir, paste0("day", d_to, "_", cl,
"Others_from_day", d_from, "_", cl_from, ".csv")),
row.names = FALSE)
}
for (d_to in c("4", "7")) {
idx <- which(descendant_day3[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ] > median(descendant_day3[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ]) &
cluster_list[[d_to]] != cl_to)
print(table(celltype[names(idx)]))
print(table(day[names(idx)]))
names(idx) <- NULL
write.csv(idx - 1,
file = file.path(grad_input_dir, paste0("day", d_to, "_", cl,
"Others_from_day", d_from, "_", cl_from, ".csv")),
row.names = FALSE)
}
for (d_to in c("4", "7")) {
idx <- which(descendant_day3[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ] > median(descendant_day3[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ]) &
cluster_list[[d_to]] != cl_to)
print(table(celltype[names(idx)]))
print(table(day[names(idx)]))
names(idx) <- NULL
write.csv(idx - 1,
file = file.path(grad_input_dir, paste0("day", d_to, "_", cl,
"Others_from_day", d_from, "_", cl_from, ".csv")),
row.names = FALSE)
}
for (d_to in c("4", "7")) {
idx <- which(descendant_day3[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ] > median(descendant_day3[[cl_from]][[paste(d_from, d_to, sep = "_")]][1, ]) &
cluster_list[[d_to]] == cl_to)
print(table(celltype[names(idx)]))
print(table(day[names(idx)]))
names(idx) <- NULL
write.csv(idx - 1,
file = file.path(grad_input_dir, paste0("day", d_to, "_", cl,
"_from_day", d_from, "_", cl_from, ".csv")),
row.names = FALSE)
}
read.csv("data/NIPS/cluster_idx/day3_HSC.csv")
read.csv("data/NIPS/cluster_idx/day3_HSC.csv")[, 1]
read.csv("data/NIPS/cluster_idx/day3_HSC.csv")[, 1] + 1
cluster_list$`3`
cluster_list$`3`[read.csv("data/NIPS/cluster_idx/day3_HSC.csv")[, 1] + 1]
table(cluster_list$`3`[read.csv("data/NIPS/cluster_idx/day3_HSC.csv")[, 1] + 1])
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
results_dir <- "/albona/nobackup2/yingxinl/melanoma/results/per_sample_analysis"
figures_dir <- "/albona/nobackup2/yingxinl/melanoma/figures"
library(SingleCellExperiment)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(ggpubr)
library(moon) # Yingxin's personal package
library(pheatmap)
library(reshape2)
library(gridExtra)
library(RColorBrewer)
library(UpSetR)
library(scattermore)
library(scater)
library(scran)
library(ggridges)
library(rcartocolor)
library(Rtsne)
library(ggalluvial)
library(ggrepel)
library(BiocParallel)
library(BiocSingular)
library(BiocNeighbors)
library(openxlsx)
library(cellyx)
ggplot2::theme_set(theme_bw() + theme_yx() +
theme(axis.text.y = element_text(color = "black"),
axis.text.x = element_text(color = "black")) )
rdbu <- colorRampPalette(rev(brewer.pal(n = 7, name = "RdBu")))(100)
reds <- colorRampPalette(c("white", brewer.pal(n = 7, name = "Reds")))(100)
source("melanoma_functions.R")
library(SingleCellExperiment)
sce <- readRDS("/albona/nobackup2/yingxinl/melanoma/data/sce_melanoma.rds")
colnames(sce) <- sce$Barcode
numbat_classify <- readRDS("/albona/nobackup2/yingxinl/melanoma/results/numbat_tumour_classification.rds")
sce_melanoma <- sce[, sce$scClassify_celltype == "Tumor cells" & numbat_classify == "tumor"]
print(sce_melanoma)
scClassify_tumour_prediction <- readRDS("/dski/nobackup/yingxinl/melanoma/results/scClassify_tumour_prediction.rds")
scClassify_tumour_prediction_coarse <- readRDS("/dski/nobackup/yingxinl/melanoma/results/scClassify_tumour_prediction_coarse.rds")
sce_melanoma$scClassify_tumour_prediction_coarse <- scClassify_tumour_prediction_coarse[colnames(sce_melanoma), 3]
sce_melanoma$scClassify_tumour_prediction_coarse[grep("_", sce_melanoma$scClassify_tumour_prediction_coarse)] <- "intermediate"
sce_melanoma$scClassify_tumour_prediction_coarse[sce_melanoma$scClassify_tumour_prediction_coarse == "intermediate"] <- "unassigned"
melanoma_markers <- readxl::read_xlsx("mmc4-2.xlsx", skip = 1)
melanoma_markers <- split(melanoma_markers$Gene, melanoma_markers$Signature)
Nazarian_markers <- read.csv("results/Nazarian_mapk_signature.csv", header = FALSE)
Nazarian_markers <- intersect(Nazarian_markers$V1, rownames(sce_melanoma))
Nazarian_scores <- apply(logcounts(sce_melanoma)[Nazarian_markers, ], 2, mean_trim_low, trim = 0.1)
cc_genes <- lapply(Seurat::cc.genes.updated.2019, function(x) intersect(x, rownames(sce_melanoma)))
cc_s_scores <- apply(logcounts(sce_melanoma)[cc_genes$s.genes, ], 2, mean_trim_low, trim = 0.1)
cc_g2m_scores <- apply(logcounts(sce_melanoma)[cc_genes$g2m.genes, ], 2, mean_trim_low, trim = 0.1)
cc_scores <- cc_s_scores + cc_g2m_scores
gene_scores <- lapply(melanoma_markers, function(x) {
apply(assay(sce_melanoma, "logcounts")[intersect(x, rownames(sce_melanoma)), ], 2,
mean_trim_low, trim = 0.1)
})
melanoma_color_coarse <- RColorBrewer::brewer.pal(8, "Dark2")[c(4, 6, 5, 3, 8)]
names(melanoma_color_coarse) <- c(names(gene_scores)[c(1, 4, 2, 6)], "unassigned")
sample_meta <- data.frame(unique(colData(sce)[, c("Sample", "Condition", "Patient", "Sample.type")]))
rownames(sample_meta) <- sample_meta$Sample
keep_idx <- !sce$scClassify_celltype %in% c("intermediate", "unassigned", "Tumor cells")
celltype_prop <- table(sce$scClassify_celltype[keep_idx], sce$Sample[keep_idx])
celltype_prop <- apply(celltype_prop, 1, function(x) x/sum(x))
celltype_prop_all <- table(sce$scClassify_celltype[keep_idx])
celltype_prop_all <- celltype_prop_all/sum(celltype_prop_all)
print(celltype_prop_all)
keep_idx <- !sce_melanoma$scClassify_tumour_prediction_coarse %in% c("intermediate", "unassigned")
mal_celltype_prop <- table(sce_melanoma$scClassify_tumour_prediction_coarse[keep_idx], sce_melanoma$Sample[keep_idx])
mal_celltype_prop <- apply(mal_celltype_prop, 1, function(x) x/sum(x))
df_tmp <- reshape2::melt(cor(celltype_prop,
mal_celltype_prop[rownames(celltype_prop), ]))
df_tmp$Var1_prop <- c(celltype_prop_all[as.character(df_tmp$Var1)])
df_tmp$Var2 <- droplevels(factor(as.character(df_tmp$Var2), levels = names(melanoma_color_coarse)))
p1 <- ggplot(df_tmp, aes(x = Var2, y = Var1, color = value, size = Var1_prop)) +
geom_point() +
scale_color_gradientn(colours = rdbu) +
xlab("") +
ylab("") +
theme(aspect.ratio = 3, axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(color = "Correlation", size = "Overall % Immune", title = "All")
df_tmp <- reshape2::melt(cor(celltype_prop[grep("DMSO", rownames(celltype_prop), value = TRUE), ],
mal_celltype_prop[grep("DMSO", rownames(celltype_prop), value = TRUE), ]))
df_tmp$Var1_prop <- c(celltype_prop_all[as.character(df_tmp$Var1)])
df_tmp$Var2 <- droplevels(factor(df_tmp$Var2, levels = names(melanoma_color_coarse)))
p2 <- ggplot(df_tmp, aes(x = Var2, y = Var1, color = value, size = Var1_prop)) +
geom_point() +
scale_color_gradientn(colours = rdbu) +
xlab("") +
ylab("") +
theme(aspect.ratio = 3, axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(color = "Correlation", size = "Overall % Immune", title = "in DMSO")
df_tmp <- reshape2::melt(cor(celltype_prop[grep("DT", rownames(celltype_prop), value = TRUE), ],
mal_celltype_prop[grep("DT", rownames(celltype_prop), value = TRUE), ]))
df_tmp$Var1_prop <- c(celltype_prop_all[as.character(df_tmp$Var1)])
df_tmp$Var2 <- droplevels(factor(df_tmp$Var2, levels = names(melanoma_color_coarse)))
p3 <- ggplot(df_tmp, aes(x = Var2, y = Var1, color = value, size = Var1_prop)) +
geom_point() +
scale_color_gradientn(colours = rdbu) +
xlab("") +
ylab("") +
theme(aspect.ratio = 3, axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(color = "Correlation", size = "Overall % Immune", title = "in DT")
ggarrange(p1, p2, p3, ncol = 3, align = "hv")
df_tmp <- reshape2::melt(cor(celltype_prop[sample_meta[sample_meta$Sample.type == "Treatment naïve", ]$Sample, ],
mal_celltype_prop[sample_meta[sample_meta$Sample.type == "Treatment naïve", ]$Sample, ]))
df_tmp$Var1_prop <- c(celltype_prop_all[as.character(df_tmp$Var1)])
df_tmp$Var2 <- droplevels(factor(df_tmp$Var2, levels = names(melanoma_color_coarse)))
p2 <- ggplot(df_tmp, aes(x = Var2, y = Var1, color = value, size = Var1_prop)) +
geom_point() +
scale_color_gradientn(colours = rdbu) +
xlab("") +
ylab("") +
theme(aspect.ratio = 3, axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(color = "Correlation", size = "Overall % Immune", title = "Treatment naïve")
df_tmp <- reshape2::melt(cor(celltype_prop[sample_meta[sample_meta$Sample.type == "PROG on IO", ]$Sample, ],
mal_celltype_prop[sample_meta[sample_meta$Sample.type == "PROG on IO", ]$Sample, ]))
df_tmp$Var1_prop <- c(celltype_prop_all[as.character(df_tmp$Var1)])
df_tmp$Var2 <- droplevels(factor(df_tmp$Var2, levels = names(melanoma_color_coarse)))
p3 <- ggplot(df_tmp, aes(x = Var2, y = Var1, color = value, size = Var1_prop)) +
geom_point() +
scale_color_gradientn(colours = rdbu) +
xlab("") +
ylab("") +
theme(aspect.ratio = 3, axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(color = "Correlation", size = "Overall % Immune", title = "PROG on IO")
ggarrange(p2, p3, ncol = 2, align = "hv")
df_prop <- cbind(celltype_prop, mal_celltype_prop)
df_prop <- data.frame(df_prop)
df_prop <- cbind(df_prop, sample_meta[rownames(df_prop), ])
getPropPlot <- function(i) {
g1 <- ggplot(df_prop, aes(x = df_prop[, i], y = df_prop$Transitory, color = Sample.type)) +
geom_point(size = 2) +
theme(aspect.ratio = 1) +
scale_color_brewer(palette = "Set2") +
xlab(paste("%", i)) +
ylab("% Transitory") +
facet_wrap(~Condition)
g2 <- ggplot(df_prop, aes(x = df_prop[, i], y = df_prop$Neural.crest.like, color = Sample.type)) +
geom_point(size = 2) +
theme(aspect.ratio = 1) +
scale_color_brewer(palette = "Set2") +
xlab(paste("%", i)) +
ylab("% Neural.crest.like")  +
facet_wrap(~Condition)
g3 <- ggplot(df_prop, aes(x = df_prop[, i], y = df_prop$Undifferentiated, color = Sample.type)) +
geom_point(size = 2) +
theme(aspect.ratio = 1) +
scale_color_brewer(palette = "Set2") +
xlab(paste("%", i)) +
ylab("% Undifferentiated")  +
facet_wrap(~Condition)
ggarrange(g1, g2, g3, ncol = 1, align = "hv", common.legend = TRUE)
}
getPropPlot("Macrophages")
getPropPlot("B.Cells")
getPropPlot("T.cells")
getPropPlot("Fibroblasts")
getPropPlot2 <- function(i) {
g1 <- ggplot(df_prop, aes(x = df_prop[, i], y = df_prop$Transitory, color = Condition)) +
geom_point(size = 2) +
theme(aspect.ratio = 1) +
scale_color_brewer(palette = "Set1") +
xlab(paste("%", i)) +
ylab("% Transitory") +
facet_wrap(~Sample.type)
g2 <- ggplot(df_prop, aes(x = df_prop[, i], y = df_prop$Neural.crest.like, color = Condition)) +
geom_point(size = 2) +
theme(aspect.ratio = 1) +
scale_color_brewer(palette = "Set1") +
xlab(paste("%", i)) +
ylab("% Neural.crest.like")  +
facet_wrap(~Sample.type)
g3 <- ggplot(df_prop, aes(x = df_prop[, i], y = df_prop$Undifferentiated, color = Condition)) +
geom_point(size = 2) +
theme(aspect.ratio = 1) +
scale_color_brewer(palette = "Set1") +
xlab(paste("%", i)) +
ylab("% Undifferentiated")  +
facet_wrap(~Sample.type)
ggarrange(g1, g2, g3, ncol = 1, align = "hv", common.legend = TRUE)
}
getPropPlot2("Macrophages")
getPropPlot2("B.Cells")
getPropPlot2("T.cells")
getPropPlot2("Fibroblasts")
sce_melanoma_T <- sce[, sce$scClassify_celltype == "T cells"]
sce_melanoma_T
JA_signatures <- readxl::read_xlsx("data/Jerby-Arnon TableS6 - other signatures.xlsx", skip = 2)
JA_signatures_T <- JA_signatures[, grep("TME T", colnames(JA_signatures), value = TRUE)]
JA_signatures_T_list <- lapply(colnames(JA_signatures_T), function(x) {
res <- unlist(na.omit(JA_signatures_T[, x]))
names(res) <- NULL
res
})
names(JA_signatures_T_list) <- colnames(JA_signatures_T)
patient_marker <- doLimma(logcounts(sce_melanoma_T), sce_melanoma_T$Patient)
patient_marker <- lapply(patient_marker, function(x) x[order(x$logFC, decreasing = TRUE), ])
hvg_fit <- scran::modelGeneVar(sce_melanoma_T, block = sce_melanoma_T$Sample)
hvg <- scran::getTopHVGs(hvg_fit, n = 500)
use_bpparam <- MulticoreParam(workers = 10)
BiocParallel::bpprogressbar(use_bpparam) <- TRUE
filter <- grepl("RPL|RPS|^MT-|ERCC|MTMR|MTND|MMP1|MT2A", rownames(sce_melanoma_T))
gene_corMat <- qlcMatrix::cosSparse(t((logcounts(sce_melanoma_T)[filter, ])),
t((logcounts(sce_melanoma_T)[!filter, ])))
gene_corMat_max <- apply(gene_corMat, 2, max, na.rm = TRUE)
names(gene_corMat_max) <- rownames(logcounts(sce_melanoma_T)[!filter, ])
exclude_genes <- c(rownames(sce_melanoma_T)[filter],
names(gene_corMat_max)[gene_corMat_max > 0.8])
filter <- rownames(sce_melanoma_T) %in% exclude_genes
hvg <- setdiff(hvg, exclude_genes)
#hvg <- intersect(unlist(JA_signatures_T_list), rownames(sce_melanoma_T))
set.seed(2022)
sce_melanoma_T <- runPCA(sce_melanoma_T, ncomponents = 20,
subset_row = hvg,
BPPARAM = use_bpparam, BSPARAM = RandomParam())
set.seed(2022)
sce_melanoma_T <- runUMAP(sce_melanoma_T, dimred = "PCA", min_dist = 0.3, verbose = TRUE)
df_toPlot <- moon::makeMoonDF(sce_melanoma_T)
g1 <- ggplot(df_toPlot, aes(x = UMAP1, y = UMAP2, color = Sample)) +
geom_point() +
theme(aspect.ratio = 1) +
scale_color_tableau(palette = "Tableau 20")
g2 <- ggplot(df_toPlot, aes(x = UMAP1, y = UMAP2, color = Condition)) +
geom_point() +
theme(aspect.ratio = 1) +
scale_color_brewer(palette = "Set1")
g3 <- ggplot(df_toPlot[sample(nrow(df_toPlot)), ], aes(x = UMAP1, y = UMAP2, color = Patient)) +
geom_point() +
theme(aspect.ratio = 1) +
scale_color_tableau(palette = "Tableau 10")
g4 <- ggplot(df_toPlot[sample(nrow(df_toPlot)), ], aes(x = UMAP1, y = UMAP2, color = Sample.type)) +
geom_point() +
theme(aspect.ratio = 1)  +
scale_color_brewer(palette = "Set2")
ggarrange(g1, g2, g3, g4, ncol = 2, nrow = 2, align = "hv")
set.seed(2022)
g <- scran::buildSNNGraph(sce_melanoma_T, k = 10, use.dimred = "PCA")
cluster <- igraph::cluster_louvain(g)$membership
cluster <- factor(cluster)
sce_melanoma_T$subcluster <- cluster
table(sce_melanoma_T$subcluster, sce_melanoma_T$Sample)
ggplot(df_toPlot, aes(x = UMAP1, y = UMAP2, color = sce_melanoma_T$subcluster)) +
geom_point() +
theme(aspect.ratio = 1)  +
scale_color_brewer(palette = "Set3")
ggplot(df_toPlot, aes(x = UMAP1, y = UMAP2, color = sce_melanoma_T$subcluster)) +
geom_point() +
theme(aspect.ratio = 1)  +
scale_color_brewer(palette = "Set3") +
facet_wrap(~Patient)
library(cellyx)
T_markers <- doLimma(logcounts(sce_melanoma_T), sce_melanoma_T$Sample.type)
T_markers <- lapply(T_markers, function(x) x[order(x$logFC, decreasing = TRUE), ])
lapply(T_markers, head)
library(cellyx)
T_markers <- doLimma(logcounts(sce_melanoma_T), sce_melanoma_T$subcluster)
T_markers <- lapply(T_markers, function(x) x[order(x$logFC, decreasing = TRUE), ])
lapply(T_markers, head)
unlist(lapply(T_markers, function(x) sum(x$logFC > 1)))
markers <- c("CD3E", "CD8A", "CCL5","NKG7", "GZMK", "GZMA", "IL7R", "GNLY")
g <- lapply(intersect(markers, rownames(sce_melanoma)), function(x) {
ggplot(df_toPlot, aes(x = UMAP1, y = UMAP2, color = logcounts(sce_melanoma_T)[x, ])) +
geom_scattermore(pointsize = 2) +
theme(aspect.ratio = 1) +
scale_color_viridis_c() +
labs(color = x)
})
ggarrange(plotlist = g, ncol = 4, nrow = 2, align = "hv")
library(cellyx)
subset_idx <- sce_melanoma_T$subcluster %in% c(8, 6)
CD8T_markers <- doLimma(logcounts(sce_melanoma_T[, subset_idx]), sce_melanoma_T$subcluster[subset_idx])
CD8T_markers <- lapply(CD8T_markers, function(x) x[order(x$logFC, decreasing = TRUE), ])
lapply(CD8T_markers, head)
selected_markers <- unlist(lapply(CD8T_markers, function(x) rownames(x[x$logFC > 1 & x$meanPct.2 > 0.5 & x$meanPct.1 < 0.5, ])))
selected_markers
pheatmap(logcounts(sce_melanoma_T[selected_markers, subset_idx]),
show_colnames = FALSE,
annotation_col = data.frame(colData(sce_melanoma_T)[subset_idx, "subcluster", drop = FALSE]),
color = reds,
clustering_method = "ward.D2")
subset_idx <- !sce_melanoma_T$subcluster %in% c(8, 6)
CD4T_markers <- doLimma(logcounts(sce_melanoma_T[, subset_idx]), sce_melanoma_T$subcluster[subset_idx])
CD4T_markers <- lapply(CD4T_markers, function(x) x[order(x$logFC, decreasing = TRUE), ])
lapply(CD4T_markers, head)
selected_markers <- unlist(lapply(CD4T_markers, function(x) rownames(x[x$logFC > 1 & x$meanPct.2 > 0.5 & x$meanPct.1 < 0.5, ])[1:10]))
selected_markers <- na.omit(unique(selected_markers))
selected_markers
pheatmap(logcounts(sce_melanoma_T[selected_markers, subset_idx]),
show_colnames = FALSE,
annotation_col = data.frame(colData(sce_melanoma_T)[subset_idx, "subcluster", drop = FALSE]),
color = reds)
reticulate::repl_python()
