#if (!requireNamespace("BiocManager", quietly = TRUE)) # install.packages("BiocManager") #BiocManager::install("limma") #install.packages("pheatmap") #引用包 library("limma") library(pheatmap) setwd("D:\\biowolf\\metabolism\\11.diff") #设置工作目录 inputFile="tcgaMetabExp.share.txt" #输入文件 fdrFilter=0.05 #fdr临界值 logFCfilter=0.5 #logFC临界值 conNum=32 #normal组样品数目 treatNum=375 #tumor组样品数目 #读取输入文件 outTab=data.frame() grade=c(rep(1,conNum),rep(2,treatNum)) rt=read.table(inputFile,sep="\t",header=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,] data[data<0]=0 #差异分析 for(i in row.names(data)){ geneName=unlist(strsplit(i,"\\|",))[1] geneName=gsub("\\/", "_", geneName) rt=rbind(expression=data[i,],grade=grade) rt=as.matrix(t(rt)) wilcoxTest<-wilcox.test(expression ~ grade, data=rt) conGeneMeans=mean(data[i,1:conNum]) treatGeneMeans=mean(data[i,(conNum+1):ncol(data)]) logFC=log2(treatGeneMeans)-log2(conGeneMeans) pvalue=wilcoxTest$p.value conMed=median(data[i,1:conNum]) treatMed=median(data[i,(conNum+1):ncol(data)]) diffMed=treatMed-conMed if( ((logFC>0) & (diffMed>0)) | ((logFC<0) & (diffMed<0)) ){ outTab=rbind(outTab,cbind(gene=i,conMean=conGeneMeans,treatMean=treatGeneMeans,logFC=logFC,pValue=pvalue)) } } pValue=outTab[,"pValue"] fdr=p.adjust(as.numeric(as.vector(pValue)),method="fdr") outTab=cbind(outTab,fdr=fdr) #输出所有基因的差异情况 write.table(outTab,file="all.xls",sep="\t",row.names=F,quote=F) #输出差异表格 outDiff=outTab[( abs(as.numeric(as.vector(outTab$logFC)))>logFCfilter & as.numeric(as.vector(outTab$fdr))logFCfilter) points(as.numeric(as.vector(diffSub$logFC)), -log10(diffSub$fdr), pch=20, col="red",cex=0.8) diffSub=subset(outTab, fdr