首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >脚本更新---高精度空间转录组的空间度量分析

脚本更新---高精度空间转录组的空间度量分析

原创
作者头像
追风少年i
发布2025-05-31 08:28:47
发布2025-05-31 08:28:47
10200
代码可运行
举报
运行总次数:0
代码可运行

作者,Evil Genius

端午佳节,大家开心么?希望大家早日发文章,文章现在成了硬通货了。

今天我们需要更新脚本,为马上到来的单细胞空间培训课程做准备。

今天我们的目标,高精度空间转录组的空间度量分析

在文章文献分享--气道壁的单细胞空间图揭示了促炎细胞生态系统及其在健康和哮喘中的相互作用中,我们看到了这种方法的运用

针对高精度的空间平台,这种距离的度量也是需要详细分析的。

我们来实现一下

首先是数据结构,大家看过文章或者介绍之后都应该是知道,用于分析的主要就是表达矩阵和空间坐标。

代码语言:javascript
代码运行次数:0
运行
复制
library(tidyverse)
library(phenoptr)

#####示例数据在https://github.com/akoyabio/phenoptr/blob/main/data/sample_cell_seg_data.rda
#####大家可以研究一下,软件自带的

csd <- sample_cell_seg_data

csd <- csd %>% filter(Phenotype!='other')

distances <- find_nearest_distance(csd)
glimpse(distances)

csd_with_distance <- bind_cols(csd, distances)

merged_with_distance <- merged %>%
  group_by(`Sample Name`) %>%
  do(bind_cols(., find_nearest_distance(.)))

####Analyzing nearest neighbor distances
csd_with_distance %>% group_by(Phenotype) %>% 
  select(Phenotype, starts_with('Distance to')) %>% 
  summarize_all(~round(mean(.), 1))

ggplot(csd_with_distance, aes(`Distance to CD8+`, color=Phenotype)) +
  geom_density(size=1)

Visualizing nearest neighbors

代码语言:javascript
代码运行次数:0
运行
复制
# Filter to just CD8+ and CK+ cells
cd8_cells = csd_with_distance %>% filter(select_rows(csd_with_distance, 'CD8+'))
ck_cells = csd_with_distance %>% filter(select_rows(csd_with_distance, 'CK+'))

# For each CD8+ cell, join with the data for the nearest CK+ cell
cd8_to_ck = cd8_cells %>% left_join(ck_cells, by=c('Cell ID CK+'='Cell ID'),
                                    suffix=c('', '.CK'))

####Show the CD8+ and CK+ cells with the nearest neighbors connected
# Read a background image and make a base plot
background_path = 
  system.file("extdata/sample/Set4_1-6plex_[16142,55840]_composite_image.jpg", package='phenoptr')
background = jpeg::readJPEG(background_path) %>% as.raster()
xlim = c(0, 934)
ylim = c(0, 700)
base_plot = ggplot(mapping=aes(`Cell X Position`, `Cell Y Position`)) %>% 
  phenoptr:::add_scales_and_background(background, xlim, ylim, scale_color='white') +
  labs(x='Cell X Position', y='Cell Y Position') +
  scale_color_manual('Phenotype', values=c('CD8+'='red', 'CK+'='cyan2'))

# Add lines and points
base_plot + geom_segment(data=cd8_to_ck,
               aes(xend=`Cell X Position.CK`, yend=`Cell Y Position.CK`),
               color='white') +
  geom_point(data=ck_cells, aes(color='CK+'), size=1) +
  geom_point(data=cd8_cells, aes(color='CD8+'), size=1) +
  labs(title='Nearest CK+ to each CD8+')
代码语言:javascript
代码运行次数:0
运行
复制
ck_to_cd8 = ck_cells %>% left_join(cd8_cells, by=c('Cell ID CD8+'='Cell ID'),
                                    suffix=c('', '.CD8'))
base_plot + 
  geom_segment(data=ck_to_cd8,
               aes(xend=`Cell X Position.CD8`, yend=`Cell Y Position.CD8`),
               color='white') +
  geom_point(data=ck_cells, aes(color='CK+'), size=1) +
  geom_point(data=cd8_cells, aes(color='CD8+'), size=1) +
  labs(title='Nearest CD8+ to each CK+')

Mutual nearest neighbors

代码语言:javascript
代码运行次数:0
运行
复制
mutual = ck_to_cd8 %>% filter(`Cell ID`==`Cell ID CK+.CD8`)
base_plot +
  geom_segment(data=mutual,
               aes(xend=`Cell X Position.CD8`, yend=`Cell Y Position.CD8`),
               size=1, color='white') +
  geom_point(data=ck_cells, aes(color='CK+'), size=1) +
  geom_point(data=cd8_cells, aes(color='CD8+'), size=1) +
  labs(title='Mutual nearest neighbors - CD8+ and CK+')

Cells within a radius

代码语言:javascript
代码运行次数:0
运行
复制
count_within(csd, from='CD68+', to='CK+', radius=25)

count_within(csd, from='CK+', to='CD68+', radius=25)

生活很好,有你更好,大家加油吧。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 作者,Evil Genius
  • 端午佳节,大家开心么?希望大家早日发文章,文章现在成了硬通货了。
  • 今天我们需要更新脚本,为马上到来的单细胞空间培训课程做准备。
  • 今天我们的目标,高精度空间转录组的空间度量分析
  • 在文章文献分享--气道壁的单细胞空间图揭示了促炎细胞生态系统及其在健康和哮喘中的相互作用中,我们看到了这种方法的运用
  • 针对高精度的空间平台,这种距离的度量也是需要详细分析的。
  • 我们来实现一下
  • 首先是数据结构,大家看过文章或者介绍之后都应该是知道,用于分析的主要就是表达矩阵和空间坐标。
  • Visualizing nearest neighbors
  • Mutual nearest neighbors
  • Cells within a radius
  • 生活很好,有你更好,大家加油吧。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档