library(DESeq2)
library("dplyr")
library("ggplot2")
library('biomaRt')
library(stringr)
library(pheatmap)
library(RColorBrewer)
library(stats)
library(ggpubr)
library(ggpmisc)
library(ggpointdensity)
library(ggrepel)
library(ggbreak)
library(EnhancedVolcano)
library(msigdbr)
library(clusterProfiler)
library("org.Hs.eg.db")
datapath <- "all_gex_matrix_genename_input_req"
metadatapath <- "metadata_input_req.csv"
out_path <- getwd()
gex <- as.matrix(read.delim(datapath,sep="\t"))
datapath <- "all_gex_matrix_genename_input_req.txt"
metadatapath <- "metadata_input_req.csv"
out_path <- getwd()
gex <- as.matrix(read.delim(datapath,sep="\t"))
metadata <- read.csv(metadatapath,header = TRUE,row.names = "sample")
rownames(gex) <- gex[,1]
#subset samples for capture bias and toxicity
columns_of_interest <- c("CT13","CT14","CT15","CT16","CT17","CT18","CT19","CT20","CT21","HScyto10","HScyto11","HScyto12")
metadatatitrate <- metadata[c(1:12), , drop = FALSE]
gex_subtitrate <- gex[, columns_of_interest]
class(gex_subtitrate) <- "numeric"
gex_subtitrate<-na.omit(gex_subtitrate)
#check how many genes are detected
count <- sum(gex_subtitrate[,1] > 10)
non_zero_count_gex <- colSums(gex_subtitrate[, 1:12] != 0)
#subset capture bias samples with geneid retained
columns_of_interest_w_name <- c("Geneid","CT13","CT14","CT15","CT16","CT17","CT18","CT19","CT20","CT21","HScyto10","HScyto11","HScyto12")
gex_sub_name <- gex[, columns_of_interest_w_name]
colnames(gex_sub_name)[colnames(gex_sub_name) == "Geneid"] <- "hgnc_symbol"
gex_sub_name<-na.omit(gex_sub_name)
#merge avg transcript length and gex for TPM calculation
gex_sub_w_length <- inner_join(avg_isoform_length, data.frame(gex_sub_name), by = "hgnc_symbol")
# Extract gene name from GEX matrix
ensembl_list <- row.names(gex)
# Extract human data from Mart
human <- useMart("ensembl", dataset="hsapiens_gene_ensembl")
gene_coords=getBM(attributes=c("hgnc_symbol", "transcript_length","ensembl_gene_id", "start_position","end_position"), filters="hgnc_symbol", values=ensembl_list, mart=human)
# Group by gene ID and calculate the mean transcript length
avg_isoform_length <- gene_coords %>%
group_by(hgnc_symbol) %>%
summarize(avg_length = mean(transcript_length, na.rm = TRUE))
avg_length <- avg_isoform_length[,2]
# Check if NA is presented
sum(is.na(avg_length))
#plot to see the distribution
hist(avg_isoform_length$avg_length,
main = "Histogram of transcript length",
xlab = "avg_transcript_length",
ylab = "Frequency",
col = "lightblue",
border = "black", xlim = c(0,5000), breaks = 5000)
#subset capture bias samples with geneid retained
columns_of_interest_w_name <- c("Geneid","CT13","CT14","CT15","CT16","CT17","CT18","CT19","CT20","CT21","HScyto10","HScyto11","HScyto12")
gex_sub_name <- gex[, columns_of_interest_w_name]
colnames(gex_sub_name)[colnames(gex_sub_name) == "Geneid"] <- "hgnc_symbol"
gex_sub_name<-na.omit(gex_sub_name)
#merge avg transcript length and gex for TPM calculation
gex_sub_w_length <- inner_join(avg_isoform_length, data.frame(gex_sub_name), by = "hgnc_symbol")
gex_sub_w_length <- as.data.frame(gex_sub_w_length)
rownames(gex_sub_w_length) <- gex_sub_w_length[,1]
gex_sub_w_length <- as.data.frame(gex_sub_w_length[,-1])
#TPM normalization
gex_sub_w_length <- gex_sub_w_length %>%  mutate(across(where(is.character), as.numeric))
gex_sub_rate <- gex_sub_w_length[,2:13] / gex_sub_w_length$avg_length
gex_sub_tpm_matrix <- t( t(gex_sub_rate) * 1e6 / colSums(gex_sub_rate, na.rm = TRUE) )
gex_sub_tpm_matrix <- data.frame(gex_sub_tpm_matrix)
#filter all zero counts row
keep <- rowSums(gex_sub_tpm_matrix) > 0
filtered_gex_sub_tpm_matrix <- gex_sub_tpm_matrix[keep,]
#make averaged correlation plot
averaged_filtered_gex_sub_tpm_matrix <- sapply(seq(1, 9, by=3), function(i) {
rowMeans(filtered_gex_sub_tpm_matrix[, i:(i+2)], na.rm=TRUE)
})
#draw plot
df_plot <- log(averaged_filtered_gex_sub_tpm_matrix[,c(2,3)]+1) #log(TPM+1)
sample_names<-colnames(df_plot)
colnames(df_plot)[1:2] <- c("x", "y")
p_rep <-  ggplot(df_plot[df_plot[[1]] > 0 | df_plot[[2]] > 0,], aes(x, y)) +
xlab("10k cells (Log TPM+1)") + ylab("1k cells (Log TPM+1)") +
geom_pointdensity(method = 'neighbors') +
scale_color_viridis_c("# of genes",option = "mako",limits = c(0, 4000),oob = scales::oob_censor, na.value = "#DEF5E5FF") +
scale_x_continuous(breaks = seq(0, 11, by = 2)) +
scale_y_continuous(breaks = seq(0, 11, by = 2)) +
theme(axis.title= element_text(size=50, colour = "black"),
axis.text = element_text(size=50, colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black",size=2),
axis.ticks = element_line(colour = "black", size = 2),
axis.ticks.length=unit(.4, "cm"))+
geom_abline(color = 'red', linetype = "dashed",lwd = 1)+
coord_cartesian(ylim = c(0, 11), x = c(0, 11), expand = FALSE)+
stat_cor(method = "pearson", cor.coef.name = "R", aes(label = ..r.label..), size = 10)
#ggsave(file.path(out_path, "10kvs1k.pdf"), p_rep, dpi = 300, width = 10, height = 10)
#write.csv(filtered_gex_sub_tpm_matrix, file = file.path(out_path, "filtered_gex_sub_tpm_matrix.csv"), row.names = TRUE)
Thip_rep
p_rep
#check each group how many of them have non-zero count
gex_sub_tpm_matrix_1M <- gex_sub_tpm_matrix[1:3]
gex_sub_tpm_matrix_10k <- gex_sub_tpm_matrix[4:6]
gex_sub_tpm_matrix_1k <- gex_sub_tpm_matrix[7:9]
filtered_gex_sub_tpm_matrix_1M <- gex_sub_tpm_matrix_1M[rowSums(gex_sub_tpm_matrix_1M) > 0,]
filtered_gex_sub_tpm_matrix_10k <- gex_sub_tpm_matrix_10k[rowSums(gex_sub_tpm_matrix_10k) > 0,]
filtered_gex_sub_tpm_matrix_1k <- gex_sub_tpm_matrix_1k[rowSums(gex_sub_tpm_matrix_1k) > 0,]
result <- colSums(gex_sub_tpm_matrix[, 1:9] != 0)
result
result_captured
# non zero filtered
df <- filtered_gex_sub_tpm_matrix
# Compute mean TPM for each gene using lysate
df$mean_TPM <- rowMeans(df[, c(10:12)])
# Assign deciles
df$decile <- ntile(df$mean_TPM, 10)
# For each gene, check if it's present
df$present_1M1 <- df[,1] > 0
df$present_1M2 <- df[,2] > 0
df$present_1M3 <- df[,3] > 0
df$present_10k1 <- df[,4] > 0
df$present_10k2 <- df[,5] > 0
df$present_10k3 <- df[,6] > 0
df$present_1k1 <- df[,7] > 0
df$present_1k2 <- df[,8] > 0
df$present_1k3 <- df[,9] > 0
df$present_cyto1 <- df[,10] > 0
df$present_cyto2 <- df[,11] > 0
df$present_cyto3 <- df[,12] > 0
# 4. For each decile, calculate fraction present for each replicate
result_captured <- df %>%
group_by(decile) %>%
summarise(
fraction_present_1M1 = mean(present_1M1, na.rm=TRUE),
fraction_present_1M2 = mean(present_1M2, na.rm=TRUE),
fraction_present_1M3 = mean(present_1M3, na.rm=TRUE),
fraction_present_10k1 = mean(present_10k1, na.rm=TRUE),
fraction_present_10k2 = mean(present_10k2, na.rm=TRUE),
fraction_present_10k3 = mean(present_10k3, na.rm=TRUE),
fraction_present_1k1 = mean(present_1k1, na.rm=TRUE),
fraction_present_1k2 = mean(present_1k2, na.rm=TRUE),
fraction_present_1k3 = mean(present_1k3, na.rm=TRUE),
)
result_cyto <- df %>%
group_by(decile) %>%
summarise(
fraction_present_cyto1 = mean(present_cyto1, na.rm=TRUE),
fraction_present_cyto2 = mean(present_cyto2, na.rm=TRUE),
fraction_present_cyto3 = mean(present_cyto3, na.rm=TRUE)
)
# bin by TPM
breaks <- c(0, 0.1, 1, 10, 100, Inf)
labels <- c("0-0.1", "0.1-1", "1-10", "10-100", ">100")
# Bin the genes
df$TPM_bin <- cut(df$mean_TPM, breaks = breaks, labels = labels, right = FALSE)
# Example: Check distribution
table(df$TPM_bin)
#
result_TPMbin <- df %>%
group_by(TPM_bin) %>%
summarise(
fraction_present_1M1 = mean(present_1M1, na.rm=TRUE),
fraction_present_1M2 = mean(present_1M2, na.rm=TRUE),
fraction_present_1M3 = mean(present_1M3, na.rm=TRUE),
fraction_present_10k1 = mean(present_10k1, na.rm=TRUE),
fraction_present_10k2 = mean(present_10k2, na.rm=TRUE),
fraction_present_10k3 = mean(present_10k3, na.rm=TRUE),
fraction_present_1k1 = mean(present_1k1, na.rm=TRUE),
fraction_present_1k2 = mean(present_1k2, na.rm=TRUE),
fraction_present_1k3 = mean(present_1k3, na.rm=TRUE),
fraction_present_cyto1 = mean(present_cyto1, na.rm=TRUE),
fraction_present_cyto2 = mean(present_cyto2, na.rm=TRUE),
fraction_present_cyto3 = mean(present_cyto3, na.rm=TRUE)
)
#write.csv(result_TPMbin, file = file.path(out_path, "result_TPMbin.csv"), row.names = TRUE)
result_captured
result_TPMbin
table(df$TPM_bin)
datapath <- "all_gex_matrix_genename_input_req.txt"
metadatapath <- "metadata_input_req.csv"
out_path <- getwd()
gex <- as.matrix(read.delim(datapath,sep="\t"))
metadata <- read.csv(metadatapath,header = TRUE,row.names = "sample")
rownames(gex) <- gex[,1]
datapath <- "all_gex_matrix_genename_input_req.txt"
metadatapath <- "metadata_input_req.csv"
out_path <- getwd()
gex <- as.matrix(read.delim(datapath,sep="\t"))
metadata <- read.csv(metadatapath,header = TRUE,row.names = "sample")
rownames(gex) <- gex[,1]
# Extract gene name from GEX matrix
ensembl_list <- row.names(gex)
# Extract human data from Mart
human <- useMart("ensembl", dataset="hsapiens_gene_ensembl")
gene_coords=getBM(attributes=c("hgnc_symbol", "transcript_length","ensembl_gene_id", "start_position","end_position"), filters="hgnc_symbol", values=ensembl_list, mart=human)
# Group by gene ID and calculate the mean transcript length
avg_isoform_length <- gene_coords %>%
group_by(hgnc_symbol) %>%
summarize(avg_length = mean(transcript_length, na.rm = TRUE))
avg_length <- avg_isoform_length[,2]
# Check if NA is presented
sum(is.na(avg_length))
#plot to see the distribution
hist(avg_isoform_length$avg_length,
main = "Histogram of transcript length",
xlab = "avg_transcript_length",
ylab = "Frequency",
col = "lightblue",
border = "black", xlim = c(0,5000), breaks = 5000)
#subset samples for capture bias and toxicity
columns_of_interest <- c("CT13","CT14","CT15","CT16","CT17","CT18","CT19","CT20","CT21","HScyto10","HScyto11","HScyto12")
metadatatitrate <- metadata[c(1:12), , drop = FALSE]
gex_subtitrate <- gex[, columns_of_interest]
class(gex_subtitrate) <- "numeric"
gex_subtitrate<-na.omit(gex_subtitrate)
#check how many genes are detected
count <- sum(gex_subtitrate[,1] > 10)
non_zero_count_gex <- colSums(gex_subtitrate[, 1:12] != 0)
#subset capture bias samples with geneid retained
columns_of_interest_w_name <- c("Geneid","CT13","CT14","CT15","CT16","CT17","CT18","CT19","CT20","CT21","HScyto10","HScyto11","HScyto12")
gex_sub_name <- gex[, columns_of_interest_w_name]
colnames(gex_sub_name)[colnames(gex_sub_name) == "Geneid"] <- "hgnc_symbol"
gex_sub_name<-na.omit(gex_sub_name)
#merge avg transcript length and gex for TPM calculation
gex_sub_w_length <- inner_join(avg_isoform_length, data.frame(gex_sub_name), by = "hgnc_symbol")
gex_sub_w_length <- as.data.frame(gex_sub_w_length)
rownames(gex_sub_w_length) <- gex_sub_w_length[,1]
gex_sub_w_length <- as.data.frame(gex_sub_w_length[,-1])
#TPM normalization
gex_sub_w_length <- gex_sub_w_length %>%  mutate(across(where(is.character), as.numeric))
gex_sub_rate <- gex_sub_w_length[,2:13] / gex_sub_w_length$avg_length
gex_sub_tpm_matrix <- t( t(gex_sub_rate) * 1e6 / colSums(gex_sub_rate, na.rm = TRUE) )
gex_sub_tpm_matrix <- data.frame(gex_sub_tpm_matrix)
#filter all zero counts row
keep <- rowSums(gex_sub_tpm_matrix) > 0
filtered_gex_sub_tpm_matrix <- gex_sub_tpm_matrix[keep,]
#make averaged correlation plot
averaged_filtered_gex_sub_tpm_matrix <- sapply(seq(1, 9, by=3), function(i) {
rowMeans(filtered_gex_sub_tpm_matrix[, i:(i+2)], na.rm=TRUE)
})
#draw plot
df_plot <- log(averaged_filtered_gex_sub_tpm_matrix[,c(2,3)]+1) #log(TPM+1)
sample_names<-colnames(df_plot)
colnames(df_plot)[1:2] <- c("x", "y")
p_rep <-  ggplot(df_plot[df_plot[[1]] > 0 | df_plot[[2]] > 0,], aes(x, y)) +
xlab("10k cells (Log TPM+1)") + ylab("1k cells (Log TPM+1)") +
geom_pointdensity(method = 'neighbors') +
scale_color_viridis_c("# of genes",option = "mako",limits = c(0, 4000),oob = scales::oob_censor, na.value = "#DEF5E5FF") +
scale_x_continuous(breaks = seq(0, 11, by = 2)) +
scale_y_continuous(breaks = seq(0, 11, by = 2)) +
theme(axis.title= element_text(size=50, colour = "black"),
axis.text = element_text(size=50, colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black",size=2),
axis.ticks = element_line(colour = "black", size = 2),
axis.ticks.length=unit(.4, "cm"))+
geom_abline(color = 'red', linetype = "dashed",lwd = 1)+
coord_cartesian(ylim = c(0, 11), x = c(0, 11), expand = FALSE)+
stat_cor(method = "pearson", cor.coef.name = "R", aes(label = ..r.label..), size = 10)
#ggsave(file.path(out_path, "10kvs1k.pdf"), p_rep, dpi = 300, width = 10, height = 10)
#write.csv(filtered_gex_sub_tpm_matrix, file = file.path("filtered_gex_sub_tpm_matrix.csv"), row.names = TRUE)
p_rep
#check each group how many of them have non-zero count
gex_sub_tpm_matrix_1M <- gex_sub_tpm_matrix[1:3]
gex_sub_tpm_matrix_10k <- gex_sub_tpm_matrix[4:6]
gex_sub_tpm_matrix_1k <- gex_sub_tpm_matrix[7:9]
filtered_gex_sub_tpm_matrix_1M <- gex_sub_tpm_matrix_1M[rowSums(gex_sub_tpm_matrix_1M) > 0,]
filtered_gex_sub_tpm_matrix_10k <- gex_sub_tpm_matrix_10k[rowSums(gex_sub_tpm_matrix_10k) > 0,]
filtered_gex_sub_tpm_matrix_1k <- gex_sub_tpm_matrix_1k[rowSums(gex_sub_tpm_matrix_1k) > 0,]
result <- colSums(gex_sub_tpm_matrix[, 1:9] != 0)
# non zero filtered
df <- filtered_gex_sub_tpm_matrix
# Compute mean TPM for each gene using lysate
df$mean_TPM <- rowMeans(df[, c(10:12)])
# Assign deciles
df$decile <- ntile(df$mean_TPM, 10)
# For each gene, check if it's present
df$present_1M1 <- df[,1] > 0
df$present_1M2 <- df[,2] > 0
df$present_1M3 <- df[,3] > 0
df$present_10k1 <- df[,4] > 0
df$present_10k2 <- df[,5] > 0
df$present_10k3 <- df[,6] > 0
df$present_1k1 <- df[,7] > 0
df$present_1k2 <- df[,8] > 0
df$present_1k3 <- df[,9] > 0
df$present_cyto1 <- df[,10] > 0
df$present_cyto2 <- df[,11] > 0
df$present_cyto3 <- df[,12] > 0
# 4. For each decile, calculate fraction present for each replicate
result_captured <- df %>%
group_by(decile) %>%
summarise(
fraction_present_1M1 = mean(present_1M1, na.rm=TRUE),
fraction_present_1M2 = mean(present_1M2, na.rm=TRUE),
fraction_present_1M3 = mean(present_1M3, na.rm=TRUE),
fraction_present_10k1 = mean(present_10k1, na.rm=TRUE),
fraction_present_10k2 = mean(present_10k2, na.rm=TRUE),
fraction_present_10k3 = mean(present_10k3, na.rm=TRUE),
fraction_present_1k1 = mean(present_1k1, na.rm=TRUE),
fraction_present_1k2 = mean(present_1k2, na.rm=TRUE),
fraction_present_1k3 = mean(present_1k3, na.rm=TRUE),
)
result_cyto <- df %>%
group_by(decile) %>%
summarise(
fraction_present_cyto1 = mean(present_cyto1, na.rm=TRUE),
fraction_present_cyto2 = mean(present_cyto2, na.rm=TRUE),
fraction_present_cyto3 = mean(present_cyto3, na.rm=TRUE)
)
# bin by TPM
breaks <- c(0, 0.1, 1, 10, 100, Inf)
labels <- c("0-0.1", "0.1-1", "1-10", "10-100", ">100")
# Bin the genes
df$TPM_bin <- cut(df$mean_TPM, breaks = breaks, labels = labels, right = FALSE)
# Example: Check distribution
table(df$TPM_bin)
#
result_TPMbin <- df %>%
group_by(TPM_bin) %>%
summarise(
fraction_present_1M1 = mean(present_1M1, na.rm=TRUE),
fraction_present_1M2 = mean(present_1M2, na.rm=TRUE),
fraction_present_1M3 = mean(present_1M3, na.rm=TRUE),
fraction_present_10k1 = mean(present_10k1, na.rm=TRUE),
fraction_present_10k2 = mean(present_10k2, na.rm=TRUE),
fraction_present_10k3 = mean(present_10k3, na.rm=TRUE),
fraction_present_1k1 = mean(present_1k1, na.rm=TRUE),
fraction_present_1k2 = mean(present_1k2, na.rm=TRUE),
fraction_present_1k3 = mean(present_1k3, na.rm=TRUE),
fraction_present_cyto1 = mean(present_cyto1, na.rm=TRUE),
fraction_present_cyto2 = mean(present_cyto2, na.rm=TRUE),
fraction_present_cyto3 = mean(present_cyto3, na.rm=TRUE)
)
#write.csv(result_TPMbin, file = file.path("result_TPMbin.csv"), row.names = TRUE)
result_TPMbin
