rm(list = ls())
setwd
source
source


suppressMessages(library(data.table))
suppressMessages(library(devtools))
suppressMessages(library(customLayout))
suppressMessages(library(stringr))
suppressMessages(library(ConsensusClusterPlus))
suppressMessages(library(tidydr))
suppressMessages(library(openxlsx))
suppressMessages(library(reshape2))
suppressMessages(library(dplyr))
suppressMessages(library(tidyr))
suppressMessages(library(tidyverse))
suppressMessages(library(clusterProfiler))
suppressMessages(library(pheatmap))
suppressMessages(library(ComplexHeatmap))
suppressMessages(library(GSVA))
suppressMessages(library(GSEABase))
suppressMessages(library(fgsea))
suppressMessages(library(corrplot))
suppressMessages(library(colorspace))
suppressMessages(library(survival))
suppressMessages(library(survminer))
suppressMessages(library(maftools))
suppressMessages(library(vegan))
suppressMessages(library(forcats))
suppressMessages(library(ggpubr))
suppressMessages(library(ggplot2))
suppressMessages(library(rstatix))
suppressMessages(library(ggstatsplot))
suppressMessages(library(ggcor))
suppressMessages(library(ggstance))
suppressMessages(library(tidyverse))
suppressMessages(library(GOplot))
suppressMessages(library(caret))
suppressMessages(library(writexl))
suppressMessages(library(rcartocolor))
suppressMessages(library(ggcorrplot))
suppressMessages(library(psych))
suppressMessages(library(clusterProfiler))
suppressMessages(library(dplyr))
suppressMessages(library(cols4all))
suppressMessages(library(org.Hs.eg.db))
suppressMessages(library(scales))
#suppressMessages(library(oncoPredict))
suppressMessages(library(gghalves))
suppressMessages(library(cowplot))
suppressMessages(library(IOBR))
suppressMessages(library(estimate))
suppressMessages(library(UpSetR))
suppressMessages(library(ggbiplot))
suppressMessages(library(ggsci))
suppressMessages(library(WGCNA))
suppressMessages(library(circlize))
suppressMessages(library(rJava))
suppressMessages(library(xlsxjars))
suppressMessages(library(xlsx))
suppressMessages(library(glmnet))
suppressMessages(library(tidyr))
suppressMessages(library(pROC))
suppressMessages(library(ROCR))




tcga.exp=readMatrix('00_origin_datas/Preprocessed/tcga.t.exp.txt')
tcga.cli=readMatrix('00_origin_datas/Preprocessed/tcga.t.cli.txt')


identical(as.vector(tcga.cli$sampleID),as.vector(colnames(tcga.exp)))
geneSets <- read.table("00_origin_datas/GeneCards-SearchResults.txt", header = TRUE, fill = TRUE, sep = "\t")
geneSets$Relevance.score=as.numeric(geneSets$Relevance.score)
geneSets=geneSets[which(geneSets$Relevance.score>7),]
write.table(geneSets,'01_ConsensusClusterPlus/geneSets.txt',sep = "\t",quote = F,row.names = T,col.names = T)

geneSets <- unique(as.vector(geneSets$Gene.Symbol))#signature read

cox.pval=0.05
exp_coxs=as.matrix(tcga.exp[which(rownames(tcga.exp)%in%as.vector(geneSets)),])
tcga.cli$OS.time=as.numeric(tcga.cli$OS.time)
tcga.cli$OS=as.numeric(tcga.cli$OS)
identical(as.vector(tcga.cli$sampleID),colnames(exp_coxs))
tcga.pcd.cox=cox_batch(t(scale(t(as.matrix(exp_coxs))))
                       ,time = tcga.cli$OS.time/365
                       ,event = tcga.cli$OS)

table(tcga.pcd.cox$p.value<0.05)
table(tcga.pcd.cox$p.value<0.01)
table(tcga.pcd.cox$p.value<0.001)

tcga.pcd.cox=tcga.pcd.cox[order(tcga.pcd.cox$HR,decreasing = T),]

tcga.pcd.cox.sig=tcga.pcd.cox[which(tcga.pcd.cox$p.value<cox.pval),]
nrow(tcga.pcd.cox.sig)
pdf('01_ConsensusClusterPlus/bioForest.pdf',height = 8,width =6,onefile = F)
bioForest(rt = tcga.pcd.cox.sig,col=c('#A5604A','#5C8980'))
dev.off()

