Skip to content

Commit d307745

Browse files
committed
add intitial tacco script.py
1 parent 778f2b8 commit d307745

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

  • src/methods_cell_type_annotation/tacco
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3
2+
3+
import anndata as ad
4+
import numpy as np
5+
import tacco
6+
7+
## VIASH START
8+
par = {
9+
'input_spatial_normalized_counts': 'resources_test/task_ist_preprocessing/mouse_brain_combined/spatial_normalized_counts.h5ad',
10+
'input_scrnaseq_reference': 'resources_test/task_ist_preprocessing/mouse_brain_combined/scrnaseq_reference.h5ad',
11+
'output': 'spatial_with_celltypes.h5ad',
12+
'celltype_key': 'cell_type',
13+
}
14+
meta = {
15+
'name': 'tacco',
16+
}
17+
## VIASH END
18+
19+
# Optional parameter check: For this specific annotation method the par['input_spatial_normalized_counts'] and par['input_scrnaseq_reference'] are required
20+
assert par['input_spatial_normalized_counts'] is not None, 'Spatial input is required for this annotation method.'
21+
assert par['input_scrnaseq_reference'] is not None, 'Single cell input is required for this annotation method.'
22+
23+
# Read input
24+
adata_sp = ad.read_h5ad(par['input_spatial_normalized_counts'])
25+
adata_sc = ad.read_h5ad(par['input_scrnaseq_reference'])
26+
27+
# Switch to raw counts
28+
adata_sp.X = adata_sp.layers['counts']
29+
adata_sc.X = adata_sc.layers['counts']
30+
31+
# Run tacco
32+
cell_type_assigment = tacco.tl.annotate(
33+
adata=adata_sp,
34+
reference=adata_sc,
35+
annotation_key=par['celltype_key']
36+
)
37+
38+
# Tacco stores the cell type proportions in a n_obs x n_celltypes matrix, so we have to extract the celltype with highest consensus
39+
cell_types = cell_type_assigment.columns
40+
highest_score_idx = np.argmax(cell_type_assignment, axis=1)
41+
adata_sp.obs[par['celltype_key']] = cell_types[highest_score_idx]
42+
43+
# Write output
44+
adata_sp.write_h5ad(par['output'])

0 commit comments

Comments
 (0)