Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
from .rctd import rctd
from .seuratv3 import seuratv3
from .stereoscope import stereoscope
from .tangram import tangram
from .vanillanmf import nmf
74 changes: 74 additions & 0 deletions openproblems/tasks/spatial_decomposition/methods/tangram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from ....tools.decorators import method
from ....tools.utils import check_version
from ..utils import split_sc_and_sp


@method(
method_name="Tangram",
paper_name="Deep learning and alignment of spatially resolved single-cell "
"transcriptomes with Tangram",
paper_url="https://doi.org/10.1038/s41592-021-01264-7",
paper_year=2021,
code_url="https://github.com/broadinstitute/Tangram",
image="openproblems-python-extras",
)
def tangram(adata, test=False, num_epochs=None, n_markers=None):
# analysis based on:
# github.com/broadinstitute/Tangram/blob/master/tutorial_tangram_with_squidpy.ipynb
# using tangram from PyPi, not github version

import pandas as pd
import scanpy as sc
import tangram as tg
import torch

if test:
num_epochs = num_epochs or 10
n_markers = n_markers or 10
else: # pragma: nocover
num_epochs = num_epochs or 1000
n_markers = n_markers or 100

# get single cell reference
ad_sc, adata = split_sc_and_sp(adata)
# pre-process single cell data
sc.pp.normalize_total(ad_sc, 1e4)
sc.pp.log1p(ad_sc)
# identify marker genes
sc.tl.rank_genes_groups(ad_sc, groupby="label", use_raw=False)

# extract marker genes to data frame
markers_df = pd.DataFrame(ad_sc.uns["rank_genes_groups"]["names"]).iloc[
0:n_markers, :
]

# get union of all marker genes
markers = list(set(markers_df.melt().value.values))

# match genes between single cell and spatial data
tg.pp_adatas(ad_sc, adata, genes=markers)

# get device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# map single cells to spatial locations
ad_map = tg.map_cells_to_space(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tangram has two mode of running: cell level and cluster level
https://github.com/broadinstitute/Tangram#how-to-run-tangram-at-cluster-level

I think it'd make sense to include both?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think adding both makes a lot of sense. Can this be done using a flag on a private function (e.g. a la MAGIC)? Probably best to add that as a separate PR, so I'll go ahead and merge this.

ad_sc,
adata,
device=device,
num_epochs=num_epochs,
)

# transfer labels from mapped cells to spatial location
tg.project_cell_annotations(adata_map=ad_map, adata_sp=adata, annotation="label")

# normalize scores
pred_props = adata.obsm["tangram_ct_pred"].to_numpy()
adata.obsm["proportions_pred"] = pred_props / pred_props.sum(axis=1)[:, None]

# remove un-normalized predictions
del adata.obsm["tangram_ct_pred"]

adata.uns["method_code_version"] = check_version("tangram-sc")

return adata