write.table(tcga.pcd.cox.sig,'01_ConsensusClusterPlus/tcga.pcd.cox.sig.txt',sep = "\t",quote = F,row.names 
clusterAlg_name=c('hc','pam','km','kmdist')[3]
distance_name=c('pearson','spearman','euclidean','binary','maximum','canberra','minkowski')[1]
tcga_consen_data=as.matrix(tcga.exp[which(rownames(tcga.exp)%in%(rownames(tcga.pcd.cox.sig))),])
tcga_consen_data=t(scale(t(tcga_consen_data),scale = F))   

#tcga_consen_data=sweep(tcga_consen_data,1,apply(tcga_consen_data, 1, median))
#tcga_consen_data=as.dist(1-cor(tcga_consen_data,method = 'spearman'))

tcga_clust_subtype <- ConsensusClusterPlus(tcga_consen_data, maxK = 10, reps = 1000, pItem = 0.8, pFeature = 1, title = "01_ConsensusClusterPlus", clusterAlg = clusterAlg_name, distance = distance_name, plot = "pdf", writeTable = F, seed = 123456)
save(tcga_clust_subtype,file='01_ConsensusClusterPlus/tcga.subtype.RData')
load('01_ConsensusClusterPlus/tcga.subtype.RData')
k=2
#colors = c("#66C5CC", "#DCB0F2", "#D3B484", "#87C55F", "#DCB0F2", "#87C55F", "#D3B484", "#F6CF71", "#9EB9F3", "#F89C74", "#66C5CC")

#subtype.cols1=c("#9EB9F3","#87C55F","#66C5CC","#F6CF71")
subtype.cols=c("#3C77AF","#D3B484","#EDA1A4","#B3D8D5","#A4CB9E","#CCC94D")
#colors=c("#B383B9","#FCED82","#3C77AF","#D1352B","#66C5CC","#AECDE1","#E89DA0","#F5D2A8","#BBDD78", "#9EB9F3")

tcga.subtype <- data.frame( Samples=names(tcga_clust_subtype[[k]]$consensusClass),Subtype=tcga_clust_subtype[[k]]$consensusClass)
tcga.subtype$Subtype=paste0('C',tcga.subtype$Subtype)
write.table(tcga.subtype,file = '01_ConsensusClusterPlus/Subtype.txt',sep = '\t',quote = F,row.names = F,col.names = T)


table(tcga.subtype$Subtype)
colnames(tcga.cli)[1]='Samples'
tcga.subtype.cli=merge(tcga.subtype,tcga.cli,by='Samples')




fig1f=ggplotKMCox(data.frame(time = tcga.subtype.cli$OS.time/365
                             , event = tcga.subtype.cli$OS
                             , tcga.subtype.cli$Subtype) 
                  ,add_text = '',show_confint = F,palette = subtype.cols)


fig1f
ggsave("01_ConsensusClusterPlus/fig1f.pdf", width = 8, height = 8)

#################PCA
tcga.subtype$Samples=as.vector(tcga.subtype$Samples)
tcga_exp_var=t(tcga.exp[rownames(tcga.pcd.cox.sig),tcga.subtype$Samples])

tcga_exp_var=tcga_exp_var[ , which(apply(tcga_exp_var, 2, var) != 0)]
dim(tcga_exp_var)
cluster.pca <- prcomp(tcga_exp_var, scale=T)
cluster.pca.plot <- ggbiplot(cluster.pca, scale=1, groups = tcga.subtype$Subtype,
                             ellipse = TRUE,ellipse.prob=0.3, circle = F,var.axes=F) +
  scale_color_manual(values = subtype.cols) + 
  theme_bw() +
  theme(legend.direction = 'horizontal', legend.position = 'top',
        panel.grid = element_blank(),text = element_text(family = 'Times')) +
  xlab('PCA1') + ylab('PCA2')+xlim(-3,3)+ylim(-3,3)
cluster.pca.plot
ggsave("01_ConsensusClusterPlus/cluster.pca.plot.pdf", cluster.pca.plot,width = 6, height = 6)

colnames(tcga.subtype.cli)[c(3,4,5,6,7,10,12)]



set.seed(123456)
pdf('01_ConsensusClusterPlus/pie_compare_plot1.pdf',height = 6,width =12,onefile = F)

pie_compare_plot(dat = tcga.subtype.cli[,c("Subtype",colnames(tcga.subtype.cli)[c(5,6,7,8,11,13)])],
                 gname = 'Subtype',
                 group_cols = c(ggsci::pal_aaas()(9)))
dev.off()


### CIBERSORT
#tcga.exp.cibersort<-deconvo_cibersort(eset=tcga.exp,arrays=F)
#save(tcga.exp.cibersort,file='01_ConsensusClusterPlus/tcga.exp.cibersort.RData')
load('01_ConsensusClusterPlus/tcga.exp.cibersort.RData')
tcga.exp.cibersort=get.IOBR.immu.format(tcga.exp.cibersort)
tcga.exp.cibersort=tcga.exp.cibersort[rownames(tcga.subtype),1:22]
cibersort_plot=get_PlotMutiBoxplot(tcga.exp.cibersort,tcga.subtype
                                  ,group_cols = subtype.cols
                                  ,legend.pos = NULL
                                  ,ylab = 'Score'
                                  ,group.val = 'Subtype',xangle=45)+labs(color='Risktype')

ggsave("01_ConsensusClusterPlus/cibersort_plot.pdf", cibersort_plot,width = 12, height = 4.5)







##############DEG##########
tcga.subtype.cli$Samples=as.vector(tcga.subtype.cli$Samples)
table(tcga.subtype.cli$Subtype)
C1_sample=as.vector(tcga.subtype.cli$Samples[which(tcga.subtype.cli$Subtype=="C1")]) 
C2_sample=as.vector(tcga.subtype.cli$Samples[which(tcga.subtype.cli$Subtype=="C2")]) 

geo.limma_C1vsC2=mg_limma_DEG(exp = tcga.exp[,c(C1_sample,C2_sample)],group = c(rep("C1",length(C1_sample)),rep("C2",length(C2_sample))), ulab = 'C1',dlab = 'C2')
geo.limma_C1vsC2$Summary
df.deg.sig=geo.limma_C1vsC2$DEG[which(geo.limma_C1vsC2$DEG$P.Value<0.05 & abs(geo.limma_C1vsC2$DEG$logFC)>log2(1.5)),]
write.table(df.deg.sig,'02_DEGs/limma_C1vsC2.txt',sep = "\t",quote = F,row.names = T,col.names = T)
write.table(rownames(df.deg.sig),'02_DEGs/hub.txt',sep = "\t",quote = F,row.names = F,col.names = F)
dim(df.deg.sig)

dotplot=my_volcano(geo.limma_C1vsC2,p_cutoff = 0.05,fc_cutoff = log2(1.5),col = c('#BC3C29FF','#20854EFF','grey'))+
  theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),
        text = element_text(color = "black",family = 'Times',size = 14),
        axis.text = element_text(color = "black",family = 'Times',size = 14),
        legend.position = 'right')#+xlim(-2,2)+ylim(0,20)
dotplot
ggsave("02_DEGs/dotplot.pdf", width = 6, height = 6)



hub=rownames(df.deg.sig)
enrichment=mg_clusterProfiler(as.vector(hub))
write.table(enrichment$Enrich_tab,file = '02_DEGs/enrichment.txt',sep = '\t',quote = F,row.names = T,col.names = T)
kegg_dot=enrichplot::dotplot(enrichment$KEGG)+scale_y_discrete(labels=function(y)str_wrap(y,width = 25))+ scale_x_continuous(limits = c(0.03, 0.16))
bp_dot=enrichplot::dotplot(enrichment$GO_BP)+scale_y_discrete(labels=function(y)str_wrap(y,width = 25))+ scale_x_continuous(limits = c(0.04, 0.1))

