deconvolution_adeno <- readRDS("~/adeno_deconv_for_score.rds")
selected_columns <- c("Plasma cell", "Plasma cell dividing", "Mast cell", "B cell", "B cell dividing")
B_matrix <- deconvolution_adeno[, selected_columns]
B_matrix <- as.matrix(B_matrix)
row_sums <- rowSums(B_matrix)
B_matrix_df <- as.data.frame(B_matrix)
B_matrix_df$B_Sum <- row_sums
B_matrix_with_sum <- as.matrix(B_matrix_df)
selected_columns <- c( "T cell CD4","T cell CD4 dividing","T cell CD8 activated","T cell CD8 dividing","T cell CD8 effector memory","T cell CD8 naive","T cell CD8 terminally exhausted","T cell NK-like","T cell regulatory"  )
T_matrix <- deconvolution_adeno[, selected_columns]
T_matrix <- as.matrix(T_matrix)
row_sums <- rowSums(T_matrix)
T_matrix_df <- as.data.frame(T_matrix)
T_matrix_df$T_Sum <- row_sums
T_matrix_with_sum <- as.matrix(T_matrix_df)
selected_columns <- c( "Macrophage" , "Myeloid dividing"  , "Macrophage alveolar", "cDC1", "cDC2", "pDC", "DC mature", "Monocyte classical", "Monocyte non-classical", "Myeloid dividing" )
M_matrix <- deconvolution_adeno[, selected_columns]
M_matrix <- as.matrix(M_matrix)
row_sums <- rowSums(M_matrix)
M_matrix_df <- as.data.frame(M_matrix)
M_matrix_df$M_Sum <- row_sums
M_matrix_with_sum <- as.matrix(M_matrix_df)
ct_selected_columns <- c("Alveolar cell type 1", "Alveolar cell type 2", "Cancer cells" ,  "Ciliated", "Club","Endothelial cell arterial","Endothelial cell capillary","Endothelial cell lymphatic","Endothelial cell venous","Fibroblast adventitial","Fibroblast alveolar","Fibroblast peribronchial","Mesothelial","Neutrophils","NK cell","NK cell dividing","Pericyte", "ROS1+ healthy epithelial","Smooth muscle cell","Stromal dividing",  "Transitional Club/AT2")
ct_matrix <- deconvolution_adeno[, ct_selected_columns]
ct_matrix <- as.matrix(ct_matrix)
row_sums <- rowSums(ct_matrix)
ct_matrix_df <- as.data.frame(ct_matrix)
ct_matrix_df$CT_Sum <- row_sums
ct_matrix_with_sum <- as.matrix(ct_matrix_df)
sscore <- cbind(B_matrix_df$B_Sum, T_matrix_df$T_Sum, M_matrix_df$M_Sum, ct_matrix_df$CT_Sum)
score_df_adeno <- data.frame(sscore)
rownames(score_df_adeno) <- rownames(B_matrix_df)
colnames(score_df_adeno) <- c("B_Sum", "T_Sum", "M_Sum","Cold_Tumor_Sum")
saveRDS(score_df_adeno,"~/score_df_adeno.rds")
score_df_adeno <- readRDS("~/score_df_adeno.rds")
reactable::reactable(score_df_adeno)
library(survival)
library(survminer)
## Loading required package: ggplot2
## Loading required package: ggpubr
## 
## Attaching package: 'survminer'
## The following object is masked from 'package:survival':
## 
##     myeloma
ADE_SURVIVAL <- readRDS("~/Documents/r_codes/clinic_deconv_adeno.rds")
clinical <- ADE_SURVIVAL
clinical$deceased <- ifelse(clinical$vital_status == "Alive", FALSE,TRUE)
clinical$overall_survival <- ifelse(clinical$vital_status == "Alive",
                                    clinical$days_to_last_follow_up,
                                    clinical$days_to_death)
data <- clinical
data$overall_survival <- as.numeric(data$overall_survival)
library(survival)
library(survminer)
library(ggplot2)

b_selected_columns <- c("Plasma cell", "Plasma cell dividing", "Mast cell", "B cell", "B cell dividing")
data$b_selected_mean <- rowMeans(data[b_selected_columns])

res.cut <- surv_cutpoint(data, time = "overall_survival", event = "deceased",
                         variables = c("b_selected_mean"))
