1+ import anndata as ad
2+ import tangram as tg
3+ import torch
4+
5+ ## VIASH START
6+ par = {
7+ 'input_spatial_normalized_counts' : 'resources_test/task_ist_preprocessing/mouse_brain_combined/spatial_normalized_counts.h5ad' ,
8+ 'input_scrnaseq_reference' : 'resources_test/task_ist_preprocessing/mouse_brain_combined/scrnaseq_reference.h5ad' ,
9+ 'output' : 'spatial_with_celltypes.h5ad' ,
10+ 'celltype_key' : 'cell_type' ,
11+ 'mode' : 'cells' ,
12+ 'num_epochs' : 1000 ,
13+ }
14+ meta = {
15+ 'name' : 'tangram' ,
16+ }
17+ ## VIASH END
18+
19+ # GPU check
20+ if torch .cuda .is_available ():
21+ device = "cuda:0"
22+ else :
23+ device = "cpu"
24+
25+ # Optional parameter check: For this specific annotation method the par['input_spatial_normalized_counts'] and par['input_scrnaseq_reference'] are required
26+ assert par ['input_spatial_normalized_counts' ] is not None , 'Spatial input is required for this annotation method.'
27+ assert par ['input_scrnaseq_reference' ] is not None , 'Single cell input is required for this annotation method.'
28+
29+ # Read input
30+ adata_sp = ad .read_h5ad (par ['input_spatial_normalized_counts' ])
31+ adata_sc = ad .read_h5ad (par ['input_scrnaseq_reference' ])
32+
33+ # use log1p noramlized values
34+ adata_sc .X = adata_sc .layers ['normalized' ]
35+ adata_sp .X = adata_sp .layers ['normalized' ]
36+
37+ adata_sp_orig = adata_sp .copy ()
38+
39+ # use all the genes from adata_sp as markers for tangram
40+ markers = adata_sp .var_names .tolist ()
41+
42+ # Removes genes that all entries are zero. Finds the intersection between adata_sc, adata_st and given marker gene list,
43+ # save the intersected markers in two adatas. Calculates density priors and save it with adata_st
44+ tg .pp_adatas (adata_sc = adata_sc , adata_sp = adata_sp , genes = markers )
45+
46+ # Map single cell data (`adata_sc`) on spatial data (`adata_sp`).
47+ # density_prior (str, ndarray or None): Spatial density of spots, when is a string, value can be 'rna_count_based' or
48+ # 'uniform', when is a ndarray, shape = (number_spots,).
49+ # use 'uniform' if the spatial voxels are at single cell resolution (e.g. MERFISH). 'rna_count_based', assumes that
50+ # cell density is proportional to the number of RNA molecules.
51+ adata_map = tg .map_cells_to_space (
52+ adata_sc = adata_sc ,
53+ adata_sp = adata_sp ,
54+ device = device ,
55+ mode = par ['mode' ],
56+ num_epochs = par ['num_epochs' ],
57+ density_prior = 'uniform'
58+ )
59+
60+ # Spatial prediction dataframe is saved in `obsm` `tangram_ct_pred` of the spatial AnnData
61+ tg .project_cell_annotations (
62+ adata_map = adata_map ,
63+ adata_sp = adata_sp ,
64+ annotation = par ['celltype_key' ]
65+ )
66+
67+ # Use original without extra layers generated from tangram
68+ df = adata_sp .obsm ['tangram_ct_pred' ].copy ()
69+ adata_sp = adata_sp_orig .copy ()
70+
71+ # Set the cell type annotation
72+ adata_sp .obs [par ['celltype_key' ]] = df .idxmax (axis = 1 )
73+
74+
75+ # # Normalize by row before setting the score
76+ # normalized_df = df.div(df.sum(axis=1), axis=0)
77+ # max_values = normalized_df.max(axis=1)
78+ # adata_sp.obs['tangram_score'] = max_values
79+ # adata_sp.obsm['ct_tangram_scores'] = normalized_df
80+
81+ # Write output
82+ adata_sp .write_h5ad (par ['output' ])
0 commit comments