方法一:
import scanpy as sc
from scipy.sparse import issparse
def pyScTransform(adata, output_file=None):
"""
Function to call scTransform from Python
"""
import rpy2.robjects as ro
import anndata2ri
ro.r('library(Seurat)')
ro.r('library(scater)')
anndata2ri.activate()
sc.pp.filter_genes(adata, min_cells=5)
if issparse(adata.X):
if not adata.X.has_sorted_indices:
adata.X.sort_indices()
for key in adata.layers:
if issparse(adata.layers[key]):
if not adata.layers[key].has_sorted_indices:
adata.layers[key].sort_indices()
ro.globalenv['adata'] = adata
ro.r('seurat_obj = as.Seurat(adata, counts="X", data = NULL)')
ro.r('res <- SCTransform(object=seurat_obj, return.only.var.genes = FALSE, do.correct.umi = FALSE)')
norm_x = ro.r('res@assays$SCT@scale.data').T
adata.layers['normalized'] = norm_x
if output_file:
adata.write(output_file)
#方法二:
import anndata2ri from rpy2.robjects.packages import importr from rpy2.robjects import r, pandas2ri import numpy as np anndata2ri.activate() pandas2ri.activate() def run_sctransform(adata, layer=None, **kwargs): if layer: mat = adata.layers[layer] else: mat = adata.X # Set names for the input matrix cell_names = adata.obs_names gene_names = adata.var_names r.assign('mat', mat.T) r.assign('cell_names', cell_names) r.assign('gene_names', gene_names) r('colnames(mat) <- cell_names') r('rownames(mat) <- gene_names') seurat = importr('Seurat') r('seurat_obj <- CreateSeuratObject(mat)') # Run for k, v in kwargs.items(): r.assign(k, v) kwargs_str = ', '.join([f'{k}={k}' for k in kwargs.keys()]) r(f'seurat_obj <- SCTransform(seurat_obj,vst.flavor="v2", {kwargs_str})') # Extract the SCT data and add it as a new layer in the original anndata object sct_data = np.asarray(r['as.matrix'](r('seurat_obj@assays$SCT@data'))) adata.layers['SCT_data'] = sct_data.T sct_data = np.asarray(r['as.matrix'](r('seurat_obj@assays$SCT@counts'))) adata.layers['SCT_counts'] = sct_data.T return adata
adata.layers["data"] = adata.X.copy() adata = run_sctransform(adata, layer="counts") R[write to console]: Running SCTransform on assay: RNA R[write to console]: Place corrected count matrix in counts slot R[write to console]: Set default assay to SCT adata layers: 'counts', 'data', 'SCT_data', 'SCT_counts'
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!