cutpoint <- res.cut[["cutpoint"]][["cutpoint"]]
data$b_selected_mean <- ifelse(data$`b_selected_mean` > cutpoint, "HIGH", "LOW")
fit_b_selected_mean <- survfit(Surv(overall_survival, deceased) ~ b_selected_mean, data = data)
ggsurvplot(fit_b_selected_mean,
           data,
           pval = TRUE,
           risk.table = TRUE)

library(survival)
library(survminer)
library(ggplot2)

t_selected_columns <- c("T cell CD4","T cell CD4 dividing","T cell CD8 activated","T cell CD8 dividing","T cell CD8 effector memory","T cell CD8 naive","T cell CD8 terminally exhausted","T cell NK-like","T cell regulatory")
data$t_selected_mean <- rowMeans(data[t_selected_columns])

res.cut <- surv_cutpoint(data, time = "overall_survival", event = "deceased",
                         variables = c("t_selected_mean"))
cutpoint <- res.cut[["cutpoint"]][["cutpoint"]]
data$t_selected_mean <- ifelse(data$`t_selected_mean` > cutpoint, "HIGH", "LOW")
fit_t_selected_mean <- survfit(Surv(overall_survival, deceased) ~ t_selected_mean, data = data)
ggsurvplot(fit_t_selected_mean,
           data,
           pval = TRUE,
           risk.table = TRUE)

library(survival)
library(survminer)
library(ggplot2)

m_selected_columns <- c("Macrophage" , "Myeloid dividing"  , "Macrophage alveolar", "cDC1", "cDC2", "pDC", "DC mature", "Monocyte classical", "Monocyte non-classical", "Myeloid dividing")
data$m_selected_mean <- rowMeans(data[m_selected_columns])

res.cut <- surv_cutpoint(data, time = "overall_survival", event = "deceased",
                         variables = c("m_selected_mean"))
cutpoint <- res.cut[["cutpoint"]][["cutpoint"]]
data$m_selected_mean <- ifelse(data$`m_selected_mean` > cutpoint, "HIGH", "LOW")
fit_m_selected_mean <- survfit(Surv(overall_survival, deceased) ~ m_selected_mean, data = data)
ggsurvplot(fit_m_selected_mean,
           data,
           pval = TRUE,
           risk.table = TRUE)

library(survival)
library(survminer)
library(ggplot2)

ct_selected_columns <- c("Alveolar cell type 1", "Alveolar cell type 2", "Cancer cells" ,  "Ciliated", "Club","Endothelial cell arterial","Endothelial cell capillary","Endothelial cell lymphatic","Endothelial cell venous","Fibroblast adventitial","Fibroblast alveolar","Fibroblast peribronchial","Mesothelial","Neutrophils","NK cell","NK cell dividing","Pericyte", "ROS1+ healthy epithelial","Smooth muscle cell","Stromal dividing",  "Transitional Club/AT2")
data$ct_selected_mean <- rowMeans(data[ct_selected_columns])

res.cut <- surv_cutpoint(data, time = "overall_survival", event = "deceased",
                         variables = c("ct_selected_mean"))
cutpoint <- res.cut[["cutpoint"]][["cutpoint"]]
data$ct_selected_mean <- ifelse(data$`ct_selected_mean` > cutpoint, "HIGH", "LOW")
fit_ct_selected_mean <- survfit(Surv(overall_survival, deceased) ~ ct_selected_mean, data = data)
ggsurvplot(fit_ct_selected_mean,
           data,
           pval = TRUE,
           risk.table = TRUE)

b_df <- data.frame(Time = fit_b_selected_mean$time, Surv = fit_b_selected_mean$surv, Group = "B")
t_df <- data.frame(Time = fit_t_selected_mean$time, Surv = fit_t_selected_mean$surv, Group = "T")
m_df <- data.frame(Time = fit_m_selected_mean$time, Surv = fit_m_selected_mean$surv, Group = "M")
ct_df <- data.frame(Time = fit_ct_selected_mean$time, Surv = fit_ct_selected_mean$surv, Group = "CT")

plot_df <- rbind(b_df, t_df, m_df, ct_df)

p <- ggplot(plot_df, aes(x = Time, y = Surv, color = Group)) +
  geom_line() +
  labs(title = "Kaplan-Meier Survival Curves for B, T, M, CT Groups",
       x = "Time",
       y = "Survival Probability") +
  theme_minimal()

print(p)