rownames(tcga.subtype.cli)=tcga.subtype.cli$Samples
GEO_expression=tcga.exp[rownames(df.deg.sig),]
GEO_expression1 <- apply(GEO_expression, 1, scale)
rownames(GEO_expression1) <- colnames(tcga.exp)
GEO_expression2 <- as.data.frame(t(GEO_expression1))
gvhd.dist <- dist(GEO_expression1)
gvhd.hclust <- hclust(gvhd.dist)
head(tcga.subtype.cli)
colnames(tcga.subtype.cli)[c(2,5,6,7,8,13)]
geo.sample1=tcga.subtype.cli[,c(2,5,6,7,8,13)]
rownames(geo.sample1)=tcga.subtype.cli$Samples
geo.sample1<-geo.sample1[gvhd.hclust$order,]
geo.sample2<-geo.sample1[str_order(geo.sample1$Subtype,decreasing = T),]

GEO_expression2 <-GEO_expression2[,rownames(geo.sample2)]
identical(rownames(geo.sample2), colnames(GEO_expression2))

Subtype.color=subtype.cols[c(1,2)]
Age.color=subtype.cols[c(1,2)]
Stage.color=subtype.cols[c(1:4)]
pathologic_T.color=subtype.cols[c(1:4)]
pathologic_N.color=subtype.cols[c(1:4)]
pathologic_M.color=subtype.cols[c(1,2)]


names(Subtype.color)=c('C1','C2')
names(Age.color)=c('<=50','>50')
names(Stage.color)=c('Stage I','Stage II',"Stage III","Stage IV")
names(pathologic_T.color)=c('T1','T2',"T3","T4")
names(pathologic_N.color)=c('N0','N1','N2','N3')
names(pathologic_M.color)=c('M0','M1')



column_ha=HeatmapAnnotation(df = geo.sample2
                            , na_col = "grey"
                            , annotation_height = unit(0.01, "mm")
                            , gap = unit(1, 'mm')
                            ,col = list(Subtype=Subtype.color
                                        ,Age=Age.color
                                        ,Stage =Stage.color
                                        ,pathologic_T=pathologic_T.color
                                        ,pathologic_N=pathologic_N.color
                                        ,pathologic_M=pathologic_M.color
                                        )
                            
)


identical(rownames(geo.sample2), colnames(GEO_expression2))

heatmap_plot=Heatmap(as.matrix(GEO_expression2),
                     col = colorRamp2(c(-3, -1.5, 0, 1.5, 3),c("#226ED1", "#7AA8E2", "white", "#F09090","#E01010")), 
                     #col = circlize::colorRamp2(c(-3, 0, 3), c('navy', 'white', 'red')),
                     name = "Expression",top_annotation = column_ha,
                     show_row_names = F,show_column_names = F,
                     clustering_method_rows = "complete",
                     row_names_gp = gpar(fontsize = 10),
                     column_names_gp = gpar(fontsize = 10),
                     column_names_rot = 45,
                     cluster_columns = F,
                     cluster_rows = T,
                     show_row_dend = F,
                     #  width = ncol(exp)*unit(cell_size, "mm"),
                     use_raster = F,
                     ##`use_raster` is automatically set to TRUE for a matrix with more than 2000 rows.
                     #  row_names_max_width = row_name_width,
                     heatmap_legend_param = list(direction = "vertical",
                                                 legend_width = unit(3.05,"cm"),
                                                 legend_height = unit(2.8, "cm"),
                                                 title_position = "lefttop-rot"))

dev.off()
library(grid)
heatmap_grob <- grid.grabExpr(draw(heatmap_plot))
Fig2=mg_merge_plot(dotplot, heatmap_grob,kegg_dot,bp_dot,nrow = 2,ncol = 2,labels=LETTERS[c(1:4)])
ggsave('PDFs/Fig2.pdf',Fig2,height = 11,width = 12.5)
ggsave('PDFs/Fig2.jpg',Fig2,height = 11,width = 12.5)


############lasso##############

hub=rownames(df.deg.sig)
length(hub)
tcga.exp=readMatrix('00_origin_datas/Preprocessed/tcga.t.exp.txt')
tcga.cli=readMatrix('00_origin_datas/Preprocessed/tcga.t.cli.txt')
identical(as.vector(tcga.cli$sampleID),as.vector(colnames(tcga.exp)))

tcga.t.exp_use=as.data.frame(tcga.exp)
tcga.subtype.cli=tcga.cli
identical(colnames(tcga.t.exp_use),as.vector(tcga.subtype.cli$sampleID))
colnames(tcga.subtype.cli)[1]="Samples"
rownames(tcga.subtype.cli)=tcga.subtype.cli$Samples


tcga.cox=cox_batch(t(scale(t(tcga.t.exp_use[hub,])))
                   ,time =  tcga.subtype.cli$OS.time/365
                   ,event =tcga.subtype.cli$OS)
dim(tcga.cox)

table(tcga.cox$p.value<0.05)
table(tcga.cox$p.value<0.01)
table(tcga.cox$p.value<0.001)
writeMatrix(tcga.cox,outpath = '03_Lasso/tcga.cox.txt')



p.cutoff=0.05
tcga.cox_use=tcga.cox
tcga.cox_use$coef=log(tcga.cox_use$HR)
tcga.cox_use$Gene=rownames(tcga.cox_use)
tcga.cox_use$type=rep('None',nrow(tcga.cox_use))
tcga.cox_use$type[which(tcga.cox_use$p.value<p.cutoff & tcga.cox_use$coef>0)]='Risk'
tcga.cox_use$type[which(tcga.cox_use$p.value<p.cutoff & tcga.cox_use$coef<0)]='Protective'
table(tcga.cox_use$type)

######### lasso
tcga.gene.sig=rownames(tcga.cox)[which(tcga.cox$p.value<p.cutoff)]
length(tcga.gene.sig)

table(tcga.cox_use$type)


tcga.cox_forVis=tcga.cox_use
tcga.cox_forVis=tcga.cox_forVis[which(tcga.cox_forVis$type %in% c('Risk','Protective')),]
tcga.cox_forVis$p.value=-log10(tcga.cox_forVis$p.value)
range(tcga.cox_forVis$p.value)


