seurattoscanpy seurat to annadata spatail images空转slice


#request 2
.libPaths(c( "/home/data/t040413/R/x86_64-pc-linux-gnu-library/4.2",
             "/home/data/t040413/R/yll/usr/local/lib/R/site-library",  
             "/home/data/refdir/Rlib/", "/usr/local/lib/R/library"))

## We load the required packages
library(Seurat)
library(decoupleR)

# Only needed for data handling and plotting
library(dplyr)
library(tibble)
library(tidyr)
library(patchwork)
library(ggplot2)
library(pheatmap)


library(CellChat)
library(patchwork)
library(ggplot2)
library(ggalluvial)
library(svglite)
library(Seurat)
library(xlsx)
library(harmony)

#https://mojaveazure.github.io/seurat-disk/reference/Convert.html
#https://github.com/saezlab/visium_heart/blob/db4d51ff033a6f25b24c99a275ea415128741f63/cellxgene_submission/convert_rds_to_h5ad_snATAC.ipynb


load("/home/data/t040413/silicosis/spatial_transcriptomics/silicosis_ST_harmony_SCT_r0.5.rds")
#save(d.all,file ="/home/data/t040413/silicosis/spatial_transcriptomics/silicosis_ST_harmony_SCT_r0.5.rds" )


load("/home/data/t040413/silicosis/silicosis_cluster_merge.rds")

dim(d.all)
dim(All.merge)

print(getwd())

dir.create("/home/data/t040413/silicosis/data/")
setwd("/home/data/t040413/silicosis/data/")
dir.create("visium_objs")
setwd("visium_objs/")

Embeddings(object = d.all) %>%head()
Embeddings(object = d.all,reduction = "umap") %>%head()
Reductions(d.all)

SpatialFeaturePlot(d.all,features = "Inmt")
FeaturePlot(d.all,features = "Inmt" )


DefaultAssay(d.all)
head([email protected])
d.all$Sample=d.all$orig.ident
d.all$cell_type=d.all$SCT_snn_res.0.5

obj_s=SplitObject(d.all)
obj_s
class(obj_s)

lapply(X = obj_s,FUN = function(obj){
  
  obj_name=unique(obj$orig.ident)
  
 # obj=obj_s[[1]]
  names(obj)
  Images(obj)
1. #
names( obj@images)=obj_name
  
  #  obj@images[2] %>%names()
  #  obj@assays$Spatial@data
  # dim(obj@images[[1]])>3
  # 
2.  
  meta.data <- [email protected]
  head(meta.data)
  unique(obj$orig.ident)=="SiO2_7"
  
  meta.data <- meta.data[, c("Sample", "cell_type")]
  meta.data$cell_type <- as.character(meta.data$cell_type)
  head(meta.data)
 
3. 
DefaultAssay(obj)
dim(obj@assays$SCT@counts)
counts=GetAssayData(object = obj,slot = "counts",assay = "SCT")


obj_visum<- CreateSeuratObject(counts = counts,
                               meta.data = meta.data,
                               assay = "Spatial",
                               names.delim = "-")

# obj_visum@assays$RNA@counts
# obj_visum@assays$RNA@data
# identical(obj_visum@assays$RNA@counts,
#           obj_visum@assays$RNA@data)
# head([email protected])
# 
4.
umap_embedding <- Embeddings(obj, reduction = "umap")
rownames(umap_embedding) <- colnames(obj_visum)
colnames(umap_embedding) <- c("UMAP_1", "UMAP_2")
head(umap_embedding)


obj_visum[["umap"]] <- CreateDimReducObject(embeddings = umap_embedding, key = "UMAP_", assay = DefaultAssay(obj_visum))


#DimPlot(obj_visum, reduction = "umap", pt.size = 0.5, group.by = "cell_type", label = TRUE)

obj_name=unique(obj$orig.ident)

# packageVersion("Seurat")
# packageVersion("spatial")
# 
5.
###有的时候需要使用SetAssayData()函数---------------------------
obj_visum@images=obj@images[paste0(obj_name)]

#head(obj@images$slice1@coordinates)
# Images(object = obj)
# Assays(obj)
# slotNames(obj)
# 
# Seurat::Images(object = obj_visum)

6.
SaveH5Seurat(obj_visum, filename = paste0(obj_name,".h5Seurat") ,overwrite = TRUE  )
print("start---------save to--h5ad")
Convert( paste0(obj_name,".h5Seurat") , dest = "h5ad",overwrite = TRUE)
print(getwd())
print(obj_name)
}
)

SpatialDimPlot(object = obj_visum, group.by = "cell_type", label = TRUE,pt.size.factor = 1)
??Convert

Idents(d.all)
Idents(d.all) %>%table()
head(d.all$SCT_snn_res.0.5)


d.all$orig.ident=factor(x = d.all$orig.ident,
                        levels = c("NS_7" ,"SiO2_7","NS_56","SiO2_56"))

table(d.all$stim)
library(dplyr)
d.all$stim=case_when(d.all$stim=="sio2_56"~"SiO2_56",
                     d.all$stim=="sio2_7"~"SiO2_7",
                     d.all$stim=="NS_7"~"NS_7",
                     d.all$stim=="NS_56"~"NS_56"
)
table(d.all$stim)


Idents(d.all)=d.all$stim
table(d.all$stim)

d.all$group=case_when(
  d.all$stim=="SiO2_56"~"SiO2_56",
  d.all$stim=="SiO2_7"~"SiO2_7",
  d.all$stim=="NS_7"~"NS",
  d.all$stim=="NS_56"~"NS"
)
table(d.all$group)



seurattoscanpy seurat to annadata spatail images空转slice_第1张图片

你可能感兴趣的:(python)