datapath <- "all_gex_matrix_genename_lenti.txt"
metadatapath <- "metadata_lenti.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]
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")
# 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)
#saveRDS(human, file = "/Users/chaoyuka/Desktop/Data/mRNAcap/RNAseq/251009_review_lenti148152_and_2448hr/human_mart.rds")
# 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("X148152cap1","X148152cap2","X148152cap3","X148152cyto1","X148152cyto2","X148152cyto3","X24hr1","X24hr2","X24hr3")
#"24hr1","24hr2","24hr3"
#"48hr1","48hr2","48hr3"
metadatatitrate <- metadata[c(1:9), , 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:9] != 0)
#subset capture bias samples with geneid retained
columns_of_interest_w_name <- c("Geneid","X148152cap1","X148152cap2","X148152cap3","X148152cyto1","X148152cyto2","X148152cyto3","X24hr1","X24hr2","X24hr3")
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:10] / 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,7)]+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)
#ggsave(file.path(out_path, "148152_cap_rep23.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)
#subset capture bias samples with geneid retained
columns_of_interest_w_name <- c("Geneid","X148152cap1","X148152cap2","X148152cap3","X148152cyto1","X148152cyto2","X148152cyto3","X24hr1","X24hr2","X24hr3")
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:10] / 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(1,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("Integration (Log TPM+1)") + ylab("Transfection (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, "averaged_148152_vs_24hr.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
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(4:6)])
# 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_lenticap1 <- df[,1] > 0
df$present_lenticap2 <- df[,2] > 0
df$present_lenticap3 <- df[,3] > 0
df$present_cyto1 <- df[,4] > 0
df$present_cyto2 <- df[,5] > 0
df$present_cyto3 <- df[,6] > 0
df$present_24hr1 <- df[,7] > 0
df$present_24hr2 <- df[,8] > 0
df$present_24hr3 <- df[,9] > 0
# 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_lenticap1 = mean(present_D11, na.rm=TRUE),
fraction_present_lenticap2 = mean(present_D12, na.rm=TRUE),
fraction_present_lenticap3 = mean(present_D13, 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),
fraction_present_24hr1 = mean(present_24hr1, na.rm=TRUE),
fraction_present_24hr2 = mean(present_24hr2, na.rm=TRUE),
fraction_present_24hr3 = mean(present_24hr3, na.rm=TRUE)
)
gex_sub_tpm_matrix
#check each group how many of them have non-zero count
gex_sub_tpm_matrix_148152cap <- gex_sub_tpm_matrix[1:3]
gex_sub_tpm_matrix_148152cyto <- gex_sub_tpm_matrix[4:6]
gex_sub_tpm_matrix_24hr <- gex_sub_tpm_matrix[7:9]
filtered_gex_sub_tpm_matrix_148152cap <- gex_sub_tpm_matrix_148152cap[rowSums(gex_sub_tpm_matrix_148152cap) > 0,]
filtered_gex_sub_tpm_matrix_148152cyto <- gex_sub_tpm_matrix_148152cyto[rowSums(gex_sub_tpm_matrix_148152cyto) > 0,]
filtered_gex_sub_tpm_matrix_24hr <- gex_sub_tpm_matrix_24hr[rowSums(gex_sub_tpm_matrix_24hr) > 0,]
result <- colSums(gex_sub_tpm_matrix[, 1:9] != 0)
result
gex_sub_name
gex_sub_name
gex
filtered_gex_sub_tpm_matrix
result
