空间转录组-多样本数据合并分析

2023-11-20  本文已影响0人  大魔王鱼鱼鱼

1. 读入单个样本

for (samp in samples){
  prefix <- paste0("Data_",samp)
  sample.cr.dir <- paste0("Cellranger/",
                          samp,"/FF/outs/")
  seurat_data <- Load10X_Spatial(
        data.dir = sample.cr.dir,# 默认filtered_feature_bc_matrix.h5文件为输入
        assay = "Spatial",
        slice = samp,
        filter.matrix = TRUE,
        to.upper = FALSE)
  #给cellid增加样本名,避免合并时barcode重复,不加的话Seurat会自动给barcode加后缀避免重复
  # seurat_data <- RenameCells(seurat_data, add.cell.id = prefix) 
  seurat_data <- seurat_data[,seurat_data$nCount_Spatial>0]
  seurat_data$orig.ident <- samp
  seurat_data <- SCTransform(seurat_data,assay = "Spatial",verbose = FALSE) #合并样本时必须都有SCT这个assay才能合并
  seurat_data <- FindVariableFeatures(seurat_data) #合并后的数据hvg来自单样本hvg数据
  assign(prefix,seurat_data)
}

2. 合并数据

if (length(samples)>1){
    other <- ls(pattern="Data_")[-1]
    m <- function(x){eval(as.name(x))}
    data <- merge(x = get(ls(pattern="MergeData_")[1]),
                  y = c(lapply(X = other, function(x) m(x))))
  }
data$Sample <- data$orig.ident
merged.hvg <- c(lapply(X=ls(pattern="Data_"),function(x) VariableFeatures(x)))
VariableFeatures(data) <- unique(merged.hvg)
DefaultAssay(data) <- "SCT"

Tips

(1)合并后的数据如何删除部分样本

selected_sample_names <- c("Sample1","Sample2","Sample3","Sample4","Sample5","Sample6")
sample6 <- subset(sample7,cells = rownames(sample7@meta.data[sample7$Sample %in% selected_sample_names,]))
sample6@images <- sample6@images[selected_sample_names]
sample6[["SCT"]]@SCTModel.list <- sample6[["SCT"]]@SCTModel.list[1:6]#需要手动先看一下哪个model list是空的

(2)HE图片背景不干净如何修改后替换

需要注意⚠️修图后保存的时候图片的长宽要和原图一致
可使用图片预览工具,上方打开“工具-修改尺寸“


image.png
library(png)
#将修改好的图片读入
new_image <- readPNG(paste0("../HEimage_adjusted/",samp,".tissue_lowres_image.png")) 
# 从image替换掉
visium_data[["Sample1"]]@image= new_image

(3)删除部分spot

有时候会有少数spot脱离组织,散落在片子空白区域,我们不想将其纳入分析。

del_spots <- read.csv("delete_spot.csv")
delspots <- del_spots$Barcode
sample1.f <- sample1[,setdiff(colnames(sample1),delspots)]
上一篇下一篇

猜你喜欢

热点阅读