######ssGSEA
#BiocManager::install('devtools')
library(devtools)
#BiocManager::install('Biostrings')
#BiocManager::install("GSVA")
#BiocManager::install('GenomeInfoDbData')
library(dplyr)
library(tidyverse)
library(GSVA)
library(ggplot2)
geneSet <- read.csv("CellReports.txt",header = F,sep = "\t",) 
table(is.na(geneSet ))# 
class(geneSet)
geneSet <- geneSet %>%column_to_rownames("V1")%>%t()
a <- geneSet
a <- a[1:nrow(a),]
set <- colnames(a)

l <- list()
#i <- "Activated CD8 T cell"
for (i in set) {
  x <-  as.character(a[,i])
  x <- x[nchar(x)!=0]
  x <-  as.character(x)
  l[[i]] <-x
}
Imu = as.list(c(l[["Central memory CD8 T cell"]],l[["Effector memeory CD8 T cell"]],l[["Activated CD4 T cell"]],
                l[["Central memory CD4 T cell"]],l[["Effector memeory CD4 T cell"]],l[["T follicular helper cell"]],
                l[["Gamma delta T cell"]],l[["Type 1 T helper cell"]],l[["Type 17 T helper cell"]],l[["Type 2 T helper cell"]],
                l[["Regulatory T cell"]],l[["Activated B cell"]],l[["Immature  B cell"]],l[["Memory B cell"]],l[["Natural killer cell"]],
                l[["CD56bright natural killer cell"]],l[["CD56dim natural killer cell"]],l[["Myeloid derived suppressor cell"]],
                l[["Natural killer T cell"]],l[["Activated dendritic cell"]],l[["Plasmacytoid dendritic cell"]],l[["Immature dendritic cell"]],
                l[["Macrophage"]],l[["Eosinophil"]],l[["Mast cell"]],l[["Monocyte"]],l[["Neutrophil"]]))

Imu = c(l[["Central memory CD8 T cell"]],l[["Effector memeory CD8 T cell"]],l[["Activated CD4 T cell"]])

# BiocManager::install('limma')
library(limma)
load("D:/项目/24110426/SD_exp_combined-human.Rdata")
exprSet<-as.matrix(exp2)
exprSet=exprSet[,-42]
exprSet[1:6,1:6]
str(exprSet)
exprSet2 = as.data.frame(exprSet)
for (i in 1:ncol(exprSet)) {
  exprSet2[,i] = as.numeric(exprSet2[,i])
}
str(exprSet2)
exprSet2 = as.matrix(exprSet2)
ssgsea<- gsva(exprSet2, l,method='ssgsea',kcdf='Gaussian',abs.ranking=TRUE)
Immune = as.data.frame(t(ssgsea))
Immune2 = Immune
Immune2$id <- rownames(Immune2)
exprSet2=as.data.frame(t(exprSet2))
exprSet2$id<-rownames(exprSet2)
Immune2 = merge(Immune2,exprSet2,by = 'id')
Immune2 = cbind(group_list,Immune2)
Immune3 <- Immune %>% as.data.frame() %>%
  rownames_to_column("Sample") %>% 
  gather(key = 'Immune Cell Type',value = 'Estimating Score',-Sample)
Immune3$Group <- ifelse(Immune3$Sample %in%  Immune2$id ,Immune2$group_list,0)
#Immune3$Group=ifelse(Immune3$Group==1,'Control','SD')
Immune3=na.omit(Immune3)
table(Immune3$Group)


library(ggplot2)
library(tidyverse)
library(ggpubr)
# plot
colnames(Immune3)
ggplot(Immune3,aes(x = reorder(`Immune Cell Type`,-`Estimating Score`) , y = `Estimating Score`, fill = Group)) +
  
  geom_violin(position = position_dodge(0.9),alpha = 1.2,
              width = 1.8,trim = T,
              color = NA) +
 
  geom_boxplot(width = 0.35,show.legend = F,
               position = position_dodge(0.9),
               color = 'black',alpha = 1.2,
               outlier.shape = 21) +
  theme_bw(base_size = 16) +
  labs(x = "Immune Cell Type", y = 'ssGSEA Estimating Score') +
  theme(axis.text.x = element_text(angle = 65,hjust = 1,color = 'black'),
        legend.position = 'top',
        aspect.ratio = 0.4) +
  scale_fill_manual(values = c("#FF0000",'#00CCFF'))+ 
 
  stat_compare_means(aes(group = Group,label = ..p.signif..),method = "t.test")#kruskal.test  p.signif
ggsave(filename = './ssGSEA.pdf',width = 16,height = 10)


library(corrplot)
library(Hmisc)
library(pheatmap)
mygene <- read.csv('ML-交集基因.csv',sep=',') 
mygene=mygene[,-1]
exp2=exp2[,-42]
nc = t(rbind(ssgsea,exp2[mygene,]))  ;
m = rcorr(nc)$r[1:nrow(ssgsea),(ncol(nc)-length(mygene)+1):ncol(nc)]


p = rcorr(nc)$P[1:nrow(ssgsea),(ncol(nc)-length(mygene)+1):ncol(nc)]
head(p)


tmp <- matrix(case_when(as.vector(p) < 0.01 ~ "**",
                        as.vector(p) < 0.05 ~ "*",
                        TRUE ~ ""), nrow = nrow(p))


p1 <- pheatmap(t(m),
               display_numbers =t(tmp),
               angle_col =45,
               color = colorRampPalette(c("#92b7d1", "white", "#d71e22"))(100),
               border_color = "white",
               cellwidth = 20, 
               cellheight = 20,
               width = 7, 
               height=9.1,
               treeheight_col = 0,
               treeheight_row = 0)
