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

#install.packages("Rtsne")
#install.packages("ggplot2")


#???ð?
library(Rtsne)
library(ggplot2)

clusterFile="cluster.txt"     #???͵Ľ????ļ?
setwd("C:\\Users\\LZD\\Desktop\\THCA\\PANoptosis\\35.PCA")    #???ù???Ŀ¼

#??ȡ?????ļ?,??ȡ????
rt=read.table(clusterFile, header=T, sep="\t", check.names=F, row.names=1)
data=rt[c(3:(ncol(rt)-3))]

#PCA????
data.pca=prcomp(data, scale. = TRUE)
pcaPredict=predict(data.pca)
PCA = data.frame(PC1=pcaPredict[,1], PC2=pcaPredict[,2], rt[,c("risk","Cluster")])
#???Ʒ??յ?PCAͼ
pdf(file="PCA.risk.pdf", width=5.5, height=4.5)
p=ggplot(data = PCA, aes(PC1, PC2)) + geom_point(aes(color = risk)) +
		scale_colour_manual(name="Risk",  values =c("#CC0033", "#0099CC"))+
	    theme_bw()+
	    theme(plot.margin=unit(rep(1.5,4),'lines'))+
	    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
print(p)
dev.off()
#???Ʒ??͵?PCAͼ
bioCol=c("#0066FF","#FF9900","#00DB00","#FF0000","#7CC767","#223D6C","#D20A13","#FFD121","#088247","#11AA4D")
bioCol=bioCol[1:length(unique(rt$Cluster))]
pdf(file="PCA.cluster.pdf", width=5.5, height=4.5)
p=ggplot(data = PCA, aes(PC1, PC2)) + geom_point(aes(color = Cluster)) +
		scale_colour_manual(name="Cluster",  values =bioCol)+
	    theme_bw()+
	    theme(plot.margin=unit(rep(1.5,4),'lines'))+
	    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
print(p)
dev.off()
	

#t-SNE????
tsneOut=Rtsne(data, dims=2, perplexity=10, verbose=F, max_iter=500,check_duplicates=F)
tsne=data.frame(tSNE1=tsneOut$Y[,1], tSNE2=tsneOut$Y[,2], rt[,c("risk","Cluster")])	
#???Ʒ??յ?tSNEͼ
pdf(file="tSNE.risk.pdf", width=5.5, height=4.5)       #???????????ļ?
p=ggplot(data = tsne, aes(tSNE1, tSNE2)) + geom_point(aes(color = risk)) +
		scale_colour_manual(name="Risk",  values =c(""#CC0033", "#0099CC""))+
	    theme_bw()+
	    theme(plot.margin=unit(rep(1.5,4),'lines'))+
	    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
print(p)
dev.off()
#???Ʒ??͵?tSNEͼ
pdf(file="tSNE.Cluster.pdf", width=5.5, height=4.5)       #???????????ļ?
p=ggplot(data = tsne, aes(tSNE1, tSNE2)) + geom_point(aes(color = Cluster)) +
		scale_colour_manual(name="Cluster",  values =bioCol)+
	    theme_bw()+
	    theme(plot.margin=unit(rep(1.5,4),'lines'))+
	    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
print(p)
dev.off()


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

