library(survival)
library(survminer)
## Loading required package: ggplot2
## Loading required package: ggpubr
## 
## Attaching package: 'survminer'
## The following object is masked from 'package:survival':
## 
##     myeloma
signature_adeno_matrix <- readRDS("~/final_results/14_02_2025_signature_adeno_matrix.rds")
adeno <- readRDS("~/final_results/14_02_2025_deconvolution_adeno.rds")
luad_clinic_for_bulk <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
deconvolution_adeno_scaled <- adeno / rowSums(adeno) * 100
data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))

data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)

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

data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))
colnames(data) <- gsub("-", "_", colnames(data))

coxa <- coxph(Surv(overall_survival, deceased) ~ Fibroblast_alveolar, data = data) 
coxa
## Call:
## coxph(formula = Surv(overall_survival, deceased) ~ Fibroblast_alveolar, 
##     data = data)
## 
##                        coef exp(coef) se(coef)     z     p
## Fibroblast_alveolar 0.07372   1.07651  0.06647 1.109 0.267
## 
## Likelihood ratio test=1.11  on 1 df, p=0.2915
## n= 91, number of events= 91 
##    (152 observations deleted due to missingness)
library(survival)
library(reactable)

signature_adeno_matrix <- as.data.frame(signature_adeno_matrix)
Alveolar_exp <- signature_adeno_matrix$`Fibroblast alveolar`
other_cells_exp <- rowMeans(signature_adeno_matrix[, colnames(signature_adeno_matrix) != "Fibroblast_alveolar"])
Alveolar_luad_filtered_genes <- signature_adeno_matrix[(Alveolar_exp - other_cells_exp) / other_cells_exp > 0.90, ]
Adeno_bulk <- readRDS("~/final_results/luad_bulk.rds")
Alveolar <- intersect(rownames(Alveolar_luad_filtered_genes), rownames(Adeno_bulk))
Alveolar <- Adeno_bulk[Alveolar,]
Alveolar <- t(Alveolar)
Alveolar <- as.data.frame(Alveolar)
LUAD_BULK_CLINIC <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
LUAD_BULK_CLINIC <- as.data.frame(LUAD_BULK_CLINIC)
select_col <- c("overall_survival", "vital_status")
clinic <- LUAD_BULK_CLINIC[,select_col]
data <- cbind(clinic, Alveolar)
data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)
library(survival)
exclude_cols <- c("overall_survival", "vital_status", "deceased")
gene_cols <- setdiff(colnames(data), exclude_cols)
cox_results <- list()
for (gene in gene_cols) {
  formula <- as.formula(paste("Surv(overall_survival, deceased) ~", gene))
  cox_model <- coxph(formula, data = data)
  cox_results[[gene]] <- summary(cox_model)
}

negative_genes <- c()
negative_coeffs <- c()
for (gene in names(cox_results)) {
  coef_value <- cox_results[[gene]]$coefficients[1, "coef"]  
  if (!is.na(coef_value) && coef_value < 0) {  
    negative_genes <- c(negative_genes, gene)
    negative_coeffs <- c(negative_coeffs, coef_value)  
  }
}

library(biomaRt)
ensembl <- useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl", version = 105)
gene_annotation <- getBM(attributes = c("ensembl_gene_id", "hgnc_symbol"), 
                         filters = "ensembl_gene_id", 
                         values = negative_genes, 
                         mart = ensembl)
ort <- intersect(negative_genes, rownames(signature_adeno_matrix))
sig <- signature_adeno_matrix[ort, ]

Fibroblast_alveolar_negative_gene_df <- data.frame(
  ENSG_ID = negative_genes,   
  Gene_Name = gene_annotation$hgnc_symbol,  
  Coefficient = negative_coeffs,
  Cell_Type = "Fibroblast_alveolar"
)
Fibroblast_alveolar_negative_gene_df$exp.level <- sig$`Fibroblast alveolar`

reactable(Fibroblast_alveolar_negative_gene_df)
library(survival)
library(survminer)
adeno <- readRDS("~/final_results/14_02_2025_deconvolution_adeno.rds")
luad_clinic_for_bulk <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
deconvolution_adeno_scaled <- adeno / rowSums(adeno) * 100
data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))

data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)

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

data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))
colnames(data) <- gsub("-", "_", colnames(data))

coxa <- coxph(Surv(overall_survival, deceased) ~ Macrophage, data = data) 
coxa
## Call:
## coxph(formula = Surv(overall_survival, deceased) ~ Macrophage, 
##     data = data)
## 
##                coef exp(coef) se(coef)      z     p
## Macrophage -0.04942   0.95178  0.03187 -1.551 0.121
## 
## Likelihood ratio test=2.54  on 1 df, p=0.1113
## n= 91, number of events= 91 
##    (152 observations deleted due to missingness)
library(survival)
library(reactable)
signature_adeno_matrix <- readRDS("~/final_results/14_02_2025_signature_adeno_matrix.rds")
signature_adeno_matrix <- as.data.frame(signature_adeno_matrix)
Alveolar_exp <- signature_adeno_matrix$Macrophage
other_cells_exp <- rowMeans(signature_adeno_matrix[, colnames(signature_adeno_matrix) != "Macrophage"])
Alveolar_luad_filtered_genes <- signature_adeno_matrix[(Alveolar_exp - other_cells_exp) / other_cells_exp > 0.90, ]
Adeno_bulk <- readRDS("~/final_results/luad_bulk.rds")
Alveolar <- intersect(rownames(Alveolar_luad_filtered_genes), rownames(Adeno_bulk))
Alveolar <- Adeno_bulk[Alveolar,]
Alveolar <- t(Alveolar)
Alveolar <- as.data.frame(Alveolar)
LUAD_BULK_CLINIC <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
LUAD_BULK_CLINIC <- as.data.frame(LUAD_BULK_CLINIC)
select_col <- c("overall_survival", "vital_status")
clinic <- LUAD_BULK_CLINIC[,select_col]
data <- cbind(clinic, Alveolar)
data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)
library(survival)
exclude_cols <- c("overall_survival", "vital_status", "deceased")
gene_cols <- setdiff(colnames(data), exclude_cols)
cox_results <- list()
for (gene in gene_cols) {
  formula <- as.formula(paste("Surv(overall_survival, deceased) ~", gene))
  cox_model <- coxph(formula, data = data)
  cox_results[[gene]] <- summary(cox_model)
}

negative_genes <- c()
negative_coeffs <- c()
for (gene in names(cox_results)) {
  coef_value <- cox_results[[gene]]$coefficients[1, "coef"] 
  
  if (!is.na(coef_value) && coef_value < 0) {  
    negative_genes <- c(negative_genes, gene)
    negative_coeffs <- c(negative_coeffs, coef_value)  
  }
}