#################### LASSO
table(tcga.cox$p.value<0.05)
table(tcga.cox$p.value<0.01)
table(tcga.cox$p.value<0.001)

tcga.exp.sig=tcga.t.exp_use[tcga.gene.sig,]
tcga.exp.sig=t(tcga.exp.sig)
dim(tcga.exp.sig)


dim(tcga.exp.sig)
options(ggrepel.max.hnscerlaps = Inf)
tcga.subtype.cli$Samples=as.vector(tcga.subtype.cli$Samples)
identical(rownames(tcga.exp.sig),tcga.subtype.cli$Samples)
tcga.lasso.res=mg_lasso_cox_use(tcga.exp.sig
                                , time = tcga.subtype.cli$OS.time/365
                                , event = tcga.subtype.cli$OS
                                , nfolds = 10
                                , lambda.min = T
                                , figLabels=c('B','C'))
tcga.lasso.res$Genes

tcga.lasso.res$lambda

tcga.lasso.res$plot

tcga.exp.for.cox=tcga.t.exp_use[match(tcga.lasso.res$Genes,row.names(tcga.t.exp_use)),]
dim(tcga.exp.for.cox)
identical(colnames(tcga.exp.for.cox),tcga.subtype.cli$Samples)

lst.modl=createCoxModel_use((t(tcga.exp.for.cox))
                            , time = tcga.subtype.cli$OS.time/365
                            , event = tcga.subtype.cli$OS
                            , isStep =T)
lst.modl$Cox
lst.modl$Genes
lst.modl$fmla

lst.modl.Coef=lst.modl$Coef
names(lst.modl.Coef)=lst.modl$Genes
lst.modl.Coef

tcga.risk.score=lst.modl$Score
#tcga.risk.score=scale(tcga.risk.score)[,1]
tcga.risk.score=mosaic::zscore(tcga.risk.score)

range(tcga.risk.score)

lst.modl$Coef

gene.coef=data.frame(Gene=lst.modl$Genes,Coef=lst.modl$Coef)
gene.coef$Type=ifelse(lst.modl$Coef>0,'Risk','Protective')
gene.coef$Type=factor(gene.coef$Type,levels=c('Risk','Protective'))
table(gene.coef$Type)

fig3c=gene.coef %>% 
  ggplot(aes(reorder(Gene, Coef), Coef)) +
  geom_col(aes(fill = Type)) +
  geom_text(aes(label=round(Coef,digits = 3)),color="black",hjust = "left")+
  
  coord_flip() +
  scale_fill_manual(values=pal_nejm(alpha = 0.9)(8)[c(1,2)]) +
  coord_flip() +
  labs(x = "") +
  labs(y = "Lasso Cox coefficient") +
  theme_classic()+theme(legend.position = c(0,1))
# theme(axis.text.y = element_text(angle = 0, hjust = 1),legend.position="top")


fig3AB=mg_plot_lasso_use(fit = tcga.lasso.res$Mode1
                         , cv_fit = tcga.lasso.res$Model2
                         , show_text = F
                         , figLabels = c('A', 'B'))
fig3AB
fig3abc=mg_merge_plot(fig3AB,fig3c,nrow = 1,ncol = 2,widths = c(2,1))
#savePDF('PDFs/Fig7AB.pdf',fig7A,height = 4,width = 9)
#savePDF('PDFs/fig3abc.pdf',fig3abc,height = 5,width = 15)


tcga.exp.forCox<- cbind(time=tcga.subtype.cli$OS.time/365,
                        status=tcga.subtype.cli$OS,
                        t(tcga.t.exp_use)[rownames(tcga.subtype.cli), lst.modl$Genes])



dim(tcga.exp.forCox)

fmla <- as.formula(paste0("Surv(time, status) ~",paste0(lst.modl$Genes,collapse = '+')))
cox <- coxph(fmla, data =as.data.frame(tcga.exp.forCox))
fig3d=survminer::ggforest(cox,data=tcga.exp.forCox,noDigits = 3)
fig3abd=mg_merge_plot(fig3AB,fig3d,nrow = 1,ncol =2,widths = c(2,1))

#savePDF('PDFs/fig3abc.pdf',fig3abcd,height = 5,width = 16)


############### TCGA
#tcga.cutoff <- survminer::surv_cutpoint(data.frame(time=tcga.subtype.cli$OS.time/365, event=tcga.subtype.cli$OS, risk=tcga.risk.score),time = "time", event = "event",variables = c("risk"))
#tcga.cutoff=tcga.cutoff$cutpoint$cutpoint
#tcga.cutoff=median(tcga.risk.score)
tcga.cutoff=0
identical(colnames(tcga.exp.for.cox),tcga.subtype.cli$Samples)
risk.group.color=c(c("#FCA709","#0C0CF4"))
names(risk.group.color)=c('High','Low')
tcga.roc=plotCoxModel_Batch_use(riskScore = tcga.risk.score
                                ,dat = t(tcga.exp.for.cox[match(lst.modl$Genes, row.names(tcga.exp.for.cox)), ])
                                , time = tcga.subtype.cli$OS.time/365
                                , event = tcga.subtype.cli$OS
                                , cutoff = tcga.cutoff
                                , labs = c('High','Low')
                                , title = 'RiskType'
                                , hetColor = c('#FCA709', 'white', '#0C0CF4')
                                
                                , pal = risk.group.color
                                , mks = c(1:5))
tcga.roc1=tcga.roc[[1]]
#pdf('PDFs/fig3e2.pdf',height = 6,width = 6)
tcga.roc1

dev.off()

tcga.group=ifelse(tcga.risk.score>tcga.cutoff,'High','Low')
tcga.group=data.frame(tcga.group)
colnames(tcga.group)='group'
table(tcga.group$group)
write.table(cbind(tcga.risk.score,tcga.group),file = '03_Lasso/tcga.group.txt',sep='\t',quote = F)





#gse.t.exp=readMatrix('00_origin_datas/Preprocessed/GSE44001_exp.txt')
#gse.t.cli=readMatrix('00_origin_datas/Preprocessed/GSE44001_cli.txt')

gse.t.exp=readMatrix('00_origin_datas/Preprocessed/GSE52903.t.exp.txt')
gse.t.cli=readMatrix('00_origin_datas/Preprocessed/GSE52903.t.cli.txt')
identical(colnames(gse.t.exp),rownames(gse.t.cli))


