1+ import anndata as ad
2+ import os
3+ import shutil
4+
5+ import singlecellexperiment as sce
6+ import singler
7+
8+ ## VIASH START
9+ # The following code has been auto-generated by Viash.
10+ par = {
11+ 'input_spatial_normalized_counts' : r'resources_test/task_ist_preprocessing/mouse_brain_combined/spatial_normalized_counts.h5ad' ,
12+ 'input_transcript_assignments' : r'resources_test/task_ist_preprocessing/mouse_brain_combined/transcript_assignments.zarr' ,
13+ 'input_scrnaseq_reference' : r'resources_test/task_ist_preprocessing/mouse_brain_combined/scrnaseq_reference.h5ad' ,
14+ 'celltype_key' : r'cell_type' ,
15+ 'output' : r'resources_test/task_ist_preprocessing/mouse_brain_combined/spatial_with_cell_types.h5ad' ,
16+ 'labels_key' : r'cell_labels'
17+ }
18+ meta = {
19+ 'name' : r'singleR' ,
20+ 'functionality_name' : r'singleR'
21+ }
22+ dep = {
23+
24+ }
25+
26+ ## VIASH END
27+ sce_h5ad = sce .read_h5ad (par ['input_spatial_normalized_counts' ])
28+ adata_sp = ad .read_h5ad (par ['input_spatial_normalized_counts' ])
29+
30+ sce_ref = sce .read_h5ad (par ['input_scrnaseq_reference' ])
31+
32+ features = [str (x ) for x in sce_h5ad .row_data .row_names ]
33+
34+ mat = sce_h5ad .assay ("counts" ) ##example has raw, not sure
35+ mat = mat .sorted_indices () ## magic line to make sure the matrix is in the right format for SingleR
36+
37+ mat_ref = sce_ref .assay ("normalized" )
38+ mat_ref = mat_ref .sorted_indices () ## magic line to make sure the matrix is in the right format for SingleR
39+
40+ ## create the reference from our sc data
41+ built = singler .train_single (ref_data = mat_ref ,
42+ ref_labels = sce_ref .get_column_data ().column ("cell_type" ),
43+ ref_features = sce_ref .get_row_names (),
44+ test_features = features ,)
45+
46+ ## annotate the dataset
47+ output = singler .classify_single (mat , ref_prebuilt = built )
48+
49+ adata_sp .obs ["cell_type" ] = output ['best' ]
50+
51+ # Write output
52+ print ('Writing output' , flush = True )
53+ adata_sp .write (par ['output' ])
0 commit comments