-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepareRef.py
More file actions
49 lines (36 loc) · 1.19 KB
/
Copy pathprepareRef.py
File metadata and controls
49 lines (36 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import anndata as ad
import pandas as pd
from rpy2.robjects import pandas2ri, numpy2ri
import rpy2.robjects as ro
import numpy as np
pandas2ri.activate()
numpy2ri.activate()
adata = ad.read_h5ad("data/76c942bd-45c3-47ec-b290-1e695ec9c177.h5ad")
# Filter for any specific cell types or tissues if needed
#adata = adata[adata.obs['tissue'] == 'liver'].copy()
# Ensure counts are in dense matrix format
if not isinstance(adata.raw.X, np.ndarray):
counts = adata.raw.X.toarray()
else:
counts = adata.raw.X
# Push counts and cell_type to R
ro.globalenv['counts'] = numpy2ri.py2rpy(counts.T) # Transpose to genes x cells
ro.globalenv['cell_types'] = pandas2ri.py2rpy(adata.obs['cell_type'].reset_index(drop=True))
# Send gene names to R
gene_names = adata.var_names.to_list()
ro.globalenv['gene_names'] = ro.StrVector(gene_names)
ro.r('''
library(Matrix)
library(InstaPrism)
# Create a sparse matrix
counts_mat <- Matrix(counts, sparse = TRUE)
# Add gene names as rownames
rownames(counts_mat) <- gene_names
# Prepare the reference object
ref_obj <- refPrepare(
sc_Expr = counts_mat,
cell.type.labels = cell_types,
cell.state.labels = cell_types
)
saveRDS(ref_obj, "guimaraes-et-al.rds")
''')