library(biomaRt)
ensembl <- useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl", mirror = "www")
## Ensembl site unresponsive, trying asia mirror
## Ensembl site unresponsive, trying useast mirror
gene_annotation <- getBM(attributes = c("ensembl_gene_id", "hgnc_symbol"), 
                         filters = "ensembl_gene_id", 
                         values = negative_genes, 
                         mart = ensembl)
ort <- intersect(negative_genes, rownames(signature_adeno_matrix))
sig <- signature_adeno_matrix[ort, ]

Macrophage_negative_gene_df <- data.frame(
  ENSG_ID = negative_genes,   
  Gene_Name = gene_annotation$hgnc_symbol,  
  Coefficient = negative_coeffs,
  Cell_Type = "Macrophage"
)

Macrophage_negative_gene_df$exp.level <- sig$`Macrophage`

reactable(Macrophage_negative_gene_df)
library(survival)
library(survminer)
adeno <- readRDS("~/final_results/14_02_2025_deconvolution_adeno.rds")
luad_clinic_for_bulk <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
deconvolution_adeno_scaled <- adeno / rowSums(adeno) * 100
data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))

data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)

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

data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))
colnames(data) <- gsub("-", "_", colnames(data))

coxa <- coxph(Surv(overall_survival, deceased) ~ T_cell_CD8_naive, data = data) 
coxa
## Call:
## coxph(formula = Surv(overall_survival, deceased) ~ T_cell_CD8_naive, 
##     data = data)
## 
##                      coef exp(coef) se(coef)      z      p
## T_cell_CD8_naive -0.19359   0.82399  0.08076 -2.397 0.0165
## 
## Likelihood ratio test=6.36  on 1 df, p=0.01167
## n= 91, number of events= 91 
##    (152 observations deleted due to missingness)
library(survival)
library(reactable)
signature_adeno_matrix <- readRDS("~/final_results/14_02_2025_signature_adeno_matrix.rds")
signature_adeno_matrix <- as.data.frame(signature_adeno_matrix)
Alveolar_exp <- signature_adeno_matrix$`T cell CD8 naive`
other_cells_exp <- rowMeans(signature_adeno_matrix[, colnames(signature_adeno_matrix) != "T_cell_CD8_naive"])
Alveolar_luad_filtered_genes <- signature_adeno_matrix[(Alveolar_exp - other_cells_exp) / other_cells_exp > 0.90, ]
Adeno_bulk <- readRDS("~/final_results/luad_bulk.rds")
Alveolar <- intersect(rownames(Alveolar_luad_filtered_genes), rownames(Adeno_bulk))
Alveolar <- Adeno_bulk[Alveolar,]
Alveolar <- t(Alveolar)
Alveolar <- as.data.frame(Alveolar)
LUAD_BULK_CLINIC <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
LUAD_BULK_CLINIC <- as.data.frame(LUAD_BULK_CLINIC)
select_col <- c("overall_survival", "vital_status")
clinic <- LUAD_BULK_CLINIC[,select_col]
data <- cbind(clinic, Alveolar)
data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)
library(survival)
exclude_cols <- c("overall_survival", "vital_status", "deceased")
gene_cols <- setdiff(colnames(data), exclude_cols)
cox_results <- list()
for (gene in gene_cols) {
  formula <- as.formula(paste("Surv(overall_survival, deceased) ~", gene))
  cox_model <- coxph(formula, data = data)
  cox_results[[gene]] <- summary(cox_model)
}

negative_genes <- c()
negative_coeffs <- c()
for (gene in names(cox_results)) {
  coef_value <- cox_results[[gene]]$coefficients[1, "coef"]  
  
  if (!is.na(coef_value) && coef_value < 0) {  
    negative_genes <- c(negative_genes, gene)
    negative_coeffs <- c(negative_coeffs, coef_value) 
  }
}

library(biomaRt)
ensembl <- useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl", version = 105)
gene_annotation <- getBM(attributes = c("ensembl_gene_id", "hgnc_symbol"), 
                         filters = "ensembl_gene_id", 
                         values = negative_genes, 
                         mart = ensembl)

ort <- intersect(negative_genes, rownames(signature_adeno_matrix))
sig <- signature_adeno_matrix[ort, ] 


T_cell_CD8_naive_negative_gene_df <- data.frame(
  ENSG_ID = negative_genes,   
  Gene_Name = gene_annotation$hgnc_symbol,  
  Coefficient = negative_coeffs,
  Cell_Type = "T_cell_CD8_naive"
)

T_cell_CD8_naive_negative_gene_df$exp.level <- sig$`T cell CD8 naive`

reactable(T_cell_CD8_naive_negative_gene_df)
library(survival)
library(survminer)
adeno <- readRDS("~/final_results/14_02_2025_deconvolution_adeno.rds")
luad_clinic_for_bulk <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
deconvolution_adeno_scaled <- adeno / rowSums(adeno) * 100
data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))

data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)

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

data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))
colnames(data) <- gsub("-", "_", colnames(data))

coxa <- coxph(Surv(overall_survival, deceased) ~ B_cell, data = data) 
coxa
## Call:
## coxph(formula = Surv(overall_survival, deceased) ~ B_cell, data = data)
## 
##           coef exp(coef) se(coef)     z     p
## B_cell -0.2520    0.7772   0.2897 -0.87 0.384
## 
## Likelihood ratio test=0.84  on 1 df, p=0.3581
## n= 91, number of events= 91 
##    (152 observations deleted due to missingness)
library(survival)
library(reactable)
signature_adeno_matrix <- readRDS("~/final_results/14_02_2025_signature_adeno_matrix.rds")
signature_adeno_matrix <- as.data.frame(signature_adeno_matrix)
Alveolar_exp <- signature_adeno_matrix$`B cell`
other_cells_exp <- rowMeans(signature_adeno_matrix[, colnames(signature_adeno_matrix) != "B_cell"])
Alveolar_luad_filtered_genes <- signature_adeno_matrix[(Alveolar_exp - other_cells_exp) / other_cells_exp > 0.90, ]
Adeno_bulk <- readRDS("~/final_results/luad_bulk.rds")
Alveolar <- intersect(rownames(Alveolar_luad_filtered_genes), rownames(Adeno_bulk))
Alveolar <- Adeno_bulk[Alveolar,]
Alveolar <- t(Alveolar)
Alveolar <- as.data.frame(Alveolar)
LUAD_BULK_CLINIC <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
LUAD_BULK_CLINIC <- as.data.frame(LUAD_BULK_CLINIC)
select_col <- c("overall_survival", "vital_status")
clinic <- LUAD_BULK_CLINIC[,select_col]
data <- cbind(clinic, Alveolar)
data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)
library(survival)
exclude_cols <- c("overall_survival", "vital_status", "deceased")
gene_cols <- setdiff(colnames(data), exclude_cols)
cox_results <- list()
for (gene in gene_cols) {
  formula <- as.formula(paste("Surv(overall_survival, deceased) ~", gene))
  cox_model <- coxph(formula, data = data)
  cox_results[[gene]] <- summary(cox_model)
}