match(lst.modl$Genes,row.names(gse.t.exp))
length(lst.modl$Genes)
gse.t.cli.os=gse.t.cli
gse.t.cli.os$Samples =as.vector(rownames(gse.t.cli.os ))
identical(gse.t.cli.os$Samples , colnames(gse.t.exp)) 
gse.model.dat=gse.t.exp[match(lst.modl$Genes,row.names(gse.t.exp)),]

gse.risk.score=predictScoreByCoxModel(coxModel = lst.modl
                                      ,(t(gse.model.dat)))
#gse.risk.score=scale(gse.risk.score)
gse.risk.score=mosaic::zscore(gse.risk.score)
#gse.cutoff=0
identical(names(gse.risk.score),rownames(gse.t.cli.os))
gse.t.cli.os$"Score"=as.vector(gse.risk.score)
gse.cutoff <- survminer::surv_cutpoint(data.frame(time=gse.t.cli.os$OS.time/365, event=gse.t.cli.os$OS, Score=gse.t.cli.os$Score),time = "time", event = "event",variables = c("Score"))
gse.cutoff=gse.cutoff$cutpoint$cutpoint

#gse.cutoff=median(gse.t.cli.os$Score)


test.roc=plotCoxModel_Batch_use(riskScore = gse.risk.score
                                , dat = t(gse.t.exp[intersect(lst.modl$Genes, row.names(gse.t.exp)),])
                                , time = as.numeric(gse.t.cli.os$OS.time/365) 
                                , event = as.numeric(gse.t.cli.os$OS)
                                , cutoff = gse.cutoff
                                , labs = c('High','Low')
                                , title = 'RiskType'
                                , hetColor = c('#FCA709', 'white', '#0C0CF4')
                                , pal = risk.group.color
                                , mks = c(1:5))
test.roc1=test.roc[[1]]
#pdf('PDFs/fig3g2.pdf',height = 6,width = 6)

test.roc1
dev.off()
gse.group=ifelse(gse.risk.score>gse.cutoff,'High','Low')
gse.group=data.frame(gse.group)
colnames(gse.group)='group'
table(gse.group)

write.table(cbind(gse.risk.score,gse.group),file = '03_Lasso//geo.group.txt',sep='\t',quote = F)


Fig4_ROC=mg_merge_plot(tcga.roc1,test.roc1,ncol=2,nrow=1)
Fig4=ggpubr::ggarrange(fig3abd,Fig4_ROC, ncol = 1, nrow = 2,heights = c(1,1.5))

ggsave('PDFs/Fig3.pdf',Fig4,height = 10,width = 12)
ggsave('PDFs/Fig3.jpg',Fig4,height = 10,width = 12)

identical(rownames(as.data.frame(tcga.risk.score)),rownames(tcga.subtype.cli))
tcga.risktype.cli=data.frame(tcga.subtype.cli,Riskscore=tcga.risk.score)
tcga.risktype.cli$Risktype=ifelse(tcga.risktype.cli$Riskscore>tcga.cutoff,'High','Low')
write.table(tcga.risktype.cli,file = '03_Lasso/tcga.risktype.cli.txt',sep='\t',quote = F)



tcga.risktype.cli=read.table('03_Lasso/tcga.risktype.cli.txt',header = T,check.names = F,fill=T,sep = "\t")
tcga.exp=readMatrix('00_origin_datas/Preprocessed/tcga.t.exp.txt')
tcga.cli=readMatrix('00_origin_datas/Preprocessed/tcga.t.cli.txt')
identical(as.vector(tcga.risktype.cli$Samples),colnames(tcga.exp))

risk.group.color1=c("#FCA709","#0C0CF4")
names(risk.group.color1)=c('High','Low')
suppressMessages(library(IOBR))
library(estimate)
#### ESTIMATE
#tcga.exp.estimate<-deconvo_estimate(eset=tcga.exp)
#save(tcga.exp.estimate,file='04_imm/tcga.exp.estimate.RData')
load('04_imm/tcga.exp.estimate.RData')
tcga.exp.estimate=get.IOBR.immu.format(tcga.exp.estimate)

############ MCP-counter 
#tcga.exp.mcp<-deconvo_mcpcounter(eset=as.matrix(tcga.exp))
#save(tcga.exp.mcp,file='04_imm/tcga.exp.mcp.RData')
load('04_imm/tcga.exp.mcp.RData')
tcga.exp.mcp=get.IOBR.immu.format(tcga.exp.mcp)

############ TIMER 
#tcga.exp.timer<-deconvo_timer(eset=as.matrix(tcga.exp),indications=rep('CESC',ncol(tcga.exp)))
#save(tcga.exp.timer,file='04_imm/tcga.exp.timer.RData')
load('04_imm/tcga.exp.timer.RData')
tcga.exp.timer=get.IOBR.immu.format(tcga.exp.timer)

### CIBERSORT
#tcga.exp.cibersort<-deconvo_cibersort(eset=tcga.exp,arrays=F)
#save(tcga.exp.cibersort,file='04_imm/tcga.exp.cibersort.RData')
load('04_imm/tcga.exp.cibersort.RData')
tcga.exp.cibersort=get.IOBR.immu.format(tcga.exp.cibersort)

#######sssGSEA#######
#geo.immu.ssgsea=immu_ssgsea(exp = tcga.exp)
#save(geo.immu.ssgsea,file='04_imm/geo.immu.ssgsea.RData')
load('04_imm/geo.immu.ssgsea.RData')



tcga.t.estimate=tcga.exp.estimate[rownames(tcga.risktype.cli),1:3]
tcga.t.mcp=tcga.exp.mcp[rownames(tcga.risktype.cli),]
tcga.t.cibersort=tcga.exp.cibersort[rownames(tcga.risktype.cli),1:22]
tcga.t.ssGSEA28=as.data.frame(geo.immu.ssgsea[rownames(tcga.risktype.cli),])
tcga.t.timer=tcga.exp.timer[rownames(tcga.risktype.cli),]

