当前位置: 首页 > news >正文

②单细胞学习-组间及样本细胞比例分析

目录

数据读入

每个样本各细胞比例

两个组间细胞比例

亚组间细胞比例差异分析(循环)

单个细胞类型亚新间比例差异

①单细胞学习-数据读取、降维和分群-CSDN博客

比较各个样本间的各类细胞比例或者亚组之间的细胞比例差异

①数据读入
#各样本细胞比例计算
rm(list = ls()) 
library(Seurat)
load("scedata1.RData")#这里是经过质控和降维后的单细胞数据
table(scedata$orig.ident)#查看各组细胞数
table(Idents(scedata))#查看各种类型细胞数目
#prop.table(table(Idents(scedata)))
table(Idents(scedata), scedata$orig.ident)#每个样本不同类型细胞数据
> table(scedata$orig.ident)#查看各组细胞数BM1  BM2  BM3  GM1  GM2  GM3 
2754  747 2158 1754 1528 1983 
> table(Idents(scedata))#查看各种类型细胞数目Fibroblast Endothelial      Immune       Other  Epithelial 2475        4321        2688         766         674 
> #prop.table(table(Idents(scedata)))
> table(Idents(scedata), scedata$orig.ident)#每个样本不同类型细胞数据BM1  BM2  BM3  GM1  GM2  GM3Fibroblast   571  135  520  651  312  286Endothelial  752  244  619  716  906 1084Immune      1220  145  539  270  149  365Other        142  161  194   55   79  135Epithelial    69   62  286   62   82  113

②每个样本各细胞比例
#换算每样样本每种细胞占有的比例:绘制总的堆积图
Cellratio <- prop.table(table(Idents(scedata),scedata$orig.ident),margin = 2)# margin = 2按照列计算每个样本比例
Cellratio <- as.data.frame(Cellratio)#计算比例绘制堆积图
library(ggplot2)#绘制细胞比例堆积图
colourCount = length(unique(Cellratio$Var1))
p1 <- ggplot(Cellratio) + geom_bar(aes(x =Var2, y= Freq, fill = Var1),stat = "identity",width = 0.7,size = 0.5,colour = '#222222')+ theme_classic() +labs(x='Sample',y = 'Ratio')+#coord_flip()+ #进行翻转theme(panel.border = element_rect(fill=NA,color="black", size=0.5, linetype="solid"))
p1
dev.off()
> head(Cellratio)Var1 Var2       Freq
1  Fibroblast  BM1 0.20733479
2 Endothelial  BM1 0.27305737
3      Immune  BM1 0.44299201
4       Other  BM1 0.05156137
5  Epithelial  BM1 0.02505447
6  Fibroblast  BM2 0.18072289

③两个组间细胞比例

这里比较BM和GM两个组间的细胞比例

##分成两个组进行比较:先查看每个样本的具体细胞数量
library(tidyverse)
library(reshape)
clusdata <- as.data.frame(table(Idents(scedata), scedata$orig.ident))
#进行长宽数据转换
clusdata1 <- clusdata %>% pivot_wider(names_from = Var2,values_from =Freq )
clusdata1 <- as.data.frame(clusdata1)
rownames(clusdata1) <- clusdata1$Var1
clusdata2 <- clusdata1[,-1]#[1] "BM1" "BM2" "BM3" "GM1" "GM2" "GM3"
#分别计算每个组每种细胞和
BM <- c("BM1","BM2","BM3")
clusdata2$BMsum <- rowSums(clusdata2[,BM])
GM <- c("GM1","GM2","GM3")
clusdata2$GMsum <- rowSums(clusdata2[,GM])#然后绘制堆积图
clus2 <- clusdata2[,c(7,8)]
clus2$ID <- rownames(clus2)
clus3  <- melt(clus2, id.vars = c("ID"))##根据分组变为长数据
p <- ggplot(data = clus3,aes(x=ID,y=value,fill=variable))+#geom_bar(stat = "identity",position = "stack")+    ##展示原来数值geom_bar(stat = "identity",position = "fill")+      ##按照比例展示:纵坐标为1scale_y_continuous(expand = expansion(mult=c(0.01,0.1)),##展示纵坐标百分比数值labels = scales::percent_format())+scale_fill_manual(values = c("BMsum"="#a56cc1","GMsum"="#769fcd"),       ##配色:"BMsum"="#98d09d","GMsum"="#e77381"limits=c("BMsum","GMsum"))+                            ##limit调整图例顺序theme(panel.background = element_blank(),          ##主题设置axis.line = element_line(),                   legend.position = "top")+                  #"bottom"labs(title = "single cell",x=NULL,y="percent")+           ##X,Y轴设置guides(fill=guide_legend(title = NULL,nrow = 1,byrow = FALSE))
p
dev.off()
> head(clus3)ID variable value
1  Fibroblast    BMsum  1226
2 Endothelial    BMsum  1615
3      Immune    BMsum  1904
4       Other    BMsum   497
5  Epithelial    BMsum   417
6  Fibroblast    GMsum  1249

