Skip to content

Commit 34b7908

Browse files
authored
Add tangram (#117)
1 parent dc27e7e commit 34b7908

8 files changed

Lines changed: 135 additions & 2 deletions

File tree

scripts/run_benchmark/run_full_local.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ celltype_annotation_methods:
5757
- ssam
5858
# - tacco
5959
# - moscot
60+
# - mapmycells
61+
# - tangram
6062
expression_correction_methods:
6163
- no_correction
6264
# - gene_efficiency_correction

scripts/run_benchmark/run_full_seqeracloud.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ celltype_annotation_methods:
5050
- tacco
5151
- moscot
5252
- mapmycells
53+
- tangram
5354
expression_correction_methods:
5455
- no_correction
5556
- gene_efficiency_correction

scripts/run_benchmark/run_test_local.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ celltype_annotation_methods:
5353
# - tacco
5454
# - moscot
5555
# - mapmycells
56+
# - tangram
5657
expression_correction_methods:
5758
- no_correction
5859
# - gene_efficiency_correction

scripts/run_benchmark/run_test_seqeracloud.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ celltype_annotation_methods:
4848
- ssam
4949
- tacco
5050
- moscot
51+
- mapmycells
52+
- tangram
5153
expression_correction_methods:
5254
- no_correction
5355
- gene_efficiency_correction
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
__merge__: /src/api/comp_method_cell_type_annotation.yaml
2+
3+
name: tangram
4+
label: "Tangram"
5+
summary: "Annotate cell types using Tangram"
6+
description: "Annotate cell types using Tangram"
7+
links:
8+
documentation: "https://tangram-sc.readthedocs.io"
9+
repository: "https://github.com/broadinstitute/Tangram"
10+
references:
11+
doi: "10.1038/s41592-021-01264-7"
12+
13+
arguments:
14+
- name: --mode
15+
required: false
16+
direction: input
17+
type: string
18+
default: "cells"
19+
- name: --num_epochs
20+
required: false
21+
direction: input
22+
type: integer
23+
default: 1000
24+
25+
resources:
26+
- type: python_script
27+
path: script.py
28+
29+
engines:
30+
- type: docker
31+
#image: openproblems/base_pytorch_nvidia:1 #NOTE: leads to dependency issues.
32+
# TODO: could try some other base image with pytorch and cuda installed.
33+
image: openproblems/base_python:1
34+
setup:
35+
- type: python
36+
pypi: [tangram-sc]
37+
- type: native
38+
39+
runners:
40+
- type: executable
41+
- type: nextflow
42+
directives:
43+
label: [ midtime, midcpu, midmem, gpu ]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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'])

src/workflows/run_benchmark/config.vsh.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ argument_groups:
9898
A list of cell type annotation methods to run.
9999
type: string
100100
multiple: true
101-
default: "ssam:tacco:moscot:mapmycells"
101+
default: "ssam:tacco:moscot:mapmycells:tangram"
102102
- name: "--expression_correction_methods"
103103
description: |
104104
A list of expression correction methods to run.
@@ -169,6 +169,7 @@ dependencies:
169169
- name: methods_cell_type_annotation/tacco
170170
- name: methods_cell_type_annotation/moscot
171171
- name: methods_cell_type_annotation/mapmycells
172+
- name: methods_cell_type_annotation/tangram
172173
- name: methods_expression_correction/no_correction
173174
- name: methods_expression_correction/gene_efficiency_correction
174175
- name: methods_expression_correction/resolvi_correction

src/workflows/run_benchmark/main.nf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ workflow run_wf {
375375
ssam,
376376
tacco,
377377
moscot,
378-
mapmycells
378+
mapmycells,
379+
tangram
379380
]
380381

381382
cta_ch = normalization_ch

0 commit comments

Comments
 (0)