专栏名称: 生信菜鸟团
生信菜鸟团荣誉归来,让所有想分析生物信息学数据的小伙伴找到归属,你值得拥有!
目录
相关文章推荐
BioArt  ·  专家点评Nature | ... ·  昨天  
生物探索  ·  Nature Genetics | ... ·  2 天前  
生物探索  ·  Nature Medicine | ... ·  4 天前  
生信宝典  ·  最全植物基因组数据平台 IMP中文教程( ... ·  2 天前  
生物学霸  ·  35 ... ·  2 天前  
51好读  ›  专栏  ›  生信菜鸟团

R tips:使用shiny和plotly获得umap图的点坐标

生信菜鸟团  · 公众号  · 生物  · 2024-11-02 20:15

主要观点总结

文章介绍了如何从单细胞umap图中获取部分点的坐标,并对这些点进行后续操作。以Seruat的pbmc数据为例,文章详细描述了以下步骤:查看umap plot并确定需要获取点的位置,使用plotly手动绘制可交互的umap plot,通过shiny打开交互式umap plot并使用套索工具获取选区边界,在shiny页面圈选感兴趣的区域,读取套索选区并找到点坐标。

关键观点总结

关键观点1: 查看umap plot并确定需要获取点的位置

使用Seruat的pbmc数据,通过RunUMAP和DimPlot函数查看降维图,确定需要获取点的位置。

关键观点2: 使用plotly手动绘制可交互的umap plot

通过plotly库,将umap数据以散点图的形式呈现出来,可以交互式地查看数据点。

关键观点3: 使用shiny打开交互式umap plot并使用套索工具获取选区边界

借助shiny包将交互式umap plot打开,利用套索工具圈选感兴趣的区域,程序会自动将选区边界坐标导出到指定文件中。

关键观点4: 读取套索选区并找到点坐标

读取保存的套索选区文件,使用sp包计算所有在选区中的点,并在umap图中展示套索选区和选中的点。


正文

有的时候需要获取单细胞umap图中的部分点的坐标,并对其进行后续操作。

本文提供一种方法用于实现这个目的。

以Seruat的pbmc数据为例。

1. 查看umap plot,检查需要获得的点

  1. library(tidyverse)

  2. library(Seurat)

  3. #1. 查看umap plot,检查需要去除的点

  4. pbmc_small <- pbmc_small %>% RunUMAP(dims = 1:10)

  5. DimPlot(pbmc_small)

查看降维图,并考虑获得右上角的点的坐标。

2. 使用plotly手动绘制可交互的umap plot

使用plotly手动实现这个umap plot散点图。

  1. library(plotly)

  2. umap_df <- pbmc_small[['umap']]@cell.embeddings %>%

  3. as.data.frame() %>%

  4. rownames_to_column("cell_id") %>%

  5. left_join(

  6. pbmc_small[['RNA_snn_res.1']] %>% rownames_to_column("cell_id"),

  7. by = "cell_id"

  8. )

  9. # 实际项目中,数据点太多,只需要随机选择部分点绘制

  10. # 能找到目标区域的位置即可

  11. sub_umap_df <-

  12. umap_df %>%

  13. group_by(RNA_snn_res.1) %>%

  14. slice_sample(n = 10000)

  15. p <-

  16. plot_ly(showlegend = T) %>%

  17. add_markers(data = sub_umap_df, x= ~umap_1, y = ~umap_2, text = ~ cell_id, color = ~RNA_snn_res.1, tooltip = c('x', 'y'))

3. 将交互式的umap plot使用shiny打开,并可以使用套索工具获得选区边界

  1. library(shiny)

  2. library(plotly)

  3. ui <- fluidPage(

  4. plotlyOutput("plot"),

  5. verbatimTextOutput("selected"),

  6. )

  7. server <- function(input, output, session) {

  8. output$"plot-text" <- renderText("Plot: ")

  9. output$plot <- renderPlotly(p)

  10. output$selected <- renderPrint({

  11. d <- event_data("plotly_brushed")

  12. if (!is.null(d)) {

  13. saveRDS(d, "lassso_boundary.rds")

  14. d

  15. }

  16. })

  17. }

  18. shinyApp(ui, server)

4. 在shiny页面圈选感兴趣的区域

在shiny页面,使用套索工具圈选感兴趣的区域,程序会自动将选区边界坐标导出到家目录中的lassso_boundary.rds文件中。

5. 读取套索选区,找到点坐标

  1. lasso_boudnary <- readRDS("lassso_boundary.rds")

  2. # find points

  3. point_in_poly_idx <-

  4. sp::point.in.polygon(

  5. point







请到「今天看啥」查看全文