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

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

#install.packages("ggpubr")


#???ð?
library(limma)
library(ggpubr)

cluFile="cluster.txt"          #???͵Ľ????ļ?
scoreFile="TMEscores.txt"      #΢?????????ļ?
setwd("C:\\Users\\LZD\\Desktop\\THCA\\PANoptosis\\36.clusterTMEdiff")     #???ù???Ŀ¼

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

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

#??Ʒȡ????
sameSample=intersect(row.names(score), row.names(cluster))
score1=score[sameSample,,drop=F]
cluster1=cluster[sameSample,"Cluster",drop=F]
data=cbind(score1, cluster1)

#???ñȽ???
type=levels(factor(data[,"Cluster"]))
data$Cluster=factor(data$Cluster, levels=type)
comp=combn(type, 2)
my_comparisons=list()
for(i in 1:ncol(comp)){my_comparisons[[i]]<-comp[,i]}

#??????ɫ
bioCol=c("#0066FF","#FF9900","#00DB00","#FF0000","#7CC767","#223D6C","#D20A13","#FFD121","#088247","#11AA4D")
bioCol=bioCol[1:length(unique(data$Cluster))]

#??????΢???????ֽ???ѭ??,????????ͼ
for(i in colnames(data)[1:(ncol(data)-1)]){
	boxplot=ggboxplot(data, x="Cluster", y=i, fill="Cluster",
			          xlab="",
			          ylab=i,
			          legend.title="Cluster",
			          palette=bioCol
			          )+ 
		    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