fig5a=get_PlotMutiBoxplot(tcga.t.estimate,tcga.risktype.cli
                          ,group_cols = risk.group.color1
                          ,legend.pos = NULL
                          ,ylab = 'Score'
                          ,group.val = 'Risktype',xangle=45)+labs(color='Risktype')

fig5a=groupViolin(tcga.t.estimate,
                  tcga.risktype.cli$Risktype,
                  ylab = 'Score',
                  group_col=risk.group.color1)

fig5a


fig5c=get_PlotMutiBoxplot(tcga.t.timer,tcga.risktype.cli
                          ,group_cols = risk.group.color1
                          ,legend.pos = NULL
                          ,ylab = 'Score'
                          ,group.val = 'Risktype',xangle=45)

fig5c=groupViolin(tcga.t.timer,
                  tcga.risktype.cli$Risktype,
                  ylab = 'Score',
                  group_col=risk.group.color1)
fig5c



fig5d=get_PlotMutiBoxplot(tcga.exp.mcp,tcga.risktype.cli
                          ,group_cols = risk.group.color1
                          ,legend.pos = NULL
                          ,ylab = 'Score'
                          ,group.val = 'Risktype',xangle=45)

fig5d=groupViolin(tcga.exp.mcp,
                  tcga.risktype.cli$Risktype,
                  ylab = 'Score',
                  group_col=risk.group.color1)
fig5d





fig5e=get_PlotMutiBoxplot(tcga.t.cibersort,tcga.risktype.cli
                          ,group_cols = risk.group.color
                          ,legend.pos = NULL
                          ,ylab = 'Score'
                          ,group.val = 'Risktype',xangle=45)+labs(color='Risktype')

fig5e=groupViolin(tcga.t.cibersort,
                  tcga.risktype.cli$Risktype,
                  ylab = 'Score',
                  group_col=risk.group.color1)


fig5e


fig5f=get_PlotMutiBoxplot(tcga.t.ssGSEA28,tcga.risktype.cli   ,group_cols = risk.group.color1,ylab = 'Score' ,group.val = 'Risktype',xangle=45)+labs(color='Risktype')

fig5f=groupViolin(tcga.t.ssGSEA28,
                  tcga.risktype.cli$Risktype,
                  ylab = 'Score',
                  group_col=risk.group.color1)

fig5f


#####TIDE######
tcga.tide<-read.csv('04_imm/TIDE.csv',row.names = 1,stringsAsFactors = F)
tcga.tide=tcga.tide[rownames(tcga.risktype.cli),]

TIDE.df=data.frame(tcga.tide,
                   tcga.risktype.cli)
head(TIDE.df)
table(TIDE.df$Responder,TIDE.df$Risktype)
chisq.test(table(TIDE.df$Responder,TIDE.df$Risktype))
tide.responder=prop.table(table(TIDE.df$Responder,TIDE.df$Risktype),margin=2)
tide.responder=melt(tide.responder)
tide.responder
fig6a=ggplot(tide.responder, aes(x= Var2, y=value, fill=Var1))+
  geom_bar(stat = "identity")+xlab('Risktype')+ylab('Percentage')+
  scale_fill_manual(values = c("#8383F2","#FFC77D"),name='Responder')+
  theme_bw()+geom_text(data=tide.responder,aes(label=paste(round(100*value,2),'%',sep='')))+
  theme(text = element_text(family = 'Times',size = 15),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank())
fig6a

fig6b=TIDE.df %>%
  ggplot(aes(x=Risktype, y=TIDE,fill=Risktype)) +
  geom_boxplot()+
  scale_fill_manual(values =risk.group.color1)+   
  ggpubr::stat_compare_means(aes(group=Risktype), label = "p.format", method = 'wilcox.test')+
  labs(x="", y = "TIDE", fill = "Risktype") +theme_bw()+
  theme(legend.position = "none",text = element_text(family = 'Times',size = 14)) 
fig6b

fig6c=ggplot(TIDE.df, aes(x = Riskscore, y = TIDE))+
  geom_point()+
  geom_smooth(method = "lm", color = "red", fill = "lightgray")+
  stat_cor(method = "spearman")+
  theme_bw()+
  theme(panel.background = element_blank(),
        text = element_text(family = 'Times',size = 14))+
  xlab('RiskScore')+ylab('TIDE')
fig6c
fig5efg=mg_merge_plot(fig6a,fig6b,fig6c,nrow = 1,ncol = 3
                      ,labels = LETTERS[5:7])

fig5ab=mg_merge_plot(fig5a,fig5e,nrow = 1,ncol = 2 ,labels =LETTERS[1:2],widths = c(1,1.5))
fig5cd=mg_merge_plot(fig5c,fig5d,nrow = 1,ncol = 2 ,labels =LETTERS[3:4])
fig5=mg_merge_plot(fig5ab,fig5cd,fig5efg,nrow = 3,ncol = 1 )

ggsave('PDFs/Fig4.pdf',fig5,height = 10,width = 12)
ggsave('PDFs/Fig4.jpg',fig5,height = 10,width = 12)

library('oncoPredict')
tcga.risktype.cli=read.table('03_Lasso/tcga.risktype.cli.txt',header = T,check.names = F,fill=T,sep = "\t")
tcga.exp=readMatrix('00_origin_datas/Preprocessed/tcga.t.exp.txt')
tcga.cli=readMatrix('00_origin_datas/Preprocessed/tcga.t.cli.txt')
identical(as.vector(tcga.risktype.cli$Samples),colnames(tcga.exp))
##ctrl + shift + C
drug_exp=as.matrix(tcga.exp)

# GDSC2_Expr = readRDS(file=file.path(dir,'Training Data/GDSC2_Expr (RMA Normalized and Log Transformed).rds'))
# GDSC2_Res = readRDS(file = file.path(dir,"Training Data/GDSC2_Res.rds"))
# GDSC2_Res <- exp(GDSC2_Res)
# calcPhenotype(trainingExprData = as.matrix(GDSC2_Expr),
#              trainingPtype = as.matrix(GDSC2_Res),
#              testExprData = as.matrix(drug_exp),
#              batchCorrect = 'eb',  #   "eb" for ComBat
#              powerTransformPhenotype = TRUE,
#              removeLowVaryingGenes = 0.2,
#              minNumSamples = 10,
#             printOutput = TRUE,
#             removeLowVaringGenesFrom = 'rawData' )

