###清空环境
rm(list = ls())
#禁止chr转成factor
options(stringsAsFactors = FALSE)
####让报错变成英文 方便google
Sys.setenv(LANGUAGE = "en")


library(limma)
corFilter=0.4            #????ϵ???Ĺ??˱?׼
pvalueFilter=0.001       #?????Լ???pֵ?Ĺ??˱?׼
setwd("D:\\Program Files (x86)\\database\\完整lncRNA\\09.cuproptosisLnc")     #???ù???Ŀ¼

#??ȡlncRNA?ı????ļ?,???????ݽ??д???
rt=read.table("lncRNA.txt", header=T, sep="\t", check.names=F)
rt=as.matrix(rt)
rownames(rt)=rt[,1]
exp=rt[,2:ncol(rt)]
dimnames=list(rownames(exp),colnames(exp))
data=matrix(as.numeric(as.matrix(exp)),nrow=nrow(exp),dimnames=dimnames)
data=avereps(data)
data=data[rowMeans(data)>0,]

#ɾ????????Ʒ
group=sapply(strsplit(colnames(data),"\\-"),"[",4)
group=sapply(strsplit(group,""), "[", 1)
group=gsub("2","1",group)
lncRNA=data[,group==0]
conNum=length(group[group==1])       #????????Ʒ??Ŀ
treatNum=length(group[group==0])     #????????Ʒ??Ŀ
sampleType=c(rep(1,conNum), rep(2,treatNum))

#??ȡͭ???????ػ????ı????ļ?,???????ݽ??д???
rt1=read.table("cuproptosisExp.txt", header=T, sep="\t", check.names=F)
rt1=as.matrix(rt1)
rownames(rt1)=rt1[,1]
exp1=rt1[,2:ncol(rt1)]
dimnames1=list(rownames(exp1),colnames(exp1))
cuproptosis=matrix(as.numeric(as.matrix(exp1)), nrow=nrow(exp1), dimnames=dimnames1)
cuproptosis=avereps(cuproptosis)
cuproptosis=cuproptosis[rowMeans(cuproptosis)>0,]

#ɾ????????Ʒ
group=sapply(strsplit(colnames(cuproptosis),"\\-"),"[",4)
group=sapply(strsplit(group,""),"[",1)
group=gsub("2","1",group)
cuproptosis=cuproptosis[,group==0]

#?????Լ???
outTab=data.frame()
for(i in row.names(lncRNA)){
	if(sd(lncRNA[i,])>0.1){
		test=wilcox.test(data[i,] ~ sampleType)
		if(test$p.value<0.05){
			for(j in row.names(cuproptosis)){
				x=as.numeric(lncRNA[i,])
				y=as.numeric(cuproptosis[j,])
				corT=cor.test(x,y)
				cor=corT$estimate
				pvalue=corT$p.value
				if((cor>corFilter) & (pvalue<pvalueFilter)){
					outTab=rbind(outTab,cbind(Cuproptosis=j,lncRNA=i,cor,pvalue,Regulation="postive"))
				}
				if((cor< -corFilter) & (pvalue<pvalueFilter)){
					outTab=rbind(outTab,cbind(Cuproptosis=j,lncRNA=i,cor,pvalue,Regulation="negative"))
				}
			}
		}
	}
}

#?????????ԵĽ???
write.table(file="corResult.txt",outTab,sep="\t",quote=F,row.names=F)

#??ȡͭ????????lncRNA?ı???��
cuproptosisLncRNA=unique(as.vector(outTab[,"lncRNA"]))
cuproptosisLncRNAexp=data[cuproptosisLncRNA,]
cuproptosisLncRNAexp=rbind(ID=colnames(cuproptosisLncRNAexp), cuproptosisLncRNAexp)
write.table(cuproptosisLncRNAexp,file="cuproptosisLncExp.txt",sep="\t",quote=F,col.names=F)


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

