CIBERSORT 免疫浸润(2023.1.2更新版)

目前主流的免疫浸润计算方法是CIBERSORT和ssgsea,今天介绍CIBERSORT。

1.输入数据要什么

下面这段话摘自CIBERSORT的介绍

Importantly, all expression data should be non-negative, devoid of missing values, and represented in non-log linear space.

For Affymetrix microarrays, a custom chip definition file (CDF) is recommended (see Subheading 3.2.2) and should be normalized with MAS5 or RMA.

Illumina Beadchip and single color Agilent arrays should be processed as described in the limma package.

Standard RNA-Seq expression quantification metrics, such as frag- ments per kilobase per million (FPKM) and transcripts per kilobase million (TPM), are suitable for use with CIBERSORT. –《Profiling Tumor Infiltrating Immune Cells with CIBERSORT》

非常清楚的写出了输入数据的要求:
1.不可以有负值和缺失值

2.不要取log

3.如果是芯片数据,昂飞芯片使用RMA标准化,Illumina 的Beadchip 和Agilent的单色芯片,用limma处理。

4.如果是RNA-Seq表达量,使用FPKM和TPM都很合适。

芯片的要求可能把你唬住了,GEO常规的表达矩阵都是这样得到的,直接下载使用即可。注意有的表达矩阵下载下来就已经取过log,需要逆转回去。有的经过了标准化或者有负值,需要处理原始数据,前面写过介绍文了。

2.来一个示例

找了一个转录组fpkm数据。

a = read.delim("GSE201050_4_genes_fpkm_expression.txt.gz",check.names = F)
library(stringr)
exp = a[,str_starts(colnames(a),"FPKM")]
k = !duplicated(a$gene_name);table(k)
## k
## FALSE  TRUE 
##  7089 43030

exp = exp[k,]
rownames(exp) = unique(a$gene_name)
colnames(exp) = str_remove(colnames(exp),"FPKM.")
exp[1:4,1:4]
##             CK_1        CK_2       CK_3      D_2_1
## TSPAN6  1.931311  2.10492200  2.6443970  3.6337456
## TNMD    0.000000  0.03177236  0.0000000  0.0000000
## DPM1   61.199326 60.15538342 63.9972350 67.9992979
## SCYL3   1.218088  1.09913176  0.8558017  0.9523167

2.3 做成cibersort要求的输入文件

需要两个输入文件:

一个是表达矩阵文件

一个是R包里的内置数据LM22.txt,记录了22种免疫细胞的基因表达特征数据。

由于读取文件的代码比较粗暴,为了适应它,导出文件之前需要把行名变成一列。不然后面就会有报错。

library(tidyverse)
exp2 = as.data.frame(exp)
exp2 = rownames_to_column(exp2)
write.table(exp2,file = "exp.txt",row.names = F,quote = F,sep = "\t")

2.4. 运行CIBERSORT

f = "ciber_GSE201050.Rdata"
if(!file.exists(f)){
  #devtools:: install_github ("Moonerss/CIBERSORT")
  library(CIBERSORT)
  lm22f = system.file("extdata", "LM22.txt", package = "CIBERSORT")
  TME.results = cibersort(lm22f, 
                        "exp.txt" , 
                        perm = 1000, 
                        QN = T)
  save(TME.results,file = f)
}
load(f)
TME.results[1:4,1:4]

##       B cells naive B cells memory Plasma cells T cells CD8
## CK_1    0.047478089    0.000000000  0.013099049 0.035400078
## CK_2    0.004488084    0.000000000  0.004812423 0.025224919
## CK_3    0.000000000    0.003064788  0.001037858 0.013915937
## D_2_1   0.003727559    0.000000000  0.000000000 0.001672263

re <- TME.results[,-(23:25)]

运行有些慢。计算出来的结果包含了22种免疫细胞的丰度,还有三列其他统计量,不管它们。

2.5. 经典的免疫细胞丰度热图

那些在一半以上样本里丰度为0的免疫细胞,就不展示在热图里了。我看了一下这个热图,从聚类的情况来看,不同分组之间没有很好的分开。

