单细胞组织偏好性分析 Ro/e OR

单细胞组织偏好性分析 Ro/e  OR

the ratio of observed to expected cell numbers (Ro/e)用于定量组织富集程度,可发现细胞群不同的组织偏好性。也可以看分组的细胞比例

有柱图 https://mp.weixin.qq.com/s/AfYRmLEw1oXE5S8zr5UQDA

KS代码:https://mp.weixin.qq.com/s/XxpguGv-DqhpezoOGlp3cA

我们改造后里面的参数可随意变化使用,可以使用不同的分组

Ro/e、Augur和miloR 的区别:https://mp.weixin.qq.com/s/MYIGKrskIWp5FXhSQffSYA

STARTRAC 做分组Ro/e :https://mp.weixin.qq.com/s/EW4WWfizX98uM8cqS-S1gg

#------------------------------------------------------ 单细胞组织偏好性分析 OR

### 教程1 KS https://mp.weixin.qq.com/s/XxpguGv-DqhpezoOGlp3cA

#remotes::install_github("Japrin/sscVis")

library("sscVis")

library("data.table")

library("grid")

library("cowplot")

library("ggrepel")

library("readr")

library("plyr")

library("ggpubr")

library("ggplot2")

library("dplyr")

library("tidyr")

setwd("D:/Monocyte subsets/Covid-Recover/单细胞组织偏好性分析 Roe")

#加载函数

source("./test_function.R")

source("./draw_analysis.R")

#数据分析

A <- do.tissueDist(cellInfo.tb = immune.5@meta.data,#这里的输入文件需要的是整理好的有分组和细胞类型的metadata文件

out.prefix = "./ORplot", #设定导出的图文件名,自己设置

pdf.width = 5, #热图的宽设置

pdf.height = 6, #热图的高度设置

verbose=1, #设置为1文件以list形式存储

meta.cluster = 'celltype', #这里是细胞类型,也可以是seurat_clusters,名称没有要求,就是你细胞类型的列名

loc = 'group', #这里就是分组,metadata中分组的列名,至于命名没有要求

z.hi=4) #热图legend最大值,根据实际情况自己设置

#查看并保存文件

A$OR.dist.mtx #做热图数据,OR值

A$p.dist.tb #p值

A$OR.dist.tb #OR值

A$count.dist.melt.ext.tb#组合表,adjust-pvalue等

#自己做图

data <- A$count.dist.melt.ext.tb

write.csv(data, file = 'data.csv')

library(ggplot2)

library(RColorBrewer)

data <- read.csv("data.csv", header = T)

ggplot(data, aes(cid, rid)) +

geom_tile(aes(fill = OR), colour = "black", size = 0.6)+

scale_fill_gradientn(name='OR',

colours=colorRampPalette(rev(brewer.pal(n = 7, name ="RdYlBu")))(100))+

theme_minimal() +

theme(axis.title.x=element_blank(),

axis.ticks.x=element_blank(),

axis.title.y=element_blank(),

axis.text.y = element_text(size = 14,color = 'black'),

axis.text.x = element_text(size = 14,color = 'black')) +

scale_y_discrete(position = "right") +

geom_text(aes(label = sprintf("%.2f", OR)), vjust = 1, hjust = 1, size = 5)

#------------------------------------------------------ 单细胞组织偏好性分析 Ro/e -适合亚群比例分析

#Sys.getenv("GITHUB_PAT")

#Sys.unsetenv("GITHUB_PAT")

#devtools::install_github("Japrin/STARTRAC")

#install.packages("tictoc")

library(Startrac)

library(ggplot2)

library(tictoc)

library(ggpubr)

library(ComplexHeatmap)

library(RColorBrewer)

library(circlize)

library(tidyverse)

library(sscVis)

library(Seurat)

library(tidyverse)

library(readr)

library(qs)

library(BiocParallel)

library(ComplexHeatmap)

register(MulticoreParam(workers = 8, progressbar = TRUE))

dat <- immune.5@meta.data

### calTissueDist函数进行计算参考 https://mp.weixin.qq.com/s/EW4WWfizX98uM8cqS-S1gg

# 计算OR比值比时可改为method = "fisher"

R_oe <- calTissueDist(dat,

byPatient = F,

colname.cluster = "celltype", # 细胞类型

colname.patient = "orig.ident", # 不同样本

colname.tissue = "group", # 分组

method = "chisq",

min.rowSum = 0)

R_oe

#col_fun <- colorRamp2(c(min(R_oe, na.rm = TRUE), 1, max(R_oe, na.rm = TRUE)),

# c("blue", "white", "red"))

### 显示数值

Heatmap(as.matrix(R_oe),

show_heatmap_legend = TRUE,

cluster_rows = TRUE,

cluster_columns = TRUE,

row_names_side = 'right',

show_column_names = TRUE,

show_row_names = TRUE,

col = col_fun,

row_names_gp = gpar(fontsize = 10),

column_names_gp = gpar(fontsize = 10),

heatmap_legend_param = list(

title = "Ro/e Index", # 自定义图注名称

at = seq(0.5, 2, by = 0.5), # 例刻度的位置/自己的数据必须修改一下!

labels = seq(0.5, 2, by = 0.5) # 每个刻度的标签/自己的数据必须修改一下!

),

cell_fun = function(j, i, x, y, width, height, fill) {

grid.text(sprintf("%.2f", R_oe[i, j]), x, y, gp = gpar(fontsize = 8, col = "black"))

}

)

### 可视化参考 https://mp.weixin.qq.com/s/MYIGKrskIWp5FXhSQffSYA

library(ComplexHeatmap)

library(grid)

# 定义颜色映射函数(根据你的数据范围调整颜色映射)

col_fun <- colorRamp2(c(0, 0.5, 1.5, 2), c("blue", "green", "orange", "red"))

### 显示 + - 号

# 绘制热图

Heatmap(as.matrix(R_oe),

show_heatmap_legend = TRUE,

cluster_rows = TRUE,

cluster_columns = TRUE,

row_names_side = 'right',

show_column_names = TRUE,

show_row_names = TRUE,

col = col_fun,

row_names_gp = gpar(fontsize = 18), # 坐标字体

column_names_gp = gpar(fontsize = 18), # 坐标字体

column_names_rot = 45, # 将列名倾斜 45°

heatmap_legend_param = list(

title = "Ro/e Index",

at = seq(0.5, 2, by = 0.5),

labels = seq(0.5, 2, by = 0.5)

),

cell_fun = function(j, i, x, y, width, height, fill) {

value <- R_oe[i, j]

if (value > 1) {

label <- "+++"

} else if (value > 0.8) {

label <- "++"

} else if (value >= 0.2) {

label <- "+"

} else if (value > 0) {

label <- "+/-"

} else if (value == 0) {

label <- "-"

} else {

label <- NA # 如果有负值或其他异常值

}

grid.text(label, x, y, gp = gpar(fontsize = 30 , col = "black")) # 调整里面

}

)

相关文章

乔一数字能量:1

乔一数字能量:1

原神深境螺旋奖励结算机制说明

原神深境螺旋奖励结算机制说明

摩托车鼓刹更换需要多少钱

摩托车鼓刹更换需要多少钱