Skip to content

Commit beaf176

Browse files
committed
merge docu branch
2 parents 2ce1df5 + 317c417 commit beaf176

13 files changed

+1747
-1597
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![PyPI version](https://badge.fury.io/py/tangram-sc.svg)](https://badge.fury.io/py/tangram-sc)
44

5-
Tangram is a Python package, written in [PyTorch](https://pytorch.org/) and based on [scanpy](https://scanpy.readthedocs.io/en/stable/), for mapping single-cell (or single-nucleus) gene expression data onto spatial gene expression data. The single-cell dataset and the spatial dataset should be collected from the same anatomical region/tissue type, ideally from a biological replicate, and need to share a set of genes. Tangram aligns the single-cell data in space by fitting gene expression on the shared genes. The best way to familiarize yourself with Tangram is to check out [our tutorial](https://github.com/broadinstitute/Tangram/blob/master/tangram_tutorial.ipynb). [![colab tutorial](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1SVLUIZR6Da6VUyvX_2RkgVxbPn8f62ge?usp=sharing)
5+
Tangram is a Python package, written in [PyTorch](https://pytorch.org/) and based on [scanpy](https://scanpy.readthedocs.io/en/stable/), for mapping single-cell (or single-nucleus) gene expression data onto spatial gene expression data. The single-cell dataset and the spatial dataset should be collected from the same anatomical region/tissue type, ideally from a biological replicate, and need to share a set of genes. Tangram aligns the single-cell data in space by fitting gene expression on the shared genes. The best way to familiarize yourself with Tangram is to check out [our tutorial](https://github.com/broadinstitute/Tangram/blob/master/example/1_tutorial_tangram.ipynb). [![colab tutorial](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gDmtiRN45OwCMu4n6l1uygQ_jIGe7NgJ)
66

77
![Tangram_overview](https://raw.githubusercontent.com/broadinstitute/Tangram/master/figures/tangram_overview.png)
88
Tangram has been tested on various types of transcriptomic data (10Xv3, Smart-seq2 and SHARE-seq for single cell data; MERFISH, Visium, Slide-seq, smFISH and STARmap as spatial data). In our [preprint](https://www.biorxiv.org/content/10.1101/2020.08.29.272831v1), we used Tangram to reveal spatial maps of cell types and gene expression at single cell resolution in the adult mouse brain. More recently, we have applied our method to different tissue types including human lung, human kidney developmental mouse brain and metastatic breast cancer.
@@ -21,11 +21,16 @@ Tangram has been tested on various types of transcriptomic data (10Xv3, Smart-se
2121

2222
To install Tangram, make sure you have [PyTorch](https://pytorch.org/) and [scanpy](https://scanpy.readthedocs.io/en/stable/) installed. If you need more details on the dependences, look at the `environment.yml` file.
2323

24+
* set up conda environment for Tangram
25+
```
26+
conda env create -f environment.yml
27+
```
2428
* install tangram-sc from shell:
2529
```
30+
conda activate tangram-env
2631
pip install tangram-sc
2732
```
28-
* import tangram
33+
* To start using Tangram, import tangram in your jupyter notebooks or/and scripts
2934
```
3035
import tangram as tg
3136
```
@@ -52,7 +57,7 @@ The returned AnnData,`ad_map`, is a cell-by-voxel structure where `ad_map.X[i, j
5257

5358
The returned `ad_ge` is a voxel-by-gene AnnData, similar to spatial data `ad_sp`, but where gene expression has been projected from the single cells. This allows to extend gene throughput, or correct for dropouts, if the single cells have higher quality (or more genes) than single cell data. It can also be used to transfer cell types onto space.
5459

55-
For more details on how to use Tangram check out [our tutorial](https://github.com/broadinstitute/Tangram/blob/master/tangram_tutorial.ipynb). [![colab tutorial](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1SVLUIZR6Da6VUyvX_2RkgVxbPn8f62ge?usp=sharing)
60+
For more details on how to use Tangram check out [our tutorial](https://github.com/broadinstitute/Tangram/blob/master/example/1_tutorial_tangram.ipynb). [![colab tutorial](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1SVLUIZR6Da6VUyvX_2RkgVxbPn8f62ge?usp=sharing)
5661

5762
***
5863

@@ -111,9 +116,6 @@ You do not need to segment cells in your histology for mapping on spatial transc
111116
#### I run out of memory when I map: what should I do?
112117
Reduce your spatial data in various parts and map each single part. If that is not sufficient, you will need to downsample your single cell data as well.
113118

114-
#### How to use Tangram with Squidpy?
115-
For tutorial, please reference the example [here](https://github.com/broadinstitute/Tangram/blob/master/tutorial_sq_tangram.ipynb). For environment setup, please use squidpy=1.1.0 and reference this [yml file](https://github.com/broadinstitute/Tangram/blob/master/environment.yml).
116-
117119
***
118120
## How to cite Tangram
119121
Tangram has been released in the following publication
@@ -127,6 +129,7 @@ If you have questions, please contact the authors of the method:
127129
PyPI maintainer:
128130
- Tommaso Biancalani - <[email protected]>
129131
- Ziqing Lu - <[email protected]>
132+
- Shreya Gaddam - <[email protected]>
130133

131134
The artwork has been curated by:
132-
- Anna Hupalowska <[email protected]>
135+
- Anna Hupalowska <[email protected]>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
tangram.plot\_utils.plot\_cell\_annotation\_sc
2+
==============================================
3+
4+
.. currentmodule:: tangram.plot_utils
5+
6+
.. autofunction:: plot_cell_annotation_sc
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
tangram.plot\_utils.plot\_genes\_sc
2+
===================================
3+
4+
.. currentmodule:: tangram.plot_utils
5+
6+
.. autofunction:: plot_genes_sc

docs/source/classes/tangram.plot_utils.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@
2727

2828
plot_cell_annotation
2929

30+
plot_cell_annotation_sc
31+
3032
plot_gene_sparsity
3133

3234
plot_genes
3335

36+
plot_genes_sc
37+
3438
plot_test_scores
3539

3640
plot_training_scores

docs/source/getting_started.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ Cell Level
1717
**************************
1818
To install Tangram, make sure you have `PyTorch <https://pytorch.org/>`_ and `scanpy <https://scanpy.readthedocs.io/en/stable/>`_ installed. If you need more details on the dependences, look at the `environment.yml <https://github.com/broadinstitute/Tangram/blob/master/environment.yml>`_ file.
1919

20+
Create a conda environment for Tangram::
21+
22+
conda env create --file environment.yml
23+
2024
Install tangram-sc from shell::
21-
25+
26+
conda activate tangram-env
2227
pip install tangram-sc
2328
2429
Import tangram::

docs/source/news.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ Tangram News
33

44
- On Jan 28th 2021, Sten Linnarsson gave a `talk <https://www.youtube.com/watch?v=0mxIe2AsSKs>`_ at the WWNDev Forum and demostrated their mappings of the developmental mouse brain using Tangram.
55

6-
- On Mar 9th 2021, Nicholas Eagles wrote a `blog post <http://research.libd.org/rstatsclub/2021/03/09/lessons-learned-applying-tangram-on-visium-data/#.YPsZphNKhb->`_ about applying Tangram on Visium data.
6+
- On Mar 9th 2021, Nicholas Eagles wrote a `blog post <http://research.libd.org/rstatsclub/2021/03/09/lessons-learned-applying-tangram-on-visium-data/#.YPsZphNKhb->`_ about applying Tangram on Visium data.
7+
8+
- The Tangram method has been used by our colleagues at Harvard and Broad Institute, to map cell types for the developmental mouse brain -see Fig. 2 (`Nature(2021) <https://www.nature.com/articles/s41586-021-03670-5>`_ )
9+
10+
- Tangram is now officially a part of `Squidpy <https://squidpy.readthedocs.io/en/stable/index.html>`_

docs/source/working.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ Tangram Under the Hood
22
===========================
33

44
Tangram instantiates a `Mapper` object passing the following arguments:
5-
* _S_: single cell matrix with shape cell-by-gene. Note that genes is the number of training genes.
6-
* _G_: spatial data matrix with shape voxels-by-genes. Voxel can contain multiple cells.
5+
| _S_: single cell matrix with shape cell-by-gene. Note that genes is the number of training genes.
6+
| _G_: spatial data matrix with shape voxels-by-genes. Voxel can contain multiple cells.
77
88
Then, Tangram searches for a mapping matrix *M*, with shape voxels-by-cells, where the element *M\_ij* signifies the probability of cell *i* of being in spot *j*. Tangram computes the matrix *M* by minimizing the following loss:
99

environment.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
name: tangram-env
12
dependencies:
2-
- python=3.8.5
3+
- python>=3.8.5
34
- pip=20.2.2
45
- pytorch=1.4.0
56
- scipy=1.5.2

tangram/mapping_utils.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,18 @@ def pp_adatas(adata_sc, adata_sp, genes=None):
7878
)
7979

8080
# Calculate uniform density prior as 1/number_of_spots
81-
rna_count_per_spot = adata_sp.X.sum(axis=1)
8281
adata_sp.obs["uniform_density"] = np.ones(adata_sp.X.shape[0]) / adata_sp.X.shape[0]
8382
logging.info(
8483
f"uniform based density prior is calculated and saved in `obs``uniform_density` of the spatial Anndata."
8584
)
8685

8786
# Calculate rna_count_based density prior as % of rna molecule count
88-
rna_count_per_spot = adata_sp.X.sum(axis=1)
89-
adata_sp.obs["rna_count_based_density"] = rna_count_per_spot / np.sum(
90-
rna_count_per_spot
91-
)
87+
rna_count_per_spot = np.array(adata_sp.X.sum(axis=1)).squeeze()
88+
adata_sp.obs["rna_count_based_density"] = rna_count_per_spot / np.sum(rna_count_per_spot)
9289
logging.info(
9390
f"rna count based density prior is calculated and saved in `obs``rna_count_based_density` of the spatial Anndata."
9491
)
95-
92+
9693

9794
def adata_to_cluster_expression(adata, cluster_label, scale=True, add_density=True):
9895
"""

tangram/plot_utils.py

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,20 +172,41 @@ def construct_obs_plot(df_plot, adata, perc=0, suffix=None):
172172
adata.obs = pd.concat([adata.obs, df_plot], axis=1)
173173

174174

175-
def plot_cell_annotation_sc(adata_sp, annotation_list, perc=0):
176-
175+
def plot_cell_annotation_sc(
176+
adata_sp,
177+
annotation_list,
178+
x="x",
179+
y="y",
180+
spot_size=None,
181+
scale_factor=0.1,
182+
perc=0,
183+
ax=None
184+
):
185+
177186
# remove previous df_plot in obs
178187
adata_sp.obs.drop(annotation_list, inplace=True, errors="ignore", axis=1)
179188

180189
# construct df_plot
181190
df = adata_sp.obsm["tangram_ct_pred"][annotation_list]
182191
construct_obs_plot(df, adata_sp, perc=perc)
183-
192+
193+
#non visium data
194+
if 'spatial' not in adata_sp.obsm.keys():
195+
#add spatial coordinates to obsm of spatial data
196+
coords = [[x,y] for x,y in zip(adata_sp.obs[x].values,adata_sp.obs[y].values)]
197+
adata_sp.obsm['spatial'] = np.array(coords)
198+
199+
if 'spatial' not in adata_sp.uns.keys() and spot_size == None and scale_factor == None:
200+
raise ValueError("Spot Size and Scale Factor cannot be None when ad_sp.uns['spatial'] does not exist")
201+
202+
#REVIEW
203+
if 'spatial' in adata_sp.uns.keys() and spot_size != None and scale_factor != None:
204+
raise ValueError("Spot Size and Scale Factor should be None when ad_sp.uns['spatial'] exists")
205+
184206
sc.pl.spatial(
185-
adata_sp, color=annotation_list, cmap="viridis", show=False, frameon=False,
207+
adata_sp, color=annotation_list, cmap="viridis", show=False, frameon=False, spot_size=spot_size, scale_factor=scale_factor, ax=ax
186208
)
187209

188-
# remove df_plot in obs
189210
adata_sp.obs.drop(annotation_list, inplace=True, errors="ignore", axis=1)
190211

191212

@@ -289,7 +310,18 @@ def plot_cell_annotation(
289310
fig.suptitle(annotation)
290311

291312

292-
def plot_genes_sc(genes, adata_measured, adata_predicted, cmap="inferno", perc=0):
313+
def plot_genes_sc(
314+
genes,
315+
adata_measured,
316+
adata_predicted,
317+
x="x",
318+
y = "y",
319+
spot_size=None,
320+
scale_factor=0.1,
321+
cmap="inferno",
322+
perc=0,
323+
return_figure=False
324+
):
293325

294326
# remove df_plot in obs
295327
adata_measured.obs.drop(
@@ -350,11 +382,24 @@ def plot_genes_sc(genes, adata_measured, adata_predicted, cmap="inferno", perc=0
350382

351383
fig = plt.figure(figsize=(7, len(genes) * 3.5))
352384
gs = GridSpec(len(genes), 2, figure=fig)
385+
386+
#non visium data
387+
if 'spatial' not in adata_measured.obsm.keys():
388+
#add spatial coordinates to obsm of spatial data
389+
coords = [[x,y] for x,y in zip(adata_measured.obs[x].values,adata_measured.obs[y].values)]
390+
adata_measured.obsm['spatial'] = np.array(coords)
391+
coords = [[x,y] for x,y in zip(adata_predicted.obs[x].values,adata_predicted.obs[y].values)]
392+
adata_predicted.obsm['spatial'] = np.array(coords)
393+
394+
if ("spatial" not in adata_measured.uns.keys()) and (spot_size==None and scale_factor==None):
395+
raise ValueError("Spot Size and Scale Factor cannot be None when ad_sp.uns['spatial'] does not exist")
396+
353397
for ix, gene in enumerate(genes):
354-
355398
ax_m = fig.add_subplot(gs[ix, 0])
356399
sc.pl.spatial(
357400
adata_measured,
401+
spot_size=spot_size,
402+
scale_factor=scale_factor,
358403
color=["{} (measured)".format(gene)],
359404
frameon=False,
360405
ax=ax_m,
@@ -364,13 +409,15 @@ def plot_genes_sc(genes, adata_measured, adata_predicted, cmap="inferno", perc=0
364409
ax_p = fig.add_subplot(gs[ix, 1])
365410
sc.pl.spatial(
366411
adata_predicted,
412+
spot_size=spot_size,
413+
scale_factor=scale_factor,
367414
color=["{} (predicted)".format(gene)],
368415
frameon=False,
369416
ax=ax_p,
370417
show=False,
371418
cmap=cmap,
372419
)
373-
420+
374421
# sc.pl.spatial(adata_measured, color=['{} (measured)'.format(gene) for gene in genes], frameon=False)
375422
# sc.pl.spatial(adata_predicted, color=['{} (predicted)'.format(gene) for gene in genes], frameon=False)
376423

@@ -387,6 +434,8 @@ def plot_genes_sc(genes, adata_measured, adata_predicted, cmap="inferno", perc=0
387434
errors="ignore",
388435
axis=1,
389436
)
437+
if return_figure==True:
438+
return fig
390439

391440

392441
def plot_genes(
@@ -631,8 +680,7 @@ def plot_auc(df_all_genes, test_genes=None):
631680
textstr = 'auc_score={}'.format(np.round(metric_dict['auc_score'], 3))
632681
props = dict(boxstyle='round', facecolor='wheat', alpha=0.3)
633682
# place a text box in upper left in axes coords
634-
plt.text(0.03, 0.1, textstr, fontsize=11,
635-
verticalalignment='top', bbox=props);
683+
plt.text(0.03, 0.1, textstr, fontsize=11, verticalalignment='top', bbox=props);
636684

637685

638686
# Colors used in the manuscript for deterministic assignment.

0 commit comments

Comments
 (0)