negative_genes <- c()
negative_coeffs <- c()
for (gene in names(cox_results)) {
  coef_value <- cox_results[[gene]]$coefficients[1, "coef"]  
  
  if (!is.na(coef_value) && coef_value < 0) {  
    negative_genes <- c(negative_genes, gene)
    negative_coeffs <- c(negative_coeffs, coef_value) 
  }
}

library(biomaRt)
ensembl <- useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl", version = 105)
gene_annotation <- getBM(attributes = c("ensembl_gene_id", "hgnc_symbol"), 
                         filters = "ensembl_gene_id", 
                         values = negative_genes, 
                         mart = ensembl)

ort <- intersect(negative_genes, rownames(signature_adeno_matrix))
sig <- signature_adeno_matrix[ort, ]

B_cell_negative_gene_df <- data.frame(
  ENSG_ID = negative_genes,   
  Gene_Name = gene_annotation$hgnc_symbol,  
  Coefficient = negative_coeffs,
  Cell_Type = "B_cell"
)

B_cell_negative_gene_df$exp.level <- sig$`B cell`

reactable(B_cell_negative_gene_df)
#ENSG00000102445: RUBCNL, ENSG00000175857: GAPT, ENSG00000122224: LY9, ENSG00000166405: RIC3
library(survival)
library(survminer)
signature_adeno_matrix <- readRDS("~/final_results/14_02_2025_signature_adeno_matrix.rds")
adeno <- readRDS("~/final_results/14_02_2025_deconvolution_adeno.rds")
luad_clinic_for_bulk <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
deconvolution_adeno_scaled <- adeno / rowSums(adeno) * 100
data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))

data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)

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

data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))
colnames(data) <- gsub("-", "_", colnames(data))

coxa <- coxph(Surv(overall_survival, deceased) ~ Fibroblast_alveolar, data = data) 
coxa
## Call:
## coxph(formula = Surv(overall_survival, deceased) ~ Fibroblast_alveolar, 
##     data = data)
## 
##                        coef exp(coef) se(coef)     z     p
## Fibroblast_alveolar 0.07372   1.07651  0.06647 1.109 0.267
## 
## Likelihood ratio test=1.11  on 1 df, p=0.2915
## n= 91, number of events= 91 
##    (152 observations deleted due to missingness)
library(survival)
library(reactable)

signature_adeno_matrix <- as.data.frame(signature_adeno_matrix)
Alveolar_exp <- signature_adeno_matrix$`Fibroblast alveolar`
other_cells_exp <- rowMeans(signature_adeno_matrix[, colnames(signature_adeno_matrix) != "Fibroblast_alveolar"])
Alveolar_luad_filtered_genes <- signature_adeno_matrix[(Alveolar_exp - other_cells_exp) / other_cells_exp > 0.90, ]
Adeno_bulk <- readRDS("~/final_results/luad_bulk.rds")
Alveolar <- intersect(rownames(Alveolar_luad_filtered_genes), rownames(Adeno_bulk))
Alveolar <- Adeno_bulk[Alveolar,]
Alveolar <- t(Alveolar)
Alveolar <- as.data.frame(Alveolar)
LUAD_BULK_CLINIC <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
LUAD_BULK_CLINIC <- as.data.frame(LUAD_BULK_CLINIC)
select_col <- c("overall_survival", "vital_status")
clinic <- LUAD_BULK_CLINIC[,select_col]
data <- cbind(clinic, Alveolar)
data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)
library(survival)
exclude_cols <- c("overall_survival", "vital_status", "deceased")
gene_cols <- setdiff(colnames(data), exclude_cols)
cox_results <- list()
for (gene in gene_cols) {
  formula <- as.formula(paste("Surv(overall_survival, deceased) ~", gene))
  cox_model <- coxph(formula, data = data)
  cox_results[[gene]] <- summary(cox_model)
}

negative_genes <- c()
negative_coeffs <- c()
for (gene in names(cox_results)) {
  coef_value <- cox_results[[gene]]$coefficients[1, "coef"]  
  if (!is.na(coef_value) && coef_value < 0) {  
    negative_genes <- c(negative_genes, gene)
    negative_coeffs <- c(negative_coeffs, coef_value)  
  }
}

library(biomaRt)
ensembl <- useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl", version = 105)
gene_annotation <- getBM(attributes = c("ensembl_gene_id", "hgnc_symbol"), 
                         filters = "ensembl_gene_id", 
                         values = negative_genes, 
                         mart = ensembl)
ort <- intersect(negative_genes, rownames(signature_adeno_matrix))
sig <- signature_adeno_matrix[ort, ]

Fibroblast_alveolar_negative_gene_df <- data.frame(
  ENSG_ID = negative_genes,   
  Gene_Name = gene_annotation$hgnc_symbol,  
  Coefficient = negative_coeffs,
  Cell_Type = "Fibroblast_alveolar"
)
Fibroblast_alveolar_negative_gene_df$exp.level <- sig$`Fibroblast alveolar`

reactable(Fibroblast_alveolar_negative_gene_df)
#ENSG00000157734: SNX22, ENSG00000035664: DAPK2, ENSG00000154065: ANKRD29, ENSG00000114790: ARHGEF26
library(survival)
library(survminer)
signature_adeno_matrix <- readRDS("~/final_results/14_02_2025_signature_adeno_matrix.rds")
adeno <- readRDS("~/final_results/14_02_2025_deconvolution_adeno.rds")
luad_clinic_for_bulk <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
deconvolution_adeno_scaled <- adeno / rowSums(adeno) * 100
data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))

data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)

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

data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))
colnames(data) <- gsub("-", "_", colnames(data))

coxa <- coxph(Surv(overall_survival, deceased) ~ Macrophage_alveolar, data = data) 
coxa
## Call:
## coxph(formula = Surv(overall_survival, deceased) ~ Macrophage_alveolar, 
##     data = data)
## 
##                         coef exp(coef) se(coef)      z     p
## Macrophage_alveolar -0.03434   0.96624  0.05707 -0.602 0.547
## 
## Likelihood ratio test=0.37  on 1 df, p=0.542
## n= 91, number of events= 91 
##    (152 observations deleted due to missingness)
library(survival)
library(reactable)

