细胞亚群等比例抽样在做gsva, 细胞通讯,转录因子,拟时序, inferCNV(肿瘤拷贝数)的时候会有用。

    1. #细胞亚群等比例抽样
    2. rm(list = ls())
    3. library(Seurat)
    4. # devtools::install_github('satijalab/seurat-data')
    5. library(SeuratData)
    6. library(ggplot2)
    7. library(patchwork)
    8. library(dplyr)
    9. load(file = 'basic.sce.pbmc.Rdata')
    10. DimPlot(pbmc,
    11. reduction = 'umap',
    12. label = TRUE, pt.size = 0.5) + NoLegend()
    13. sce=pbmc
    14. features= c('IL7R', 'CCR7','CD14', 'LYZ', 'IL7R', 'S100A4',"MS4A1", "CD8A",'FOXP3',
    15. 'FCGR3A', 'MS4A7', 'GNLY', 'NKG7',
    16. 'FCER1A', 'CST3','PPBP')
    17. DoHeatmap(subset(sce ),
    18. features = features,
    19. size = 3
    20. )
    21. table(Idents(sce))

    image.png
    每个细胞数量不一致的话,有些小的细胞亚群都已经看不到了,所以可能就需要等比例抽取

    #抽取15个细胞
    DoHeatmap(subset(sce, downsample = 15), 
              features = features, 
              size = 3)
    #做gsva, 细胞通讯,转录因子,拟时序, inferCNV(肿瘤拷贝数)的时候会有用
    # 真实项目,10万+
    

    image.png
    图 抽取15个细胞

    #每个细胞亚群抽10 
    allCells=names(Idents(sce)) #所有细胞
    allType = levels(Idents(sce)) #所以细胞亚群
    
    #写循环在所有亚群中找基因
    choose_Cells = unlist(lapply(allType, function(x){
      cgCells = allCells[Idents(sce)== x ]
      cg=sample(cgCells,10)
      cg
      }))
    
    
    #9种细胞亚型,各抽出10个,共90个
    cg_sce = sce[, allCells %in% choose_Cells]
    cg_sce
    as.data.frame(table(Idents(cg_sce)))
    
    #看原始表达量 
    DoHeatmap(subset(sce), 
              features = features, 
              size = 3,
              slot = 'data')
    

    image.png
    图 原始表达量