④亚组间细胞比例差异分析(循环)
#组间差异分析:仍然是使用这个比例数据进行分析,不过却是在各个样本中进行比例比较
table(scedata$orig.ident)#查看各组细胞数
table(Idents(scedata))#查看各种类型细胞数目
table(Idents(scedata), scedata$orig.ident)#各组不同细胞群细胞数
Cellratio <- prop.table(table(Idents(scedata), scedata$orig.ident), margin = 2)#计算各组样本不同细胞群比例
Cellratio <- data.frame(Cellratio)
#需要进行数据转换,计算每个样本比例后进行差异分析
library(reshape2)
cellper <- dcast(Cellratio,Var2~Var1, value.var = "Freq")
rownames(cellper) <- cellper[,1]
cellper <- cellper[,-1]
###添加分组信息dataframe
sample <- c("BM1","BM2","BM3","GM1","GM2","GM3")
group <- c("BM","BM","BM","GM","GM","GM")
samples <- data.frame(sample, group)#创建数据框
rownames(samples)=samples$sample
cellper$sample <- samples[rownames(cellper),'sample']#R添加列
cellper$group <- samples[rownames(cellper),'group']#R添加列###作图展示
pplist = list()##循环作图建立空表
library(ggplot2)
library(dplyr)
library(ggpubr)
library(cowplot)
sce_groups = c('Endothelial','Fibroblast','Immune','Epithelial','Other')
for(group_ in sce_groups){cellper_  = cellper %>% select(one_of(c('sample','group',group_)))#选择一组数据colnames(cellper_) = c('sample','group','percent')#对选择数据列命名cellper_$percent = as.numeric(cellper_$percent)#数值型数据cellper_ <- cellper_ %>% group_by(group) %>% mutate(upper =  quantile(percent, 0.75), lower = quantile(percent, 0.25),mean = mean(percent),median = median(percent))#上下分位数print(group_)print(cellper_$median)pp1 = ggplot(cellper_,aes(x=group,y=percent)) + #ggplot作图geom_jitter(shape = 21,aes(fill=group),width = 0.25) + stat_summary(fun=mean, geom="point", color="grey60") +#stat_summary添加平均值theme_cowplot() +theme(axis.text = element_text(size = 10),axis.title = element_text(size = 10),legend.text = element_text(size = 10),legend.title = element_text(size = 10),plot.title = element_text(size = 10,face = 'plain'),legend.position = 'none') + labs(title = group_,y='Percentage') +geom_errorbar(aes(ymin = lower, ymax = upper),col = "grey60",width =  1)###组间t检验分析labely = max(cellper_$percent)compare_means(percent ~ group,  data = cellper_)my_comparisons <- list( c("GM", "BM") )pp1 = pp1 + stat_compare_means(comparisons = my_comparisons,size = 3,method = "t.test")pplist[[group_]] = pp1
}#批量绘制
plot_grid(pplist[['Endothelial']],pplist[['Fibroblast']],pplist[['Immune']],pplist[['Epithelial']],pplist[['Other']],#nrow = 5,#列数ncol = 5)#行数

⑤单个细胞类型亚新间比例差异
##数据处理
##单个细胞类型比例计算
rm(list = ls()) 
library(Seurat)
library(tidyverse)
library(reshape2)
library(ggplot2)
library(dplyr)
library(ggpubr)
library(cowplot)
load("scedata1.RData")#计算各个样本细胞,各种类型细胞
Cellratio <- prop.table(table(Idents(scedata), scedata$orig.ident), margin = 2)#计算样本比例
Cellratio <- data.frame(Cellratio)
cellper <- dcast(Cellratio,Var2~Var1, value.var = "Freq")##长数据转宽数据
rownames(cellper) <- cellper[,1]
cellper <- cellper[,-1]
sample <- c("BM1","BM2","BM3","GM1","GM2","GM3")###添加分组信息dataframe
group <- c("BM","BM","BM","GM","GM","GM")
samples <- data.frame(sample, group)#创建数据框
rownames(samples)=samples$sample
cellper$sample <- samples[rownames(cellper),'sample']#R添加列
cellper$group <- samples[rownames(cellper),'group']#R添加列dat <- cellper[,c(1,7)]#提取需要分析的细胞类型"Fibroblast" "group"  
#根据分组计算四分位及中位数
dat1 <- dat %>% group_by(group) %>% mutate(upper =  quantile(Fibroblast, 0.75), lower = quantile(Fibroblast, 0.25),mean = mean(Fibroblast),median = median(Fibroblast))
#table(dat1$group)#BM GM 

作图

#pdf("单个细胞类型组间比较.pdf",width = 4,height = 4)##一定添加大小
my_comparisons =list( c("BM","GM"))
P <- ggplot(dat1,aes(x=group,y= Fibroblast)) + #ggplot作图geom_jitter(shape = 21,aes(fill=group),width = 0.25) + stat_summary(fun=mean, geom="point", color="grey60") +theme_cowplot() +theme(axis.text = element_text(size = 10),axis.title = element_text(size = 10),legend.text = element_text(size = 10),legend.title = element_text(size = 10),plot.title = element_text(size = 10,face = 'plain'),legend.position = 'none') + labs(title = "group",y='Fibroblastage') +geom_errorbar(aes(ymin = lower, ymax = upper),col = "grey60",width =  1)+#误差棒#差异检验stat_compare_means(comparisons=my_comparisons,label.y = c(0.4),method="t.test",#wilcox.testlabel="p.signif")
P
dev.off()

感谢: TS的美梦-CSDN博客

参考:跟着Cell学单细胞转录组分析(六):细胞比例计算及可视化 (qq.com)

跟着Cell学单细胞转录组分析(十四):细胞比例柱状图---连线堆叠柱状图_单细胞细胞占比图怎么画-CSDN博客

相关文章:

  • 深度剖析:为什么 Spring 和 IDEA 都不推荐使用 @Autowired 注解
  • k8s问题
  • 代码质量与可维护性提升
  • 生成式AI的GPU网络技术架构
  • 5月28(信息差)
  • CGAL 获取网格相交面片
  • 深入学习 torch.distributions
  • 如何关闭或者减少屏蔽 CloudFlare 的真人检测
  • 效率工作:一键为多种资产添加统一材质(小插件)
  • 【智能算法应用】灰狼算法GWO求解三维路径规划问题
  • 基于双PI结构FOC闭环控制的永磁同步电机控制系统simulink建模与仿真
  • pinpoint服务监控
  • 安全工具合集:内含渗透测试及黑客利器(持续更新中)
  • 错误模块路径: ...\v4.0.30319\clr.dll,v4.0.30319 .NET 运行时中出现内部错误,进程终止,退出代码为 80131506。
  • LeetCode精华75题(持续更新)
  • 【React系列】如何构建React应用程序
  • es6--symbol
  • HTTP中GET与POST的区别 99%的错误认识
  • JavaScript 奇技淫巧
  • JavaScript对象详解
  • Mac 鼠须管 Rime 输入法 安装五笔输入法 教程
  • Python打包系统简单入门
  • Python十分钟制作属于你自己的个性logo
  • Spring声明式事务管理之一:五大属性分析
  • use Google search engine
  • windows-nginx-https-本地配置
  • 更好理解的面向对象的Javascript 1 —— 动态类型和多态
  • 工程优化暨babel升级小记
  • 基于webpack 的 vue 多页架构
  • 模型微调
  • 配置 PM2 实现代码自动发布
  • 全栈开发——Linux
  • 算法之不定期更新(一)(2018-04-12)
  • 一、python与pycharm的安装
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • 资深实践篇 | 基于Kubernetes 1.61的Kubernetes Scheduler 调度详解 ...
  • ​ssh免密码登录设置及问题总结
  • ​总结MySQL 的一些知识点:MySQL 选择数据库​
  • # Java NIO(一)FileChannel
  • #APPINVENTOR学习记录
  • #java学习笔记(面向对象)----(未完结)
  • (12)目标检测_SSD基于pytorch搭建代码
  • (3) cmake编译多个cpp文件
  • (ibm)Java 语言的 XPath API
  • (回溯) LeetCode 46. 全排列
  • (三)docker:Dockerfile构建容器运行jar包
  • (删)Java线程同步实现一:synchronzied和wait()/notify()
  • (循环依赖问题)学习spring的第九天
  • (一)模式识别——基于SVM的道路分割实验(附资源)
  • (原創) 物件導向與老子思想 (OO)
  • (转)iOS字体
  • (转)Sublime Text3配置Lua运行环境
  • .axf 转化 .bin文件 的方法
  • .NET 6 在已知拓扑路径的情况下使用 Dijkstra,A*算法搜索最短路径