###清空环境
rm(list = ls())
#禁止chr转成factor
options(stringsAsFactors = FALSE)

#if (!requireNamespace("BiocManager", quietly = TRUE))
#    install.packages("BiocManager")
#BiocManager::install("limma")

#install.packages("ggpubr")


#???ð?
library(limma)
library(ggpubr)
scoreFile="TMEscores.txt"     #????΢?????????ļ?
riskFile="risk.all.txt"       #?????ļ?
setwd("C:\\Users\\LZD\\Desktop\\THCA\\PANoptosis\\29.TMEdiff")     #???ù???Ŀ¼

#??ȡ????΢?????????ļ????????ļ?????????
rt=read.table(scoreFile, header=T, sep="\t", check.names=F, row.names=1)
data=as.matrix(rt)
rownames(data)=gsub("(.*?)\\-(.*?)\\-(.*?)\\-(.*?)\\-.*", "\\1\\-\\2\\-\\3", rownames(data))
data=avereps(data)

#??ȡ?????ļ?
risk=read.table(riskFile, header=T, sep="\t", check.names=F, row.names=1)

#?ϲ?????
sameSample=intersect(row.names(data), row.names(risk))
data=data[sameSample,,drop=F]
risk=risk[sameSample,"risk",drop=F]
rt=cbind(data, risk)

#???ñȽ???
rt$risk=factor(rt$risk, levels=c("low", "high"))
group=levels(factor(rt$risk))
rt$risk=factor(rt$risk, levels=group)
comp=combn(group,2)
my_comparisons=list()
for(i in 1:ncol(comp)){my_comparisons[[i]]<-comp[,i]}

#??????΢?????Ĵ??ֽ???ѭ??,????????ͼ
for(i in colnames(rt)[1:3]){
	boxplot=ggboxplot(rt, x="risk", y=i, fill="risk",
			          xlab="",
			          ylab=i,
			          legend.title="Risk",
			          palette=c("#0099CC","#CC0033")
			          )+ 
		    stat_compare_means(comparisons=my_comparisons)

	#????ͼ??
	pdf(file=paste0(i, ".pdf"), width=5, height=4.5)
	print(boxplot)
	dev.off()
}


######Video source: https://ke.biowolf.cn
######??????ѧ??: https://www.biowolf.cn/
######΢?Ź??ںţ?biowolf_cn
######???????䣺biowolf@foxmail.com
######????΢??: 18520221056

