Seurat轉(zhuǎn)h5ad
library(SeuratDisk)
SaveH5Seurat(SeuratObj, filename = '../data/h5ad/RNA.h5Seurat',overwrite = T)
Convert('../data/h5ad/RNA.h5Seurat', dest = "h5ad",overwrite = T)
永遠(yuǎn)不會報錯版:
new_SeuratObj_RNA <- CreateSeuratObject(SeuratObj_RNA@assays$RNA@counts)
new_SeuratObj_RNA@meta.data <- SeuratObj_RNA@meta.data
SaveH5Seurat(new_SeuratObj_RNA, filename = 'RNA.h5Seurat',overwrite = T)
Convert('RNA.h5Seurat', dest = "h5ad",overwrite = T)
h5ad轉(zhuǎn)Seurat
library(SeuratDisk)
Convert('./GSE153643_RAW/raw_counts.h5ad', dest = "h5seurat",overwrite = TRUE,assay = "RNA")
scRNA <- LoadH5Seurat("./GSE153643_RAW/raw_counts.h5seurat")
scRNA
h5ad轉(zhuǎn)Seurat的另一種策略:用reticulate在R中調(diào)用python讀h5ad
read_h5ad_as_Seurat <- function(h5ad_file_name){
# h5ad_file_name <- "/mdshare/node8/txmdata/scREGION/Cortex_plot/raw_data/atac_cortex.h5ad"
library(reticulate)
library(Seurat)
# python_path <- system("which python",intern = TRUE)
python_path = '/home/timo/anaconda3/bin/python3'
use_python(python_path)
numpy <- import("numpy")
pandas <- import("pandas")
scanpy <- import("scanpy")
adata <- scanpy$read(h5ad_file_name)
count_mtx <- Matrix::t(adata$X) ##### 注1
dimnames(count_mtx) <- list(rownames(adata$var),rownames(adata$obs))
SeuratObj <- CreateSeuratObject(count_mtx,meta.data = adata$obs)
return(SeuratObj)
}
注1: 如果這一步報錯:invalid class “dgRMatrix” object: 'x' slot is not of type "double"淳梦,則說明adata.X中的數(shù)字不是double澡谭,要在python里轉(zhuǎn)化成float,然后寫出到文件,最后再用R讀取:
# python
import numpy as np
import scipy
float_sparse_matrix = adata_RNA.X.astype(np.float32)
scipy.io.mmwrite('RNA_mtx.mtx',float_sparse_matrix)
# R
matrix <- Matrix::readMM("RNA_mtx.mtx")
loom轉(zhuǎn)Seurat
loom_in <- Connect(filename = "/mdshare/node8/txmdata/scREGION/Cortex_plot/raw_data/atac_cortex.loom", mode = 'r')
mat <- t(loom_in[["matrix"]][,])
sel_cells = loom_in[["col_attrs"]][['obs_names']][]
sel_genes <- loom_in[["row_attrs"]][['var_names']][]
dimnames(mat) <- list(sel_genes,sel_cells)
seurat_atac <- CreateSeuratObject(counts = mat, assay = 'ATAC')
seurat_atac@meta.data$celltype <- loom_in[["col_attrs"]][['celltype']][]
sce轉(zhuǎn)Seurat
counts <- assay(sce)
rownames(counts) <- GRangesToString(rowRanges(sce))
scregat_ATAC <- CreateSeuratObject(counts,meta.data = colData(sce))