Seurat转h5ad
library(SeuratDisk)
SaveH5Seurat(SeuratObj, filename = '../data/h5ad/RNA.h5Seurat',overwrite = T)
Convert('../data/h5ad/RNA.h5Seurat', dest = "h5ad",overwrite = T)
永远不会报错版:
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转Seurat
library(SeuratDisk)
Convert('./GSE153643_RAW/raw_counts.h5ad', dest = "h5seurat",overwrite = TRUE,assay = "RNA")
scRNA <- LoadH5Seurat("./GSE153643_RAW/raw_counts.h5seurat")
scRNA
h5ad转Seurat的另一种策略:用reticulate在R中调用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中的数字不是double,要在python里转化成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转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转Seurat
counts <- assay(sce)
rownames(counts) <- GRangesToString(rowRanges(sce))
scregat_ATAC <- CreateSeuratObject(counts,meta.data = colData(sce))