1、目的
这次教程交给大家的肿瘤微环境分析主要分析基质细胞和免疫细胞之间的打分差异。 单基因套路8:肿瘤微环境分析 (qq.com)
肿瘤微环境概念就不做过多解释了,我们直接上代码吧
数据准备:需要准备表达矩阵和打分文件
2、代码
2.1肿瘤微环境
肿瘤微环境分析主要分析基质细胞和免疫细胞之间的打分差异。
#引用包
library(limma)
library(estimate)
library(reshape2)
library(ggpubr)
setwd("D:/生信代码复现/8.肿瘤微环境/8.肿瘤微环境") ##💚💚💚💚💚设置路径
expFile="combined_RNAseq_counts-after.txt" #💚💚💚💚💚表达数据文件
#读取文件,并对输入文件进行整理
rt=read.table(expFile, 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=data[rowMeans(data)>0,]
data=avereps(data)
#删除正常样品
group=sapply(strsplit(colnames(data),"\\-"), "[", 4)
group=sapply(strsplit(group,""), "[", 1)
group=gsub("2", "1", group)
data=data[,group==0]
#输出整理好的数据
out=rbind(ID=colnames(data),data)
write.table(out,file="uniq.symbol.txt",sep="\t",quote=F,col.names=F)
#运行estimate包
filterCommonGenes(input.f="uniq.symbol.txt",
output.f="commonGenes.gct",
id="GeneSymbol")
estimateScore(input.ds = "commonGenes.gct",
output.ds="estimateScore.gct")
#输出每个样品的打分
scores=read.table("estimateScore.gct", skip=2, header=T)
rownames(scores)=scores[,1]
scores=t(scores[,3:ncol(scores)])
rownames(scores)=gsub("\\.", "\\-", rownames(scores))
out=rbind(ID=colnames(scores), scores)
write.table(out, file="TMEscores.txt", sep="\t", quote=F, col.names=F)
2.2肿瘤微环境可视化代码
#引用包
library(limma)
library(reshape2)
library(ggpubr)
setwd("D:/生信代码复现/8.肿瘤微环境/8.肿瘤微环境") ##💚💚💚💚💚设置路径
expFile="geneExp.txt" #表达数据文件💚💚💚💚这个文件在番外篇告诉了如何获得
scoreFile="TMEscores.txt" #肿瘤微环境的打分文件
#读取表达数据文件
exp=read.table(expFile, header=T, sep="\t", check.names=F, row.names=1)
gene=colnames(exp)[1]
exp=exp[exp$Type=="Tumor",1,drop=F]
exp$Type=ifelse(exp[,gene]>median(exp[,gene]), "High", "Low")
#读取肿瘤微环境打分文件,并对数据进行整理
score=read.table(scoreFile, header=T, sep="\t", check.names=F, row.names=1)
score=score[,1:3]
#样品取交集
sameSample=intersect(row.names(exp), row.names(score))
exp=exp[sameSample,"Type",drop=F]
score=score[sameSample,,drop=F]
rt=cbind(score, exp)
rt$Type=factor(rt$Type, levels=c("Low", "High"))
#将合并后的数据转换为ggplot2的输入文件
data=melt(rt, id.vars=c("Type"))
colnames(data)=c("Type", "scoreType", "Score")
#绘制小提琴图
p=ggviolin(data, x="scoreType", y="Score", fill = "Type",
xlab="",
ylab="TME score",
legend.title=gene,
add = "boxplot", add.params = list(color="white"),
palette = c("blue","red"), width=1)
p=p+rotate_x_text(45)
p1=p+stat_compare_means(aes(group=Type),
method="wilcox.test",
symnum.args=list(cutpoints = c(0, 0.001, 0.01, 0.05, 1), symbols = c("***", "**", "*", " ")),
label = "p.signif")
#输出图形
pdf(file="vioplot.pdf", width=6, height=5)
print(p1)
dev.off()
3、效果
做完肿瘤微环境以后文件夹的效果

R语言中的效果