tcga.drug.ic50=read.csv('04_imm/calcPhenotype_Output/DrugPredictions.csv',row.names = 1)
dim(tcga.drug.ic50)

head(tcga.drug.ic50)
dim(tcga.drug.ic50)
all(rownames(tcga.drug.ic50)==rownames(tcga.risktype.cli))

cr=psych::corr.test(y=data.frame(tcga.drug.ic50[rownames(tcga.risktype.cli),],check.names = F),
                    x=data.frame(t(tcga.exp)[rownames(tcga.risktype.cli),lst.modl$Genes],RiskScore=tcga.risktype.cli$Riskscore))

df_cor=cr$r
df_pval=cr$p
#df_cor=round(df_cor,2)

inds=which(abs(df_cor[nrow(df_cor),])>0.4 & df_pval[nrow(df_cor),]<0.05)
length(inds)
df_cor=df_cor[,inds]
df_pval=df_pval[,inds]
### Pivot data from wide to long
library(tidyverse)
g = pivot_longer(data=rownames_to_column(as.data.frame(df_cor),var = "from"),
                 cols = 2:(ncol(df_cor)+1), ## Columns to pivot into longer format
                 names_to = "to",
                 values_to = "cor")
gp = pivot_longer(data=rownames_to_column(as.data.frame(df_pval)),
                  cols = 2:(ncol(df_pval)+1),
                  names_to = "gene",
                  values_to = "p")
all(g$from==gp$rowname & g$to==gp$gene)
g$p.adj = gp$p


df=g
df <- df %>%
  mutate(col = cut(cor, breaks = c(-1, 0, 1),
                   labels = c("negative", "positive")),
         p.signif = cut(p.adj, breaks = c(0,0.0001, 0.001, 0.01, 0.05,1),
                        labels = c("****", "**", "**","*",""),
                        right = FALSE, include.lowest = TRUE))
df=data.frame(df)
head(df)
mode(df$p.adj)="numeric"
mode(df$cor)="numeric"

# df=df[which(df$p.adj<0.05 & abs(df$cor)>0.4),]
length(unique(df$to))
head(df)
writeMatrix(df,outpath = '04_imm/tcga.drug.model.cor.txt')

corr.mat=pivot_wider(df[,c(1,2,3)],names_from  ="from",values_from ='cor')
p.mat=pivot_wider(df[,c(1,2,4)],names_from  ="from",values_from ='p.adj')
corr.mat=data.frame(corr.mat)
p.mat=data.frame(p.mat)
corr.mat[1:4,1:5]
rownames(corr.mat)=corr.mat$to
corr.mat=corr.mat[-1]
rownames(p.mat)=p.mat$to
p.mat=p.mat[-1]
dim(corr.mat)

corr.mat[1:4,1:5]
####################
library(corrplot)
pdf('04_imm/drug_plot.pdf',height = 10,width = 10,onefile = F)
drug_plot=corrplot(corr = t(as.matrix(corr.mat)),
                   p.mat = t(as.matrix(p.mat)),
                   mar = c(0,0,0,0),
                   col=colorRampPalette(c('#3969AC', 'white','#E73F74'))(50),
                   tl.srt = 90,tl.cex = 1,tl.col = 'black',tl.offset = 0.5,
                   cl.pos = c("b","r","n")[2],cl.align.text = 'l',cl.length = 5,
                   cl.ratio = 0.1,cl.cex = 0.8,
                   addgrid.col = 'white',
                   method = "pie",
                   insig = 'label_sig',
                   sig.level=c(0.001,0.01,0.05),
                   pch.cex=1,is.corr=T,xpd=T)
dev.off()
rownames(corr.mat)




##IMvigor210######
library("IMvigor210CoreBiologies")
data(cds)
pheno<-pData(cds)
head(pheno)
exper_tpm=mg_get_immu_pd1_treament_exp()
exper_id=exper_tpm$fpkm
exper_id$symbol=rownames(exper_id)
rownames(exper_id)<-exper_id$symbol
exper_id$symbol<-NULL
range(exper_id)
exper_id_use<-log2(exper_id+1)
dim(exper_id_use)
# 31085   348
range(exper_id_use)
#rownames(exper_id_use)=gsub('-','__',rownames(exper_id_use))
exper_id_use[1:5,1:5]
exper_id_use=exper_id_use[,rownames(pheno[which(pheno$binaryResponse!='NA'),])]
dim(exper_id_use)
# 31085   298
pheno=pheno[which(pheno$binaryResponse!='NA'),]

IMvigor210_model_data=data.frame(OS=pheno$censOS,OS.time = pheno$os,
                                 t(exper_id_use[intersect(lst.modl$Genes,rownames(exper_id_use)),rownames(pheno)]))
head(IMvigor210_model_data)

fmla.IMvigor210 <- as.formula(paste0("Surv(OS.time, OS) ~"
                                     ,paste0(lst.modl$Genes,collapse = '+')))
cox.IMvigor210 <- coxph(fmla.IMvigor210, data =as.data.frame(IMvigor210_model_data))
IMvigor210_lan <- coef(cox.IMvigor210)

#IMvigor210_lan <- lan
risk.imv210=as.numeric(IMvigor210_lan%*%as.matrix(t(IMvigor210_model_data[rownames(pheno),names(IMvigor210_lan)])))

imv210.risktype.cli=cbind.data.frame(pheno,Riskscore=risk.imv210)

imv210.data.point <- surv_cutpoint(imv210.risktype.cli, time = "os", event = "censOS",variables = 'Riskscore')
imv210.cutoff <- as.numeric(summary(imv210.data.point)[1])
#imv210.cutoff <-median(imv210.risktype.cli$Riskscore)
imv210.risktype.cli$Risktype=ifelse(risk.imv210>imv210.cutoff,'High','Low')

imv210.roc=ggplotTimeROC(imv210.risktype.cli$os,
                         imv210.risktype.cli$censOS,
                         imv210.risktype.cli$Riskscore,mks = c(1,2,3,4,5))
imv210.roc


