Repository navigation
Add tangram to spatial_decomposition #587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f8b82b5
add tangram back in
scottgigante-immunai 46632f0
convert result to numpy
scottgigante-immunai f1ab4da
Merge branch 'main' into spatial_decomposition/tangram
scottgigante-immunai c2347ef
Merge branch 'main' into spatial_decomposition/tangram
giovp a1a30a1
Merge branch 'main' into spatial_decomposition/tangram
scottgigante-immunai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
openproblems/tasks/spatial_decomposition/methods/tangram.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( | ||
| 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 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.