signature_adeno_matrix <- as.data.frame(signature_adeno_matrix)
Alveolar_exp <- signature_adeno_matrix$`Macrophage alveolar`
other_cells_exp <- rowMeans(signature_adeno_matrix[, colnames(signature_adeno_matrix) != "Macrophage_alveolar"])
Alveolar_luad_filtered_genes <- signature_adeno_matrix[(Alveolar_exp - other_cells_exp) / other_cells_exp > 0.90, ]
Adeno_bulk <- readRDS("~/final_results/luad_bulk.rds")
Alveolar <- intersect(rownames(Alveolar_luad_filtered_genes), rownames(Adeno_bulk))
Alveolar <- Adeno_bulk[Alveolar,]
Alveolar <- t(Alveolar)
Alveolar <- as.data.frame(Alveolar)
LUAD_BULK_CLINIC <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
LUAD_BULK_CLINIC <- as.data.frame(LUAD_BULK_CLINIC)
select_col <- c("overall_survival", "vital_status")
clinic <- LUAD_BULK_CLINIC[,select_col]
data <- cbind(clinic, Alveolar)
data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)
library(survival)
exclude_cols <- c("overall_survival", "vital_status", "deceased")
gene_cols <- setdiff(colnames(data), exclude_cols)
cox_results <- list()
for (gene in gene_cols) {
  formula <- as.formula(paste("Surv(overall_survival, deceased) ~", gene))
  cox_model <- coxph(formula, data = data)
  cox_results[[gene]] <- summary(cox_model)
}

negative_genes <- c()
negative_coeffs <- c()
for (gene in names(cox_results)) {
  coef_value <- cox_results[[gene]]$coefficients[1, "coef"]  
  if (!is.na(coef_value) && coef_value < 0) {  
    negative_genes <- c(negative_genes, gene)
    negative_coeffs <- c(negative_coeffs, coef_value)  
  }
}

library(biomaRt)
ensembl <- useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl", version = 105)
gene_annotation <- getBM(attributes = c("ensembl_gene_id", "hgnc_symbol"), 
                         filters = "ensembl_gene_id", 
                         values = negative_genes, 
                         mart = ensembl)
ort <- intersect(negative_genes, rownames(signature_adeno_matrix))
sig <- signature_adeno_matrix[ort, ]

Macrophage_alveolar_negative_gene_df <- data.frame(
  ENSG_ID = negative_genes,   
  Gene_Name = gene_annotation$hgnc_symbol,  
  Coefficient = negative_coeffs,
  Cell_Type = "Macrophage_alveolar"
)
Macrophage_alveolar_negative_gene_df$exp.level <- sig$`Macrophage alveolar`

reactable(Macrophage_alveolar_negative_gene_df)
#ENSG00000157734: SNX22, ENSG00000035664: DAPK2, ENSG00000154065: ANKRD29, ENSG00000114790: ARHGEF26
library(survival)
library(survminer)
signature_adeno_matrix <- readRDS("~/final_results/14_02_2025_signature_adeno_matrix.rds")
adeno <- readRDS("~/final_results/14_02_2025_deconvolution_adeno.rds")
luad_clinic_for_bulk <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
deconvolution_adeno_scaled <- adeno / rowSums(adeno) * 100
data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))

data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)

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

data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))
colnames(data) <- gsub("-", "_", colnames(data))

coxa <- coxph(Surv(overall_survival, deceased) ~ Neutrophils, data = data) 
coxa
## Call:
## coxph(formula = Surv(overall_survival, deceased) ~ Neutrophils, 
##     data = data)
## 
##               coef exp(coef) se(coef)     z     p
## Neutrophils 0.1280    1.1365   0.1633 0.784 0.433
## 
## Likelihood ratio test=0.55  on 1 df, p=0.4568
## n= 91, number of events= 91 
##    (152 observations deleted due to missingness)
library(survival)
library(reactable)

signature_adeno_matrix <- as.data.frame(signature_adeno_matrix)
Alveolar_exp <- signature_adeno_matrix$`Neutrophils`
other_cells_exp <- rowMeans(signature_adeno_matrix[, colnames(signature_adeno_matrix) != "Neutrophils"])
Alveolar_luad_filtered_genes <- signature_adeno_matrix[(Alveolar_exp - other_cells_exp) / other_cells_exp > 0.90, ]
Adeno_bulk <- readRDS("~/final_results/luad_bulk.rds")
Alveolar <- intersect(rownames(Alveolar_luad_filtered_genes), rownames(Adeno_bulk))
Alveolar <- Adeno_bulk[Alveolar,]
Alveolar <- t(Alveolar)
Alveolar <- as.data.frame(Alveolar)
LUAD_BULK_CLINIC <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
LUAD_BULK_CLINIC <- as.data.frame(LUAD_BULK_CLINIC)
select_col <- c("overall_survival", "vital_status")
clinic <- LUAD_BULK_CLINIC[,select_col]
data <- cbind(clinic, Alveolar)
data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)
library(survival)
exclude_cols <- c("overall_survival", "vital_status", "deceased")
gene_cols <- setdiff(colnames(data), exclude_cols)
cox_results <- list()
for (gene in gene_cols) {
  formula <- as.formula(paste("Surv(overall_survival, deceased) ~", gene))
  cox_model <- coxph(formula, data = data)
  cox_results[[gene]] <- summary(cox_model)
}

negative_genes <- c()
negative_coeffs <- c()
for (gene in names(cox_results)) {
  coef_value <- cox_results[[gene]]$coefficients[1, "coef"]  
  if (!is.na(coef_value) && coef_value < 0) {  
    negative_genes <- c(negative_genes, gene)
    negative_coeffs <- c(negative_coeffs, coef_value)  
  }
}

library(biomaRt)
ensembl <- useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl", version = 105)
gene_annotation <- getBM(attributes = c("ensembl_gene_id", "hgnc_symbol"), 
                         filters = "ensembl_gene_id", 
                         values = negative_genes, 
                         mart = ensembl)
ort <- intersect(negative_genes, rownames(signature_adeno_matrix))
sig <- signature_adeno_matrix[ort, ]

Neutrophils_negative_gene_df <- data.frame(
  ENSG_ID = negative_genes,   
  Gene_Name = gene_annotation$hgnc_symbol,  
  Coefficient = negative_coeffs,
  Cell_Type = "Neutrophils"
)
Neutrophils_negative_gene_df$exp.level <- sig$`Neutrophils`

reactable(Neutrophils_negative_gene_df)
#ENSG00000157734: SNX22, ENSG00000035664: DAPK2, ENSG00000154065: ANKRD29, ENSG00000114790: ARHGEF26
library(survival)
library(survminer)
signature_adeno_matrix <- readRDS("~/final_results/14_02_2025_signature_adeno_matrix.rds")
adeno <- readRDS("~/final_results/14_02_2025_deconvolution_adeno.rds")
luad_clinic_for_bulk <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
deconvolution_adeno_scaled <- adeno / rowSums(adeno) * 100
data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))