imv210.km2=ggplotKMCox(data.frame(imv210.risktype.cli$os, 
                                  imv210.risktype.cli$censOS,
                                  imv210.risktype.cli$Risktype),
                       title='IMvigor210',show_confint = T,palette="jco"
)



head(imv210.risktype.cli)
table(imv210.risktype.cli$binaryResponse)

imv210.boxplot=imv210.risktype.cli%>%
  ggplot(aes(x=binaryResponse, y=Riskscore,fill = binaryResponse)) +
  #geom_violin()+  
  scale_fill_manual(values = c("#0073C2","#EFC000"))+
  geom_boxplot()+
  theme_classic(base_size = 20)+
  ggpubr::stat_compare_means(aes(group=binaryResponse), label = "p.format", method = 'wilcox.test')+
  theme_classic()+
  theme(legend.position = 'none',axis.text = element_text(color = 'black'),
        axis.text.x = element_text(size = 12),axis.title.x = element_text(size = 15),
        axis.text.y = element_text(size = 12),axis.title.y = element_text(size = 15))



paste0('-log10(p.value)=',round(-log10(chisq.test(table(imv210.risktype.cli$binaryResponse,imv210.risktype.cli$Risktype))$p.value),2))
compaired <- list(c("High", "Low"))
results=prop.table(table(imv210.risktype.cli$binaryResponse,imv210.risktype.cli$Risktype),margin=2)
results1=reshape2::melt(results)
colnames(results1)<-c("binaryResponse","Senescore","Percentage")
results1$Percentage<-round(results1$Percentage,digits=2)

imv210.bar=ggplot(results1,aes(x=Senescore,y=Percentage,fill=binaryResponse))+
  geom_bar(position = "fill",stat="identity")+
  #geom_signif(comparisons = compaired, map_signif_level = T,test = 'chisq.test')+
  theme_bw()+labs(x="Risktype", y = "Percentage",
                  title = paste0('P value=',round(chisq.test(table(imv210.risktype.cli$binaryResponse,imv210.risktype.cli$Risktype))$p.value,5)))+scale_fill_manual(values = c("#0073C2","#EFC000"))+
  geom_text(aes(label = Percentage),position=position_stack(vjust =0.5),size = 5)+
  theme(legend.position = 'top')
imv210.bar
fig6abc=mg_merge_plot(imv210.km2,imv210.bar,imv210.boxplot,nrow = 1,ncol = 3 ,labels =c("A","B","C"))
ggsave('PDFs/Fig5a.pdf',fig6abc,height = 6,width = 12)
ggsave('PDFs/Fig5a.jpg',fig6abc,height = 6,width = 12)




Risktype.color=risk.group.color
tcga.risktype.cli=read.table('03_Lasso/tcga.risktype.cli.txt',header = T,check.names = F,fill=T,sep = "\t")


tcga_cox_datas=tcga.risktype.cli
colnames(tcga_cox_datas)[c(4:7,12,13)]

table(tcga_cox_datas$pathologic_N)
tcga_cox_datas$pathologic_N=as.vector(tcga_cox_datas$pathologic_N)

table(tcga_cox_datas$pathologic_T)
tcga_cox_datas$pathologic_T=as.vector(tcga_cox_datas$pathologic_T)

table(tcga_cox_datas$pathologic_M )
tcga_cox_datas$pathologic_M =as.vector(tcga_cox_datas$pathologic_M )


table(tcga_cox_datas$Stage)




table(tcga_cox_datas$pathologic_T)
tcga_cox_datas$pathologic_T=as.vector(tcga_cox_datas$pathologic_T)
tcga_cox_datas$pathologic_T[tcga_cox_datas$pathologic_T=='T1'|tcga_cox_datas$pathologic_T=='T2']<-'T1+T2'
tcga_cox_datas$pathologic_T[tcga_cox_datas$pathologic_T=='T3'|tcga_cox_datas$pathologic_T=='T4']<-'T3+T4'
table(tcga_cox_datas$pathologic_T)


table(tcga_cox_datas$Stage)
tcga_cox_datas$Stage=as.vector(tcga_cox_datas$Stage)
tcga_cox_datas$Stage[tcga_cox_datas$Stage=='Stage I'|tcga_cox_datas$Stage=='Stage II']<-'Stage I+II'
tcga_cox_datas$Stage[tcga_cox_datas$Stage=='Stage III'|tcga_cox_datas$Stage=='Stage IV']<-'Stage III+IV'
table(tcga_cox_datas$Stage)





univar_res<-unicox(vars=colnames(tcga_cox_datas)[c(4:6,12,13)],time = tcga_cox_datas$OS.time,event = tcga_cox_datas$OS,data=tcga_cox_datas)
univar_res
univar_res[which(univar_res$pvalue<0.05),]



mutivar_res<-multicox(vars=rownames(univar_res[which(univar_res$pvalue<0.05),]),time = tcga_cox_datas$OS.time,event = tcga_cox_datas$OS,data=tcga_cox_datas,forest = F)
mutivar_res
mutivar_res[which(mutivar_res$pvalue<0.05),]



mg_Forestplot(df_m = univar_res,outFile = '05_nomo/tcga.univar.forestplot.pdf',height = 4,width = 6)
mg_Forestplot(df_m = mutivar_res,outFile = '05_nomo/tcga.mutivar.forestplot.pdf',height = 4,width = 6)


###################nomo

dt=data.frame(RiskScore=tcga_cox_datas$Riskscore,
              #T.Stage=tcga_cox_datas$pathologic_T,
              N.Stage =tcga_cox_datas$pathologic_N
              #M.Stage=tcga_cox_datas$M.Stage,
              #Stage =tcga_cox_datas$Stage ,
              #Age =tcga_cox_datas$Age 
             
)

pdf('05_nomo/nomogram.pdf', width = 12, height = 10)

nom.plot=mg_nomogram(clinical_riskscore=dt,
                     os = as.numeric(tcga_cox_datas$OS.time/365),
                     status = as.numeric(tcga_cox_datas$OS),
                     mks = c(1,3,5))



dev.off()

#pdf('PDFs/fig4b.pdf', width = 9, height = 6)
mg_nomogram_buti(nom.plot$Mod,cut.time = c(1,3,5))
dev.off()

