作者,追風少年i~
國慶前的最后一彈,分享一個簡單的內容,空間軌跡向量場。
其中關于空間軌跡,我也寫了很多,文章放在下面,供大家參考
圖片.png
首先我們來解讀以下這個圖片,這個地方類似于基因、細胞類型或者通路的區域轉換(細胞遷移)。為了探索代謝改變區域中遷移基因表達特征的富集,確定了特定基因表達特征的低富集和高富集之間的定向梯度的空間方向。 簡化后,每個點的方向向量是基于其局部鄰域中所研究的基因表達特征的分級富集。這些向量場計算使我們能夠近似空間基因表達軌跡,從而能夠識別空間上相反的轉錄途徑。基于這些矢量場計算,報告缺氧響應和遷移特征顯示反向空間軌跡(上圖C、D)。 總之,研究結果為代謝變化和氧化應激是基因組多樣性的潛在互惠驅動因素提供了證據,從而導致 GBM 中的克隆進化。
其中我們要實現的部分在
空間軌跡向量場
話不多說,我們直接來
library(ggplot2)
library(Seurat)
library(SPATA2)
library(dplyr)
source('runVectorFields.R')
source('plotVectorFields.R')
####腳本放在最后
x = readRDS(Seurat.spatial.rds)
y = transformSeuratToSpata(seurat_object = x, sample_name = 'FT')
####這個時候我們把需要分析的軌跡基因、通路或者細胞類型進行分析,這里以CD3D為例
data = runVectorFields(y,'CD3D')
head(data)
data
有了data,就可以進行向量場繪圖
p = plotVectorFields(data,'CD3D')
pdf('eg.pdf')
print(p)
dev.off()
就會得到向量場圖。
圖片.png
其中的顏色,點的大小都可以更改,選擇自己喜歡的搭配,當然了,我這里是拿一個基因作為展示,更為有生物學意義的是細胞類型和信號通路,照貓畫虎就可以了(就把對應一個的基因值替換成你想要的細胞類型分數或者通路得分)。
附上source的代碼
- runVectorFields.R
runVectorFields <- function(object,
features,
cut_off=NULL,
normalize=T,
smooth=T,
smooth_span=NULL,
dist.spot=10,
run.mcor=T,
workers=8,
ram=50){
# Get the data:
df <- SPATA2::hlpr_join_with_aes(object,
df = SPATA2::getCoordsDf(object),
color_by = features,
normalize = normalize,
smooth = smooth,
smooth_span=smooth_span)
if(!is.null(cut_off)){df[df[,features]<cut_off, features]=0}
# Prepare data ------------------------------------------------------------
NN.file <- SPATAwrappers::getSurroundedSpots(object)
if(run.mcor==T){
base::options(future.fork.enable = TRUE)
future::plan("multisession", workers = workers)
future::supportsMulticore()
base::options(future.globals.maxSize = ram *100* 1024^2)
message("... Run multicore ... ")
}
if(dist.spot<min(NN.file %>% filter(distance!=0) %>% pull(distance))){
dist.spot <-min(NN.file %>% filter(distance!=0) %>% pull(distance))
message(paste0("The distance was adopted for the minimal distance: ",dist.spot, "px"))}
VF <- furrr::future_map(.x=1:nrow(df), .f=function(i){
#Spot Def.
bc <- df[i, c("barcodes")]
cc <- df[i, c("x", "y")]
#Neighbour Spots
NN <-
NN.file %>%
dplyr::filter(xo < cc$x+dist.spot & xo > cc$x-dist.spot) %>%
dplyr::filter(yo < cc$y+dist.spot & yo > cc$y-dist.spot) %>%
dplyr::pull(bc_destination)
#Filter input DF
NN.df <- df %>% dplyr::filter(barcodes %in% NN) %>% as.data.frame()
parameter <- features
#Create Vector
V <- -c(as.numeric(cc) - c(NN.df$x[which.max(NN.df[,parameter])], NN.df$y[which.max(NN.df[,parameter])]))
if(length(V)==0){out <- data.frame(barcodes=bc, t.x=0, t.y=0)}else{out <- data.frame(barcodes=bc, t.x=V[1], t.y=V[2])}
return(out)
}, .progress = T) %>%
do.call(rbind, .) %>%
as.data.frame()
out <- cbind(df, VF)
out[is.na(out)] <- 0
return(out)
}
- plotVectorFields.R
plotVectorFields <- function(VF, parameter, pt.size=6,pt.alpha=0.8,color.extern=NULL,skip=1){
VF <-
VF %>%
dplyr::select(x,y,{{parameter}}, t.x, t.y) %>%
dplyr::rename("parameter":=!!sym(parameter))
color.points <- VF$parameter
if(!is.null(color.extern)){color.points <- color.extern}
if(color.points %>% class()=="factor"){
p <-
ggplot2::ggplot(data=VF, aes(x,y))+
ggplot2::geom_point(data=VF , mapping=aes(x,y, color=color.points), size=pt.size, alpha=pt.alpha)+
metR::geom_vector(aes(dx = t.x, dy = t.y),skip=skip) +
metR::scale_mag()+
ggplot2::theme_void()+
Seurat::NoLegend()
}else{
p <-
ggplot2::ggplot(data=VF, aes(x,y))+
ggplot2::geom_point(data=VF , mapping=aes(x,y, color=color.points), size=pt.size, alpha=pt.alpha)+
ggplot2::scale_color_viridis_c(guide = "none")+
metR::geom_vector(aes(dx = t.x, dy = t.y),skip=skip) +
ggplot2::theme_void()+
Seurat::NoLegend()+
metR::scale_mag()
}
return(p)
}
關于軌跡分析,推薦大家采用SPATA2。
生活很好,有你更好,祝打賞的老板們國慶快樂