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

#install.packages("pheatmap")


library(pheatmap)         #???ð?
setwd("C:\\Users\\LZD\\Desktop\\THCA\\PANoptosis\\19.riskPlot")      #???ù???Ŀ¼

#???????????ߵĺ???
bioRiskPlot=function(inputFile=null, project=null){
	rt=read.table(inputFile, header=T, sep="\t", check.names=F, row.names=1)    #??ȡ?????ļ?
	rt=rt[order(rt$riskScore),]      #???ղ??˷??մ??ֶ???Ʒ????????
		
	#???Ʒ???????
	riskClass=rt[,"risk"]
	lowLength=length(riskClass[riskClass=="low"])
	highLength=length(riskClass[riskClass=="high"])
	lowMax=max(rt$riskScore[riskClass=="low"])
	line=rt[,"riskScore"]
	line[line>10]=10
	pdf(file=paste0(project, ".riskScore.pdf"), width=7, height=4)
	plot(line, type="p", pch=20,
		 xlab="Patients (increasing risk socre)",
		 ylab="Risk score",
		 col=c(rep("#0099CC",lowLength),rep("#CC3333",highLength)) )
	abline(h=lowMax,v=lowLength,lty=2)
	legend("topleft", c("High risk","Low Risk"),bty="n",pch=19,col=c("#CC3333","#0099CC"),cex=1.2)
	dev.off()
		
	#????????״̬ͼ
	color=as.vector(rt$fustat)
	color[color==1]="#CC3333"
	color[color==0]="#0099CC"
	pdf(file=paste0(project, ".survStat.pdf"), width=7, height=4)
	plot(rt$futime, pch=19,
		 xlab="Patients (increasing risk socre)",
		 ylab="Survival time (years)",
		 col=color)
	legend("topleft", c("Dead","Alive"),bty="n",pch=19,col=c("#CC3333","#0099CC"),cex=1.2)
	abline(v=lowLength,lty=2)
	dev.off()
	
	#??????ͼע?͵???ɫ
	ann_colors=list()
	bioCol=c("#0099CC", "#CC3333")
	names(bioCol)=c("low", "high")
	ann_colors[["Risk"]]=bioCol

	#???Ʒ?????ͼ
	rt1=rt[c(3:(ncol(rt)-2))]
	rt1=t(rt1)
	annotation=data.frame(Risk=rt[,ncol(rt)])
	rownames(annotation)=rownames(rt)
	pdf(file=paste0(project, ".heatmap.pdf"), width=7, height=4)
	pheatmap(rt1, 
		     annotation=annotation,
		     annotation_colors = ann_colors, 
		     cluster_cols = FALSE,
		     cluster_rows = FALSE,
		     show_colnames = F,
		     scale="row",
		     color = colorRampPalette(c(rep("#0099CC",3.5), "white", rep("#CC3333",3.5)))(50),
		     fontsize_col=3,
		     fontsize=7,
		     fontsize_row=8)
	dev.off()
}

#tarin??????????
bioRiskPlot(inputFile="risk.train.txt", project="train")
#test??????????
bioRiskPlot(inputFile="risk.test.txt", project="test")
#??????Ʒ????????
bioRiskPlot(inputFile="risk.all.txt", project="all")


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