data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)

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

data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))
colnames(data) <- gsub("-", "_", colnames(data))

coxa <- coxph(Surv(overall_survival, deceased) ~ Pericyte, data = data) 
coxa
## Call:
## coxph(formula = Surv(overall_survival, deceased) ~ Pericyte, 
##     data = data)
## 
##             coef exp(coef) se(coef)      z     p
## Pericyte -0.2611    0.7702   0.1617 -1.615 0.106
## 
## Likelihood ratio test=2.67  on 1 df, p=0.1023
## n= 91, number of events= 91 
##    (152 observations deleted due to missingness)
library(survival)
library(reactable)

signature_adeno_matrix <- as.data.frame(signature_adeno_matrix)
Alveolar_exp <- signature_adeno_matrix$`Pericyte`
other_cells_exp <- rowMeans(signature_adeno_matrix[, colnames(signature_adeno_matrix) != "Pericyte"])
Alveolar_luad_filtered_genes <- signature_adeno_matrix[(Alveolar_exp - other_cells_exp) / other_cells_exp > 0.90, ]
Adeno_bulk <- readRDS("~/final_results/luad_bulk.rds")
Alveolar <- intersect(rownames(Alveolar_luad_filtered_genes), rownames(Adeno_bulk))
Alveolar <- Adeno_bulk[Alveolar,]
Alveolar <- t(Alveolar)
Alveolar <- as.data.frame(Alveolar)
LUAD_BULK_CLINIC <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
LUAD_BULK_CLINIC <- as.data.frame(LUAD_BULK_CLINIC)
select_col <- c("overall_survival", "vital_status")
clinic <- LUAD_BULK_CLINIC[,select_col]
data <- cbind(clinic, Alveolar)
data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)
library(survival)
exclude_cols <- c("overall_survival", "vital_status", "deceased")
gene_cols <- setdiff(colnames(data), exclude_cols)
cox_results <- list()
for (gene in gene_cols) {
  formula <- as.formula(paste("Surv(overall_survival, deceased) ~", gene))
  cox_model <- coxph(formula, data = data)
  cox_results[[gene]] <- summary(cox_model)
}

negative_genes <- c()
negative_coeffs <- c()
for (gene in names(cox_results)) {
  coef_value <- cox_results[[gene]]$coefficients[1, "coef"]  
  if (!is.na(coef_value) && coef_value < 0) {  
    negative_genes <- c(negative_genes, gene)
    negative_coeffs <- c(negative_coeffs, coef_value)  
  }
}

library(biomaRt)
ensembl <- useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl", version = 105)
gene_annotation <- getBM(attributes = c("ensembl_gene_id", "hgnc_symbol"), 
                         filters = "ensembl_gene_id", 
                         values = negative_genes, 
                         mart = ensembl)
ort <- intersect(negative_genes, rownames(signature_adeno_matrix))
sig <- signature_adeno_matrix[ort, ]

Pericyte_negative_gene_df <- data.frame(
  ENSG_ID = negative_genes,   
  Gene_Name = gene_annotation$hgnc_symbol,  
  Coefficient = negative_coeffs,
  Cell_Type = "Pericyte"
)
Pericyte_negative_gene_df$exp.level <- sig$`Pericyte`

reactable(Pericyte_negative_gene_df)
library(survival)
library(survminer)
signature_adeno_matrix <- readRDS("~/final_results/14_02_2025_signature_adeno_matrix.rds")
adeno <- readRDS("~/final_results/14_02_2025_deconvolution_adeno.rds")
luad_clinic_for_bulk <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
deconvolution_adeno_scaled <- adeno / rowSums(adeno) * 100
data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))

data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)

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

data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))
colnames(data) <- gsub("-", "_", colnames(data))

coxa <- coxph(Surv(overall_survival, deceased) ~ Mast_cell, data = data) 
coxa
## Call:
## coxph(formula = Surv(overall_survival, deceased) ~ Mast_cell, 
##     data = data)
## 
##             coef exp(coef) se(coef)     z     p
## Mast_cell 0.6361    1.8891   0.6100 1.043 0.297
## 
## Likelihood ratio test=1  on 1 df, p=0.3174
## n= 91, number of events= 91 
##    (152 observations deleted due to missingness)
library(survival)
library(reactable)

signature_adeno_matrix <- as.data.frame(signature_adeno_matrix)
Alveolar_exp <- signature_adeno_matrix$`Mast cell`
other_cells_exp <- rowMeans(signature_adeno_matrix[, colnames(signature_adeno_matrix) != "Mast_cell"])
Alveolar_luad_filtered_genes <- signature_adeno_matrix[(Alveolar_exp - other_cells_exp) / other_cells_exp > 0.90, ]
Adeno_bulk <- readRDS("~/final_results/luad_bulk.rds")
Alveolar <- intersect(rownames(Alveolar_luad_filtered_genes), rownames(Adeno_bulk))
Alveolar <- Adeno_bulk[Alveolar,]
Alveolar <- t(Alveolar)
Alveolar <- as.data.frame(Alveolar)
LUAD_BULK_CLINIC <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
LUAD_BULK_CLINIC <- as.data.frame(LUAD_BULK_CLINIC)
select_col <- c("overall_survival", "vital_status")
clinic <- LUAD_BULK_CLINIC[,select_col]
data <- cbind(clinic, Alveolar)
data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)
library(survival)
exclude_cols <- c("overall_survival", "vital_status", "deceased")
gene_cols <- setdiff(colnames(data), exclude_cols)
cox_results <- list()
for (gene in gene_cols) {
  formula <- as.formula(paste("Surv(overall_survival, deceased) ~", gene))
  cox_model <- coxph(formula, data = data)
  cox_results[[gene]] <- summary(cox_model)
}

negative_genes <- c()
negative_coeffs <- c()
for (gene in names(cox_results)) {
  coef_value <- cox_results[[gene]]$coefficients[1, "coef"]  
  if (!is.na(coef_value) && coef_value < 0) {  
    negative_genes <- c(negative_genes, gene)
    negative_coeffs <- c(negative_coeffs, coef_value)  
  }
}

library(biomaRt)
ensembl <- useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl", version = 105)
gene_annotation <- getBM(attributes = c("ensembl_gene_id", "hgnc_symbol"), 
                         filters = "ensembl_gene_id", 
                         values = negative_genes, 
                         mart = ensembl)
ort <- intersect(negative_genes, rownames(signature_adeno_matrix))
sig <- signature_adeno_matrix[ort, ]

