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_0831_timelength_withcapbias_cyto.txt"
metadatapath <- "metadata_0831_capbias.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("day11","day12","day13","day31","day32","day33","day51","day52","day53","pp_1_cyto","pp_2_cyto","pp_3_cyto")
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)
columns_of_interest_w_name <- c("Geneid","day11","day12","day13","day31","day32","day33","day51","day52","day53","pp_1_cyto","pp_2_cyto","pp_3_cyto")
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 < 1
keep <- rowSums(gex_sub_tpm_matrix) > 0
filtered_gex_sub_tpm_matrix <- gex_sub_tpm_matrix[keep,]
#draw replicates
df_plot <- log(filtered_gex_sub_tpm_matrix[,c(1,9)]+1) #log(TPM+1)
sample_names<-colnames(df_plot)
colnames(df_plot)[1:2] <- c("x", "y")
ggplot(df_plot[df_plot[[1]] > 0 | df_plot[[2]] > 0,], aes(x, y)) + #df_plot[df_plot[[1]] > 0 & df_plot[[2]] > 0
xlab("Captured_rep1 (Log TPM+1)") + ylab("Captured_rep2 (Log TPM+1)") +  #xlab("Captured_rep2 (Log TPM+1)") + ylab("Lysate_rep (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)
#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("Day3 (Log TPM+1)") + ylab("Day5 (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, "D3vsD5.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)
p_rep
#check each group how many of them have non-zero count
gex_sub_tpm_matrix_10kc <- gex_sub_tpm_matrix[1:3]
gex_sub_tpm_matrix_100c <- gex_sub_tpm_matrix[4:6]
gex_sub_tpm_matrix_10c <- gex_sub_tpm_matrix[7:9]
filtered_gex_sub_tpm_matrix_10kc <- gex_sub_tpm_matrix_10kc[rowSums(gex_sub_tpm_matrix_10kc) > 0,]
filtered_gex_sub_tpm_matrix_100c <- gex_sub_tpm_matrix_100c[rowSums(gex_sub_tpm_matrix_100c) > 0,]
filtered_gex_sub_tpm_matrix_10c <- gex_sub_tpm_matrix_10c[rowSums(gex_sub_tpm_matrix_10c) > 0,]
result <- colSums(gex_sub_tpm_matrix[, 1:9] != 0)
result
result_TPMbin
df <- filtered_gex_sub_tpm_matrix # non zero filtered
# Compute mean TPM for each gene across all six replicates
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 in VLP (TPM > 0 in any VLP column)
df$present_D11 <- df[,1] > 0
df$present_D12 <- df[,2] > 0
df$present_D13 <- df[,3] > 0
df$present_D31 <- df[,4] > 0
df$present_D32 <- df[,5] > 0
df$present_D33 <- df[,6] > 0
df$present_D51 <- df[,7] > 0
df$present_D52 <- df[,8] > 0
df$present_D53 <- 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 VLP replicate
result_captured <- df %>%
group_by(decile) %>%
summarise(
fraction_present_D11 = mean(present_D11, na.rm=TRUE),
fraction_present_D12 = mean(present_D12, na.rm=TRUE),
fraction_present_D13 = mean(present_D13, na.rm=TRUE),
fraction_present_D31 = mean(present_D31, na.rm=TRUE),
fraction_present_D32 = mean(present_D32, na.rm=TRUE),
fraction_present_D33 = mean(present_D33, na.rm=TRUE),
fraction_present_D51 = mean(present_D51, na.rm=TRUE),
fraction_present_D52 = mean(present_D52, na.rm=TRUE),
fraction_present_D53 = mean(present_D53, 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_D11 = mean(present_D11, na.rm=TRUE),
fraction_present_D12 = mean(present_D12, na.rm=TRUE),
fraction_present_D13 = mean(present_D13, na.rm=TRUE),
fraction_present_D31 = mean(present_D31, na.rm=TRUE),
fraction_present_D32 = mean(present_D32, na.rm=TRUE),
fraction_present_D33 = mean(present_D33, na.rm=TRUE),
fraction_present_D51 = mean(present_D51, na.rm=TRUE),
fraction_present_D52 = mean(present_D52, na.rm=TRUE),
fraction_present_D53 = mean(present_D53, 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)
)
result_TPMbin
result_TPMbin
datapath <- "all_gex_matrix_genename_0831_timelength_with_capbias_cyto.txt"
metadatapath <- "metadata_0831_capbias.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]