library(pheatmap)
k <- apply(re,2,function(x) {sum(x == 0) < nrow(TME.results)/2})
table(k)

## k
## FALSE  TRUE 
##    14     8

re2 <- as.data.frame(t(re[,k]))
Group = str_sub(colnames(exp),1,str_length(colnames(exp))-2)
table(Group)

## Group
##     CK    D_2    D_3   DP_3 DP_3_1     YS 
##      3      3      3      3      3      3

an = data.frame(group = Group,
                row.names = colnames(exp))
pheatmap(re2,scale = "row",
         show_colnames = F,
         cluster_cols = F,
         annotation_col = an,
         color = colorRampPalette(c("navy", "white", "firebrick3"))(50))

2.6. 经典柱状图

可以展示出每个样本的免疫细胞比例

library(RColorBrewer)
mypalette <- colorRampPalette(brewer.pal(8,"Set1"))
dat <- re %>% 
  as.data.frame() %>%
  rownames_to_column("Sample") %>% 
  mutate(group = Group) %>% 
  gather(key = Cell_type,value = Proportion,-Sample,-group) %>% 
  arrange(group)

dat$Sample = factor(dat$Sample,ordered = T,levels = unique(dat$Sample)) #定横坐标顺序
# 先把group排序,然后将sample设为了因子,确定排序后的顺序为水平,所以两图的顺序是对应的。
dat2 = data.frame(a = 1:ncol(exp),
                  b = 1,
                  group = sort(Group)) 

p1 = ggplot(dat2,aes(x = a, y = b)) + 
      geom_tile(aes(fill = group)) + 
      scale_fill_manual(values = mypalette(22)[1:length(unique(Group))]) +
      theme(panel.grid = element_blank(), 
            panel.background = element_blank(), 
            axis.line = element_blank(), 
            axis.ticks = element_blank(), 
            axis.text = element_blank(), 
            axis.title = element_blank()) + 
      scale_x_continuous(expand = c(0, 0)) +
      labs(fill = "Group")

p2 = ggplot(dat,aes(Sample, Proportion,fill = Cell_type)) + 
  geom_bar(stat = "identity") +
  labs(fill = "Cell Type",x = "",y = "Estiamted Proportion") + 
  theme_bw() +
  theme(#axis.text.x = element_blank(),
        axis.ticks.x = element_blank()
        ) + 
  scale_y_continuous(expand = c(0.01,0)) +
  scale_fill_manual(values = mypalette(22))

library(patchwork)
p1 / p2 + plot_layout(heights = c(1,10),guides = "collect" ) &
  theme(legend.position = "bottom")

3.7 箱线图

展示免疫细胞之间的比较。

ggplot(dat,aes(Cell_type,Proportion,fill = Cell_type)) + 
  geom_boxplot() + 
  theme_bw() + 
  labs(x = "Cell Type", y = "Estimated Proportion") +
    theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        legend.position = "bottom") + 
  scale_fill_manual(values = mypalette(22))

每种免疫细胞不同分组的结果做个比较:

画出全部在组间差异显著的细胞

# 全是0的行去掉
k = colSums(re)>0;table(k)

## k
## FALSE  TRUE 
##     2    20

re = re[,k]
library(tinyarray)
draw_boxplot(t(re),factor(Group),
             drop = T,
             color = mypalette(length(unique(Group))))+
  labs(x = "Cell Type", y = "Estimated Proportion") 

单画某一个感兴趣的免疫细胞

dat2 = dat[dat$Cell_type=="Eosinophils",]
library(ggpubr)
ggplot(dat2,aes(Group,Proportion,fill = Group)) + 
  geom_boxplot() + 
  theme_bw() + 
  labs(x = "Group", y = "Estimated Proportion") +
  theme(legend.position = "top") + 
  scale_fill_manual(values = mypalette(length(unique(Group))))+ stat_compare_means(aes(group = Group,label = ..p.signif..),method = "kruskal.test")

搞掂~

你可能感兴趣的:(CIBERSORT 免疫浸润(2023.1.2更新版))