Mast_cell_negative_gene_df <- data.frame(
  ENSG_ID = negative_genes,   
  Gene_Name = gene_annotation$hgnc_symbol,  
  Coefficient = negative_coeffs,
  Cell_Type = "Mast_cell"
)
Mast_cell_negative_gene_df$exp.level <- sig$`Mast cell`

reactable(Mast_cell_negative_gene_df)
library(survival)
library(survminer)
signature_adeno_matrix <- readRDS("~/final_results/14_02_2025_signature_adeno_matrix.rds")
adeno <- readRDS("~/final_results/14_02_2025_deconvolution_adeno.rds")
luad_clinic_for_bulk <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
deconvolution_adeno_scaled <- adeno / rowSums(adeno) * 100
data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))

data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)

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

data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))
colnames(data) <- gsub("-", "_", colnames(data))

coxa <- coxph(Surv(overall_survival, deceased) ~ Mesothelial, data = data) 
coxa
## Call:
## coxph(formula = Surv(overall_survival, deceased) ~ Mesothelial, 
##     data = data)
## 
##                coef exp(coef) se(coef)     z    p
## Mesothelial 0.01638   1.01652  0.01515 1.081 0.28
## 
## Likelihood ratio test=0.91  on 1 df, p=0.3408
## n= 91, number of events= 91 
##    (152 observations deleted due to missingness)
library(survival)
library(reactable)

signature_adeno_matrix <- as.data.frame(signature_adeno_matrix)
Alveolar_exp <- signature_adeno_matrix$`Mesothelial`
other_cells_exp <- rowMeans(signature_adeno_matrix[, colnames(signature_adeno_matrix) != "Mesothelial"])
Alveolar_luad_filtered_genes <- signature_adeno_matrix[(Alveolar_exp - other_cells_exp) / other_cells_exp > 0.90, ]
Adeno_bulk <- readRDS("~/final_results/luad_bulk.rds")
Alveolar <- intersect(rownames(Alveolar_luad_filtered_genes), rownames(Adeno_bulk))
Alveolar <- Adeno_bulk[Alveolar,]
Alveolar <- t(Alveolar)
Alveolar <- as.data.frame(Alveolar)
LUAD_BULK_CLINIC <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
LUAD_BULK_CLINIC <- as.data.frame(LUAD_BULK_CLINIC)
select_col <- c("overall_survival", "vital_status")
clinic <- LUAD_BULK_CLINIC[,select_col]
data <- cbind(clinic, Alveolar)
data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)
library(survival)
exclude_cols <- c("overall_survival", "vital_status", "deceased")
gene_cols <- setdiff(colnames(data), exclude_cols)
cox_results <- list()
for (gene in gene_cols) {
  formula <- as.formula(paste("Surv(overall_survival, deceased) ~", gene))
  cox_model <- coxph(formula, data = data)
  cox_results[[gene]] <- summary(cox_model)
}

negative_genes <- c()
negative_coeffs <- c()
for (gene in names(cox_results)) {
  coef_value <- cox_results[[gene]]$coefficients[1, "coef"]  
  if (!is.na(coef_value) && coef_value < 0) {  
    negative_genes <- c(negative_genes, gene)
    negative_coeffs <- c(negative_coeffs, coef_value)  
  }
}

library(biomaRt)
ensembl <- useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl", version = 105)
gene_annotation <- getBM(attributes = c("ensembl_gene_id", "hgnc_symbol"), 
                         filters = "ensembl_gene_id", 
                         values = negative_genes, 
                         mart = ensembl)
ort <- intersect(negative_genes, rownames(signature_adeno_matrix))
sig <- signature_adeno_matrix[ort, ]

Mesothelial_negative_gene_df <- data.frame(
  ENSG_ID = negative_genes,   
  Gene_Name = gene_annotation$hgnc_symbol,  
  Coefficient = negative_coeffs,
  Cell_Type = "Mesothelial"
)
Mesothelial_negative_gene_df$exp.level <- sig$`Mesothelial`

reactable(Mesothelial_negative_gene_df)
library(survival)
library(survminer)
signature_adeno_matrix <- readRDS("~/final_results/14_02_2025_signature_adeno_matrix.rds")
adeno <- readRDS("~/final_results/14_02_2025_deconvolution_adeno.rds")
luad_clinic_for_bulk <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
deconvolution_adeno_scaled <- adeno / rowSums(adeno) * 100
data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))

data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)

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

data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))
colnames(data) <- gsub("-", "_", colnames(data))

coxa <- coxph(Surv(overall_survival, deceased) ~ T_cell_CD8_activated, data = data) 
coxa
## Call:
## coxph(formula = Surv(overall_survival, deceased) ~ T_cell_CD8_activated, 
##     data = data)
## 
##                         coef exp(coef) se(coef)     z     p
## T_cell_CD8_activated 0.09551   1.10022  0.07094 1.346 0.178
## 
## Likelihood ratio test=1.52  on 1 df, p=0.2175
## n= 91, number of events= 91 
##    (152 observations deleted due to missingness)
library(survival)
library(reactable)

signature_adeno_matrix <- as.data.frame(signature_adeno_matrix)
Alveolar_exp <- signature_adeno_matrix$`T cell CD8 activated`
other_cells_exp <- rowMeans(signature_adeno_matrix[, colnames(signature_adeno_matrix) != "T_cell_CD8_activated"])
Alveolar_luad_filtered_genes <- signature_adeno_matrix[(Alveolar_exp - other_cells_exp) / other_cells_exp > 0.90, ]
Adeno_bulk <- readRDS("~/final_results/luad_bulk.rds")
Alveolar <- intersect(rownames(Alveolar_luad_filtered_genes), rownames(Adeno_bulk))
Alveolar <- Adeno_bulk[Alveolar,]
Alveolar <- t(Alveolar)
Alveolar <- as.data.frame(Alveolar)
LUAD_BULK_CLINIC <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
LUAD_BULK_CLINIC <- as.data.frame(LUAD_BULK_CLINIC)
select_col <- c("overall_survival", "vital_status")
clinic <- LUAD_BULK_CLINIC[,select_col]
data <- cbind(clinic, Alveolar)
data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)
library(survival)
exclude_cols <- c("overall_survival", "vital_status", "deceased")
gene_cols <- setdiff(colnames(data), exclude_cols)
cox_results <- list()
for (gene in gene_cols) {
  formula <- as.formula(paste("Surv(overall_survival, deceased) ~", gene))
  cox_model <- coxph(formula, data = data)
  cox_results[[gene]] <- summary(cox_model)
}

negative_genes <- c()
negative_coeffs <- c()
for (gene in names(cox_results)) {
  coef_value <- cox_results[[gene]]$coefficients[1, "coef"]  
  if (!is.na(coef_value) && coef_value < 0) {  
    negative_genes <- c(negative_genes, gene)
    negative_coeffs <- c(negative_coeffs, coef_value)  
  }
}

