作者弱左,Evil Genius
關(guān)于RCTD,本來都不打算更新了炕淮,R版本的聯(lián)合分析我覺得大家自己寫寫就完了拆火,現(xiàn)在看來,還是需要整理一下涂圆。
至于引用該方法的文章们镜,那就很多了。
RCTD有三種模式:
(1)doublet mode:每個spot分配1-2種細(xì)胞類型润歉,推薦用于具有高空間分辨率的技術(shù)模狭,如Stereo-seq、HD等(注意bin的大胁锐谩);
(2)full mode:每個spot分配任意數(shù)量的細(xì)胞類型嚼鹉,推薦用于具有低空間分辨率的技術(shù),如Visium;
(3)multi mode : doublet mode的擴(kuò)展驱富,可以每個spot發(fā)現(xiàn)兩個以上的細(xì)胞類型锚赤,作為全模式的替代選項(xiàng)。
很多博主褐鸥、公司推文寫了非常多的介紹和代碼示例线脚,大家可以借鑒一下。
不過對于這些網(wǎng)絡(luò)寫手叫榕,我更喜歡網(wǎng)絡(luò)俠客這個稱呼浑侥。
官網(wǎng)的教程寫了很多,針對不同精度的平臺都有晰绎,列舉一下:
不過官網(wǎng)分析的結(jié)果是真的丑
示例代碼如下,采用了Doublet mode模式
library(spacexr)
# set up reference
ref <- readRDS("../data/mouse_hippocampus_reference.rds")
ref <- UpdateSeuratObject(ref)
Idents(ref) <- "celltype"
# extract information to pass to the RCTD Reference function
counts <- ref[["RNA"]]$counts
cluster <- as.factor(ref$celltype)
names(cluster) <- colnames(ref)
nUMI <- ref$nCount_RNA
names(nUMI) <- colnames(ref)
reference <- Reference(counts, cluster, nUMI)
# set up query with the RCTD function SpatialRNA
slide.seq <- SeuratData::LoadData("ssHippo")
counts <- slide.seq[["Spatial"]]$counts
coords <- GetTissueCoordinates(slide.seq)
colnames(coords) <- c("x", "y")
coords[is.na(colnames(coords))] <- NULL
query <- SpatialRNA(coords, counts, colSums(counts))
RCTD <- create.RCTD(query, reference, max_cores = 8)
RCTD <- run.RCTD(RCTD, doublet_mode = "doublet")
slide.seq <- AddMetaData(slide.seq, metadata = RCTD@results$results_df)
RCTD目前大多數(shù)用在高精度的平臺荞下,比如Stereo-seq的bin20伶选、bin30的情況,HD的8um情況等锄弱。
但是對于真正的文章考蕾,一般都要根據(jù)自己的課題進(jìn)行修改。
下方是一個文章分析代碼示例
# Load required libraries
library(spacexr)
library(Seurat)
library(ggplot2)
library(dplyr)
library(tidyr)
library(pheatmap)
library(progeny)
# Data Preprocessing
# Load the Spatial data
pdac <- readRDS("/path/to/pdac_most_updated.rds")
# Load the Single cell data
sc <- readRDS("/path/to/sc_rctd.rds")
# Prepare data for RCTD
counts <- sc@assays$RNA@counts
Idents(sc) <- "SCT"
cluster <- as.factor(sc$celltype_nicheDE)
names(cluster) <- colnames(sc)
nUMI <- sc$nCount_RNA
names(nUMI) <- colnames(sc)
# Create the reference object
reference <- Reference(counts, cluster, nUMI)
# Prepare spatial transcriptomics data
counts <- pdac@assays$Spatial@counts
coordinates_list <- lapply(image_names, function(image_name) {
pos <- GetTissueCoordinates(pdac, image = image_name)
colnames(pos) <- c('x','y')
return(pos)
})
coords <- do.call(rbind, coordinates_list)
rownames(coords) <- gsub("^.+\\.", "", rownames(coords))
# Create SpatialRNA object
query <- SpatialRNA(coords, counts, colSums(counts))
# Run RCTD
RCTD <- create.RCTD(query, reference, max_cores = 8)
RCTD.full <- run.RCTD(RCTD, doublet_mode = "full")
# Add RCTD results to Seurat object
pdac <- AddMetaData(pdac, metadata = RCTD.full@results$results_df)
# Normalize weights
weights <- RCTD.full@results$weights
norm_weights <- normalize_weights(weights)
# Add RCTD results as a new assay
pdac[["rctd_full"]] <- CreateAssayObject(data = t(as.matrix(norm_weights)))
if (length(pdac@assays$rctd_full@key) == 0) {
pdac@assays$rctd_full@key <- "rctd_full_"
}
這樣就獲取了RCTD的解卷積空間細(xì)胞矩陣会宪,當(dāng)然了肖卧,通常分析到這里還沒結(jié)束,我們需要繼續(xù)分析空間細(xì)胞聚類掸鹅,共定位等分析內(nèi)容塞帐。
我們把RCTD拦赠、空間細(xì)胞聚類、共定位分析一起封裝起來葵姥,注意要適用各種空間平臺荷鼠,寫好參數(shù)的設(shè)定。跟cell2location一樣的榔幸,最好匹配的樣本聯(lián)合分析允乐。
#! usr/bin/R
### zhaoyunfei
### 20241111
suppressMessages({
library(Seurat)
library(compositions)
library(tidyverse)
library(clustree)
library(patchwork)
library(argparse)
library(robustbase)
library(ISCHIA)
library(factoextra)
library(dplyr)
library(scran)
library(ggplot2)
library(spacexr)
library(cluster)
library(showtext)
library(gridExtra)
library(pdftools)
})
parser = ArgumentParser()
parser$add_argument("--sc_rds", help="the sc data",required = T)
parser$add_argument("--spatial_rds", help="the sp data",required = T)
parser$add_argument("--sample", help="the sample name",required = T)
parser$add_argument("--outdir", help="the outdir",default = './')
parser$add_argument("--celltype", help="the annotation for celltype")
parser$add_argument("--normalization_method",default = 'SCT')
parser$add_argument("--reduction",help='Dimensional reduction to perform when finding anchors',choices = c('pcaproject','cca'),default = 'pcaproject')
parser$add_argument("--mode",help='mode for RCTD',default = 'doublet',choices = c('doublet','full','multi'))
parser$add_argument("--cell_interest", help="the interest of cell type,eg : 'T,B:Fobro,epi'")
args <- parser$parse_args()
str(args)
sc_rds = args$sc_rds
spatial_rds = args$spatial_rds
outdir = args$outdir
celltype = args$celltype
normalization_method = args$normalization_method
reduction = args$reduction
sample = args$sample
mode = args$mode
cell_interest = args$cell_interest
接下來就是腳本主體