CIBERSORT | 免疫浸润分析R包

1. 安装

library(devtools)
devtools::install_github("Moonerss/CIBERSORT")
library(CIBERSORT)
library(ggplot2)
library(dplyr)
library(ggthemes)
library(pheatmap)
library(tibble)
library(tidyr)
library(ggpubr)
library(ggsci)
library(ggthemes)

2. 数据准备

需要两个数据:LM22和你需要分析的表达矩阵,我的是bulkRNA的表达矩阵,tpm标准化后的

#读取LM22文件(免疫细胞特征基因文件)
sig_matrix <- system.file("extdata", "LM22.txt", package = "CIBERSORT")
#表达矩阵文件
exp <- read.table(file="tpm_exp.txt",row.names = 1)

3. 运行

res <- cibersort(sig_matrix, tpm_exp, perm = 1000, QN = F)
#QN如果是芯片设置为T,如果是测序就设置为F
write.table(res, "res.txt", 
            sep = "\t", row.names = T, col.names = T, quote = F)

4. 可视化

#1.H和D组箱线图
res1 <- data.frame(res[,1:22])%>%
  mutate(group = c(rep('H',3),rep('D',3)))%>%
  rownames_to_column("sample")%>%
  pivot_longer(cols = colnames(.)[2:23],
               names_to = "cell.type",
               values_to = 'value')

ggplot(res1,aes(cell.type,value,fill = group)) + 
  geom_boxplot(outlier.shape = 21,color = "black") + 
  theme_bw() + 
  labs(x = "Cell Type", y = "Estimated Proportion") +
  theme(legend.position = "top") + 
  theme(axis.text.x = element_text(angle=80,vjust = 0.5,size = 14,face = "italic",colour = 'black'),
        axis.text.y = element_text(face = "italic",size = 14,colour = 'black'))+
  scale_fill_nejm()+
  stat_compare_means(aes(group = group),label = "p.format",size=3,method = "kruskal.test")

#2.热图
#展示所有的免疫细胞
normalize <- function(x) {
  if((max(x) - min(x)) == 0){
    return(mean(x))
  }else{
    return((x - min(x)) / (max(x) - min(x)))
  }
}
res1 <- res[,-(23:25)]
res2 <- apply(res1, 2,normalize)
res2 <- t(as.data.frame(res2))
pheatmap(res2,
         angle_col = "45",
         show_colnames = T,
         cluster_cols = F,
         color = colorRampPalette(c("navy", "white", "firebrick3"))(50))

#展示在一半以上样本里丰度大于0的免疫细胞
k <- apply(res1,2,function(x) {sum(x == 0) < nrow(exp)/2})
table(k)
#FALSE  TRUE 
#11    11 
res3 <- as.data.frame(t(res1[,k]))
pheatmap(res3,scale = "row",
         angle_col = "45",
         show_colnames = T,
         cluster_cols = F,
         color = colorRampPalette(c("navy", "white", "firebrick3"))(50))

#3.柱状图
res4 <- exp %>%
  as.data.frame() %>%
  rownames_to_column("sample") %>%
  pivot_longer(cols = 2:23,
               names_to = "CellType",
               values_to = "Composition")
ggbarplot(
  res4,
  x = "sample",
  y = "Composition",
  size = 0.5,
  fill = "CellType",
  color = "CellType") +
  theme_base() +
  theme(axis.text.x = element_text(
      angle = 45,
      hjust = 1,
      vjust = 1,
      size = 20),
    legend.position = "bottom",
    legend.key.size = unit(10,"pt")
  )

参考:量化免疫浸润时CIBERSORT的注意事项。 · Issue #694 · gege-circle/home · GitHub

肿瘤免疫浸润分析:CIBERSORT包(超简单的使用方法) - 知乎 (zhihu.com)

CIBERSORT 学习笔记_菠萝西斯的博客-CSDN博客

CIBERSORT 免疫浸润(2023.1.2更新版) - 简书 (jianshu.com)

### CIBERSORT 相关性分析方法 #### 安装与配置 为了使用 CIBERSORT 进行相关性分析,首先需要准备好必要的软件工具和依赖项。通常情况下,CIBERSORT 的运行基于 R 或 Python 编程环境。 对于 R 用户来说,可以利用 Bioconductor 来安装 `cibersort` : ```r if (!requireNamespace("BiocManager", quietly = TRUE)) install.packages("BiocManager") BiocManager::install("cibersort") library(cibersort) ``` Python 用户则可以通过 pip 工具来获取相应的库支持: ```bash pip install cibersortx ``` #### 准备输入文件 在执行 CIBERSORT 分析之前,需准备两个主要类型的输入文件:一个是目标样本的基因表达矩阵;另一个是由用户定义或从已有研究中获得的标准特征集(signature matrix)。这些数据应当被整理成适合程序读取的形式[^1]。 #### 执行 CIBERSORT 分析 一旦准备工作完成,便可通过调用特定函数来进行实际计算。以下是采用默认参数设置的一个简单例子,在此过程中会输出估计得到的各种细胞比例结果。 ```r # 加载所需并导入数据 expr_matrix <- read.csv("path/to/expression_data.csv", row.names=1) # 设定签名矩阵路径 sig_file_path <- "path/to/signature_matrix.txt" # 调用 CIBERSORT 函数进行估算 result <- run_cibersort(expr_matrix, sig_file_path) # 查看部分结果概览 head(result$Fraction) ``` 上述代码片段展示了如何加载外部数据以及启动核心算法的过程。值得注意的是,具体实现细节可能会因版本差异而有所不同,请参照官方文档获取最新指导说明。 #### 结果解释与可视化 经过处理后所获得的结果通常是各类型细胞占总群体的比例数值列表。为进一步探索潜在关联模式,可借助多种统计测试手段评估不同条件下相对丰度变化情况,并绘制热图、散点图等形式直观呈现出来。 例如,下面这段脚本实现了对两组间显著改变成分的筛选及其图形化展示功能: ```r # 假设有两个条件 A 和 B 对应的数据框分别为 df_A 和 df_B diff_cells <- compare_fractions(df_A$result$Fraction, df_B$result$Fraction) # 绘制火山图表示差异程度分布状况 plot_volcano(diff_cells$logFC, diff_cells$pvalue) ``` 以上操作不仅有助于理解个体内部复杂的免疫状态构成,也为发现新的生物标记物提供了有力依据。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值