library(biomaRt)
ensembl <- useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl", version = 105)
gene_annotation <- getBM(attributes = c("ensembl_gene_id", "hgnc_symbol"), 
                         filters = "ensembl_gene_id", 
                         values = negative_genes, 
                         mart = ensembl)
ort <- intersect(negative_genes, rownames(signature_adeno_matrix))
sig <- signature_adeno_matrix[ort, ]

T_cell_CD8_activated_negative_gene_df <- data.frame(
  ENSG_ID = negative_genes,   
  Gene_Name = gene_annotation$hgnc_symbol,  
  Coefficient = negative_coeffs,
  Cell_Type = "T_cell_CD8_activated"
)
T_cell_CD8_activated_negative_gene_df$exp.level <- sig$`T cell CD8 activated`

reactable(T_cell_CD8_activated_negative_gene_df)
library(survival)
library(survminer)
adeno <- readRDS("~/final_results/14_02_2025_deconvolution_adeno.rds")
luad_clinic_for_bulk <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
deconvolution_adeno_scaled <- adeno / rowSums(adeno) * 100
data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))

data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)

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

data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))
colnames(data) <- gsub("-", "_", colnames(data))

coxa <- coxph(Surv(overall_survival, deceased) ~ T_cell_CD8_dividing, data = data) 
coxa
## Call:
## coxph(formula = Surv(overall_survival, deceased) ~ T_cell_CD8_dividing, 
##     data = data)
## 
##                        coef exp(coef) se(coef)    z      p
## T_cell_CD8_dividing 0.18378   1.20176  0.08876 2.07 0.0384
## 
## Likelihood ratio test=3.98  on 1 df, p=0.04618
## n= 91, number of events= 91 
##    (152 observations deleted due to missingness)
library(survival)
library(reactable)
signature_adeno_matrix <- readRDS("~/final_results/14_02_2025_signature_adeno_matrix.rds")
signature_adeno_matrix <- as.data.frame(signature_adeno_matrix)
Alveolar_exp <- signature_adeno_matrix$`T cell CD8 dividing`
other_cells_exp <- rowMeans(signature_adeno_matrix[, colnames(signature_adeno_matrix) != "T_cell_CD8_dividing"])
Alveolar_luad_filtered_genes <- signature_adeno_matrix[(Alveolar_exp - other_cells_exp) / other_cells_exp > 0.90, ]
Adeno_bulk <- readRDS("~/final_results/luad_bulk.rds")
Alveolar <- intersect(rownames(Alveolar_luad_filtered_genes), rownames(Adeno_bulk))
Alveolar <- Adeno_bulk[Alveolar,]
Alveolar <- t(Alveolar)
Alveolar <- as.data.frame(Alveolar)
LUAD_BULK_CLINIC <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
LUAD_BULK_CLINIC <- as.data.frame(LUAD_BULK_CLINIC)
select_col <- c("overall_survival", "vital_status")
clinic <- LUAD_BULK_CLINIC[,select_col]
data <- cbind(clinic, Alveolar)
data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)
library(survival)
exclude_cols <- c("overall_survival", "vital_status", "deceased")
gene_cols <- setdiff(colnames(data), exclude_cols)
cox_results <- list()
for (gene in gene_cols) {
  formula <- as.formula(paste("Surv(overall_survival, deceased) ~", gene))
  cox_model <- coxph(formula, data = data)
  cox_results[[gene]] <- summary(cox_model)
}

negative_genes <- c()
negative_coeffs <- c()
for (gene in names(cox_results)) {
  coef_value <- cox_results[[gene]]$coefficients[1, "coef"]  
  
  if (!is.na(coef_value) && coef_value > 0) {  
    negative_genes <- c(negative_genes, gene)
    negative_coeffs <- c(negative_coeffs, coef_value) 
  }
}

library(biomaRt)
ensembl <- useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl", version = 105)
gene_annotation <- getBM(attributes = c("ensembl_gene_id", "hgnc_symbol"), 
                         filters = "ensembl_gene_id", 
                         values = negative_genes, 
                         mart = ensembl)
ort <- intersect(negative_genes, rownames(signature_adeno_matrix))
sig <- signature_adeno_matrix[ort, ]

T_cell_CD8_dividing_negative_gene_df <- data.frame(
  ENSG_ID = negative_genes,   
  Gene_Name = gene_annotation$hgnc_symbol,  
  Coefficient = negative_coeffs,
  Cell_Type = "T_cell_CD8_dividing"
)
T_cell_CD8_dividing_negative_gene_df$exp.level <- sig$`T cell CD8 dividing`


reactable(T_cell_CD8_dividing_negative_gene_df)
#ENSG00000228716: DHFR, ENSG00000148019: CEP78, ENSG00000127152: BCL11B, ENSG00000134545: KLRC1
library(survival)
library(survminer)
adeno <- readRDS("~/final_results/14_02_2025_deconvolution_adeno.rds")
luad_clinic_for_bulk <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
deconvolution_adeno_scaled <- adeno / rowSums(adeno) * 100
data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))
colnames(data) <- gsub("-", "_", colnames(data))

data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)

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

data <- cbind(luad_clinic_for_bulk, deconvolution_adeno_scaled)
colnames(data) <- gsub(" ", "_", colnames(data))
colnames(data) <- gsub("-", "_", colnames(data))

coxa <- coxph(Surv(overall_survival, deceased) ~ T_cell_NK_like, data = data) 
coxa
## Call:
## coxph(formula = Surv(overall_survival, deceased) ~ T_cell_NK_like, 
##     data = data)
## 
##                    coef exp(coef) se(coef)     z     p
## T_cell_NK_like 0.006271  1.006290 0.085374 0.073 0.941
## 
## Likelihood ratio test=0.01  on 1 df, p=0.9416
## n= 91, number of events= 91 
##    (152 observations deleted due to missingness)
library(survival)
library(reactable)
signature_adeno_matrix <- readRDS("~/final_results/14_02_2025_signature_adeno_matrix.rds")
signature_adeno_matrix <- as.data.frame(signature_adeno_matrix)
Alveolar_exp <- signature_adeno_matrix$`T cell NK-like`
other_cells_exp <- rowMeans(signature_adeno_matrix[, colnames(signature_adeno_matrix) != "T_cell_NK_like"])
Alveolar_luad_filtered_genes <- signature_adeno_matrix[(Alveolar_exp - other_cells_exp) / other_cells_exp > 0.90, ]
Adeno_bulk <- readRDS("~/final_results/luad_bulk.rds")
Alveolar <- intersect(rownames(Alveolar_luad_filtered_genes), rownames(Adeno_bulk))
Alveolar <- Adeno_bulk[Alveolar,]
Alveolar <- t(Alveolar)
Alveolar <- as.data.frame(Alveolar)
LUAD_BULK_CLINIC <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
LUAD_BULK_CLINIC <- as.data.frame(LUAD_BULK_CLINIC)
select_col <- c("overall_survival", "vital_status")
clinic <- LUAD_BULK_CLINIC[,select_col]
data <- cbind(clinic, Alveolar)
data$deceased <- ifelse(data$vital_status == "Alive", FALSE, TRUE)
library(survival)
exclude_cols <- c("overall_survival", "vital_status", "deceased")
gene_cols <- setdiff(colnames(data), exclude_cols)
cox_results <- list()
for (gene in gene_cols) {
  formula <- as.formula(paste("Surv(overall_survival, deceased) ~", gene))
  cox_model <- coxph(formula, data = data)
  cox_results[[gene]] <- summary(cox_model)
}

negative_genes <- c()
negative_coeffs <- c()
for (gene in names(cox_results)) {
  coef_value <- cox_results[[gene]]$coefficients[1, "coef"]  
  
  if (!is.na(coef_value) && coef_value > 0) {  
    negative_genes <- c(negative_genes, gene)
    negative_coeffs <- c(negative_coeffs, coef_value) 
  }
}

library(biomaRt)
ensembl <- useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl", version = 105)
gene_annotation <- getBM(attributes = c("ensembl_gene_id", "hgnc_symbol"), 
                         filters = "ensembl_gene_id", 
                         values = negative_genes, 
                         mart = ensembl)
ort <- intersect(negative_genes, rownames(signature_adeno_matrix))
sig <- signature_adeno_matrix[ort, ]

T_cell_NK_like_negative_gene_df <- data.frame(
  ENSG_ID = negative_genes,   
  Gene_Name = gene_annotation$hgnc_symbol,  
  Coefficient = negative_coeffs,
  Cell_Type = "T_cell_NK_like"
)
colnames(sig) <- gsub("-", "_", colnames(sig))

T_cell_NK_like_negative_gene_df$exp.level <- sig$`T cell NK_like`

reactable(T_cell_NK_like_negative_gene_df)
#ENSG00000180739: S1PR5, ENSG00000101695: RNF125, ENSG00000100351: GRAP2, ENSG00000169252: ADRB2
library(survival)
library(survminer)
library(ggplot2)



df_list <- list(
  T_cell_NK_like_negative_gene_df,
  T_cell_CD8_dividing_negative_gene_df,
  T_cell_CD8_naive_negative_gene_df,
  T_cell_NK_like_negative_gene_df,
  Mesothelial_negative_gene_df,
  Mast_cell_negative_gene_df,
  Pericyte_negative_gene_df,
  Neutrophils_negative_gene_df,
  Macrophage_alveolar_negative_gene_df,
  Macrophage_negative_gene_df,
  Fibroblast_alveolar_negative_gene_df,
  B_cell_negative_gene_df
)

df_names <- c(
  "T_cell_NK_like",
  "T_cell_CD8_dividing",
  "T_cell_CD8_naive",
  "T_cell_NK_like",
  "Mesothelial",
  "Mast_cell",
  "Pericyte",
  "Neutrophils",
  "Macrophage_alveolar",
  "Macrophage",
  "Fibroblast_alveolar",
  "B_cell"
)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:biomaRt':
## 
##     select
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
select <- unique(unlist(lapply(df_list, function(df) {
  if (!is.null(df)) {
    df %>%
      arrange(desc(exp.level)) %>%  
      head(5) %>%  
      pull(ENSG_ID)  
  }
})))


adeno <- readRDS("~/final_results/luad_bulk.rds") |> as.data.frame() |> t() |> as.data.frame()
luad_clinic_for_bulk <- readRDS("~/final_results/luad_clinic_for_bulk.rds")
data <- cbind(luad_clinic_for_bulk, adeno)
data$deceased <- ifelse(data$vital_status == "Alive", 0, 1)
ensembl_ids <- select


mart <- useEnsembl(biomart = "genes", dataset = "hsapiens_gene_ensembl", version = 105)
gene_map <- getBM(
  attributes = c("ensembl_gene_id", "external_gene_name"),
  filters = "ensembl_gene_id",
  values = ensembl_ids,
  mart = mart
)

colnames(data)[colnames(data) %in% gene_map$ensembl_gene_id] <- gene_map$external_gene_name
data <- cbind(luad_clinic_for_bulk, data[, gene_map$external_gene_name])
data$overall_survival <- as.numeric(data$overall_survival)
univ_cox <- function(variable) {
  formula <- reformulate(variable, response = "Surv(overall_survival, deceased)")
  cox_model <- coxph(formula, data = data)
  cox_summary <- summary(cox_model)
  
  c(
    coef = cox_summary$coefficients[1],  
    HR = exp(cox_summary$coefficients[1]), 
    p_value = cox_summary$coefficients[5], 
    CI_lower = exp(cox_summary$conf.int[ ,3]), 
    CI_upper = exp(cox_summary$conf.int[ ,4])
  )
}

univ_results <- lapply(colnames(data[, 8:ncol(data)]), univ_cox) 
univ_results_df <- do.call(rbind, univ_results) |> as.data.frame()
rownames(univ_results_df) <- colnames(data[, 8:ncol(data)])
gen_expression <- data[, 8:ncol(data)]
coef_values <- univ_results_df$coef
risk_scores <- apply(gen_expression, 1, function(x) sum(x * coef_values))
risk_scores <- as.data.frame(risk_scores)
data$risk_scores <- risk_scores$risk_scores
res.cut <- surv_cutpoint(data, time = "overall_survival", event = "deceased", 
                         variables = c("risk_scores"))
cutpoint <- res.cut[["cutpoint"]][["cutpoint"]]
data$risk_scores <- ifelse(data$risk_scores > cutpoint, "HIGH", "LOW")
fit_risk_scores <- survfit(Surv(overall_survival, deceased) ~ risk_scores, data = data)
ggsurvplot(fit_risk_scores, data, pval = TRUE, risk.table = TRUE)

coxa <- coxph(Surv(overall_survival, deceased) ~ risk_scores, data = data)
coxa
## Call:
## coxph(formula = Surv(overall_survival, deceased) ~ risk_scores, 
##     data = data)
## 
##                   coef exp(coef) se(coef)      z       p
## risk_scoresLOW -0.6988    0.4972   0.2279 -3.066 0.00217
## 
## Likelihood ratio test=9.95  on 1 df, p=0.00161
## n= 91, number of events= 91 
##    (152 observations deleted due to missingness)